From 70b7b358cca374d69ee61393615e52b1a603dfea Mon Sep 17 00:00:00 2001 From: Abdulkhakimov <89521577+Abdulkhakimov@users.noreply.github.com> Date: Fri, 19 Apr 2024 15:50:40 +0500 Subject: [PATCH] [MODORDERS-1085, MODORDERS-1047] - Remove unnecessary holding verification (#906) (cherry picked from commit 55d19074eb9c2c77ea190fbbccdcab1bf6ab8126) --- .../service/inventory/InventoryManager.java | 45 +++++++++++++------ .../orders/PurchaseOrderLineService.java | 7 ++- .../service/orders/utils/HelperUtils.java | 4 ++ 3 files changed, 42 insertions(+), 14 deletions(-) diff --git a/src/main/java/org/folio/service/inventory/InventoryManager.java b/src/main/java/org/folio/service/inventory/InventoryManager.java index 9c58be94e..3455e2960 100644 --- a/src/main/java/org/folio/service/inventory/InventoryManager.java +++ b/src/main/java/org/folio/service/inventory/InventoryManager.java @@ -34,6 +34,7 @@ import java.util.Objects; import java.util.Optional; import java.util.concurrent.CompletionException; +import java.util.function.UnaryOperator; import java.util.stream.Collectors; import org.apache.commons.collections4.CollectionUtils; @@ -352,8 +353,17 @@ private static void handleHoldingsError(String holdingId, Throwable throwable) { } public Future> getHoldingsByIds(List holdingIds, RequestContext requestContext) { + return getHoldingsByIds(holdingIds, requestContext, this::fetchHoldingsByHoldingIds); + } + + public Future> getHoldingsByIdsWithoutVerification(List holdingIds, RequestContext requestContext) { + return getHoldingsByIds(holdingIds, requestContext, this::fetchHoldingsByHoldingIdsWithoutVerification); + } + + public Future> getHoldingsByIds(List holdingIds, RequestContext requestContext, + org.folio.service.orders.utils.HelperUtils.BiFunctionReturningFuture, RequestContext, List> biFunction) { return collectResultsOnSuccess( - ofSubLists(new ArrayList<>(holdingIds), MAX_IDS_FOR_GET_RQ_15).map(ids -> fetchHoldingsByFundIds(ids, requestContext)).toList()) + ofSubLists(new ArrayList<>(holdingIds), MAX_IDS_FOR_GET_RQ_15).map(ids -> biFunction.apply(ids, requestContext)).toList()) .map(lists -> lists.stream() .flatMap(Collection::stream) .collect(Collectors.toList())); @@ -1254,7 +1264,26 @@ private Future> searchStorageExistingItems(String poLineId, Str }); } - private Future> fetchHoldingsByFundIds(List holdingIds, RequestContext requestContext) { + private Future> fetchHoldingsByHoldingIds(List holdingIds, RequestContext requestContext) { + return fetchHoldingsByHoldingIds(holdingIds, requestContext, holdings -> { + if (holdings.size() == holdingIds.size()) { + return holdings; + } + List parameters = holdingIds.stream() + .filter(id -> holdings.stream() + .noneMatch(holding -> holding.getString(ID).equals(id))) + .map(id -> new Parameter().withValue(id).withKey("holdings")) + .collect(Collectors.toList()); + throw new HttpException(404, PARTIALLY_RETURNED_COLLECTION.toError().withParameters(parameters)); + }); + } + + private Future> fetchHoldingsByHoldingIdsWithoutVerification(List holdingIds, RequestContext requestContext) { + return fetchHoldingsByHoldingIds(holdingIds, requestContext, UnaryOperator.identity()); + } + + private Future> fetchHoldingsByHoldingIds(List holdingIds, RequestContext requestContext, + UnaryOperator> unaryOperator) { String query = convertIdsToCqlQuery(holdingIds); RequestEntry requestEntry = new RequestEntry(INVENTORY_LOOKUP_ENDPOINTS.get(HOLDINGS_RECORDS)) .withQuery(query).withOffset(0).withLimit(MAX_IDS_FOR_GET_RQ_15); @@ -1263,17 +1292,7 @@ private Future> fetchHoldingsByFundIds(List holdingIds, .stream() .map(JsonObject.class::cast) .collect(toList())) - .map(holdings -> { - if (holdings.size() == holdingIds.size()) { - return holdings; - } - List parameters = holdingIds.stream() - .filter(id -> holdings.stream() - .noneMatch(holding -> holding.getString(ID).equals(id))) - .map(id -> new Parameter().withValue(id).withKey("holdings")) - .collect(Collectors.toList()); - throw new HttpException(404, PARTIALLY_RETURNED_COLLECTION.toError().withParameters(parameters)); - }); + .map(unaryOperator); } void updateItemWithPieceFields(Piece piece, JsonObject item) { diff --git a/src/main/java/org/folio/service/orders/PurchaseOrderLineService.java b/src/main/java/org/folio/service/orders/PurchaseOrderLineService.java index ef4027349..5ceeb06ff 100644 --- a/src/main/java/org/folio/service/orders/PurchaseOrderLineService.java +++ b/src/main/java/org/folio/service/orders/PurchaseOrderLineService.java @@ -512,7 +512,12 @@ public Future> retrieveSearchLocationIds(PoLine poLine, RequestCont return Future.succeededFuture(new ArrayList<>(locationIds)); } - return inventoryManager.getHoldingsByIds(holdingIds, requestContext) + /* + * Possible scenarios where holding can be removed but the operation is not yet complete, and this would + * result in halting the entire flow. To avoid this, we do not compare the number of holdingIds with + * the final result from the inventory. + */ + return inventoryManager.getHoldingsByIdsWithoutVerification(holdingIds, requestContext) .map(holdings -> StreamEx.of(holdings).map(holding -> holding.getString(HOLDING_PERMANENT_LOCATION_ID)) .nonNull().toList()) .map(holdingsPermanentLocationIds -> StreamEx.of(locationIds).append(holdingsPermanentLocationIds) diff --git a/src/main/java/org/folio/service/orders/utils/HelperUtils.java b/src/main/java/org/folio/service/orders/utils/HelperUtils.java index daba18e8d..ecfd16658 100644 --- a/src/main/java/org/folio/service/orders/utils/HelperUtils.java +++ b/src/main/java/org/folio/service/orders/utils/HelperUtils.java @@ -51,4 +51,8 @@ public static Future> collectResultsOnSuccess(List> future public interface FunctionReturningFuture { Future apply(I item); } + + public interface BiFunctionReturningFuture { + Future apply(T t, U u); + } }