Files
inscripciones_api/server/controller/Admin/validarTicket.js
T
2022-03-21 15:17:20 -06:00

62 lines
1.5 KiB
JavaScript

const { validarNumeroEntero } = require('../../helper/validar');
const dbPath = '../../db/tablas';
const Inscripcion = require(`${dbPath}/Inscripcion`);
const validarTicket = async (body) => {
const validacion = validarNumeroEntero(body.validacion, 'validacion', true);
const folio = validarNumeroEntero(body.folio, 'folio', true);
return Inscripcion.findOne({
where: { folio: body.folio },
})
.then((res) => {
if (!res) throw new Error('Este folio no existe');
idUsuario = res.idUsuario;
if (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,
},
{
where: {
folio,
},
}
);
})
.then((res) => {
if (body.validacion === 0) {
return Inscripcion.update(
{ motivo: body.motivo },
{
where: {
folio: body.folio,
},
}
);
}
})
.then((res) => {
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;