Files
servicio_social_api/server/controller/Servicio/cancelar.js
T

56 lines
1.8 KiB
JavaScript
Raw Normal View History

2020-11-22 19:27:29 -06:00
const validar = require('../../helper/validar');
const gmail = require('../../helper/gmail');
const correos = require('../../helper/correos');
2020-11-22 19:27:29 -06:00
const Servicio = require('../../db/tablas/Servicio');
2020-11-23 12:56:24 -06:00
const Usuario = require('../../db/tablas/Usuario');
const Programa = require('../../db/tablas/Programa');
2020-11-22 19:27:29 -06:00
const cancelar = async (body) => {
2021-01-14 03:13:37 -06:00
const idServicio = validar.validarId(body.idServicio);
const mensaje = validar.validarAlfanumerico(body.mensaje, 'El mensaje', 800);
let correoResponsable = {};
2021-01-12 21:09:37 -06:00
let correoAlumno = {};
let emailResponsable = '';
let idUsuario;
2020-11-22 19:27:29 -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-22 19:27:29 -06:00
.then((res) => {
if (!res) throw new Error('No existe este Servicio Social.');
2020-11-22 19:27:29 -06:00
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;
2021-01-12 21:09:37 -06:00
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);
})
2021-01-12 21:09:37 -06:00
.then((res) =>
gmail(correoResponsable.subject, emailResponsable, correoResponsable.msj)
)
.then((res) =>
Usuario.update(
{ 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;