diff --git a/src/controller/prestamo/activar.js b/src/controller/prestamo/activar.js new file mode 100644 index 0000000..e69de29 diff --git a/src/controller/prestamo/cancelar.js b/src/controller/prestamo/cancelar.js new file mode 100644 index 0000000..e69de29 diff --git a/src/controller/prestamo/equipo.js b/src/controller/prestamo/equipo.js new file mode 100644 index 0000000..6ca9bca --- /dev/null +++ b/src/controller/prestamo/equipo.js @@ -0,0 +1,153 @@ +const moment = require('moment') +const { Op } = require('sequelize') +const validador = require('../validador') +const Prestamo = require('../../db/tables/Prestamo') +const Horario = require('../../db/tables/Horario') +const Equipo = require('../../db/tables/Equipo') +const Carrito = require('../../db/tables/Carrito') +const TipoCarrito = require('../../db/tables/TipoCarrito') +const Status = require('../../db/tables/Status') + +const equipo = async (body) => { + let idUsuario = await validador.validarId(body.idUsuario).then((res) => res) + // let idTipoUsuario = await validador + // .validarId(body.idTipoUsuario) + // .then((res) => res) + let idTipoCarrito = await validador + .validarId(body.idTipoCarrito) + .then((res) => res) + let idHorario = await validador.validarId(body.idHorario).then((res) => res) + let kiosko = '1' + let horaFin = await validador.validarTexto(body.horario).then((res) => { + let hora = res.substr(res.indexOf('-') + 1) + let horaNumero = Number(hora.substr(0, hora.indexOf(':'))) + let horaLimite = moment() + + horaLimite.set('hour', horaNumero) + horaLimite.set('minute', 0) + horaLimite.set('second', 0) + return horaLimite + }) + let busquedaPrestamo = { + where: { [Op.and]: [{ idUsuario }, { activo: true }] }, + } + let busquedaEquipo = { + include: [ + { + model: Carrito, + where: { kiosko }, + include: [{ model: TipoCarrito, where: { idTipoCarrito } }], + }, + ], + where: { idStatus: 1 }, + } + + return Prestamo.findOne(busquedaPrestamo) + .then((res) => { + if (res) validador.error('Este usuario ya tiene un prestamo activo.', 400) + busquedaPrestamo = { + where: { + [Op.and]: [ + { idHorario }, + { activo: true }, + { idEquipo: { [Op.not]: null } }, + ], + }, + include: [ + { + model: Equipo, + include: [ + { + model: Carrito, + include: [{ model: TipoCarrito, where: { idTipoCarrito } }], + }, + ], + }, + ], + } + return Prestamo.findAll(busquedaPrestamo) + }) + .then(async (res) => { + let equipo = await Equipo.findAll(busquedaEquipo).then((res2) => { + for (let i = 0; i < res2.length; i++) { + let enUso = false + + for (let j = 0; j < res.length; j++) { + if (res2[i].idEquipo === res[i].idEquipo) { + enUso = true + break + } + } + if (!enUso) return res2[i] + } + return {} + }) + let data = { + idUsuario, + idEquipo: equipo.idEquipo, + idHorario, + horaFin, + } + + if (!equipo) + validador.error('No hay equipos disponibles en ese horario', 400) + return Prestamo.create(data) + }) + .then((res) => { + return Prestamo.findOne({ + where: { idPrestamo: res.idPrestamo }, + include: [ + { model: Horario }, + { + model: Equipo, + include: [ + { model: Carrito, include: [{ model: TipoCarrito }] }, + { model: Status }, + ], + }, + ], + }) + }) + .then((res) => { + return { + code: 200, + data: { + idPrestamo: res.idPrestamo, + horaFin: res.horaFin, + activo: res.activo, + createdAt: res.createdAt, + updatedAt: res.updatedAt, + idUsuario: res.idUsuario, + idOperadorEntrega: res.idOperadorEntrega, + idOperadorRegreso: res.idOperadorRegreso, + Horario: { + idHorario: res.Horario.idHorario, + horario: res.Horario.horario, + }, + Equipo: { + idEquipo: res.Equipo.idEquipo, + noSerie: res.Equipo.noSerie, + noInventario: res.Equipo.noInventario, + sobrenombre: res.Equipo.sobrenombre, + activo: res.Equipo.activo, + updatedAt: res.Equipo.updatedAt, + Status: { + idStatus: res.Equipo.Status.idStatus, + status: res.Equipo.Status.status, + }, + Carrito: { + idCarrito: res.Equipo.Carrito.idCarrito, + sobrenombre: res.Equipo.Carrito.sobrenombre, + kiosko: res.Equipo.Carrito.kiosko, + TipoCarrito: { + idTipoCarrito: res.Equipo.Carrito.TipoCarrito.idTipoCarrito, + nombre: res.Equipo.Carrito.TipoCarrito.nombre, + }, + }, + }, + }, + } + }) +} + +module.exports = equipo diff --git a/src/controller/prestamo/regreso.js b/src/controller/prestamo/regreso.js new file mode 100644 index 0000000..e69de29 diff --git a/src/controller/prestamo/salon.js b/src/controller/prestamo/salon.js new file mode 100644 index 0000000..e69de29 diff --git a/src/controller/usuario/info.js b/src/controller/usuario/info.js new file mode 100644 index 0000000..e69de29 diff --git a/src/controller/validador.js b/src/controller/validador.js index e3e2e6c..2e6a14b 100644 --- a/src/controller/validador.js +++ b/src/controller/validador.js @@ -1,4 +1,3 @@ -const { text } = require('body-parser') const validator = require('validator') const objetoError = (message, code) => { diff --git a/src/db/tables/Multa.js b/src/db/tables/Multa.js index 42ed045..f3222a4 100644 --- a/src/db/tables/Multa.js +++ b/src/db/tables/Multa.js @@ -18,7 +18,7 @@ Multa.init( allowNull: false, }, multa: { - type: DataTypes.STRING(100), + type: DataTypes.DATE, allowNull: false, }, }, diff --git a/src/db/tables/Prestamo.js b/src/db/tables/Prestamo.js index fb618b1..c84d17f 100644 --- a/src/db/tables/Prestamo.js +++ b/src/db/tables/Prestamo.js @@ -17,13 +17,9 @@ Prestamo.init( autoIncrement: true, unique: true, }, - horaInicio: { - type: DataTypes.STRING(100), - allowNull: false, - }, horaFin: { - type: DataTypes.STRING(100), - allowNull: false, + type: DataTypes.DATE, + allowNull: true, }, activo: { type: DataTypes.BOOLEAN, diff --git a/src/routes/index.js b/src/routes/index.js index e6f04bb..8326445 100644 --- a/src/routes/index.js +++ b/src/routes/index.js @@ -8,5 +8,6 @@ app.use(require('./carrera')) app.use(require('./horario')) app.use(require('./equipo')) app.use(require('./operador')) +app.use(require('./prestamo')) module.exports = app diff --git a/src/routes/prestamo.js b/src/routes/prestamo.js index e69de29..f70375a 100644 --- a/src/routes/prestamo.js +++ b/src/routes/prestamo.js @@ -0,0 +1,25 @@ +const express = require('express') +const app = express() +const get = require('../controller/prestamo/get') +const equipo = require('../controller/prestamo/equipo') +const route = '/prestamo' + +// app.get(`${route}`, (req, res) => { +// return get(req.query) +// .then((data) => { +// res.status(data.code).json({ prestamo: data.data }) +// }) +// .catch((err) => res.status(err.code).json({ err: err.message })) +// }) + +app.post(`${route}/equipo`, (req, res) => { + return equipo(req.body) + .then((data) => { + res.status(data.code).json({ prestamo: data.data }) + }) + .catch((err) => { + res.status(err.code).json({ err: err.message }) + }) +}) + +module.exports = app