Files

40 lines
1.3 KiB
JavaScript
Raw Permalink Normal View History

2022-04-17 22:23:39 -05:00
const Profesor = require('../../db/tablas/Profesor');
2022-05-13 22:10:54 -05:00
const ProfesorPremiacion = require('../../db/tablas/ProfesorPremiacion');
2022-04-17 22:23:39 -05:00
const validar = require('../../helper/validar');
const entradaInvitado = async (body) => {
const idProfesor = validar.validarNumeroEntero(body.idProfesor, 'idProfesor');
2022-05-13 22:10:54 -05:00
const idPremiacion = validar.validarNumeroEntero(
body.idPremiacion,
'idPremiacion'
);
2022-04-17 22:23:39 -05:00
const profesor = await Profesor.findOne({ where: idProfesor });
2022-05-13 22:10:54 -05:00
if (!profesor) throw new Error('Este profesor no existe.');
const profPrem = await ProfesorPremiacion.findOne({
where: { idPremiacion, idProfesor },
});
if (!profPrem)
throw new Error(
'Este profesor no está registrado en esta entrega de medallas.'
);
if (!profPrem.numeroInvitados)
throw new Error('Este profesor no registró a ninún invitado.');
let noInvDentro = profPrem.dataValues.numeroInvitadosDentro;
let noInv = profPrem.dataValues.numeroInvitados;
2022-04-17 22:23:39 -05:00
if (noInvDentro && noInvDentro >= noInv)
throw new Error('Este profesor ya ingreso a todos sus invitados.');
2022-05-13 22:10:54 -05:00
const res = await profPrem.update({ numeroInvitadosDentro: noInvDentro + 1 });
2022-04-17 22:23:39 -05:00
if (!res)
throw new Error(
2022-05-13 22:10:54 -05:00
'Ha ocurrido un error al registrar los invitados del profesor.'
2022-04-17 22:23:39 -05:00
);
return res;
};
module.exports = entradaInvitado;