From 506a027bed8e51e15b348708701c09ecbf732f6b Mon Sep 17 00:00:00 2001 From: avicenan Date: Thu, 19 Feb 2026 16:39:26 +0700 Subject: [PATCH] queue monitoring display by slug --- src/controllers/queue_monitoring.ts | 98 ++++++++++++++++++++++++++++- 1 file changed, 97 insertions(+), 1 deletion(-) diff --git a/src/controllers/queue_monitoring.ts b/src/controllers/queue_monitoring.ts index 2038e5d..3935811 100644 --- a/src/controllers/queue_monitoring.ts +++ b/src/controllers/queue_monitoring.ts @@ -92,7 +92,103 @@ export class QueueMonitoringController { if (!queueMonitoring) throw { message: "Queue Monitoring Not Found" }; - return ReturnHelper.successResponseAny(res, 200, Language.lang.success_view, queueMonitoring); + + if (queueMonitoring.rooms && queueMonitoring.rooms.length > 0) { + try { + const roomIds = queueMonitoring.rooms.map((room: any) => room.id); + const today = moment().format("YYYY-MM-DD"); + + const placeholders = roomIds.map((_: any, index: number) => `$${index + 1}`).join(", "); + const dateParamIndex = roomIds.length + 1; + const queryParams = [...roomIds, today]; + + const queueQuery = ` + SELECT + ro.id, + ro.qeue_number, + ro.registration_status, + ro."roomId", + ro.updated_at, + TRIM(CONCAT( + COALESCE(p."first_name", ''), + ' ', + COALESCE(p."last_name", '') + )) AS "fullname_patient" + FROM emr.emr_registration_outpatient ro + LEFT JOIN emr.emr_registrations r ON ro."registrationId" = r.id + LEFT JOIN emr.emr_patients p ON r."patientId" = p.id + WHERE ro."roomId" IN (${placeholders}) + AND DATE(ro.visit_date) = $${dateParamIndex} + AND ro.registration_status IN ('ON GOING', 'REGISTERED') + ORDER BY ro."roomId", + CASE WHEN ro.registration_status = 'ON GOING' THEN 0 + WHEN ro.registration_status = 'REGISTERED' THEN 1 + ELSE 2 END, + ro.qeue_number ASC + `; + + const allQueues = await OrmHelper.DB.manager.query(queueQuery, queryParams); + + const queuesByRoom: { [key: string]: any[] } = {}; + allQueues.forEach((queue: any) => { + const roomId = queue.roomId; + if (roomId) { + if (!queuesByRoom[roomId]) { + queuesByRoom[roomId] = []; + } + queuesByRoom[roomId].push({ + id: queue.id, + qeue_number: queue.qeue_number, + registration_status: queue.registration_status, + fullname_patient: queue.fullname_patient + }); + } + }); + + queueMonitoring.rooms = queueMonitoring.rooms.map((room: any) => ({ + ...room, + queues: queuesByRoom[room.id] || [] + })); + + const currentQueues: any[] = []; + + queueMonitoring.rooms.forEach((room: any) => { + const roomQueues = allQueues.filter((queue: any) => queue.roomId === room.id); + + const onGoingQueues = roomQueues.filter((q: any) => q.registration_status === 'ON GOING'); + + if (onGoingQueues.length > 0) { + const latestOnGoing = onGoingQueues.reduce((latest: any, current: any) => { + const latestDate = new Date(latest.updated_at || latest.updatedAt || 0); + const currentDate = new Date(current.updated_at || current.updatedAt || 0); + return currentDate > latestDate ? current : latest; + }); + + currentQueues.push({ + room: room.room || room.name || room.roomName, + code: room.code, + qeue_number: latestOnGoing.qeue_number, + fullname_patient: latestOnGoing.fullname_patient + }); + } + }); + + (queueMonitoring as any).currentQueues = currentQueues; + } catch (queryError: any) { + log.warn(`Failed to fetch queue data from emr.emr_registration_outpatient: ${queryError.message}`); + queueMonitoring.rooms = queueMonitoring.rooms.map((room: any) => ({ + ...room, + queues: [] + })); + (queueMonitoring as any).currentQueues = []; + } + } + + const responseData = { + ...queueMonitoring + }; + + return ReturnHelper.successResponseAny(res, 200, Language.lang.success_view, responseData); } catch (e: unknown) { log.error(e); const err = e as Error;