cancelar
This commit is contained in:
@@ -0,0 +1,5 @@
|
||||
const Modulo = require('../../db/tablas/Modulo');
|
||||
|
||||
const get = async () => Modulo.findAll();
|
||||
|
||||
module.exports = get;
|
||||
@@ -0,0 +1,34 @@
|
||||
const validar = require('../../helper/validar');
|
||||
const dbPath = '../../db/tablas';
|
||||
const Prestamo = require(`${dbPath}/Prestamo`);
|
||||
const Equipo = require(`${dbPath}/Equipo`);
|
||||
|
||||
const cancelar = async (body) => {
|
||||
const idPrestamo = validar.validarId(body.idPrestamo);
|
||||
|
||||
return Prestamo.findOne({
|
||||
where: { idPrestamo },
|
||||
include: [{ model: Equipo }],
|
||||
})
|
||||
.then((res) => {
|
||||
if (!res) throw new Error('Este prestamo no existe.');
|
||||
if (!res.activo)
|
||||
throw new Error(
|
||||
'Este prestamo ya no esta activo, ya no se puede cancelar.'
|
||||
);
|
||||
if (res.Equipo.enUso)
|
||||
throw new Error(
|
||||
'El equipo ya fue entregado al usuario, ya no se puede cancelar.'
|
||||
);
|
||||
return Equipo.update(
|
||||
{ apartado: false },
|
||||
{ where: { idEquipo: res.Equipo.idEquipo } }
|
||||
);
|
||||
})
|
||||
.then((res) =>
|
||||
Prestamo.update({ activo: false }, { where: { idPrestamo } })
|
||||
)
|
||||
.then((res) => ({ message: 'Se cancelo el prestamo correctamente.' }));
|
||||
};
|
||||
|
||||
module.exports = cancelar;
|
||||
@@ -57,7 +57,7 @@ const pedir = async (body) => {
|
||||
include: [
|
||||
{
|
||||
model: Carrito,
|
||||
where: { idTipoCarrito, idModulo },
|
||||
where: { idTipoCarrito, idModulo, activo: true },
|
||||
include: [{ model: TipoCarrito }, { model: Modulo }],
|
||||
},
|
||||
{ model: Status },
|
||||
|
||||
@@ -0,0 +1,5 @@
|
||||
const Status = require('../../db/tablas/Status');
|
||||
|
||||
const get = async () => Status.findAll();
|
||||
|
||||
module.exports = get;
|
||||
@@ -1,4 +1,3 @@
|
||||
const validar = require('../../helper/validar');
|
||||
const TipoCarrito = require('../../db/tablas/TipoCarrito');
|
||||
|
||||
const get = async () => TipoCarrito.findAll();
|
||||
|
||||
@@ -12,6 +12,7 @@ const recoger = require(`${controllerPath}/recoger`);
|
||||
const regresar = require(`${controllerPath}/regresar`);
|
||||
const prestamo = require(`${controllerPath}/prestamo`);
|
||||
const prestamoUsuario = require(`${controllerPath}/prestamoUsuario`);
|
||||
const cancelar = require(`${controllerPath}/cancelar`);
|
||||
|
||||
app.post(
|
||||
`${route}/pedir`,
|
||||
@@ -55,6 +56,20 @@ app.put(
|
||||
}
|
||||
);
|
||||
|
||||
app.put(
|
||||
`${route}/cancelar`,
|
||||
// verificaToken,
|
||||
(req, res) => {
|
||||
return cancelar(req.body)
|
||||
.then((data) => {
|
||||
res.status(200).json(data);
|
||||
})
|
||||
.catch((err) => {
|
||||
res.status(400).json({ message: err.message });
|
||||
});
|
||||
}
|
||||
);
|
||||
|
||||
app.get(
|
||||
`${route}/activos`,
|
||||
// verificaToken,
|
||||
|
||||
Reference in New Issue
Block a user