queue monitoring display by slug
This commit is contained in:
@ -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;
|
||||
|
||||
Reference in New Issue
Block a user