2022-02-13 12:24:43 -06:00
|
|
|
const validar = require('../../helper/validar');
|
2021-05-04 01:08:19 -05:00
|
|
|
const dbPath = '../../db/tablas';
|
|
|
|
|
const Carrito = require(`${dbPath}/Carrito`);
|
2022-02-13 12:24:43 -06:00
|
|
|
const TipoCarrito = require(`${dbPath}/TipoCarrito`);
|
2021-05-04 01:08:19 -05:00
|
|
|
|
2021-05-26 19:47:13 -05:00
|
|
|
const update = async (body) => {
|
2022-02-13 12:24:43 -06:00
|
|
|
const idCarrito = validar.validarNumeroEntero(
|
|
|
|
|
body.idCarrito,
|
|
|
|
|
'id carrito',
|
|
|
|
|
true
|
|
|
|
|
);
|
|
|
|
|
const idTipoCarrito = body.idTipoCarrito
|
|
|
|
|
? validar.validarNumeroEntero(body.idTipoCarrito, 'id tipo carrito', true)
|
2022-02-13 19:10:21 -06:00
|
|
|
: '';
|
2021-05-26 19:47:13 -05:00
|
|
|
let update = {};
|
|
|
|
|
|
2022-02-13 12:24:43 -06:00
|
|
|
return TipoCarrito.findOne({ where: { idTipoCarrito } })
|
|
|
|
|
.then((res) => {
|
2022-02-13 13:34:54 -06:00
|
|
|
if (idTipoCarrito) {
|
|
|
|
|
if (!res) throw new Error('No existe este tipo carrito.');
|
|
|
|
|
update.idTipoCarrito = idTipoCarrito;
|
|
|
|
|
}
|
2022-02-13 12:24:43 -06:00
|
|
|
return Carrito.findOne({ where: { idCarrito } });
|
|
|
|
|
})
|
2021-05-26 19:47:13 -05:00
|
|
|
.then((res) => {
|
|
|
|
|
if (!res) throw new Error('No existe este carrito.');
|
2022-02-01 06:44:34 -06:00
|
|
|
if (body.activo === 'desactivar') update.activo = false;
|
|
|
|
|
else if (body.activo === 'activar') update.activo = true;
|
2022-02-13 12:24:43 -06:00
|
|
|
if (validar.validarObjetoVacio(update))
|
2022-02-13 13:34:54 -06:00
|
|
|
throw new Error('No se envió nada para actualizar.');
|
2021-05-26 19:47:13 -05:00
|
|
|
return Carrito.update(update, { where: { idCarrito } });
|
|
|
|
|
})
|
2022-02-01 06:44:34 -06:00
|
|
|
.then(() => ({ message: 'Se actualizó correctamente el carrito.' }));
|
2021-05-26 19:47:13 -05:00
|
|
|
};
|
2021-05-04 01:08:19 -05:00
|
|
|
|
|
|
|
|
module.exports = update;
|