Skip to content

Commit

Permalink
CIRC-2116 Allowed SP endpoint should support patronGroupId parameter
Browse files Browse the repository at this point in the history
  • Loading branch information
MagzhanArtykov committed Jul 5, 2024
1 parent ea17564 commit c6742f8
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,18 +75,18 @@ public CompletableFuture<Result<Request>> lookupRequestPolicies(Request request)
public CompletableFuture<Result<RequestPolicy>> 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<Result<Map<RequestPolicy, Set<Item>>>> lookupRequestPolicies(
Collection<Item> items, User user) {
Collection<Item> items, String patronGroupId) {

log.debug("lookupRequestPolicies:: parameters items: {}, user: {}",
items::size, () -> asJson(user));
log.debug("lookupRequestPolicies:: parameters items: {}, patronGroupId: {}",
items::size, () -> asJson(patronGroupId));

Map<CirculationRuleCriteria, Set<Item>> 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())
Expand All @@ -96,12 +96,12 @@ public CompletableFuture<Result<Map<RequestPolicy, Set<Item>>>> lookupRequestPol
.thenCompose(r -> r.after(this::lookupRequestPolicies));
}

public CompletableFuture<Result<RequestPolicy>> lookupRequestPolicy(User user) {
public CompletableFuture<Result<RequestPolicy>> 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));
}

Expand All @@ -110,7 +110,7 @@ private BinaryOperator<Set<Item>> itemsMergeOperator() {
.collect(Collectors.toSet());
}

private CompletableFuture<Result<JsonObject>> lookupRequestPolicy(
private CompletableFuture<Result<JsonObject>> lookupRequestPolicyById(
String requestPolicyId) {

log.debug("lookupRequestPolicy:: parameters requestPolicyId: {}", requestPolicyId);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,18 +75,23 @@ private static Result<AllowedServicePointsRequest> buildRequest(RoutingContext r
String requestId = queryParams.get("requestId");
String useStubItem = queryParams.get("useStubItem");
String ecsRequestRouting = queryParams.get("ecsRequestRouting");
String patronGroupId = queryParams.get("patronGroupId");

List<String> errors = new ArrayList<>();

// Checking UUID validity

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));
}

Expand Down Expand Up @@ -144,8 +149,9 @@ private static Result<AllowedServicePointsRequest> 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<String> errors) {
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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<Result<AllowedServicePointsRequest>> fetchInstance(
Expand Down Expand Up @@ -128,26 +129,33 @@ private CompletableFuture<Result<AllowedServicePointsRequest>> fetchRequest(
.thenApply(r -> r.map(allowedServicePointsRequest::updateWithRequestInformation));
}

private CompletableFuture<Result<User>> fetchUser(AllowedServicePointsRequest request) {
private CompletableFuture<Result<String>> 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<Result<Map<RequestType, Set<AllowedServicePoint>>>>
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<Result<Map<RequestType, Set<AllowedServicePoint>>>>
getAllowedServicePoints(AllowedServicePointsRequest request, User user, Collection<Item> items) {
getAllowedServicePoints(AllowedServicePointsRequest request, String patronGroupId,
Collection<Item> items) {

if (items.isEmpty() && request.isForTitleLevelRequest()) {
log.info("getAllowedServicePoints:: requested instance has no items");
Expand All @@ -160,12 +168,12 @@ private CompletableFuture<Result<User>> 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
Expand Down

0 comments on commit c6742f8

Please sign in to comment.