reservacion
This commit is contained in:
@@ -0,0 +1,11 @@
|
||||
import { Request, Response } from 'express'
|
||||
import { getRepository } from 'typeorm'
|
||||
import { Horario } from '@src/entity/Horario'
|
||||
|
||||
export class HorarioController {
|
||||
static async getAll (req: Request, res: Response) {
|
||||
const repository = getRepository(Horario)
|
||||
const horarios = await repository.find()
|
||||
res.status(200).json(horarios)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,94 @@
|
||||
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'
|
||||
|
||||
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)
|
||||
}
|
||||
|
||||
static async reservar (req: Request, res: Response) {
|
||||
const prestamoRepository = getRepository(Prestamo)
|
||||
const horarioRepository = getRepository(Horario)
|
||||
const usuarioRepository = getRepository(Usuario)
|
||||
const equipoRepository = getRepository(Equipo)
|
||||
|
||||
const { usuarioId, tipoId, horarioId } = req.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 horario = await horarioRepository.findOne(horarioId)
|
||||
|
||||
if (!horario) {
|
||||
res.status(400).json({ err: 'El horario no existe' })
|
||||
}
|
||||
|
||||
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' })
|
||||
}
|
||||
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()
|
||||
|
||||
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
|
||||
|
||||
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
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -4,6 +4,7 @@ import { validate } from 'class-validator'
|
||||
import { getRepository } from 'typeorm'
|
||||
export class UsuarioController {
|
||||
static async create (req: Request, res: Response) {
|
||||
console.log(req.file)
|
||||
const {
|
||||
id,
|
||||
nombre,
|
||||
@@ -11,10 +12,19 @@ export class UsuarioController {
|
||||
apellidoMaterno,
|
||||
correoAlternativo,
|
||||
numeroTelefonico,
|
||||
identificacion,
|
||||
institucion,
|
||||
secreto
|
||||
} = req.body
|
||||
secreto,
|
||||
interno
|
||||
} = JSON.parse(req.body.usuario)
|
||||
|
||||
const identificacion = req.file
|
||||
if (!interno && !identificacion) {
|
||||
res.status(400).send({
|
||||
status: false,
|
||||
data: 'Un usuario externo tiene que mandar fotoo de su identificacion'
|
||||
})
|
||||
return false
|
||||
}
|
||||
|
||||
const usuario = new Usuario()
|
||||
usuario.id = id
|
||||
@@ -23,9 +33,10 @@ export class UsuarioController {
|
||||
usuario.apellidoMaterno = apellidoMaterno
|
||||
usuario.correoAlternativo = correoAlternativo
|
||||
usuario.numeroTelefonico = numeroTelefonico
|
||||
usuario.identificacion = identificacion
|
||||
usuario.identificacion = interno ? 'null' : JSON.stringify(identificacion)
|
||||
usuario.institucion = institucion
|
||||
usuario.secreto = secreto
|
||||
usuario.interno = interno
|
||||
|
||||
usuario.hashPassword()
|
||||
|
||||
@@ -83,10 +94,45 @@ export class UsuarioController {
|
||||
res.status(200).json({
|
||||
nombre: usuario.nombre,
|
||||
apellidoPaterno: usuario.apellidoPaterno,
|
||||
apellidoMaterno: usuario.apellidoMaterno
|
||||
apellidoMaterno: usuario.apellidoMaterno,
|
||||
interno: usuario.interno
|
||||
})
|
||||
} catch (err) {
|
||||
res.status(400).send(err.message)
|
||||
}
|
||||
}
|
||||
|
||||
static async verificar (req: Request, res: Response) {
|
||||
const usuarios = ['415112132', '41511216']
|
||||
const externos = ['315112132', '31511216']
|
||||
|
||||
const usuarioId = req.params.id
|
||||
|
||||
const usuarioInterno = {
|
||||
nombre: 'Arturo',
|
||||
apellidoPaterno: 'holdda',
|
||||
apellidoMaterno: 'holdda',
|
||||
institucion: 'Fes acatlan',
|
||||
carrera: 'Matematicas aplicadas',
|
||||
interno: true
|
||||
}
|
||||
const usuarioExterno = {
|
||||
nombre: 'Paulina',
|
||||
apellidoPaterno: 'Guerrero',
|
||||
apellidoMaterno: 'Lopez',
|
||||
institucion: 'FES Iztacala',
|
||||
carrera: 'Biologia',
|
||||
interno: false
|
||||
}
|
||||
|
||||
if (usuarios.includes(usuarioId)) {
|
||||
res.status(200).json(usuarioInterno)
|
||||
return
|
||||
}
|
||||
if (externos.includes(usuarioId)) {
|
||||
res.status(200).json(usuarioExterno)
|
||||
return
|
||||
}
|
||||
res.status(400).json({ err: 'no se encontro un usuario' })
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user