const moment = require('moment'); const helperPath = '../../helper'; const correos = require(`${helperPath}/correos`); const gmail = require(`${helperPath}/gmail`); const validar = require(`${helperPath}/validar`); const dbPath = '../../db/tablas'; const Programa = require(`${dbPath}/Programa`); const Servicio = require(`${dbPath}/Servicio`); const Usuario = require(`${dbPath}/Usuario`); const liberacion = async (body) => { const idServicio = validar.validarNumeroEntero( body.idServicio, 'id servicio' ); const update = { idStatus: 6, fechaLiberacion: moment().format() }; let correo = {}; return Servicio.findOne({ where: { idServicio }, include: [{ model: Usuario }, { model: Programa }], }) .then(async (res) => { if (!res) throw new Error('No existe este Servicio Social.'); if (res.Programa.acatlan && !body.vistoBuenoAcatlan) throw new Error( 'El programa de este Servicio Social es Acatlán Contigo y no se envio la variable validando este servicio.' ); switch (res.idStatus) { case 5: correo = correos.terminoValidado(res.Usuario.nombre); if (body.vistoBuenoAcatlan) update.vistoBuenoAcatlan = true; return gmail(correo.subject, res.correo, correo.msj); case 1: case 2: case 3: case 4: throw new Error( 'Este Servicio Social aun no puede pasar a Liberación.' ); case 6: throw new Error( 'Este Servicio Social ya se encuentra en Liberación.' ); case 7: case 8: case 9: throw new Error( 'Este Servicio Social se encuentra rechazado. No se puede aceptar hasta que se corriga lo necesario.' ); case 10: throw new Error('Este Servicio Social esta cancelado.'); default: throw new Error('Id status no valido.'); } }) .then((res) => Servicio.update(update, { where: { idServicio }, }) ) .then((res) => ({ message: 'Se cambio de estatus correctamente y se envio un correo al alumno informandole de su liberación.', })); }; module.exports = liberacion;