pretsamo equipo listo
This commit is contained in:
@@ -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
|
||||
@@ -1,4 +1,3 @@
|
||||
const { text } = require('body-parser')
|
||||
const validator = require('validator')
|
||||
|
||||
const objetoError = (message, code) => {
|
||||
|
||||
@@ -18,7 +18,7 @@ Multa.init(
|
||||
allowNull: false,
|
||||
},
|
||||
multa: {
|
||||
type: DataTypes.STRING(100),
|
||||
type: DataTypes.DATE,
|
||||
allowNull: false,
|
||||
},
|
||||
},
|
||||
|
||||
@@ -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,
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user