63 lines
1.9 KiB
JavaScript
63 lines
1.9 KiB
JavaScript
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) => {
|
|
let idServicio = validar.validarId(body.idServicio);
|
|
let mensaje = validar.validarAlfanumerico(body.mensaje, 'El mensaje', 800);
|
|
let correoResponsable = {};
|
|
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.'
|
|
);
|
|
|
|
let correoAlumno = correos.canceladoAlumno(mensaje, res.Usuario.nombre);
|
|
|
|
idUsuario = res.idUsuario;
|
|
correoResponsable = correos.canceladoResponsable(
|
|
mensaje,
|
|
res.Usuario.nombre
|
|
);
|
|
emailResponsable = res.Programa.Usuario.usuario;
|
|
return gmail(correoAlumno.subject, res.correo, correoAlumno.msj);
|
|
})
|
|
.then((res) => {
|
|
return gmail(
|
|
correoResponsable.subject,
|
|
emailResponsable,
|
|
correoResponsable.msj
|
|
);
|
|
})
|
|
.then((res) => {
|
|
return Usuario.update(
|
|
{ activo: false, password: null },
|
|
{ where: { idUsuario } }
|
|
);
|
|
})
|
|
.then((res) => {
|
|
return Servicio.update({ idStatus: 10 }, { where: { idServicio } });
|
|
})
|
|
.then((res) => {
|
|
return { message: 'Se cancelo correctamente este Servicio Social.' };
|
|
});
|
|
};
|
|
|
|
module.exports = cancelar;
|