diff --git a/src/bitacora_mesa/bitacora_mesa.service.ts b/src/bitacora_mesa/bitacora_mesa.service.ts index c65ef86..a46d9ae 100644 --- a/src/bitacora_mesa/bitacora_mesa.service.ts +++ b/src/bitacora_mesa/bitacora_mesa.service.ts @@ -14,8 +14,7 @@ export class BitacoraMesaService { ) { } async assignment(dto: CreateBitacoraMesaDto) { - - // VALIDAR: el alumno ya tiene una mesa activa + const alumnoConMesa = await this.bitacoraMesaRepository .createQueryBuilder('bitacora') .where('bitacora.id_alumno_inscrito = :id_alumno_inscrito', { diff --git a/src/mesa/mesa.service.ts b/src/mesa/mesa.service.ts index c089e2b..03b63b7 100644 --- a/src/mesa/mesa.service.ts +++ b/src/mesa/mesa.service.ts @@ -38,15 +38,42 @@ export class MesaService { }); } - - async findAll(): Promise { return await this.mesaRepository.find(); } - async findAllActivo(): Promise { - return await this.mesaRepository.find({ where: { activo: true } }); + const mesas = await this.mesaRepository + .createQueryBuilder('mesa') + + .andWhere('mesa.activo = true') + + .andWhere((qb) => { + const subQuery = qb + .subQuery() + .select('bitacora_mesa.id_mesa') + .from('bitacora_mesa', 'bitacora_mesa') + .where( + `TIMESTAMPDIFF( + SECOND, + NOW(), + DATE_ADD(bitacora_mesa.tiempo_entrada, INTERVAL bitacora_mesa.tiempo_asignado MINUTE) + ) > 0`, + ) + .getQuery(); + + return `mesa.id_mesa NOT IN ${subQuery}`; + }) + + .orderBy('mesa.id_mesa', 'ASC') + + .getMany(); + + if (!mesas.length) { + throw new NotFoundException('No usable table found'); + } + + return mesas; } async updateActivo(id: number, updateMesaDto: UpdateMesaDto) {