diff --git a/src/mesa/mesa.controller.ts b/src/mesa/mesa.controller.ts index e048d98..c655be9 100644 --- a/src/mesa/mesa.controller.ts +++ b/src/mesa/mesa.controller.ts @@ -15,7 +15,12 @@ export class MesaController { async findAllActivo() { return this.mesaService.findAllActivo(); } - + + @Get('uso') + async findActiveMesas() { + return this.mesaService.findActiveMesas(); + } + @Patch(':id') async updateActivo( @Param('id') id: number, diff --git a/src/mesa/mesa.service.ts b/src/mesa/mesa.service.ts index 03b63b7..c6189ec 100644 --- a/src/mesa/mesa.service.ts +++ b/src/mesa/mesa.service.ts @@ -119,4 +119,27 @@ export class MesaService { activo: nuevoActivo, }; } + + findActiveMesas() { + return this.mesaRepository + .createQueryBuilder('mesa') + .select([ + 'mesa.id_mesa AS id_mesa', + ` + EXISTS ( + SELECT 1 + FROM bitacora_mesa bm + WHERE bm.id_mesa = mesa.id_mesa + AND TIMESTAMPDIFF( + SECOND, + NOW(), + DATE_ADD(bm.tiempo_entrada, INTERVAL bm.tiempo_asignado MINUTE) + ) > 0 + ) AS ocupado + `, + ]) + .where('mesa.activo = 1') + .orderBy('mesa.id_mesa', 'ASC') + .getRawMany(); + } }