diff --git a/api/src/main/java/org/openmrs/module/queue/api/digitalSignage/QueueTicketAssignments.java b/api/src/main/java/org/openmrs/module/queue/api/digitalSignage/QueueTicketAssignments.java index b1ca0a2d..db8c0f22 100644 --- a/api/src/main/java/org/openmrs/module/queue/api/digitalSignage/QueueTicketAssignments.java +++ b/api/src/main/java/org/openmrs/module/queue/api/digitalSignage/QueueTicketAssignments.java @@ -13,6 +13,7 @@ import java.io.IOException; import java.util.HashMap; import java.util.Map; +import java.util.stream.Collectors; import org.apache.commons.lang3.StringUtils; @@ -23,7 +24,7 @@ public class QueueTicketAssignments { /** * The object has: service point/room name as key for ease of search and update and object with - * status and ticket number + * status, ticket number, and location */ private static final Map ACTIVE_QUEUE_TICKETS = new HashMap<>(); @@ -33,44 +34,65 @@ public class QueueTicketAssignments { * @param servicePointName * @param ticketNumber * @param status + * @param locationUuid - optional location UUID to associate with the ticket */ - synchronized public static void updateTicketAssignment(String servicePointName, String ticketNumber, String status) { + synchronized public static void updateTicketAssignment(String servicePointName, String ticketNumber, String status, + String locationUuid) { + if (StringUtils.isNotBlank(servicePointName) && StringUtils.isNotBlank(ticketNumber) && StringUtils.isNotBlank(status)) { - /** remove the ticket number from any assignment */ - // Remove the ticket number from any assignment - for (String key : ACTIVE_QUEUE_TICKETS.keySet()) { - TicketAssignment assignment = ACTIVE_QUEUE_TICKETS.get(key); - if (assignment.getTicketNumber().equals(ticketNumber)) { - ACTIVE_QUEUE_TICKETS.remove(key); - if (status.equals("completed")) { - return; - } - break; - } + ACTIVE_QUEUE_TICKETS.entrySet().removeIf(entry -> ticketNumber.equals(entry.getValue().getTicketNumber())); + + // If ticket is completed, no need to reassign + if ("completed".equalsIgnoreCase(status)) { + return; } - /** Assign ticket to a room if the room already exist */ // Assign ticket to a room if the room already exists if (ACTIVE_QUEUE_TICKETS.containsKey(servicePointName)) { TicketAssignment tAssignment = ACTIVE_QUEUE_TICKETS.get(servicePointName); tAssignment.setStatus(status); tAssignment.setTicketNumber(ticketNumber); + tAssignment.setLocationUuid(locationUuid); ACTIVE_QUEUE_TICKETS.put(servicePointName, tAssignment); } else { // Else create a new assignment - TicketAssignment ticketAssignment = new TicketAssignment(status, ticketNumber); + TicketAssignment ticketAssignment = new TicketAssignment(status, ticketNumber, locationUuid); ACTIVE_QUEUE_TICKETS.put(servicePointName, ticketAssignment); } } } + /** + * Overloaded method for backward compatibility - calls main method with null location + */ + synchronized public static void updateTicketAssignment(String servicePointName, String ticketNumber, String status) { + updateTicketAssignment(servicePointName, ticketNumber, status, null); + } + public static Map getActiveTicketAssignments() { return ACTIVE_QUEUE_TICKETS; } + /** + * Get active ticket assignments filtered by location + * + * @param locationUuid - the location UUID to filter by + * @return Map of filtered ticket assignments for the specified location + */ + public static Map getActiveTicketAssignmentsByLocation(String locationUuid) { + if (StringUtils.isBlank(locationUuid)) { + return ACTIVE_QUEUE_TICKETS; + } + + return ACTIVE_QUEUE_TICKETS.entrySet().stream().filter(entry -> { + String ticketLocation = entry.getValue().getLocationUuid(); + return ticketLocation != null && ticketLocation.equals(locationUuid); + }).collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue)); + } + /** * Extracts the request body and returns it as a string * @@ -97,9 +119,17 @@ public static class TicketAssignment { private String ticketNumber; - public TicketAssignment(String status, String ticketNumber) { + private String locationUuid; + + public TicketAssignment(String status, String ticketNumber, String locationUuid) { this.status = status; this.ticketNumber = ticketNumber; + this.locationUuid = locationUuid; + } + + // Backward compatible constructor + public TicketAssignment(String status, String ticketNumber) { + this(status, ticketNumber, null); } public String getStatus() { @@ -117,5 +147,13 @@ public String getTicketNumber() { public void setTicketNumber(String ticketNumber) { this.ticketNumber = ticketNumber; } + + public String getLocationUuid() { + return locationUuid; + } + + public void setLocationUuid(String locationUuid) { + this.locationUuid = locationUuid; + } } } diff --git a/omod/src/main/java/org/openmrs/module/queue/web/Legacy1xRestController.java b/omod/src/main/java/org/openmrs/module/queue/web/Legacy1xRestController.java index ca26b45b..d122d63e 100644 --- a/omod/src/main/java/org/openmrs/module/queue/web/Legacy1xRestController.java +++ b/omod/src/main/java/org/openmrs/module/queue/web/Legacy1xRestController.java @@ -65,6 +65,7 @@ import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.ResponseBody; /** @@ -110,7 +111,8 @@ public Object getVisitQueueEntries(HttpServletRequest request, HttpServletRespon result.add("results", visitQueueEntries); RequestContext requestContext = RestUtil.getRequestContext(request, response, Representation.REF); Map parameters = new HashMap(requestContext.getRequest().getParameterMap()); - // The queueEntryResource does not limit to active by default, but the legacy resource does + // The queueEntryResource does not limit to active by default, but the legacy + // resource does if (!parameters.containsKey("isEnded")) { parameters.put("isEnded", new String[] { "false" }); } @@ -211,19 +213,32 @@ public Object assignTicketToServicePoint(HttpServletRequest request) throws Exce String ticketNumber = actualObj.get("ticketNumber").textValue(); String status = actualObj.get("status").textValue(); + // Location is optional + String locationUuid = actualObj.has("locationUuid") ? actualObj.get("locationUuid").textValue() : null; + if (servicePointName.isEmpty() || ticketNumber.isEmpty() || status.isEmpty()) { return new ResponseEntity("One of the required fields is empty", new HttpHeaders(), BAD_REQUEST); } - QueueTicketAssignments.updateTicketAssignment(servicePointName, ticketNumber, status); + QueueTicketAssignments.updateTicketAssignment(servicePointName, ticketNumber, status, locationUuid); return new ResponseEntity("Ticket successfully assigned!", new HttpHeaders(), HttpStatus.OK); } return new ResponseEntity("The request could not be interpreted", new HttpHeaders(), BAD_REQUEST); } @RequestMapping(method = GET, value = "/rest/" + RestConstants.VERSION_1 + "/queueutil/active-tickets") - public Object getActiveTickets() { - return new ResponseEntity<>(QueueTicketAssignments.getActiveTicketAssignments(), new HttpHeaders(), HttpStatus.OK); + @ResponseBody + public Object getActiveTickets(@RequestParam(value = "locationUuid", required = false) String locationUuid) { + Map activeTickets; + + // If locationUuid parameter is provided, filter by locationUuid + if (StringUtils.isNotBlank(locationUuid)) { + activeTickets = QueueTicketAssignments.getActiveTicketAssignmentsByLocation(locationUuid); + } else { + activeTickets = QueueTicketAssignments.getActiveTicketAssignments(); + } + + return new ResponseEntity<>(activeTickets, new HttpHeaders(), HttpStatus.OK); } @RequestMapping(method = { GET, POST }, value = "/rest/" + RestConstants.VERSION_1 + "/queue-entry-number")