Files
api/src/controller/Prestamo.ts
T

192 lines
5.5 KiB
TypeScript
Raw Normal View History

2020-08-19 23:22:13 -05:00
import { Request, Response } from 'express'
2020-09-08 15:39:32 -05:00
import { getRepository } from 'typeorm'
2020-08-19 23:22:13 -05:00
import { Equipo } from '@src/entity/Equipo'
2020-08-22 21:54:46 -05:00
import { Prestamo } from '@src/entity/Prestamo'
import { Usuario } from '@src/entity/Usuario'
import { Operador } from '@src/entity/Operador'
import * as moment from 'moment'
2020-09-08 15:39:32 -05:00
import { Mesa } from '@src/entity/Mesa'
2020-08-28 11:29:44 -05:00
export class PrestamoController {
2020-09-08 15:39:32 -05:00
static async crear (request: Request, res: Response) {
2020-08-22 21:54:46 -05:00
const usuarioRepository = await getRepository(Usuario)
const equipoRepository = await getRepository(Equipo)
const prestamoRepository = await getRepository(Prestamo)
2020-08-28 11:29:44 -05:00
const { usuarioId, tipoId } = request.body
const usuario = await usuarioRepository.findOne(usuarioId, {
2020-08-22 21:54:46 -05:00
select: ['id', 'baja', 'multa']
})
if (!usuario) {
res.status(400).json({ err: 'Usuario no encontrado' })
return
}
2020-08-28 11:29:44 -05:00
if (usuario.baja) {
res.status(400).json({ err: 'El usuario esta dado de baja' })
return
}
2020-08-22 21:54:46 -05:00
const prestamoActivo = await prestamoRepository
.createQueryBuilder('prestamo')
.where('prestamo.usuarioId = :idUsuario', { idUsuario: usuario.id })
.andWhere('prestamo.activo = true')
2020-08-28 11:29:44 -05:00
.orWhere('prestamo.reservado = true')
2020-08-22 21:54:46 -05:00
.getOne()
2020-08-28 11:29:44 -05:00
2020-08-22 21:54:46 -05:00
if (prestamoActivo) {
res.status(400).json({ err: 'El usuario tiene un prestamo activo' })
return
}
const equipo = await equipoRepository
2020-08-19 23:22:13 -05:00
.createQueryBuilder('equipo')
.leftJoinAndSelect('equipo.carrito', 'carrito')
.leftJoinAndSelect('carrito.tipo', 'tipo')
.where('equipo.activo = :activo', { activo: false })
.andWhere('carrito.kiosko = :kiosko', { kiosko: 1 })
2020-08-28 11:29:44 -05:00
.andWhere('tipo.id = :tipoId', { tipoId })
2020-08-19 23:22:13 -05:00
.orderBy('equipo.updatedAt', 'ASC')
.getOne()
2020-08-22 21:54:46 -05:00
const prestamo = new Prestamo()
2020-08-19 23:22:13 -05:00
equipo.activo = true
2020-08-22 21:54:46 -05:00
prestamo.equipo = equipo
prestamo.usuario = usuario
2020-09-17 01:42:30 -05:00
prestamo.tipo = 'equipo'
2020-08-22 21:54:46 -05:00
const m = moment()
2020-08-24 21:25:39 -05:00
.add({ hours: 2, minutes: 35 })
2020-08-31 17:46:16 -05:00
.toLocaleString()
2020-08-22 21:54:46 -05:00
prestamo.horaMaxEntrega = m
2020-08-24 21:25:39 -05:00
await equipoRepository.save(equipo)
2020-08-22 21:54:46 -05:00
await prestamoRepository.save(prestamo)
2020-08-24 21:25:39 -05:00
prestamo.qr = `https://api.qrserver.com/v1/create-qr-code/?size=150x150&data=${prestamo.id}`
2020-08-28 11:29:44 -05:00
console.log(prestamo)
2020-08-22 21:54:46 -05:00
res.status(200).json({
prestamo
})
}
2020-09-08 15:39:32 -05:00
static async confirmar (req: Request, res: Response) {
2020-08-24 21:25:39 -05:00
const prestamoRepository = getRepository(Prestamo)
const operadorRepository = getRepository(Operador)
2020-08-28 11:29:44 -05:00
const { prestamoId, operadorId } = req.body
const prestamo = await prestamoRepository.findOne(prestamoId, {
2020-08-24 21:25:39 -05:00
relations: ['equipo']
})
if (!prestamo) {
res.status(400).json({
err: 'El prestamo no existe'
})
return
}
2020-08-28 11:29:44 -05:00
const operador = await operadorRepository.findOne(operadorId)
2020-08-24 21:25:39 -05:00
if (!operador) {
res.status(400).json({
err: 'El operador no existe'
})
return
}
2020-08-28 11:29:44 -05:00
prestamo.operadorEntrega = operador
2020-08-24 21:25:39 -05:00
prestamo.reservado = false
prestamo.activo = true
2020-08-28 11:29:44 -05:00
try {
await prestamoRepository.save(prestamo)
res.status(200).json(prestamo)
} catch (err) {
res.status(400).json({ err: err.message })
}
2020-08-24 21:25:39 -05:00
}
2020-08-22 21:54:46 -05:00
static async regreso (req: Request, res: Response) {
const equipoRepository = getRepository(Equipo)
const prestamoRepository = getRepository(Prestamo)
const operadorRepository = getRepository(Operador)
2020-09-08 15:39:32 -05:00
const mesaRepository = getRepository(Mesa)
2020-08-22 21:54:46 -05:00
2020-09-08 15:39:32 -05:00
const { prestamoId, operadorId } = req.body
const prestamo = await prestamoRepository.findOne(prestamoId, {
relations: ['equipo', 'usuario', 'mesa']
2020-08-22 21:54:46 -05:00
})
if (!prestamo) {
res.status(400).json({ err: 'Prestamo no encontrado' })
}
2020-09-08 15:39:32 -05:00
const operador = await operadorRepository.findOne(operadorId)
2020-08-22 21:54:46 -05:00
if (!operador) {
res.status(400).json({ err: 'Operador no encontrado' })
}
2020-09-08 15:39:32 -05:00
2020-09-17 01:42:30 -05:00
if (prestamo.usuario.tipoUsuario.nombre === 'usuario_externo') {
2020-09-08 15:39:32 -05:00
prestamo.mesa.activo = false
}
2020-08-22 21:54:46 -05:00
prestamo.activo = false
prestamo.equipo.activo = false
prestamo.operadorRegreso = operador
2020-09-08 15:39:32 -05:00
try {
2020-09-17 01:42:30 -05:00
if (prestamo.usuario.tipoUsuario.nombre === 'usuario_externo') {
2020-09-08 15:39:32 -05:00
await mesaRepository.save(prestamo.mesa)
}
await equipoRepository.save(prestamo.equipo)
await prestamoRepository.save(prestamo)
res.status(200).send('Prestamo regresado Exitosamente')
} catch (err) {
res.status(400).json({ err: err.message })
}
2020-08-19 23:22:13 -05:00
}
2020-08-28 11:29:44 -05:00
2020-09-08 15:39:32 -05:00
static async estatus (req: Request, res: Response) {
2020-08-28 11:29:44 -05:00
const repository = getRepository(Prestamo)
2020-09-08 11:29:11 -05:00
const { usuarioId } = req.query
console.log(usuarioId)
2020-08-28 11:29:44 -05:00
2020-09-08 11:29:11 -05:00
const prestamo = await repository
.createQueryBuilder('prestamo')
.leftJoinAndSelect('prestamo.usuario', 'usuario')
2020-09-08 15:39:32 -05:00
.leftJoinAndSelect('prestamo.mesa', 'mesa')
2020-09-08 11:29:11 -05:00
.where('usuario.id = :usuarioId', { usuarioId })
.andWhere('prestamo.activo = :activo', { activo: true })
.getOne()
2020-08-28 11:29:44 -05:00
if (!prestamo) {
2020-09-08 15:39:32 -05:00
res.status(200).json({ err: 'El usuario no tiene prestamos activos' })
2020-08-28 11:29:44 -05:00
return
}
2020-09-08 15:39:32 -05:00
console.log(prestamo)
2020-08-28 11:29:44 -05:00
res.status(200).json(prestamo)
}
2020-09-08 15:39:32 -05:00
static async cancelar (req: Request, res: Response) {
2020-08-28 11:29:44 -05:00
const repository = getRepository(Prestamo)
const { prestamoId } = req.body
const prestamo = await repository.findOne(prestamoId)
if (!prestamo) {
res.status(400).json({ err: 'El prestamo no existe' })
}
prestamo.reservado = false
try {
await repository.save(prestamo)
res.status(200).send('Cancelado correctamente')
} catch (err) {
res.status(400).json({ err: err.message })
}
}
2020-08-19 23:22:13 -05:00
}