Skip to content

Commit

Permalink
[MODORDERS-1085, MODORDERS-1047] - Remove unnecessary holding verific…
Browse files Browse the repository at this point in the history
…ation (#906)

(cherry picked from commit 55d1907)
  • Loading branch information
Abdulkhakimov authored and azizbekxm committed Apr 19, 2024
1 parent 5d7a054 commit 70b7b35
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 14 deletions.
45 changes: 32 additions & 13 deletions src/main/java/org/folio/service/inventory/InventoryManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -352,8 +353,17 @@ private static void handleHoldingsError(String holdingId, Throwable throwable) {
}

public Future<List<JsonObject>> getHoldingsByIds(List<String> holdingIds, RequestContext requestContext) {
return getHoldingsByIds(holdingIds, requestContext, this::fetchHoldingsByHoldingIds);
}

public Future<List<JsonObject>> getHoldingsByIdsWithoutVerification(List<String> holdingIds, RequestContext requestContext) {
return getHoldingsByIds(holdingIds, requestContext, this::fetchHoldingsByHoldingIdsWithoutVerification);
}

public Future<List<JsonObject>> getHoldingsByIds(List<String> holdingIds, RequestContext requestContext,
org.folio.service.orders.utils.HelperUtils.BiFunctionReturningFuture<List<String>, RequestContext, List<JsonObject>> 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()));
Expand Down Expand Up @@ -1254,7 +1264,26 @@ private Future<List<JsonObject>> searchStorageExistingItems(String poLineId, Str
});
}

private Future<List<JsonObject>> fetchHoldingsByFundIds(List<String> holdingIds, RequestContext requestContext) {
private Future<List<JsonObject>> fetchHoldingsByHoldingIds(List<String> holdingIds, RequestContext requestContext) {
return fetchHoldingsByHoldingIds(holdingIds, requestContext, holdings -> {
if (holdings.size() == holdingIds.size()) {
return holdings;
}
List<Parameter> 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<List<JsonObject>> fetchHoldingsByHoldingIdsWithoutVerification(List<String> holdingIds, RequestContext requestContext) {
return fetchHoldingsByHoldingIds(holdingIds, requestContext, UnaryOperator.identity());
}

private Future<List<JsonObject>> fetchHoldingsByHoldingIds(List<String> holdingIds, RequestContext requestContext,
UnaryOperator<List<JsonObject>> 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);
Expand All @@ -1263,17 +1292,7 @@ private Future<List<JsonObject>> fetchHoldingsByFundIds(List<String> holdingIds,
.stream()
.map(JsonObject.class::cast)
.collect(toList()))
.map(holdings -> {
if (holdings.size() == holdingIds.size()) {
return holdings;
}
List<Parameter> 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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -512,7 +512,12 @@ public Future<List<String>> 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)
Expand Down
4 changes: 4 additions & 0 deletions src/main/java/org/folio/service/orders/utils/HelperUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -51,4 +51,8 @@ public static <T> Future<List<T>> collectResultsOnSuccess(List<Future<T>> future
public interface FunctionReturningFuture<I, O> {
Future<O> apply(I item);
}

public interface BiFunctionReturningFuture<T, U, R> {
Future<R> apply(T t, U u);
}
}

0 comments on commit 70b7b35

Please sign in to comment.