163 lines
6.4 KiB
JavaScript
163 lines
6.4 KiB
JavaScript
"use strict";
|
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
const typeorm_1 = require("typeorm");
|
|
const Equipo_1 = require("../entity/Equipo");
|
|
const Prestamo_1 = require("../entity/Prestamo");
|
|
const Usuario_1 = require("../entity/Usuario");
|
|
const Operador_1 = require("../entity/Operador");
|
|
const moment = require("moment");
|
|
const Mesa_1 = require("../entity/Mesa");
|
|
class PrestamoController {
|
|
static async crear(request, res) {
|
|
const usuarioRepository = await typeorm_1.getRepository(Usuario_1.Usuario);
|
|
const equipoRepository = await typeorm_1.getRepository(Equipo_1.Equipo);
|
|
const prestamoRepository = await typeorm_1.getRepository(Prestamo_1.Prestamo);
|
|
const { usuarioId, tipoId } = request.body;
|
|
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 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 equipo = await equipoRepository
|
|
.createQueryBuilder('equipo')
|
|
.leftJoinAndSelect('equipo.carrito', 'carrito')
|
|
.leftJoinAndSelect('carrito.tipo', 'tipo')
|
|
.where('equipo.activo = :activo', { activo: false })
|
|
.andWhere('carrito.kiosko = :kiosko', { kiosko: 1 })
|
|
.andWhere('tipo.id = :tipoId', { tipoId })
|
|
.orderBy('equipo.updatedAt', 'ASC')
|
|
.getOne();
|
|
const prestamo = new Prestamo_1.Prestamo();
|
|
equipo.activo = true;
|
|
prestamo.equipo = equipo;
|
|
prestamo.usuario = usuario;
|
|
const m = moment()
|
|
.add({ hours: 2, minutes: 35 })
|
|
.toLocaleString();
|
|
prestamo.horaMaxEntrega = m;
|
|
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
|
|
});
|
|
}
|
|
static async confirmar(req, res) {
|
|
const prestamoRepository = typeorm_1.getRepository(Prestamo_1.Prestamo);
|
|
const operadorRepository = typeorm_1.getRepository(Operador_1.Operador);
|
|
const { prestamoId, operadorId } = req.body;
|
|
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;
|
|
}
|
|
prestamo.operadorEntrega = operador;
|
|
prestamo.reservado = false;
|
|
prestamo.activo = true;
|
|
try {
|
|
await prestamoRepository.save(prestamo);
|
|
res.status(200).json(prestamo);
|
|
}
|
|
catch (err) {
|
|
res.status(400).json({ err: err.message });
|
|
}
|
|
}
|
|
static async regreso(req, res) {
|
|
const equipoRepository = typeorm_1.getRepository(Equipo_1.Equipo);
|
|
const prestamoRepository = typeorm_1.getRepository(Prestamo_1.Prestamo);
|
|
const operadorRepository = typeorm_1.getRepository(Operador_1.Operador);
|
|
const mesaRepository = typeorm_1.getRepository(Mesa_1.Mesa);
|
|
const { prestamoId, operadorId } = req.body;
|
|
const prestamo = await prestamoRepository.findOne(prestamoId, {
|
|
relations: ['equipo', 'usuario', 'mesa']
|
|
});
|
|
if (!prestamo) {
|
|
res.status(400).json({ err: 'Prestamo no encontrado' });
|
|
}
|
|
const operador = await operadorRepository.findOne(operadorId);
|
|
if (!operador) {
|
|
res.status(400).json({ err: 'Operador no encontrado' });
|
|
}
|
|
if (!prestamo.usuario.interno) {
|
|
prestamo.mesa.activo = false;
|
|
}
|
|
prestamo.activo = false;
|
|
prestamo.equipo.activo = false;
|
|
prestamo.operadorRegreso = operador;
|
|
try {
|
|
if (!prestamo.usuario.interno) {
|
|
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 });
|
|
}
|
|
}
|
|
static async estatus(req, res) {
|
|
const repository = typeorm_1.getRepository(Prestamo_1.Prestamo);
|
|
const { usuarioId } = req.query;
|
|
console.log(usuarioId);
|
|
const prestamo = await repository
|
|
.createQueryBuilder('prestamo')
|
|
.leftJoinAndSelect('prestamo.usuario', 'usuario')
|
|
.leftJoinAndSelect('prestamo.mesa', 'mesa')
|
|
.where('usuario.id = :usuarioId', { usuarioId })
|
|
.andWhere('prestamo.activo = :activo', { activo: true })
|
|
.getOne();
|
|
if (!prestamo) {
|
|
res.status(200).json({ err: 'El usuario no tiene prestamos activos' });
|
|
return;
|
|
}
|
|
console.log(prestamo);
|
|
res.status(200).json(prestamo);
|
|
}
|
|
static async cancelar(req, res) {
|
|
const repository = typeorm_1.getRepository(Prestamo_1.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 });
|
|
}
|
|
}
|
|
}
|
|
exports.PrestamoController = PrestamoController;
|
|
//# sourceMappingURL=Prestamo.js.map
|