From 0db519bb0dc05fdea0a0ff8dbec75d12ae00bb20 Mon Sep 17 00:00:00 2001 From: Janis Saldabols Date: Mon, 23 Sep 2024 16:59:31 +0300 Subject: [PATCH] CIRC-2141 Improvements after review --- .../org/folio/circulation/domain/Item.java | 12 ++++++++++ .../services/ItemForTlrService.java | 22 +++++++------------ .../requests/RequestsAPICreationTests.java | 2 +- 3 files changed, 21 insertions(+), 15 deletions(-) diff --git a/src/main/java/org/folio/circulation/domain/Item.java b/src/main/java/org/folio/circulation/domain/Item.java index c27a296ef1..ca7436dd3b 100644 --- a/src/main/java/org/folio/circulation/domain/Item.java +++ b/src/main/java/org/folio/circulation/domain/Item.java @@ -415,4 +415,16 @@ public String getLendingLibraryCode() { public String getDcbItemTitle() { return getProperty(itemRepresentation, "instanceTitle"); } + + public boolean isAtLocation(String locationCode) { + if (locationCode == null || locationCode.isEmpty()) { + return true; + } else { + return getLocation() != null && ( + locationCode.equals(getLocation().getCode()) || + locationCode.equals(getLocation().getLibrary().getCode()) || + locationCode.equals(getLocation().getCampus().getCode()) || + locationCode.equals(getLocation().getInstitution().getCode())); + } + } } diff --git a/src/main/java/org/folio/circulation/services/ItemForTlrService.java b/src/main/java/org/folio/circulation/services/ItemForTlrService.java index b8147312e6..49e19e6ff9 100644 --- a/src/main/java/org/folio/circulation/services/ItemForTlrService.java +++ b/src/main/java/org/folio/circulation/services/ItemForTlrService.java @@ -70,27 +70,21 @@ private Result> refusePageRequestWhenNoAvailablePageableItemsExist(Re return failedValidation(message, INSTANCE_ID, request.getInstanceId()); } - List finalAvailablePageableItems; - if (request.geItemLocationCode() != null) { - finalAvailablePageableItems = availablePageableItems.stream() - .filter(item -> request.geItemLocationCode().equals(item.getLocation().getCode()) || - request.geItemLocationCode().equals(item.getLocation().getLibrary().getCode()) || - request.geItemLocationCode().equals(item.getLocation().getCampus().getCode()) || - request.geItemLocationCode().equals(item.getLocation().getInstitution().getCode()) - ) + if (request.geItemLocationCode() == null) { + return of(() -> availablePageableItems); + } else { + List finalAvailablePageableItems = availablePageableItems.stream() + .filter(item -> item.isAtLocation(request.geItemLocationCode())) .toList(); if (finalAvailablePageableItems.isEmpty()) { String message = "Cannot create page TLR for this instance ID - no pageable available " + - "items found in forced location"; - log.info("{}. Instance ID: {}, Forced location code {}", + "items found in requested location"; + log.info("{}. Instance ID: {}, Requested location code {}", message, request.getInstanceId(), request.geItemLocationCode()); return failedValidation(message, ITEM_LOCATION_CODE, request.geItemLocationCode()); } - } else { - finalAvailablePageableItems = availablePageableItems; + return of(() -> finalAvailablePageableItems); } - - return of(() -> finalAvailablePageableItems); } private static Item pickClosestItem(Collection requestedLocations, List availableItems) { diff --git a/src/test/java/api/requests/RequestsAPICreationTests.java b/src/test/java/api/requests/RequestsAPICreationTests.java index 590a6416bf..b463e766a6 100644 --- a/src/test/java/api/requests/RequestsAPICreationTests.java +++ b/src/test/java/api/requests/RequestsAPICreationTests.java @@ -686,7 +686,7 @@ void createTitleLevelRequestWhenTlrEnabledSetLocationNoItems() { assertThat(response.getStatusCode(), is(422)); assertThat(response.getJson(), hasErrorWith( hasMessage("Cannot create page TLR for this instance ID - no pageable " + - "available items found in forced location"))); + "available items found in requested location"))); } @Test