Files
api/src/controller/Equipo.ts
T

130 lines
3.9 KiB
TypeScript
Raw Normal View History

2020-08-19 23:22:13 -05:00
import { Request, Response } from 'express'
import { getRepository } from 'typeorm'
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-08-19 23:22:13 -05:00
export class EquipoController {
static async prestamo (request: Request, res: Response) {
2020-08-22 21:54:46 -05:00
const usuarioRepository = await getRepository(Usuario)
const operadorRepository = await getRepository(Operador)
const equipoRepository = await getRepository(Equipo)
const prestamoRepository = await getRepository(Prestamo)
const usuario = await usuarioRepository.findOne(request.body.idUsuario, {
select: ['id', 'baja', 'multa']
})
if (!usuario) {
res.status(400).json({ err: 'Usuario no encontrado' })
return
}
const prestamoActivo = await prestamoRepository
.createQueryBuilder('prestamo')
.where('prestamo.usuarioId = :idUsuario', { idUsuario: usuario.id })
.andWhere('prestamo.activo = true')
.getOne()
console.log(prestamoActivo)
if (prestamoActivo) {
res.status(400).json({ err: 'El usuario tiene un prestamo activo' })
return
}
const operador = await operadorRepository.findOne(request.body.idOperador, {
select: ['id', 'admin']
})
if (!operador) {
res.status(400).json({ err: 'Operador no encontrado' })
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 })
.andWhere('tipo.nombre = :tipo', { tipo: 'chromebook' })
.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
prestamo.operadorEntrega = operador
const m = moment()
2020-08-24 21:25:39 -05:00
.add({ hours: 2, minutes: 35 })
2020-08-22 21:54:46 -05:00
.format('MMMM Do YYYY, h:mm:ss a')
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-22 21:54:46 -05:00
res.status(200).json({
prestamo
})
}
2020-08-24 21:25:39 -05:00
static async confirmarPrestamo (req: Request, res: Response) {
const prestamoRepository = getRepository(Prestamo)
const operadorRepository = getRepository(Operador)
const prestamo = await prestamoRepository.findOne(req.body.prestamoId, {
relations: ['equipo']
})
if (!prestamo) {
res.status(400).json({
err: 'El prestamo no existe'
})
return
}
const operador = await operadorRepository.findOne(req.body.operadorId)
if (!operador) {
res.status(400).json({
err: 'El operador no existe'
})
return
}
prestamo.operadorRegreso = operador
prestamo.reservado = false
prestamo.activo = true
res.status(200).json(prestamo)
}
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)
const prestamo = await prestamoRepository.findOne(req.body.prestamoId, {
relations: ['equipo']
})
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
await equipoRepository.save(prestamo.equipo)
await prestamoRepository.save(prestamo)
res.status(200).json(prestamo)
2020-08-19 23:22:13 -05:00
}
}