const { Op } = require('sequelize'); const Servicio = require('../../db/tablas/Servicio'); const Usuario = require('../../db/tablas/Usuario'); const Programa = require('../../db/tablas/Programa'); const Carrera = require('../../db/tablas/Carrera'); const validar = require('../../helper/validar'); const nuevo = async (body) => { let idUsuario = validar.validarId(body.idUsuarioAlumno); let idPrograma = validar.validarId(body.idPrograma); let idCarrera = validar.validarId(body.idCarrera); let creditos = body.creditos; let nombre = validar.validarTexto(body.nombre); let correo = validar.validarCorreo(body.correo); let fechaInicio = validar.validarFecha(boyd.fechaInicio); let fechaFin = validar.validarFecha(boyd.fechaFin); let cartaAceptacion = body.cartaAceptacion; return Usuario.findOne({ where: { [Op.and]: [{ idUsuario }, { idTipoUsuario: 3 }] }, }) .then((res) => { if (!res) throw new Error('Este alumno no existe en la db.'); return Programa.findOne({ where: { idPrograma } }); }) .then((res) => { if (!res) throw new Error('Este programa no existe en la db.'); return Carrera.findOne({ where: { idCarrera } }); }) .then((res) => { if (!res) throw new Error('Esta carrera no existe en la db.'); return Servicio.findOne({ where: { [Op.and]: [{ idUsuario }, { idStatus: { [Op.ne]: 10 } }] }, }); }) .then((res) => { if (res) throw new Error('Este alumno ya tienen un Servicio Social activo.'); return Servicio.create({ idUsuario, idPrograma, idCarrera, creditos, nombre, correo, fechaInicio, fechaFin, cartaAceptacion, }); }) .then((res) => { return { message: `Se Pre-Registro correctamente al alumno ${res.nombre}`, }; }); }; module.exports = nuevo;