diff --git a/src/catalogos.ts b/src/catalogos.ts index 1240661..a16ca5d 100644 --- a/src/catalogos.ts +++ b/src/catalogos.ts @@ -1,11 +1,15 @@ import { createConnection, getRepository } from 'typeorm' import { Horario } from './entity/Horario' import { Mesa } from './entity/Mesa' +import { TipoUsuario } from './entity/TipoUsuario' +import { Sala } from './entity/Sala' createConnection() .then(async (connection) => { const horarioRepository = getRepository(Horario) const mesaRepository = getRepository(Mesa) + const tipoUsuarioRepository = getRepository(TipoUsuario) + const salaRepository = getRepository(Sala) const horarios = [ '8:00-10:00', '10:00-12:00', @@ -25,6 +29,18 @@ createConnection() mesa.nombre = String(i) await mesaRepository.save(mesa) } + for (let i = 1; i < 30; i++) { + const sala = new Sala() + sala.nombre = String(i) + await salaRepository.save(sala) + } + const tipoUsuario = new TipoUsuario() + tipoUsuario.nombre = 'alumno_interno' + await tipoUsuarioRepository.save(tipoUsuario) + tipoUsuario.nombre = 'alumno_externo' + await tipoUsuarioRepository.save(tipoUsuario) + tipoUsuario.nombre = 'profesor_interno' + await tipoUsuarioRepository.save(tipoUsuario) process.exit() }) diff --git a/src/controller/Reservacion.ts b/src/controller/Reservacion.ts index d4a3ad9..9c649da 100644 --- a/src/controller/Reservacion.ts +++ b/src/controller/Reservacion.ts @@ -7,6 +7,7 @@ import { Usuario } from '@src/entity/Usuario' import { Equipo } from '@src/entity/Equipo' import { Operador } from '@src/entity/Operador' import { Mesa } from '@src/entity/Mesa' +import { Sala } from '@src/entity/Sala' export class ResevacionController { static async espaciosDisponibles (req: Request, res: Response) { @@ -22,7 +23,7 @@ export class ResevacionController { return res.status(200).json(espacios) } - static async reservar (req: Request, res: Response) { + static async reservarEquipo (req: Request, res: Response) { const prestamoRepository = getRepository(Prestamo) const horarioRepository = getRepository(Horario) const usuarioRepository = getRepository(Usuario) @@ -100,6 +101,83 @@ export class ResevacionController { }) } + 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 + + const usuario = await usuarioRepository.findOne(usuarioId, { + select: ['id', 'baja', 'multa', 'tipoUsuario'] + }) + + if (!usuario) { + res.status(400).json({ err: 'Usuario no encontrado' }) + return + } + if (usuario.tipoUsuario.nombre !== 'profesor') { + 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' }) + 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') + .orderBy('equipo.updatedAt', 'ASC') + .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 + + 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 + }) + } + static async confirmar (req: Request, res: Response) { const prestamoRepository = getRepository(Prestamo) const operadorRepository = getRepository(Operador) diff --git a/src/controller/Usuario.ts b/src/controller/Usuario.ts index 243a4c5..d53c469 100644 --- a/src/controller/Usuario.ts +++ b/src/controller/Usuario.ts @@ -110,19 +110,8 @@ export class UsuarioController { } 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: 'Guerrero', - apellidoMaterno: 'Lopez', - institucion: 'Fes acatlan', - carrera: 'Matematicas aplicadas', - interno: true - } const alumnos = [ { cuenta: 124493, @@ -131,7 +120,7 @@ export class UsuarioController { apellidoMaterno: 'Mayans', institucion: 'FES Acatlan', carrera: 'Sociologia', - interno: false + tipo: 1 }, { cuenta: 91770, @@ -140,7 +129,7 @@ export class UsuarioController { apellidoMaterno: 'Justo', institucion: 'FES Acatlan', carrera: 'Derecho', - interno: false + tipo: 1 }, { cuenta: 123456789, @@ -149,7 +138,7 @@ export class UsuarioController { apellidoMaterno: 'Wiechers', institucion: 'Facultad de Mediciona', carrera: 'Medicina', - interno: false + tipo: 1 }, { cuenta: 40108, @@ -158,7 +147,7 @@ export class UsuarioController { apellidoMaterno: 'Ramirez', institucion: 'Facultad de Ciencias', carrera: 'Matematicas', - interno: false + tipo: 1 }, { cuenta: 415112133, @@ -167,7 +156,7 @@ export class UsuarioController { apellidoMaterno: 'Lopez', institucion: 'FES Iztacala', carrera: 'Biologia', - interno: false + tipo: 1 } ] for (let index = 0; index < alumnos.length; index++) { @@ -182,7 +171,7 @@ export class UsuarioController { // apellidoMaterno: 'Lopez', // institucion: 'FES Iztacala', // carrera: 'Biologia', - // interno: false + // tipo: false // } // if (usuarios.includes(usuarioId)) { diff --git a/src/entity/Prestamo.ts b/src/entity/Prestamo.ts index 83faf38..268aa57 100644 --- a/src/entity/Prestamo.ts +++ b/src/entity/Prestamo.ts @@ -12,6 +12,7 @@ import { Equipo } from './Equipo' import { Mesa } from './Mesa' import { Operador } from './Operador' import { Horario } from './Horario' +import { Sala } from './Sala' @Entity() export class Prestamo { @@ -30,6 +31,9 @@ export class Prestamo { @CreateDateColumn() createdAt: Date + @Column() + tipo: String + horaMaxEntrega: string qr: string @@ -68,11 +72,17 @@ export class Prestamo { (type) => Operador, (operador) => operador.regresos ) + operadorRegreso: Operador + @ManyToOne( (type) => Horario, (horario) => horario.prestamos ) horario: Horario - operadorRegreso: Operador + @ManyToOne( + (type) => Sala, + (sala) => sala.prestamos + ) + sala: Sala } diff --git a/src/entity/Sala.ts b/src/entity/Sala.ts new file mode 100644 index 0000000..70cd554 --- /dev/null +++ b/src/entity/Sala.ts @@ -0,0 +1,27 @@ +import { + Entity, + UpdateDateColumn, + CreateDateColumn, + OneToMany, + Column +} from 'typeorm' +import { Catalogo } from './Catalogo' +import { Prestamo } from './Prestamo' + +@Entity() +export class Sala extends Catalogo { + @Column() + activo: boolean + + @UpdateDateColumn() + updatedAt: Date + + @CreateDateColumn() + createdAt: Date + + @OneToMany( + (type) => Prestamo, + (prestamo) => prestamo.sala + ) + prestamos: Prestamo[] +} diff --git a/src/entity/TipoUsuario.ts b/src/entity/TipoUsuario.ts new file mode 100644 index 0000000..5cedf8c --- /dev/null +++ b/src/entity/TipoUsuario.ts @@ -0,0 +1,18 @@ +import { Entity, UpdateDateColumn, CreateDateColumn, OneToMany } from 'typeorm' +import { Catalogo } from './Catalogo' +import { Usuario } from './Usuario' + +@Entity() +export class TipoUsuario extends Catalogo { + @UpdateDateColumn() + updatedAt: Date + + @CreateDateColumn() + createdAt: Date + + @OneToMany( + (type) => Usuario, + (usuario) => usuario.tipoUsuario + ) + usuarios: Usuario[] +} diff --git a/src/entity/Usuario.ts b/src/entity/Usuario.ts index 9ba7669..c472860 100644 --- a/src/entity/Usuario.ts +++ b/src/entity/Usuario.ts @@ -7,7 +7,8 @@ import { OneToMany, Unique, CreateDateColumn, - UpdateDateColumn + UpdateDateColumn, + ManyToOne } from 'typeorm' import { IsDate, @@ -20,6 +21,7 @@ import { import { Reservacion } from './Reservacion' import { Prestamo } from './Prestamo' import { Log } from './Log' +import { TipoUsuario } from './TipoUsuario' @Entity() @Unique(['id', 'correoAlternativo']) @@ -106,6 +108,12 @@ export class Usuario { ) logs: Log[] + @ManyToOne( + (type) => TipoUsuario, + (tipoUsuario) => tipoUsuario.usuarios + ) + tipoUsuario: TipoUsuario + hashPassword () { this.secreto = bcrypt.hashSync(this.secreto, 10) } diff --git a/src/routes/reservacion.ts b/src/routes/reservacion.ts index a2f8412..43de1d0 100644 --- a/src/routes/reservacion.ts +++ b/src/routes/reservacion.ts @@ -3,7 +3,8 @@ import { ResevacionController } from '@src/controller/Reservacion' const router = Router() -router.post('/', ResevacionController.reservar) +router.post('/equipo', ResevacionController.reservarEquipo) +router.post('/sala', ResevacionController.reservarSala) router.get('/espacios', ResevacionController.espaciosDisponibles) router.put('/confirmar', ResevacionController.confirmar)