60 lines
1.7 KiB
JavaScript
60 lines
1.7 KiB
JavaScript
const moment = require('moment');
|
|
const fs = require('fs');
|
|
const {
|
|
validarNumeroEntero,
|
|
validarNumeroCuenta,
|
|
} = require('../../helper/validar');
|
|
const { eliminarArchivo } = require('../../helper/helper');
|
|
const dbPath = '../../db/tablas';
|
|
const Carrera = require(`${dbPath}/Carrera`);
|
|
const Inscripcion = require(`${dbPath}/Inscripcion`);
|
|
|
|
const inscripcion = async (body, file) => {
|
|
const ahora = moment();
|
|
const idUsuario = validarNumeroEntero(body.idUsuario, 'id Usuario', true);
|
|
const idArea = validarNumeroEntero(body.idArea, 'id Area', true);
|
|
const folio = body.folio;
|
|
|
|
return Inscripcion.findOne({
|
|
where: { folio },
|
|
})
|
|
.then((res) => {
|
|
if (res) throw new Error('Este folio ya esta registrado');
|
|
return Inscripcion.findOne({
|
|
where: { idUsuario, idArea },
|
|
}).then((res) => {
|
|
if (res) throw new Error('Ya estas registrado en esta área');
|
|
if (file) {
|
|
return Inscripcion.create({
|
|
idUsuario,
|
|
folio,
|
|
monto: body.monto,
|
|
idArea,
|
|
fechaInscripcion: ahora.format(),
|
|
fechaTicket: body.fechaTicket,
|
|
rutaTicket: file.path,
|
|
});
|
|
} else {
|
|
return Inscripcion.create({
|
|
idUsuario,
|
|
idArea,
|
|
fechaInscripcion: ahora.format(),
|
|
});
|
|
}
|
|
});
|
|
})
|
|
.then((res) => {
|
|
if (file)
|
|
fs.renameSync(file.path, file.path + '.' + file.mimetype.split('/')[1]);
|
|
})
|
|
.then((res) => ({
|
|
message: '¡La inscripción se realizo de manera correcta!',
|
|
}))
|
|
.catch((err) => {
|
|
if (file) eliminarArchivo(file.path);
|
|
throw new Error('No es posible realizar esta inscripción. ' + err);
|
|
});
|
|
};
|
|
|
|
module.exports = inscripcion;
|