Files
servicio_social_api/server/controller/Servicio/cancelar.js
T
2021-05-13 14:06:07 -05:00

58 lines
1.9 KiB
JavaScript

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.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;