Files
pcpuma_acatlan_api/server/controller/Equipo/update.js
T

57 lines
1.8 KiB
JavaScript
Raw Normal View History

2021-05-04 01:08:19 -05:00
const validar = require('../../helper/validar');
const dbPath = '../../db/tablas';
2021-05-27 10:58:49 -05:00
const Carrito = require(`${dbPath}/Carrito`);
2021-09-20 00:21:36 -05:00
const Equipo = require(`${dbPath}/Equipo`);
2021-05-27 10:58:49 -05:00
const Programa = require(`${dbPath}/Programa`);
2021-05-04 01:08:19 -05:00
const Status = require(`${dbPath}/Status`);
2021-05-26 19:14:11 -05:00
const update = async (body) => {
2022-01-03 16:47:25 -06:00
const idEquipo = validar.validarNumeroEntero(
body.idEquipo,
'id equipo',
true
);
const idStatus = body.idStatus
? validar.validarNumeroEntero(body.idStatus, 'id status', true)
: null;
const idCarrito = body.idCarrito
? validar.validarNumeroEntero(body.idCarrito, 'id carrito', true)
: null;
2021-05-27 10:58:49 -05:00
const idPrograma = body.idPrograma
2022-01-03 16:47:25 -06:00
? validar.validarNumeroEntero(body.idPrograma, 'id programa', true)
2021-05-27 10:58:49 -05:00
: null;
let update = {};
2021-05-26 19:14:11 -05:00
2022-01-24 15:58:34 -06:00
return Status.findOne({ where: { idStatus } })
.then((res) => {
if (idStatus) {
if (!res) throw new Error('No existe este status.');
update.idStatus = idStatus;
}
return Carrito.findOne({ where: { idCarrito } });
})
.then((res) => {
if (idCarrito) {
if (!res) throw new Error('No existe este carrito.');
update.idCarrito = idCarrito;
}
return Programa.findOne({ where: { idPrograma } });
})
.then((res) => {
if (idPrograma) {
if (!res) throw new Error('No existe este programa.');
update.idPrograma = idPrograma;
}
if (validar.validarObjetoVacio(update))
throw new Error('No se envio nada para actualizar.');
return Equipo.findOne({ where: { idEquipo } });
})
2021-05-26 19:14:11 -05:00
.then((res) => {
if (!res) throw new Error('No existe este equipo.');
2021-05-27 10:58:49 -05:00
return Equipo.update(update, { where: { idEquipo } });
2021-05-26 19:14:11 -05:00
})
2022-02-01 06:44:34 -06:00
.then(() => ({ message: 'Se actualizó correctamente el equipo.' }));
2021-05-26 19:14:11 -05:00
};
2021-05-04 01:08:19 -05:00
module.exports = update;