diff --git a/server/controller/Servicio/cancelar.js b/server/controller/Servicio/cancelar.js index e69de29..e0a2445 100644 --- a/server/controller/Servicio/cancelar.js +++ b/server/controller/Servicio/cancelar.js @@ -0,0 +1,19 @@ +const validar = require('../../helper/validar'); +const Servicio = require('../../db/tablas/Servicio'); + +const cancelar = async (body) => { + let idServicio = validar.validarId(body.idServicio); + + return Servicio.findOne({ where: { idServicio } }) + .then((res) => { + if (!res) throw new Error('No existe este servicio.'); + if (res.idStatus === 10) + throw new Error('Este servicio ya fue cancelado'); + return Servicio.update({ idStatus: 10 }, { where: { idServicio } }); + }) + .then((res) => { + return { message: 'Se cancelo correctamente el servicio.' }; + }); +}; + +module.exports = cancelar; diff --git a/server/controller/Servicio/serviciosAdmin.js b/server/controller/Servicio/serviciosAdmin.js index 2af8762..0036d4c 100644 --- a/server/controller/Servicio/serviciosAdmin.js +++ b/server/controller/Servicio/serviciosAdmin.js @@ -16,9 +16,9 @@ const serviciosAdmin = async (body) => { let numeroCuenta = body.numeroCuenta ? validar.validarNumeroCuenta(body.numeroCuenta) : ''; - let where = {}; + let where = { [Op.and]: [{ idStatus: { [Op.ne]: 10 } }] }; - if (idStatus) where.idStatus = idStatus; + if (idStatus) where[Op.and].push({ idStatus }); return Usuario.findOne({ where: { idUsuario } }) .then((res) => { if (!res) throw new Error('No existe este usuario.'); diff --git a/server/controller/Servicio/serviciosResponsable.js b/server/controller/Servicio/serviciosResponsable.js index 7d91bb7..f70af6b 100644 --- a/server/controller/Servicio/serviciosResponsable.js +++ b/server/controller/Servicio/serviciosResponsable.js @@ -17,9 +17,9 @@ const serviciosResponsable = async (body) => { let numeroCuenta = body.numeroCuenta ? validar.validarNumeroCuenta(body.numeroCuenta) : ''; - let where = {}; + let where = { [Op.and]: [{ idStatus: { [Op.ne]: 10 } }] }; - if (idStatus) where.idStatus = idStatus; + if (idStatus) where[Op.and].push({ idStatus }); return Usuario.findOne({ where: { idUsuario } }) .then((res) => { if (!res) throw new Error('No existe este usuario.'); diff --git a/server/routes/Servicio.js b/server/routes/Servicio.js index 8c30d88..6f3c670 100644 --- a/server/routes/Servicio.js +++ b/server/routes/Servicio.js @@ -8,6 +8,8 @@ const serviciosAdmin = require('../controller/Servicio/serviciosAdmin'); const serviciosResponsable = require('../controller/Servicio/serviciosResponsable'); const admin = require('../controller/Servicio/admin'); const alumno = require('../controller/Servicio/alumno'); +const cancelar = require('../controller/Servicio/cancelar'); + app.post(`${route}/nuevo`, upload.single('cartaAceptacion'), (req, res) => { return nuevo(JSON.parse(req.body.alumno), req.file.filename) @@ -59,4 +61,14 @@ app.get(`${route}/alumno`, (req, res) => { }); }); +app.put(`${route}/cancelar`, (req, res) => { + return cancelar(req.body) + .then((data) => { + res.status(200).json(data); + }) + .catch((err) => { + res.status(400).json({ message: err.message }); + }); +}); + module.exports = app;