Files
pcpuma_acatlan_api/server/controller/Prestamo/recoger.js
T

65 lines
2.2 KiB
JavaScript
Raw Normal View History

2021-04-30 16:04:27 -05:00
const moment = require('moment');
2021-05-18 22:40:30 -05:00
const { validarId } = require('../../helper/validar');
2021-04-29 02:54:25 -05:00
const dbPath = '../../db/tablas';
2021-04-29 22:17:40 -05:00
const Carrito = require(`${dbPath}/Carrito`);
2021-04-29 02:54:25 -05:00
const Equipo = require(`${dbPath}/Equipo`);
2021-04-29 22:17:40 -05:00
const Modulo = require(`${dbPath}/Modulo`);
2021-04-29 02:54:25 -05:00
const Operador = require(`${dbPath}/Operador`);
2021-04-29 22:17:40 -05:00
const Prestamo = require(`${dbPath}/Prestamo`);
const TipoCarrito = require(`${dbPath}/TipoCarrito`);
2021-04-29 02:54:25 -05:00
const recoger = async (body) => {
2021-04-30 16:04:27 -05:00
const ahora = moment();
2021-05-18 22:40:30 -05:00
const idPrestamo = validarId(body.idPrestamo);
const idOperadorEntrega = validarId(body.idOperadorEntrega);
2021-04-29 22:17:40 -05:00
let idEquipo;
2021-04-29 02:54:25 -05:00
return Operador.findOne({ where: { idOperador: idOperadorEntrega } })
.then((res) => {
if (!res) throw new Error('No existe este operador.');
return Prestamo.findOne({
where: { idPrestamo },
include: [{ model: Equipo }],
});
})
2021-04-30 16:04:27 -05:00
.then(async (res) => {
2021-04-29 02:54:25 -05:00
if (!res) throw new Error('No existe este prestamo.');
2021-04-30 16:04:27 -05:00
idEquipo = res.Equipo.idEquipo;
2021-04-29 21:22:57 -05:00
if (!res.activo) throw new Error('Este prestamo ya no esta activo.');
2021-04-30 16:04:27 -05:00
else if (res.Equipo.enUso)
2021-04-29 21:22:57 -05:00
throw new Error('Ya se entrego el equipo al usuario.');
2021-04-30 16:04:27 -05:00
else if (moment(res.horaInicio) < ahora) {
await Equipo.update({ apartado: false }, { where: { idEquipo } });
2021-05-26 17:01:23 -05:00
await Prestamo.update({ activo: false }, { where: { idPrestamo } });
2021-04-30 16:04:27 -05:00
throw new Error(
'Ya paso el tiempo limite para recoger el dispositivo.'
);
}
2021-04-29 22:17:40 -05:00
return Equipo.update({ enUso: true }, { where: { idEquipo } });
2021-04-29 02:54:25 -05:00
})
.then((res) => {
Prestamo.update({ idOperadorEntrega }, { where: { idPrestamo } });
})
2021-04-29 22:17:40 -05:00
.then((res) => {
return Equipo.findOne({
where: { idEquipo },
include: [
{
model: Carrito,
include: [{ model: TipoCarrito }, { model: Modulo }],
},
],
});
})
.then((res) => {
delete res.dataValues.idCarrito;
delete res.dataValues.idStatus;
delete res.dataValues.idPrograma;
delete res.dataValues.Carrito.dataValues.idTipoCarrito;
delete res.dataValues.Carrito.dataValues.idModulo;
return res;
});
2021-04-29 02:54:25 -05:00
};
module.exports = recoger;