const validar = require('../../helper/validar'); const encriptar = require('../../helper/encriptar'); const gmail = require('../../helper/gmail'); const correos = require('../../helper/correos'); const Servicio = require('../../db/tablas/Servicio'); const Usuario = require('../../db/tablas/Usuario'); const registro = 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.'); if (res.idStatus === 2) throw new Error('Este servicio ya se encuentra en Registro.'); else if (res.idStatus >= 7) throw new Error( 'Este servicio se encuentra cancelado o rechazado. No se puede aceptar hasta que se corriga lo necesario.' ); else if (res.idStatus !== 1) throw new Error('Este servicio ya paso por el Registro.'); 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 } } ); }) .then((res) => { return Servicio.update({ idStatus: 2 }, { where: { idServicio } }); }) .then((res) => { return { message: 'Se cambio de estatus correctamente y se envio un correo al alumno con sus credenciales.', }; }); }; module.exports = registro;