Files
citas_recorridos_api/server/controller/PrimerSemestre/registrar.js
T
2021-07-15 20:24:29 -05:00

86 lines
2.8 KiB
JavaScript

const helperPath = '../../helper';
const gmail = require(`${helperPath}/gmail`);
const correos = require(`${helperPath}/correos`);
const validar = require(`${helperPath}/validar`);
const dbPath = '../../db/tablas';
const Carrera = require(`${dbPath}/Carrera`);
const CarreraHorario = require(`${dbPath}/CarreraHorario`);
const Horario = require(`${dbPath}/Horario`);
const Grupo = require(`${dbPath}/Grupo`);
const PrimerSemestre = require(`${dbPath}/PrimerSemestre`);
const get = async (body) => {
const idCarrera = validar.validarId(body.idCarrera);
const idGrupo = validar.validarId(body.idGrupo);
const idHorario = validar.validarId(body.idHorario);
const numeroCuenta = validar.validarNumeroCuenta(body.numeroCuenta);
const correo = validar.validarCorreo(body.correo);
const nombre = validar.validarTexto(body.nombre, 'El nombre', 70);
let mail = {};
let horario = {};
let carrera = {};
return Carrera.findOne({ where: { idCarrera } })
.then((res) => {
if (!res) throw new Error('No existe esta carrera.');
carrera = res;
return Grupo.findOne({
where: { idGrupo },
});
})
.then((res) => {
if (!res) throw new Error('No existe este grupo.');
return Grupo.findOne({ where: { idGrupo, idCarrera } });
})
.then((res) => {
if (!res)
throw new Error('Este grupo no pertenece a la carrera indicada.');
if (res.grupo[0] === '2' && res.grupo[1] === '2')
throw new Error(
'Este grupo es de segundo semestre, no es valido para este registro.'
);
return Horario.findOne({ where: { idHorario } });
})
.then((res) => {
if (!res) throw new Error('No existe este horario.');
horario = res;
return CarreraHorario.findOne({ where: { idCarrera, idHorario } });
})
.then((res) => {
if (!res) throw new Error('Esta carrera no tiene asignado este horario.');
return PrimerSemestre.findOne({ where: { numeroCuenta } });
})
.then((res) => {
if (res) throw new Error('Ya se registro a este alumno al recorrido.');
return PrimerSemestre.findAll({
where: { idHorario },
});
})
.then((res) => {
if (res.length >= 50)
throw new Error('Este horario ya esta lleno, elige otro.');
return PrimerSemestre.create({
numeroCuenta,
correo,
nombre,
idHorario,
idGrupo,
});
})
.then((res) => {
mail = correos.correoPrimerSemestre(
res.numeroCuenta,
res.nombre,
carrera.carrera,
horario.horario
);
return gmail(mail.subject, res.correo, mail.message);
})
.then((res) => ({
message:
'Te has registrado correctamente, hemos mandado tu comprobante a tu correo.',
}));
};
module.exports = get;