65 lines
2.0 KiB
JavaScript
65 lines
2.0 KiB
JavaScript
const moment = require('moment');
|
|
const dbHelper = '../../helper';
|
|
const { validarId } = require(`${dbHelper}/validar`);
|
|
const { correoAlumno } = require(`${dbHelper}/correos`);
|
|
const gmail = require(`${dbHelper}/gmail`);
|
|
const dbPath = '../../db/tablas';
|
|
const Alumno = require(`${dbPath}/Alumno`);
|
|
const Carrera = require(`${dbPath}/Carrera`);
|
|
const Horario = require(`${dbPath}/Horario`);
|
|
const ahora = moment();
|
|
|
|
const registrar = async (body) => {
|
|
throw new Error('Se ha cerrado el registro.');
|
|
const idHorario = validarId(body.idHorario);
|
|
const idAlumno = validarId(body.idAlumno);
|
|
let alumno = {};
|
|
let horario = {};
|
|
let correo = {};
|
|
|
|
return Alumno.findOne({
|
|
where: { idAlumno },
|
|
include: [{ model: Carrera }],
|
|
})
|
|
.then((res) => {
|
|
if (!res) throw new Error('No existe este alumno.');
|
|
if (res.idHorario)
|
|
throw new Error('Este alumno ya tiene un horario asignado.');
|
|
alumno = res;
|
|
return Horario.findOne({ where: { idHorario } });
|
|
})
|
|
.then((res) => {
|
|
if (!res) throw new Error('No existe este horario.');
|
|
horario = res;
|
|
if (alumno.turno != horario.turno)
|
|
throw new Error('Este horario no corresponde con el turno del alumno.');
|
|
if (moment(horario.horario).add(20, 'm') < ahora)
|
|
throw new Error('Ya no puedes incribirte a este horario.');
|
|
return Alumno.findAll({ where: { idHorario } });
|
|
})
|
|
.then((res) => {
|
|
if (res.length >= 50)
|
|
throw new Error('Ya no hay espacio en este horario.');
|
|
return Alumno.update({ idHorario }, { where: { idAlumno } });
|
|
})
|
|
.then((res) => {
|
|
correo = correoAlumno(
|
|
alumno.numeroCuenta,
|
|
alumno.Carrera.carrera,
|
|
horario.horario,
|
|
alumno.idAlumno
|
|
);
|
|
return gmail(
|
|
correo.subject,
|
|
`${alumno.numeroCuenta}@pcpuma.acatlan.unam.mx`,
|
|
correo.message
|
|
);
|
|
})
|
|
.then((res) => ({
|
|
message:
|
|
'Se registró correctamente tu horario. Se envió un comprobante a tu correo institucional pcpuma.',
|
|
}));
|
|
};
|
|
|
|
module.exports = registrar;
|