2020-09-03 01:45:50 -05:00
|
|
|
import { Request, Response } from 'express'
|
|
|
|
|
import { getRepository } from 'typeorm'
|
|
|
|
|
import { Prestamo } from '@src/entity/Prestamo'
|
|
|
|
|
import * as moment from 'moment'
|
|
|
|
|
import { Horario } from '@src/entity/Horario'
|
|
|
|
|
import { Usuario } from '@src/entity/Usuario'
|
|
|
|
|
import { Equipo } from '@src/entity/Equipo'
|
2020-09-08 15:39:32 -05:00
|
|
|
import { Operador } from '@src/entity/Operador'
|
|
|
|
|
import { Mesa } from '@src/entity/Mesa'
|
2020-09-16 19:20:19 -05:00
|
|
|
import { Sala } from '@src/entity/Sala'
|
2020-09-03 01:45:50 -05:00
|
|
|
|
|
|
|
|
export class ResevacionController {
|
|
|
|
|
static async espaciosDisponibles (req: Request, res: Response) {
|
|
|
|
|
const repository = getRepository(Horario)
|
|
|
|
|
const fecha = moment().format('L')
|
|
|
|
|
console.log(fecha)
|
|
|
|
|
const espacios = await repository
|
|
|
|
|
.createQueryBuilder('horario')
|
|
|
|
|
.leftJoinAndSelect('horario.prestamos', 'prestamo')
|
|
|
|
|
.where('prestamo.createdAt >= :fecha', { fecha })
|
|
|
|
|
.groupBy('horario.hora')
|
|
|
|
|
.getMany()
|
|
|
|
|
return res.status(200).json(espacios)
|
|
|
|
|
}
|
|
|
|
|
|
2020-09-16 19:20:19 -05:00
|
|
|
static async reservarEquipo (req: Request, res: Response) {
|
2020-09-03 01:45:50 -05:00
|
|
|
const prestamoRepository = getRepository(Prestamo)
|
|
|
|
|
const horarioRepository = getRepository(Horario)
|
|
|
|
|
const usuarioRepository = getRepository(Usuario)
|
|
|
|
|
const equipoRepository = getRepository(Equipo)
|
|
|
|
|
|
|
|
|
|
const { usuarioId, tipoId, horarioId } = req.body
|
|
|
|
|
|
2020-09-17 01:42:30 -05:00
|
|
|
console.log('equipo', usuarioId)
|
2020-09-03 01:45:50 -05:00
|
|
|
const usuario = await usuarioRepository.findOne(usuarioId, {
|
|
|
|
|
select: ['id', 'baja', 'multa']
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
if (!usuario) {
|
|
|
|
|
res.status(400).json({ err: 'Usuario no encontrado' })
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (usuario.baja) {
|
|
|
|
|
res.status(400).json({ err: 'El usuario esta dado de baja' })
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
const horario = await horarioRepository.findOne(horarioId)
|
|
|
|
|
|
|
|
|
|
if (!horario) {
|
|
|
|
|
res.status(400).json({ err: 'El horario no existe' })
|
2020-09-08 15:39:32 -05:00
|
|
|
return
|
2020-09-03 01:45:50 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const prestamoActivo = await prestamoRepository
|
|
|
|
|
.createQueryBuilder('prestamo')
|
|
|
|
|
.where('prestamo.usuarioId = :idUsuario', { idUsuario: usuario.id })
|
|
|
|
|
.andWhere('prestamo.activo = true')
|
|
|
|
|
.orWhere('prestamo.reservado = true')
|
|
|
|
|
.getOne()
|
|
|
|
|
|
|
|
|
|
if (prestamoActivo) {
|
|
|
|
|
res.status(400).json({ err: 'El usuario tiene un prestamo activo' })
|
2020-09-08 15:39:32 -05:00
|
|
|
return
|
2020-09-03 01:45:50 -05:00
|
|
|
}
|
|
|
|
|
const equipo = await equipoRepository
|
|
|
|
|
.createQueryBuilder('equipo')
|
|
|
|
|
.leftJoinAndSelect('equipo.carrito', 'carrito')
|
|
|
|
|
.leftJoinAndSelect('carrito.tipo', 'tipo')
|
|
|
|
|
.where('equipo.activo = :activo', { activo: false })
|
|
|
|
|
.andWhere('carrito.kiosko = :kiosko', { kiosko: 3 })
|
|
|
|
|
.andWhere('tipo.id = :tipoId', { tipoId })
|
|
|
|
|
.orderBy('equipo.updatedAt', 'ASC')
|
|
|
|
|
.getOne()
|
|
|
|
|
|
2020-09-08 15:39:32 -05:00
|
|
|
if (!equipo) {
|
|
|
|
|
res.status(400).json({ err: 'Ya no hay equipos' })
|
|
|
|
|
return
|
|
|
|
|
}
|
2020-09-03 01:45:50 -05:00
|
|
|
const prestamo = new Prestamo()
|
|
|
|
|
const horamax = horario.hora.split('-')[1].split(':')[0]
|
|
|
|
|
const m = moment()
|
|
|
|
|
.hour(+horamax)
|
|
|
|
|
.minute(0)
|
|
|
|
|
.toLocaleString()
|
|
|
|
|
|
|
|
|
|
equipo.activo = true
|
|
|
|
|
|
|
|
|
|
prestamo.equipo = equipo
|
|
|
|
|
prestamo.usuario = usuario
|
|
|
|
|
prestamo.horaMaxEntrega = m
|
|
|
|
|
prestamo.horario = horario
|
2020-09-17 01:42:30 -05:00
|
|
|
prestamo.tipo = 'equipo'
|
2020-09-03 01:45:50 -05:00
|
|
|
|
|
|
|
|
await equipoRepository.save(equipo)
|
|
|
|
|
await prestamoRepository.save(prestamo)
|
|
|
|
|
|
|
|
|
|
prestamo.qr = `https://api.qrserver.com/v1/create-qr-code/?size=150x150&data=${prestamo.id}`
|
|
|
|
|
|
|
|
|
|
console.log(prestamo)
|
|
|
|
|
res.status(200).json({
|
|
|
|
|
prestamo
|
|
|
|
|
})
|
|
|
|
|
}
|
2020-09-08 15:39:32 -05:00
|
|
|
|
2020-09-16 19:20:19 -05:00
|
|
|
static async reservarSala (req: Request, res: Response) {
|
|
|
|
|
const prestamoRepository = getRepository(Prestamo)
|
|
|
|
|
const horarioRepository = getRepository(Horario)
|
|
|
|
|
const usuarioRepository = getRepository(Usuario)
|
|
|
|
|
const salaRepository = getRepository(Sala)
|
|
|
|
|
|
|
|
|
|
const { usuarioId, horarioId } = req.body
|
2020-09-17 01:42:30 -05:00
|
|
|
console.log('sala', usuarioId)
|
2020-09-16 19:20:19 -05:00
|
|
|
const usuario = await usuarioRepository.findOne(usuarioId, {
|
2020-09-17 01:42:30 -05:00
|
|
|
relations: ['tipoUsuario'],
|
2020-09-16 19:20:19 -05:00
|
|
|
select: ['id', 'baja', 'multa', 'tipoUsuario']
|
|
|
|
|
})
|
2020-09-17 01:42:30 -05:00
|
|
|
console.log(usuario)
|
2020-09-16 19:20:19 -05:00
|
|
|
|
|
|
|
|
if (!usuario) {
|
|
|
|
|
res.status(400).json({ err: 'Usuario no encontrado' })
|
|
|
|
|
return
|
|
|
|
|
}
|
2020-09-17 01:42:30 -05:00
|
|
|
if (usuario.tipoUsuario.nombre !== 'profesor_interno') {
|
|
|
|
|
res.status(400).json({ err: 'Usuario no es profesor' })
|
2020-09-16 19:20:19 -05:00
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (usuario.baja) {
|
|
|
|
|
res.status(400).json({ err: 'El usuario esta dado de baja' })
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
const horario = await horarioRepository.findOne(horarioId)
|
|
|
|
|
|
|
|
|
|
if (!horario) {
|
|
|
|
|
res.status(400).json({ err: 'El horario no existe' })
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const prestamoActivo = await prestamoRepository
|
|
|
|
|
.createQueryBuilder('prestamo')
|
|
|
|
|
.where('prestamo.usuarioId = :idUsuario', { idUsuario: usuario.id })
|
|
|
|
|
.andWhere('prestamo.activo = true')
|
|
|
|
|
.orWhere('prestamo.reservado = true')
|
|
|
|
|
.getOne()
|
|
|
|
|
|
|
|
|
|
if (prestamoActivo) {
|
|
|
|
|
res.status(400).json({ err: 'El usuario tiene un prestamo activo' })
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
const sala = await salaRepository
|
|
|
|
|
.createQueryBuilder('sala')
|
2020-09-17 01:42:30 -05:00
|
|
|
.orderBy('sala.updatedAt', 'ASC')
|
2020-09-16 19:20:19 -05:00
|
|
|
.getOne()
|
|
|
|
|
|
|
|
|
|
if (!sala) {
|
|
|
|
|
res.status(400).json({ err: 'Ya no hay salas' })
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
const prestamo = new Prestamo()
|
|
|
|
|
const horamax = horario.hora.split('-')[1].split(':')[0]
|
|
|
|
|
const m = moment()
|
|
|
|
|
.hour(+horamax)
|
|
|
|
|
.minute(0)
|
|
|
|
|
.toLocaleString()
|
|
|
|
|
|
|
|
|
|
sala.activo = true
|
|
|
|
|
|
2020-09-17 01:42:30 -05:00
|
|
|
prestamo.tipo = 'sala'
|
2020-09-16 19:20:19 -05:00
|
|
|
prestamo.sala = sala
|
|
|
|
|
prestamo.usuario = usuario
|
|
|
|
|
prestamo.horaMaxEntrega = m
|
|
|
|
|
prestamo.horario = horario
|
|
|
|
|
|
|
|
|
|
await salaRepository.save(sala)
|
|
|
|
|
await prestamoRepository.save(prestamo)
|
|
|
|
|
|
|
|
|
|
prestamo.qr = `https://api.qrserver.com/v1/create-qr-code/?size=150x150&data=${prestamo.id}`
|
|
|
|
|
|
|
|
|
|
console.log(prestamo)
|
|
|
|
|
res.status(200).json({
|
|
|
|
|
prestamo
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
2020-09-08 15:39:32 -05:00
|
|
|
static async confirmar (req: Request, res: Response) {
|
|
|
|
|
const prestamoRepository = getRepository(Prestamo)
|
|
|
|
|
const operadorRepository = getRepository(Operador)
|
|
|
|
|
const mesaRepository = getRepository(Mesa)
|
|
|
|
|
|
|
|
|
|
const mesa = await mesaRepository
|
|
|
|
|
.createQueryBuilder('mesa')
|
|
|
|
|
.where('mesa.activo = :activo', { activo: false })
|
|
|
|
|
.orderBy('mesa.updatedAt', 'ASC')
|
|
|
|
|
.getOne()
|
|
|
|
|
|
|
|
|
|
const { prestamoId, operadorId } = req.body
|
|
|
|
|
|
|
|
|
|
if (!mesa) {
|
|
|
|
|
res.status(400).json({
|
|
|
|
|
err: 'Ya no hay mesas disponbles'
|
|
|
|
|
})
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
const prestamo = await prestamoRepository.findOne(prestamoId, {
|
|
|
|
|
relations: ['equipo']
|
|
|
|
|
})
|
|
|
|
|
if (!prestamo) {
|
|
|
|
|
res.status(400).json({
|
|
|
|
|
err: 'El prestamo no existe'
|
|
|
|
|
})
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
const operador = await operadorRepository.findOne(operadorId)
|
|
|
|
|
|
|
|
|
|
if (!operador) {
|
|
|
|
|
res.status(400).json({
|
|
|
|
|
err: 'El operador no existe'
|
|
|
|
|
})
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
mesa.activo = true
|
|
|
|
|
|
|
|
|
|
prestamo.operadorEntrega = operador
|
|
|
|
|
prestamo.reservado = false
|
|
|
|
|
prestamo.activo = true
|
|
|
|
|
prestamo.mesa = mesa
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
await mesaRepository.save(mesa)
|
|
|
|
|
await prestamoRepository.save(prestamo)
|
|
|
|
|
res.status(200).json(prestamo)
|
|
|
|
|
} catch (err) {
|
|
|
|
|
res.status(400).json({ err: err.message })
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static async regreso (req: Request, res: Response) {
|
|
|
|
|
const equipoRepository = getRepository(Equipo)
|
|
|
|
|
const prestamoRepository = getRepository(Prestamo)
|
|
|
|
|
const operadorRepository = getRepository(Operador)
|
|
|
|
|
const mesaRepository = getRepository(Mesa)
|
|
|
|
|
|
|
|
|
|
const prestamo = await prestamoRepository.findOne(req.body.prestamoId, {
|
|
|
|
|
relations: ['equipo', 'mesa']
|
|
|
|
|
})
|
|
|
|
|
if (!prestamo) {
|
|
|
|
|
res.status(400).json({ err: 'Prestamo no encontrado' })
|
|
|
|
|
}
|
|
|
|
|
const operador = await operadorRepository.findOne(req.body.operadorId)
|
|
|
|
|
if (!operador) {
|
|
|
|
|
res.status(400).json({ err: 'Operador no encontrado' })
|
|
|
|
|
}
|
|
|
|
|
prestamo.activo = false
|
|
|
|
|
prestamo.equipo.activo = false
|
|
|
|
|
prestamo.operadorRegreso = operador
|
|
|
|
|
prestamo.mesa.activo = false
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
await equipoRepository.save(prestamo.equipo)
|
|
|
|
|
await mesaRepository.save(prestamo.mesa)
|
|
|
|
|
await prestamoRepository.save(prestamo)
|
|
|
|
|
res.status(200).json(prestamo)
|
|
|
|
|
} catch (err) {
|
|
|
|
|
res.status(400).json({ err: err.message })
|
|
|
|
|
}
|
|
|
|
|
}
|
2020-09-03 01:45:50 -05:00
|
|
|
}
|