const validar = require('../../helper/validar'); const gmail = require('../../helper/gmail'); const correos = require('../../helper/correos'); const Servicio = require('../../db/tablas/Servicio'); const Usuario = require('../../db/tablas/Usuario'); const Programa = require('../../db/tablas/Programa'); const cancelar = async (body) => { const idServicio = validar.validarId(body.idServicio); const mensaje = validar.validarAlfanumerico(body.mensaje, 'El mensaje', 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;