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 cancelar = async (body) => { const idServicio = validar.validarNumeroEntero( body.idServicio, 'id servicio' ); const mensaje = validar.validarAlfanumerico( body.mensaje, 'mensaje', true, 800 ); let correoResponsable = {}; let correoAlumno = {}; let emailResponsable = ''; let idUsuario; return Servicio.findOne({ where: { idServicio }, include: [ { model: Usuario }, { model: Programa, include: [{ model: Usuario }] }, ], }) .then((res) => { if (!res) throw new Error('No existe este Servicio Social.'); if (res.idStatus === 10) throw new Error('Este Servicio Social ya fue cancelado.'); if (res.idStatus === 6) throw new Error( 'Este Servicio Social ya fue finalizado, no se puede cancelar.' ); idUsuario = res.idUsuario; correoAlumno = correos.canceladoAlumno(mensaje, res.Usuario.nombre); correoResponsable = correos.canceladoResponsable( mensaje, res.Usuario.nombre ); emailResponsable = res.Programa.Usuario.usuario; return gmail(correoAlumno.subject, res.correo, correoAlumno.msj); }) .then((res) => gmail(correoResponsable.subject, emailResponsable, correoResponsable.msj) ) .then((res) => Usuario.update( { activo: false, password: null }, { where: { idUsuario } } ) ) .then((res) => Servicio.update({ idStatus: 10 }, { where: { idServicio } })) .then((res) => ({ message: 'Se cancelo correctamente este Servicio Social.', })); }; module.exports = cancelar;