Files
pcpuma_acatlan_api/server/controller/Multa/multar.js
T

51 lines
1.6 KiB
JavaScript
Raw Normal View History

2021-09-02 08:21:35 -05:00
const moment = require('moment');
const { validarId, validarTexto } = require('../../helper/validar');
const dbPath = '../../db/tablas';
2021-09-02 09:33:24 -05:00
const Infraccion = require(`${dbPath}/Infraccion`);
2021-09-02 08:21:35 -05:00
const Prestamo = require(`${dbPath}/Prestamo`);
const Usuario = require(`${dbPath}/Usuario`);
const Multa = require(`${dbPath}/Multa`);
const multar = async (body) => {
const idInfraccion = validarId(body.idInfraccion);
const idPrestamo = validarId(body.idPrestamo);
const descripcion = validarTexto(body.descripcion, 'La descripción', 500);
2021-09-02 09:33:24 -05:00
let prestamo = {};
2021-09-02 08:21:35 -05:00
let fechaFin = moment();
return Infraccion.findOne({ where: { idInfraccion } })
.then((res) => {
if (!res) throw new Error('No existe esta infracción.');
2021-09-02 09:33:24 -05:00
if (res.gravedad == '1') fechaFin.add(7, 'd');
else fechaFin.add(100, 'y');
2021-09-02 08:21:35 -05:00
return Prestamo.findOne({
where: { idPrestamo },
include: [{ model: Usuario }],
});
})
.then((res) => {
if (!res) throw new Error('No existe este prestamo.');
2021-09-02 09:33:24 -05:00
prestamo = res;
return Multa.findOne({ where: { idPrestamo } });
})
.then((res) => {
if (res)
throw new Error('A este prestamo ya se le ha asignado una multa.');
2021-09-02 08:21:35 -05:00
return Usuario.update(
{ multa: true },
2021-09-02 09:33:24 -05:00
{ where: { idUsuario: prestamo.Usuario.idUsuario } }
2021-09-02 08:21:35 -05:00
);
})
2021-09-02 09:33:24 -05:00
.then((res) =>
Multa.create({
2021-09-02 08:21:35 -05:00
descripcion,
idInfraccion,
idPrestamo,
fechaFin: fechaFin.format(),
2021-09-02 09:33:24 -05:00
})
)
2021-09-02 08:21:35 -05:00
.then((res) => ({ message: `Se multo correctamente al alumno.` }));
};
module.exports = multar;