diff --git a/server/controller/Carrito/equiposCarrito.js b/server/controller/Carrito/equiposCarrito.js index 54eb1de..51393d4 100644 --- a/server/controller/Carrito/equiposCarrito.js +++ b/server/controller/Carrito/equiposCarrito.js @@ -18,8 +18,6 @@ const equiposCarrito = async (body) => { 'numeroSerie', 'numeroInventario', 'sobrenombre', - 'apartado', - 'enUso', ], }); }); diff --git a/server/controller/Equipo/equipo.js b/server/controller/Equipo/equipo.js index 10237ab..e3d23db 100644 --- a/server/controller/Equipo/equipo.js +++ b/server/controller/Equipo/equipo.js @@ -26,14 +26,7 @@ const equipo = async (body) => { { model: Status }, { model: Programa }, ], - attributes: [ - 'idEquipo', - 'numeroSerie', - 'numeroInventario', - 'sobrenombre', - 'apartado', - 'enUso', - ], + attributes: ['idEquipo', 'numeroSerie', 'numeroInventario', 'sobrenombre'], }).then((res) => { if (!res) throw new Error('No existe este equipo.'); return res; diff --git a/server/controller/Equipo/equipos.js b/server/controller/Equipo/equipos.js index c90dc81..52925fb 100644 --- a/server/controller/Equipo/equipos.js +++ b/server/controller/Equipo/equipos.js @@ -17,8 +17,6 @@ const equipos = async () => { 'numeroSerie', 'numeroInventario', 'sobrenombre', - 'apartado', - 'enUso', ], }); return res; diff --git a/server/controller/Prestamo/activos.js b/server/controller/Prestamo/activos.js index cfde950..387af5b 100644 --- a/server/controller/Prestamo/activos.js +++ b/server/controller/Prestamo/activos.js @@ -64,8 +64,6 @@ const activos = async (body) => { 'numeroSerie', 'numeroInventario', 'sobrenombre', - 'apartado', - 'enUso', ], }, { diff --git a/server/controller/Prestamo/cancelarOperador.js b/server/controller/Prestamo/cancelarOperador.js index c1f9cfb..f75c237 100644 --- a/server/controller/Prestamo/cancelarOperador.js +++ b/server/controller/Prestamo/cancelarOperador.js @@ -27,12 +27,12 @@ const cancelarOperador = async (body) => { 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.' - ); + // if (res.Equipo.idStatus === 2) + // throw new Error( + // 'El equipo ya fue entregado al usuario, ya no se puede cancelar.' + // ); return Equipo.update( - { apartado: false }, + { idStatus: 3 }, { where: { idEquipo: res.Equipo.idEquipo } } ); }) diff --git a/server/controller/Prestamo/cancelarUsuario.js b/server/controller/Prestamo/cancelarUsuario.js index 519aa8f..85aa0c6 100644 --- a/server/controller/Prestamo/cancelarUsuario.js +++ b/server/controller/Prestamo/cancelarUsuario.js @@ -16,12 +16,12 @@ const cancelarUsuario = async (body) => { throw new Error( 'Este prestamo ya no esta activo, ya no se puede cancelar.' ); - if (res.Equipo.enUso) + if (res.Equipo.idStatus === 2) throw new Error( 'El equipo ya fue entregado al usuario, ya no se puede cancelar.' ); return Equipo.update( - { apartado: false }, + { idStatus: 3 }, { where: { idEquipo: res.Equipo.idEquipo } } ); }) diff --git a/server/controller/Prestamo/entregar.js b/server/controller/Prestamo/entregar.js index dd366e1..d5461cb 100644 --- a/server/controller/Prestamo/entregar.js +++ b/server/controller/Prestamo/entregar.js @@ -7,9 +7,9 @@ const Modulo = require(`${dbPath}/Modulo`); const Operador = require(`${dbPath}/Operador`); const Prestamo = require(`${dbPath}/Prestamo`); const TipoCarrito = require(`${dbPath}/TipoCarrito`); +const ahora = moment(); const entregar = async (body) => { - const ahora = moment(); const idPrestamo = validarNumeroEntero(body.idPrestamo, 'id prestamo', true); const idOperadorEntrega = validarNumeroEntero( body.idOperadorEntrega, @@ -30,16 +30,19 @@ const entregar = async (body) => { if (!res) throw new Error('No existe este prestamo.'); idEquipo = res.Equipo.idEquipo; if (!res.activo) throw new Error('Este prestamo ya no esta activo.'); - else if (res.Equipo.enUso) + else if (res.Equipo.idStatus === 2) throw new Error('Ya se entrego el equipo al usuario.'); - else if (moment(res.horaMaxRecoger) < ahora) { - await Equipo.update({ apartado: false }, { where: { idEquipo } }); - await Prestamo.update({ activo: false }, { where: { idPrestamo } }); - throw new Error( - 'Ya paso el tiempo limite para recoger el dispositivo.' - ); - } - return Equipo.update({ enUso: true }, { where: { idEquipo } }); + else if (moment(res.horaMaxRecoger) < ahora) + await Equipo.update({ idStatus: 3 }, { where: { idEquipo } }) + .then((res) => + Prestamo.update({ activo: false }, { where: { idPrestamo } }) + ) + .then((res) => { + throw new Error( + 'Ya paso el tiempo limite para recoger el dispositivo.' + ); + }); + return Equipo.update({ idStatus: 2 }, { where: { idEquipo } }); }) .then((res) => { Prestamo.update( @@ -58,6 +61,7 @@ const entregar = async (body) => { { model: Carrito, include: [{ model: TipoCarrito }, { model: Modulo }], + attributes: ['idCarrito', 'sobrenombre'], }, ], attributes: [ diff --git a/server/controller/Prestamo/pedir.js b/server/controller/Prestamo/pedir.js index 9871cc3..03a47f0 100644 --- a/server/controller/Prestamo/pedir.js +++ b/server/controller/Prestamo/pedir.js @@ -9,19 +9,19 @@ const Programa = require(`${dbPath}/Programa`); const Status = require(`${dbPath}/Status`); const TipoCarrito = require(`${dbPath}/TipoCarrito`); const Usuario = require(`${dbPath}/Usuario`); +const ahora = moment(); +const horaMax = moment( + `${ahora.year()}-${ + ahora.month() < 9 ? '0' + (ahora.month() + 1) : ahora.month() + 1 + }-${ahora.date() < 9 ? '0' + ahora.date() : ahora.date()} 19:00` +); +const horaMin = moment( + `${ahora.year()}-${ + ahora.month() < 9 ? '0' + (ahora.month() + 1) : ahora.month() + 1 + }-${ahora.date() < 9 ? '0' + ahora.date() : ahora.date()} 09:00` +); const pedir = async (body) => { - const ahora = moment(); - const horaMax = moment( - `${ahora.year()}-${ - ahora.month() < 9 ? '0' + (ahora.month() + 1) : ahora.month() + 1 - }-${ahora.date() < 9 ? '0' + ahora.date() : ahora.date()} 19:00` - ); - const horaMin = moment( - `${ahora.year()}-${ - ahora.month() < 9 ? '0' + (ahora.month() + 1) : ahora.month() + 1 - }-${ahora.date() < 9 ? '0' + ahora.date() : ahora.date()} 09:00` - ); const idUsuario = validarNumeroEntero(body.idUsuario, 'id usuario', true); const idTipoCarrito = validarNumeroEntero( body.idTipoCarrito, @@ -69,18 +69,14 @@ const pedir = async (body) => { ); return Equipo.findOne({ where: { - apartado: false, - idStatus: 1, + idStatus: 3, idPrograma, }, include: [ { model: Carrito, where: { idTipoCarrito, idModulo, activo: true }, - include: [{ model: TipoCarrito }, { model: Modulo }], }, - { model: Status }, - { model: Programa }, ], order: [['updatedAt']], }); @@ -91,7 +87,7 @@ const pedir = async (body) => { 'No hay un equipo de cómputo disponible con las características solicitadas. Intenta más tarde o elige otro tipo de equipo.' ); idEquipo = res.idEquipo; - return Equipo.update({ apartado: true }, { where: { idEquipo } }); + return Equipo.update({ idStatus: 1 }, { where: { idEquipo } }); }) .then((res) => Prestamo.create({ @@ -112,6 +108,7 @@ const pedir = async (body) => { include: [{ model: Modulo }, { model: TipoCarrito }], attributes: ['idCarrito', 'sobrenombre'], }, + { model: Status }, { model: Programa }, ], attributes: [ diff --git a/server/controller/Prestamo/regresar.js b/server/controller/Prestamo/regresar.js index 2acf67a..2fefd22 100644 --- a/server/controller/Prestamo/regresar.js +++ b/server/controller/Prestamo/regresar.js @@ -5,6 +5,8 @@ const Equipo = require(`${dbPath}/Equipo`); const Multa = require(`${dbPath}/Multa`); const Operador = require(`${dbPath}/Operador`); const Prestamo = require(`${dbPath}/Prestamo`); +const ahora = moment(); +const ahoraAux = moment(); const regresar = async (body) => { const idPrestamo = validarNumeroEntero(body.idPrestamo, 'id prestamo', true); @@ -13,7 +15,6 @@ const regresar = async (body) => { 'id operador regreso', true ); - const ahora = moment(); let horaFin = moment(); let diferencia = null; @@ -28,23 +29,20 @@ const regresar = async (body) => { .then(async (res) => { if (!res) throw new Error('No existe este préstamo.'); if (!res.activo) throw new Error('Este préstamo ya no esta activo.'); - if (!res.Equipo.enUso) + if (!res.Equipo.idStatus === 1) throw new Error('Aun no se ha entregado el equipo al usuario.'); horaFin = moment(res.horaFin); diferencia = (ahora - horaFin) / 60000; - if (diferencia >= 15) { - const ahora = moment(); - + if (diferencia >= 15) await Multa.create({ idPrestamo, idInfraccion: 1, idOperadorMulta: idOperadorRegreso, descripcion: `Se paso por ${diferencia} minutos para entregar el equipo.`, - fechaFin: ahora.add(parseInt(diferencia / 15) * 7, 'd').format(), + fechaFin: ahoraAux.add(parseInt(diferencia / 15) * 7, 'd').format(), }); - } return Equipo.update( - { enUso: false, apartado: false }, + { idStatus: 3 }, { where: { idEquipo: res.Equipo.idEquipo } } ); }) diff --git a/server/controller/Prestamo/regresarNumeroInventario.js b/server/controller/Prestamo/regresarNumeroInventario.js index 5b4a4b5..5fd4beb 100644 --- a/server/controller/Prestamo/regresarNumeroInventario.js +++ b/server/controller/Prestamo/regresarNumeroInventario.js @@ -5,6 +5,8 @@ const Equipo = require(`${dbPath}/Equipo`); const Multa = require(`${dbPath}/Multa`); const Operador = require(`${dbPath}/Operador`); const Prestamo = require(`${dbPath}/Prestamo`); +const ahora = moment(); +const ahoraAux = moment(); const regresarNumeroInventario = async (body) => { const numeroInventario = validar.validarAlfanumerico( @@ -18,10 +20,9 @@ const regresarNumeroInventario = async (body) => { 'id operador regreso', true ); - const ahora = moment(); let horaFin = moment(); let diferencia = null; - let idPrestamo = {}; + let idPrestamo = null; return Operador.findOne({ where: { idOperador: idOperadorRegreso } }) .then((res) => { @@ -34,24 +35,21 @@ const regresarNumeroInventario = async (body) => { .then(async (res) => { if (!res) throw new Error('No existe un prestamo activo con este equipo.'); - if (!res.Equipo.enUso) + if (!res.Equipo.idStatus === 1) throw new Error('Aun no se ha entregado el equipo al usuario.'); idPrestamo = res.idPrestamo; horaFin = moment(res.horaFin); diferencia = (ahora - horaFin) / 60000; - if (diferencia >= 15) { - const ahora = moment(); - + if (diferencia >= 15) await Multa.create({ idPrestamo, idInfraccion: 1, idOperadorMulta: idOperadorRegreso, descripcion: `Se paso por ${diferencia} minutos para entregar el equipo.`, - fechaFin: ahora.add(parseInt(diferencia / 15) * 7, 'd').format(), + fechaFin: ahoraAux.add(parseInt(diferencia / 15) * 7, 'd').format(), }); - } return Equipo.update( - { enUso: false, apartado: false }, + { idStatus: 3 }, { where: { idEquipo: res.Equipo.idEquipo } } ); }) diff --git a/server/controller/Prestamo/status.js b/server/controller/Prestamo/status.js index d0b7c70..07ecfe3 100644 --- a/server/controller/Prestamo/status.js +++ b/server/controller/Prestamo/status.js @@ -6,87 +6,75 @@ const Equipo = require(`${dbPath}/Equipo`); const Modulo = require(`${dbPath}/Modulo`); const Prestamo = require(`${dbPath}/Prestamo`); const Programa = require(`${dbPath}/Programa`); +const Status = require(`${dbPath}/Status`); const TipoCarrito = require(`${dbPath}/TipoCarrito`); +const ahora = moment(); const status = async (body) => { const idPrestamo = validarNumeroEntero(body.idPrestamo, 'id prestamo', true); - const ahora = moment(); + let message = ''; + let atraso = false; return Prestamo.findOne({ where: { idPrestamo }, - include: [ - { - model: Equipo, + include: [{ model: Equipo }], + }) + .then(async (res) => { + if (!res) throw new Error('No existe este prestamo.'); + if (!res.activo) { + if (!res.horaEntrega) message = 'Este prestamo fue cancelado.'; + else message = 'Este prestamo no es encuentra activo.'; + } else if (res.Equipo.idStatus === 2) { + if (moment(res.horaFin) < ahora) { + message = + 'Ya paso la hora de entrega. El usuario puede ser acreedor a una multa por el retraso.'; + atraso = true; + } else message = 'El equipo ya fue entregado al usuario.'; + } else if (moment(res.horaMaxRecoger) < ahora) { + await Equipo.update( + { idStatus: 3 }, + { where: { idEquipo: res.Equipo.idEquipo } } + ).then((res) => + Prestamo.update({ activo: false }, { where: { idPrestamo } }) + ); + message = + 'Ya paso el tiempo limite para recoger el dispositivo. Se ha cancelado tu prestamo.'; + } else message = 'Aun no se entrega el equipo al usuario.'; + return Prestamo.findOne({ + where: { idPrestamo }, include: [ { - model: Carrito, - include: [{ model: TipoCarrito }, { model: Modulo }], - attributes: ['idCarrito', 'sobrenombre'], + model: Equipo, + include: [ + { + model: Carrito, + include: [{ model: TipoCarrito }, { model: Modulo }], + attributes: ['idCarrito', 'sobrenombre'], + }, + { model: Status }, + { model: Programa }, + ], + attributes: [ + 'idEquipo', + 'numeroSerie', + 'numeroInventario', + 'sobrenombre', + ], }, - { model: Programa }, ], attributes: [ - 'idEquipo', - 'numeroSerie', - 'numeroInventario', - 'sobrenombre', + 'idPrestamo', + 'activo', + 'horaMaxRecoger', + 'horaInicio', + 'horaFin', + 'horaEntrega', + 'canceladoUsuario', + 'createdAt', ], - }, - ], - attributes: [ - 'idPrestamo', - 'activo', - 'horaMaxRecoger', - 'horaInicio', - 'horaFin', - 'horaEntrega', - 'canceladoUsuario', - 'createdAt', - ], - }).then(async (res) => { - if (!res) throw new Error('No existe este prestamo.'); - if (!res.activo) { - if (!res.horaEntrega) - return { - message: 'Este prestamo fue cancelado.', - prestamo: res, - }; - return { - message: 'Este prestamo no es encuentra activo.', - prestamo: res, - }; - } else if (res.Equipo.enUso) { - if (moment(res.horaFin) < ahora) { - return { - message: - 'Ya paso la hora de entrega. El usuario puede ser acreedor a una multa por el retraso.', - prestamo: res, - atraso: true, - }; - } - return { - message: 'El equipo ya fue entregado al usuario.', - prestamo: res, - }; - } else if (moment(res.horaMaxRecoger) < ahora) { - await Equipo.update( - { apartado: false }, - { where: { idEquipo: res.Equipo.idEquipo } } - ); - await Prestamo.update({ activo: false }, { where: { idPrestamo } }); - res.Equipo.apartado = false; - res.activo = false; - return { - message: - 'Ya paso el tiempo limite para recoger el dispositivo. Se ha cancelado tu prestamo.', - prestamo: res, - }; - } - return { - message: 'Aun no se entrega el equipo al usuario.', - prestamo: res, - }; - }); + }); + }) + .then((res) => ({ message, prestamo: res, atraso })); }; module.exports = status; diff --git a/server/controller/Status/get.js b/server/controller/Status/get.js index 2bed16c..29d3503 100644 --- a/server/controller/Status/get.js +++ b/server/controller/Status/get.js @@ -1,5 +1,6 @@ +const { Op } = require('sequelize'); const Status = require('../../db/tablas/Status'); -const get = async () => Status.findAll(); +const get = async () => Status.findAll({ where: { idStatus: { [Op.gt]: 2 } } }); module.exports = get; diff --git a/server/db/install.js b/server/db/install.js index 046494a..86ec4b2 100644 --- a/server/db/install.js +++ b/server/db/install.js @@ -138,12 +138,19 @@ const dataTipoCarrito = async () => { tipoCarrito: data[i].tipo, letra: data[i].letra, }); - console.log(`Se agrego el tipo carrito ${data[i]}.`.magenta); + console.log(`Se agrego el tipo carrito ${data[i].tipo}.`.magenta); } }; const dataStatus = async () => { - const data = ['Activo', 'Cargando', 'Reparación', 'Mantenimiento']; + const data = [ + 'Apartado', + 'En uso', + 'Activo', + 'Cargando', + 'Reparación', + 'Mantenimiento', + ]; console.log('\nPaso 6) Insertando data status.'.bold.blue); for (let i = 0; i < data.length; i++) { diff --git a/server/db/tablas/Equipo.js b/server/db/tablas/Equipo.js index f2c3232..d39ec5a 100644 --- a/server/db/tablas/Equipo.js +++ b/server/db/tablas/Equipo.js @@ -26,16 +26,6 @@ Equipo.init( type: DataTypes.STRING(4), allowNull: false, }, - apartado: { - type: DataTypes.BOOLEAN, - allowNull: false, - defaultValue: false, - }, - enUso: { - type: DataTypes.BOOLEAN, - allowNull: false, - defaultValue: false, - }, }, { sequelize, @@ -60,7 +50,7 @@ Equipo.belongsTo(Status, { name: 'idStatus', type: DataTypes.INTEGER, allowNull: false, - defaultValue: 1, + defaultValue: 3, }, });