89 lines
2.6 KiB
JavaScript
89 lines
2.6 KiB
JavaScript
const moment = require('moment');
|
|
const fs = require('fs');
|
|
const {
|
|
validarNumeroEntero,
|
|
validarNumero,
|
|
validarNumeroCuenta,
|
|
} = require('../../helper/validar');
|
|
const {
|
|
agregarRecibo,
|
|
agregarDetalleServicio,
|
|
} = require('../../config/mariadb.conf');
|
|
const { eliminarArchivo } = require('../../helper/helper');
|
|
const { NUMBER } = require('sequelize');
|
|
const dbPath = '../../db/tablas';
|
|
const Inscripcion = require(`${dbPath}/Inscripcion`);
|
|
|
|
const inscripcion = async (body, file) => {
|
|
const ahora = moment();
|
|
const idUsuario = validarNumeroEntero(body.idUsuario, 'id Usuario', true);
|
|
const numeroCuenta = validarNumeroCuenta(
|
|
body.numeroCuenta,
|
|
'numero de cuenta',
|
|
true
|
|
);
|
|
const idArea = validarNumeroEntero(body.idArea, 'id Area', true);
|
|
const monto = validarNumero(body.monto, 'monto', true);
|
|
const folio = validarNumero(body.folio, 'folio', true, 6);
|
|
const fechaTicket = body.fechaTicket;
|
|
|
|
return Inscripcion.findOne({
|
|
where: { folio, cancelacion: 0 },
|
|
})
|
|
.then((res) => {
|
|
if (res) throw new Error('Este folio ya esta registrado');
|
|
return Inscripcion.findOne({
|
|
where: {
|
|
idUsuario,
|
|
idArea,
|
|
cancelacion: 0,
|
|
idPeriodo: Number(process.env.PERIODO),
|
|
},
|
|
}).then((res) => {
|
|
if (res) throw new Error('Ya estas registrado en esta área');
|
|
|
|
if (file && folio && fechaTicket) {
|
|
return Inscripcion.create({
|
|
idUsuario,
|
|
idArea,
|
|
folio,
|
|
monto,
|
|
hizoPago: true,
|
|
rutaTicket: file.path,
|
|
extension: file.mimetype.split('/')[1],
|
|
fechaInscripcion: ahora.format(),
|
|
fechaTicket,
|
|
idPeriodo: Number(process.env.PERIODO),
|
|
});
|
|
} else {
|
|
return Inscripcion.create({
|
|
idUsuario,
|
|
idArea,
|
|
hizoPago: false,
|
|
fechaInscripcion: ahora.format(),
|
|
});
|
|
}
|
|
});
|
|
})
|
|
.then((res) => {
|
|
if (file)
|
|
fs.renameSync(file.path, file.path + '.' + file.mimetype.split('/')[1]);
|
|
agregarRecibo(
|
|
folio,
|
|
monto,
|
|
moment(fechaTicket).format('L'),
|
|
numeroCuenta
|
|
);
|
|
agregarDetalleServicio(monto, numeroCuenta);
|
|
})
|
|
.then((res) => ({
|
|
message: `Tu inscripción está sujeta a validación por parte de un operador de CEDETEC, espera mas información en tu correo de Pc-puma.`,
|
|
}))
|
|
.catch((err) => {
|
|
if (file) eliminarArchivo(file.path);
|
|
throw new Error('No es posible realizar esta inscripción. ' + err);
|
|
});
|
|
};
|
|
|
|
module.exports = inscripcion;
|