From c6742f887a6e3bfc55aa357751e1ae2d81779989 Mon Sep 17 00:00:00 2001 From: MagzhanArtykov Date: Fri, 5 Jul 2024 16:15:40 +0500 Subject: [PATCH] CIRC-2116 Allowed SP endpoint should support patronGroupId parameter --- .../domain/AllowedServicePointsRequest.java | 1 + .../requests/RequestPolicyRepository.java | 18 ++++++------ .../AllowedServicePointsResource.java | 14 +++++++--- .../rules/CirculationRuleCriteria.java | 5 ++-- .../services/AllowedServicePointsService.java | 28 ++++++++++++------- 5 files changed, 40 insertions(+), 26 deletions(-) diff --git a/src/main/java/org/folio/circulation/domain/AllowedServicePointsRequest.java b/src/main/java/org/folio/circulation/domain/AllowedServicePointsRequest.java index 16ea46f1ea..c0ec5b2f06 100644 --- a/src/main/java/org/folio/circulation/domain/AllowedServicePointsRequest.java +++ b/src/main/java/org/folio/circulation/domain/AllowedServicePointsRequest.java @@ -17,6 +17,7 @@ public class AllowedServicePointsRequest { private Request.Operation operation; private String requesterId; + private String patronGroupId; private String instanceId; private String itemId; private String requestId; diff --git a/src/main/java/org/folio/circulation/infrastructure/storage/requests/RequestPolicyRepository.java b/src/main/java/org/folio/circulation/infrastructure/storage/requests/RequestPolicyRepository.java index 7df5c56de1..42a1fe5abf 100644 --- a/src/main/java/org/folio/circulation/infrastructure/storage/requests/RequestPolicyRepository.java +++ b/src/main/java/org/folio/circulation/infrastructure/storage/requests/RequestPolicyRepository.java @@ -75,18 +75,18 @@ public CompletableFuture> lookupRequestPolicies(Request request) public CompletableFuture> lookupRequestPolicy(Item item, User user) { log.debug("lookupRequestPolicy:: parameters item: {}, user: {}", item, user); return lookupRequestPolicyId(item, user) - .thenComposeAsync(r -> r.after(this::lookupRequestPolicy)) + .thenComposeAsync(r -> r.after(this::lookupRequestPolicyById)) .thenApply(result -> result.map(RequestPolicy::from)); } public CompletableFuture>>> lookupRequestPolicies( - Collection items, User user) { + Collection items, String patronGroupId) { - log.debug("lookupRequestPolicies:: parameters items: {}, user: {}", - items::size, () -> asJson(user)); + log.debug("lookupRequestPolicies:: parameters items: {}, patronGroupId: {}", + items::size, () -> asJson(patronGroupId)); Map> criteriaMap = items.stream() - .map(item -> new CirculationRuleCriteria(item, user)) + .map(item -> new CirculationRuleCriteria(item, patronGroupId)) .collect(toMap(identity(), criteria -> Set.of(criteria.getItem()), itemsMergeOperator())); return allOf(criteriaMap.entrySet(), entry -> lookupRequestPolicyId(entry.getKey()) @@ -96,12 +96,12 @@ public CompletableFuture>>> lookupRequestPol .thenCompose(r -> r.after(this::lookupRequestPolicies)); } - public CompletableFuture> lookupRequestPolicy(User user) { + public CompletableFuture> lookupRequestPolicy(String patronGroupId) { // Circulation rules need to be executed with the patron group parameter only. // All the item-related parameters should be random UUIDs. - return lookupRequestPolicyId(UUID.randomUUID().toString(), user.getPatronGroupId(), + return lookupRequestPolicyId(UUID.randomUUID().toString(), patronGroupId, UUID.randomUUID().toString(), UUID.randomUUID().toString()) - .thenCompose(r -> r.after(this::lookupRequestPolicy)) + .thenCompose(r -> r.after(this::lookupRequestPolicyById)) .thenApply(result -> result.map(RequestPolicy::from)); } @@ -110,7 +110,7 @@ private BinaryOperator> itemsMergeOperator() { .collect(Collectors.toSet()); } - private CompletableFuture> lookupRequestPolicy( + private CompletableFuture> lookupRequestPolicyById( String requestPolicyId) { log.debug("lookupRequestPolicy:: parameters requestPolicyId: {}", requestPolicyId); diff --git a/src/main/java/org/folio/circulation/resources/AllowedServicePointsResource.java b/src/main/java/org/folio/circulation/resources/AllowedServicePointsResource.java index 11fa7dff36..bd1c74f8ed 100644 --- a/src/main/java/org/folio/circulation/resources/AllowedServicePointsResource.java +++ b/src/main/java/org/folio/circulation/resources/AllowedServicePointsResource.java @@ -75,6 +75,7 @@ private static Result buildRequest(RoutingContext r String requestId = queryParams.get("requestId"); String useStubItem = queryParams.get("useStubItem"); String ecsRequestRouting = queryParams.get("ecsRequestRouting"); + String patronGroupId = queryParams.get("patronGroupId"); List errors = new ArrayList<>(); @@ -82,11 +83,15 @@ private static Result buildRequest(RoutingContext r if (requesterId != null && !isUuid(requesterId)) { log.warn("Requester ID is not a valid UUID: {}", requesterId); - errors.add(String.format("Requester ID is not a valid UUID: %s.", requesterId)); + if (patronGroupId != null && !isUuid(patronGroupId)) { + log.warn("Patron Group ID is not a valid UUID: {}", patronGroupId); + errors.add(String.format("Not valid UUID for Requester ID : %s and " + + "Patron Group ID: %s.", requesterId, patronGroupId)); + } } if (instanceId != null && !isUuid(instanceId)) { - log.warn("Instance ID is not a valid UUID: {}", requesterId); + log.warn("Instance ID is not a valid UUID: {}", instanceId); errors.add(String.format("Instance ID is not a valid UUID: %s.", instanceId)); } @@ -144,8 +149,9 @@ private static Result buildRequest(RoutingContext r return failed(new BadRequestFailure(errorMessage)); } - return succeeded(new AllowedServicePointsRequest(operation, requesterId, instanceId, itemId, - requestId, Boolean.parseBoolean(useStubItem), Boolean.parseBoolean(ecsRequestRouting))); + return succeeded(new AllowedServicePointsRequest(operation, requesterId, + patronGroupId, instanceId, itemId, requestId, Boolean.parseBoolean(useStubItem), + Boolean.parseBoolean(ecsRequestRouting))); } private static void validateBoolean(String parameter, String parameterName, List errors) { diff --git a/src/main/java/org/folio/circulation/rules/CirculationRuleCriteria.java b/src/main/java/org/folio/circulation/rules/CirculationRuleCriteria.java index 3052577f4f..29f1d903d5 100644 --- a/src/main/java/org/folio/circulation/rules/CirculationRuleCriteria.java +++ b/src/main/java/org/folio/circulation/rules/CirculationRuleCriteria.java @@ -1,7 +1,6 @@ package org.folio.circulation.rules; import org.folio.circulation.domain.Item; -import org.folio.circulation.domain.User; import lombok.EqualsAndHashCode; import lombok.Getter; @@ -20,11 +19,11 @@ public class CirculationRuleCriteria { @EqualsAndHashCode.Exclude private Item item; - public CirculationRuleCriteria(@NonNull Item item, @NonNull User user) { + public CirculationRuleCriteria(@NonNull Item item, @NonNull String patronGroupId) { this.materialTypeId = item.getMaterialTypeId(); this.loanTypeId = item.getLoanTypeId(); this.locationId = item.getEffectiveLocationId(); - this.patronGroupId = user.getPatronGroupId(); + this.patronGroupId = patronGroupId; this.item = item; } } diff --git a/src/main/java/org/folio/circulation/services/AllowedServicePointsService.java b/src/main/java/org/folio/circulation/services/AllowedServicePointsService.java index c5ee898a43..d358408926 100644 --- a/src/main/java/org/folio/circulation/services/AllowedServicePointsService.java +++ b/src/main/java/org/folio/circulation/services/AllowedServicePointsService.java @@ -90,8 +90,9 @@ public AllowedServicePointsService(Clients clients, boolean isEcsRequestRouting) return ofAsync(request) .thenCompose(r -> r.after(this::fetchInstance)) .thenCompose(r -> r.after(this::fetchRequest)) - .thenCompose(r -> r.after(this::fetchUser)) - .thenCompose(r -> r.after(user -> getAllowedServicePoints(request, user))); + .thenCompose(r -> r.after(this::getPatronGroupId)) + .thenCompose(r -> r.after(patronGroupId -> getAllowedServicePoints(request, + patronGroupId))); } private CompletableFuture> fetchInstance( @@ -128,26 +129,33 @@ private CompletableFuture> fetchRequest( .thenApply(r -> r.map(allowedServicePointsRequest::updateWithRequestInformation)); } - private CompletableFuture> fetchUser(AllowedServicePointsRequest request) { + private CompletableFuture> getPatronGroupId(AllowedServicePointsRequest request) { + + if (request.getPatronGroupId() != null) { + return ofAsync(request.getPatronGroupId()); + } + final String userId = request.getRequesterId(); return userRepository.getUser(userId) .thenApply(r -> r.failWhen( user -> succeeded(user == null), - user -> notFoundValidationFailure(userId, User.class))); + user -> notFoundValidationFailure(userId, User.class))) + .thenApply(result -> result.map(User::getPatronGroupId)); } private CompletableFuture>>> - getAllowedServicePoints(AllowedServicePointsRequest request, User user) { + getAllowedServicePoints(AllowedServicePointsRequest request, String patronGroupId) { - log.debug("getAllowedServicePoints:: parameters request: {}, user: {}", request, user); + log.debug("getAllowedServicePoints:: parameters request: {}, patronGroupId: {}", request, patronGroupId); return fetchItems(request) - .thenCompose(r -> r.after(items -> getAllowedServicePoints(request, user, items))); + .thenCompose(r -> r.after(items -> getAllowedServicePoints(request, patronGroupId, items))); } private CompletableFuture>>> - getAllowedServicePoints(AllowedServicePointsRequest request, User user, Collection items) { + getAllowedServicePoints(AllowedServicePointsRequest request, String patronGroupId, + Collection items) { if (items.isEmpty() && request.isForTitleLevelRequest()) { log.info("getAllowedServicePoints:: requested instance has no items"); @@ -160,12 +168,12 @@ private CompletableFuture> fetchUser(AllowedServicePointsRequest re : this::extractAllowedServicePointsConsideringItemStatus; if (request.isUseStubItem()) { - return requestPolicyRepository.lookupRequestPolicy(user) + return requestPolicyRepository.lookupRequestPolicy(patronGroupId) .thenCompose(r -> r.after(policy -> extractAllowedServicePointsIgnoringItemStatus( policy, new HashSet<>()))); } - return requestPolicyRepository.lookupRequestPolicies(items, user) + return requestPolicyRepository.lookupRequestPolicies(items, patronGroupId) .thenCompose(r -> r.after(policies -> allOf(policies, mappingFunction))) .thenApply(r -> r.map(this::combineAllowedServicePoints)); // TODO: remove irrelevant request types for REPLACE