const moment = require('moment'); const { validarNumeroEntero, validarNumeroCuenta, } = require('../../helper/validar'); const { usuarioInscrito, agregarRecibo, agregarDetalleServicio, } = require('../../config/mariadb.conf'); const dbPath = '../../db/tablas'; const Inscripcion = require(`${dbPath}/Inscripcion`); const gmail = require('../../helper/gmail'); const correoDeclinar = require('../../helper/correoDeclinar'); const correoInscripcion = require('../../helper/correo'); const validarTicket = async (body) => { const folio = validarNumeroEntero(body.folio, 'folio', true); const idArea = validarNumeroEntero(body.idArea, 'IDAREA', true); const monto = validarNumeroEntero(body.monto, 'monto', true); const fechaTicket = body.fechaTicket; const numeroCuenta = validarNumeroCuenta( body.numeroCuenta, 'número de cuenta', true ); let correo = {}; let email = ''; return Inscripcion.findOne({ where: { folio }, }) .then((res) => { if (!res) throw new Error('Este folio no existe'); idUsuario = res.idUsuario; if (body.validacion == 1) { return Inscripcion.findOne({ where: { folio, validacion: 1 }, }); } }) .then((res) => { if (res) throw new Error('Este folio ya fue validado'); return Inscripcion.update( { validacion: body.validacion, }, { where: { folio, cancelacion: 0, }, } ); }) .then((res) => { email = `${numeroCuenta}@pcpuma.acatlan.unam.mx`; if (body.validacion === 1) { correo = correoInscripcion(); usuarioInscrito(numeroCuenta, idArea); agregarRecibo( folio, monto, moment(fechaTicket).format('L'), numeroCuenta ); agregarDetalleServicio(monto, numeroCuenta); } else { correo = correoDeclinar(); return Inscripcion.update( { motivo: body.motivo }, { where: { folio, }, } ); } }) .then((res) => { gmail(correo.subject, email, correo.msj); if (body.validacion === 1) { return { message: '¡El ticket a sido validado de forma correcta!', }; } else return { message: '¡El ticket a sido declinado', }; }) .catch((err) => { throw new Error('No fue posible validar el ticket. ' + err); }); }; module.exports = validarTicket;