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

83 lines
2.5 KiB
JavaScript
Raw Normal View History

2021-04-30 16:04:27 -05:00
const moment = require('moment');
2022-01-25 22:08:13 -06:00
const io = require('../../socket');
2022-01-03 16:47:25 -06:00
const { validarNumeroEntero } = 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
2021-05-31 16:01:12 -05:00
const entregar = async (body) => {
2022-01-25 22:08:13 -06:00
const ahora = moment();
2022-01-03 16:47:25 -06:00
const idPrestamo = validarNumeroEntero(body.idPrestamo, 'id prestamo', true);
const idOperadorEntrega = validarNumeroEntero(
body.idOperadorEntrega,
'id operador entrega',
true
);
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.');
2022-01-20 23:41:44 -06:00
else if (res.Equipo.idStatus === 2)
2021-04-29 21:22:57 -05:00
throw new Error('Ya se entrego el equipo al usuario.');
2022-01-20 23:41:44 -06:00
else if (moment(res.horaMaxRecoger) < ahora)
2022-01-27 13:12:38 -06:00
await Equipo.update({ idStatus: 4 }, { where: { idEquipo } })
2022-02-01 09:48:07 -06:00
.then(() =>
2022-01-20 23:41:44 -06:00
Prestamo.update({ activo: false }, { where: { idPrestamo } })
)
2022-02-01 09:48:07 -06:00
.then(() => {
2022-01-20 23:41:44 -06:00
throw new Error(
2022-02-01 09:48:07 -06:00
'Ya paso el tiempo límite para recoger el equipo de cómputo.'
2022-01-20 23:41:44 -06:00
);
});
return Equipo.update({ idStatus: 2 }, { where: { idEquipo } });
2021-04-29 02:54:25 -05:00
})
2022-02-01 09:48:07 -06:00
.then(() => {
2021-06-29 09:55:18 -05:00
Prestamo.update(
{
idOperadorEntrega,
horaInicio: ahora.format(),
horaFin: ahora.add(2, 'h').format(),
},
{ where: { idPrestamo } }
);
2021-04-29 02:54:25 -05:00
})
2022-02-01 09:48:07 -06:00
.then(() =>
2022-01-20 22:01:14 -06:00
Equipo.findOne({
2021-04-29 22:17:40 -05:00
where: { idEquipo },
include: [
{
model: Carrito,
include: [{ model: TipoCarrito }, { model: Modulo }],
2022-01-20 23:41:44 -06:00
attributes: ['idCarrito', 'sobrenombre'],
2021-04-29 22:17:40 -05:00
},
],
2022-01-20 22:01:14 -06:00
attributes: [
'idEquipo',
'numeroSerie',
'numeroInventario',
'sobrenombre',
],
})
2022-01-25 22:08:13 -06:00
)
.then((res) => {
io.getIO().emit('actualizar');
return res;
});
2021-04-29 02:54:25 -05:00
};
2021-05-31 16:01:12 -05:00
module.exports = entregar;