2021-05-05 18:49:23 -05:00
|
|
|
const helperPath = '../../helper';
|
|
|
|
|
const correos = require(`${helperPath}/correos`);
|
2021-05-13 14:06:07 -05:00
|
|
|
const gmail = require(`${helperPath}/gmail`);
|
|
|
|
|
const validar = require(`${helperPath}/validar`);
|
2021-05-05 18:49:23 -05:00
|
|
|
const dbPath = '../../db/tablas';
|
|
|
|
|
const Programa = require(`${dbPath}/Programa`);
|
|
|
|
|
const Servicio = require(`${dbPath}/Servicio`);
|
|
|
|
|
const Usuario = require(`${dbPath}/Usuario`);
|
2020-11-22 19:27:29 -06:00
|
|
|
|
|
|
|
|
const cancelar = async (body) => {
|
2022-01-02 15:07:49 -06:00
|
|
|
const idServicio = validar.validarNumeroEntero(
|
|
|
|
|
body.idServicio,
|
|
|
|
|
'id servicio'
|
|
|
|
|
);
|
2022-01-02 17:45:29 -06:00
|
|
|
const mensaje = validar.validarAlfanumerico(
|
|
|
|
|
body.mensaje,
|
|
|
|
|
'mensaje',
|
|
|
|
|
true,
|
|
|
|
|
800
|
|
|
|
|
);
|
2020-11-27 13:07:13 -06:00
|
|
|
let correoResponsable = {};
|
2021-01-12 21:09:37 -06:00
|
|
|
let correoAlumno = {};
|
2020-11-27 13:07:13 -06:00
|
|
|
let emailResponsable = '';
|
|
|
|
|
let idUsuario;
|
2020-11-22 19:27:29 -06:00
|
|
|
|
2020-11-27 13:07:13 -06:00
|
|
|
return Servicio.findOne({
|
|
|
|
|
where: { idServicio },
|
2020-12-08 21:32:58 -06:00
|
|
|
include: [
|
|
|
|
|
{ model: Usuario },
|
|
|
|
|
{ model: Programa, include: [{ model: Usuario }] },
|
|
|
|
|
],
|
2020-11-27 13:07:13 -06:00
|
|
|
})
|
2020-11-22 19:27:29 -06:00
|
|
|
.then((res) => {
|
2020-11-27 18:55:18 -06:00
|
|
|
if (!res) throw new Error('No existe este Servicio Social.');
|
2020-11-22 19:27:29 -06:00
|
|
|
if (res.idStatus === 10)
|
2020-11-27 13:07:13 -06:00
|
|
|
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;
|
2021-01-12 21:09:37 -06:00
|
|
|
correoAlumno = correos.canceladoAlumno(mensaje, res.Usuario.nombre);
|
2020-11-27 13:07:13 -06:00
|
|
|
correoResponsable = correos.canceladoResponsable(
|
|
|
|
|
mensaje,
|
|
|
|
|
res.Usuario.nombre
|
|
|
|
|
);
|
|
|
|
|
emailResponsable = res.Programa.Usuario.usuario;
|
|
|
|
|
return gmail(correoAlumno.subject, res.correo, correoAlumno.msj);
|
|
|
|
|
})
|
2021-01-12 21:09:37 -06:00
|
|
|
.then((res) =>
|
|
|
|
|
gmail(correoResponsable.subject, emailResponsable, correoResponsable.msj)
|
|
|
|
|
)
|
|
|
|
|
.then((res) =>
|
|
|
|
|
Usuario.update(
|
2020-11-27 13:07:13 -06:00
|
|
|
{ activo: false, password: null },
|
|
|
|
|
{ where: { idUsuario } }
|
2021-01-12 21:09:37 -06:00
|
|
|
)
|
|
|
|
|
)
|
|
|
|
|
.then((res) => Servicio.update({ idStatus: 10 }, { where: { idServicio } }))
|
2021-01-14 16:02:57 -06:00
|
|
|
.then((res) => ({
|
|
|
|
|
message: 'Se cancelo correctamente este Servicio Social.',
|
|
|
|
|
}));
|
2020-11-22 19:27:29 -06:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
module.exports = cancelar;
|