From 08610054dbfa71e47d1fdd923736ea0dc75e52a1 Mon Sep 17 00:00:00 2001 From: Lemuel Marquez Date: Mon, 23 Nov 2020 12:43:33 -0600 Subject: [PATCH] liberacion --- server/controller/Servicio/admin.js | 2 +- server/controller/Servicio/cartaTermino.js | 10 ++-- server/controller/Servicio/informeGlobal.js | 8 +-- server/controller/Servicio/liberacion.js | 54 +++++++++++++++++++ server/controller/Servicio/registro.js | 26 +++++---- .../controller/Servicio/registroValidado.js | 38 +++++++------ server/helper/validar.js | 5 +- server/routes/Servicio.js | 11 ++++ 8 files changed, 115 insertions(+), 39 deletions(-) create mode 100644 server/controller/Servicio/liberacion.js diff --git a/server/controller/Servicio/admin.js b/server/controller/Servicio/admin.js index 47f941d..b7fc44d 100644 --- a/server/controller/Servicio/admin.js +++ b/server/controller/Servicio/admin.js @@ -6,7 +6,7 @@ const Carrera = require('../../db/tablas/Carrera'); const Status = require('../../db/tablas/Status'); const Programa = require('../../db/tablas/Programa'); -const admin = (body) => { +const admin = async (body) => { let idServicio = validar.validarId(body.idServicio); return Servicio.findOne({ diff --git a/server/controller/Servicio/cartaTermino.js b/server/controller/Servicio/cartaTermino.js index c9f1eb9..1891dfa 100644 --- a/server/controller/Servicio/cartaTermino.js +++ b/server/controller/Servicio/cartaTermino.js @@ -47,11 +47,11 @@ const cartaTermino = async (body, file) => { .then((res) => { return Servicio.update({ cartaTermino: res }, { where: { idServicio } }); }) - .then(async (res) => { - let message = `Se subio el informe global correctamente.`; - - message += await validar.validarPreTermino(idServicio); - return { message }; + .then((res) => { + return validar.validarPreTermino(idServicio); + }) + .then((res) => { + return { message: `Se subio el informe global correctamente. ${res}` }; }); }; diff --git a/server/controller/Servicio/informeGlobal.js b/server/controller/Servicio/informeGlobal.js index 6649cdb..7b6a2a7 100644 --- a/server/controller/Servicio/informeGlobal.js +++ b/server/controller/Servicio/informeGlobal.js @@ -48,10 +48,10 @@ const informeGlobal = async (body, file) => { return Servicio.update({ informeGlobal: res }, { where: { idServicio } }); }) .then(async (res) => { - let message = `Se subio el informe global correctamente.`; - - message += await validar.validarPreTermino(idServicio); - return { message }; + return validar.validarPreTermino(idServicio); + }) + .then((res) => { + return { message: `Se subio el informe global correctamente. ${res}` }; }); }; diff --git a/server/controller/Servicio/liberacion.js b/server/controller/Servicio/liberacion.js new file mode 100644 index 0000000..1cf1958 --- /dev/null +++ b/server/controller/Servicio/liberacion.js @@ -0,0 +1,54 @@ +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 liberacion = async (body) => { + let idServicio = validar.validarId(body.idServicio); + + return Servicio.findOne({ + where: { idServicio }, + include: [{ model: Usuario }], + }) + .then(async (res) => { + if (!res) throw new Error('No existe este Servicio Social.'); + + switch (res.idStatus) { + case 5: + let correo = correos.terminoValidado(res.Usuario.nombre); + + return gmail(correo.subject, res.correo, correo.msj); + case 1: + case 2: + case 3: + case 4: + throw new Error( + 'Este Servicio Social aun no puede pasar a Liberación.' + ); + case 6: + throw new Error( + 'Este Servicio Social ya se encuentra en Liberación.' + ); + case 7: + case 8: + case 9: + throw new Error( + 'Este Servicio Social se encuentra rechazado. No se puede aceptar hasta que se corriga lo necesario.' + ); + case 10: + throw new Error('Este Servicio Social esta cancelado.'); + } + }) + .then((res) => { + return Servicio.update({ idStatus: 6 }, { where: { idServicio } }); + }) + .then((res) => { + return { + message: + 'Se cambio de estatus correctamente y se envio un correo al alumno informandole de su liberación.', + }; + }); +}; + +module.exports = liberacion; diff --git a/server/controller/Servicio/registro.js b/server/controller/Servicio/registro.js index 300b247..27660db 100644 --- a/server/controller/Servicio/registro.js +++ b/server/controller/Servicio/registro.js @@ -7,25 +7,23 @@ const Usuario = require('../../db/tablas/Usuario'); const registro = async (body) => { let idServicio = validar.validarId(body.idServicio); + let password = ''; + let correo = {}; + let idUsuario = null; return Servicio.findOne({ where: { idServicio }, include: [{ model: Usuario }], }) - .then(async (res) => { - if (!res) throw new Error('No existe este servicio.'); + .then((res) => { + if (!res) throw new Error('No existe este Servicio Social.'); switch (res.idStatus) { case 1: - let password = encriptar.generarPassword(); - let correo = correos.preRegistro(password, res.Usuario.nombre); - - await gmail(correo.subject, res.correo, correo.msj); - - return Usuario.update( - { password: encriptar.encriptar(password) }, - { where: { idUsuario: res.idUsuario } } - ); + password = encriptar.generarPassword(); + correo = correos.preRegistro(password, res.Usuario.nombre); + idUsuario = res.idUsuario; + return gmail(correo.subject, res.correo, correo.msj); case 2: throw new Error('Este Servicio Social ya se encuentra en Registro.'); case 3: @@ -43,6 +41,12 @@ const registro = async (body) => { throw new Error('Este Servicio Social esta cancelado.'); } }) + .then((res) => { + return Usuario.update( + { password: encriptar.encriptar(password) }, + { where: { idUsuario } } + ); + }) .then((res) => { return Servicio.update({ idStatus: 2 }, { where: { idServicio } }); }) diff --git a/server/controller/Servicio/registroValidado.js b/server/controller/Servicio/registroValidado.js index caddded..d15cdfa 100644 --- a/server/controller/Servicio/registroValidado.js +++ b/server/controller/Servicio/registroValidado.js @@ -10,6 +10,9 @@ const registroValidado = async (body) => { let fechaNacimiento = validar.validarFecha(body.fechaNacimiento); let telefono = validar.validarNumero(body.telefono, 10); let direccion = validar.validar(body.direccion, 'La dirección', 200); + let correoAlumno = {}; + let correoResponsable = {}; + let emailResponsable = ''; return Servicio.findOne({ where: { idServicio }, @@ -18,27 +21,17 @@ const registroValidado = async (body) => { { model: Programa, include: [{ model: Usuario }] }, ], }) - .then(async (res) => { - if (!res) throw new Error('No existe este servicio.'); + .then((res) => { + if (!res) throw new Error('No existe este Servicio Social.'); switch (res.idStatus) { case 2: - let correoAlumno = correos.registroValidadoAlumno(res.Usuario.nombre); - let correoResponsable = correos.registroValidadoResponsable( + correoAlumno = correos.registroValidadoAlumno(res.Usuario.nombre); + correoResponsable = correos.registroValidadoResponsable( res.Usuario.nombre ); - - await gmail(correoAlumno.subject, res.correo, correoAlumno.msj); - await gmail( - correoResponsable.subject, - res.Programa.Usuario.usuario, - correoResponsable.msj - ); - - return Servicio.update( - { idStatus: 3, direccion, telefono, fechaNacimiento }, - { where: { idServicio } } - ); + emailResponsable = res.Programa.Usuario.usuario; + return gmail(correoAlumno.subject, res.correo, correoAlumno.msj); case 1: throw new Error( 'Este Servicio Social aun no puede pasar a Registro Validado.' @@ -65,6 +58,19 @@ const registroValidado = async (body) => { ); } }) + .then((res) => { + return gmail( + correoResponsable.subject, + emailResponsable, + correoResponsable.msj + ); + }) + .then((res) => { + return Servicio.update( + { idStatus: 3, direccion, telefono, fechaNacimiento }, + { where: { idServicio } } + ); + }) .then((res) => { return { message: diff --git a/server/helper/validar.js b/server/helper/validar.js index 8f84393..252ab3c 100644 --- a/server/helper/validar.js +++ b/server/helper/validar.js @@ -97,8 +97,9 @@ const validarPreTermino = (idServicio) => { return false; }) .then((res) => { - if (!res) return ''; - return ' Se actualizo correctamente el status del Servicio Social.'; + if (res) + return 'Este Servicio Social paso a Termino, espera a que COESI autorice tu liberación.'; + return ''; }); }; diff --git a/server/routes/Servicio.js b/server/routes/Servicio.js index 2dccfcd..fde9612 100644 --- a/server/routes/Servicio.js +++ b/server/routes/Servicio.js @@ -14,6 +14,7 @@ const registro = require('../controller/Servicio/registro'); const registroValidado = require('../controller/Servicio/registroValidado'); const cartaTermino = require('../controller/Servicio/cartaTermino'); const informeGlobal = require('../controller/Servicio/informeGlobal'); +const liberacion = require('../controller/Servicio/liberacion'); app.post(`${route}/nuevo`, upload.single('cartaAceptacion'), (req, res) => { return nuevo(JSON.parse(req.body.alumno), req.file.filename) @@ -125,4 +126,14 @@ app.put( } ); +app.put(`${route}/liberacion`, (req, res) => { + return liberacion(req.body) + .then((data) => { + res.status(200).json(data); + }) + .catch((err) => { + res.status(400).json({ message: err.message }); + }); +}); + module.exports = app;