Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CIRC-2050 Add useStubItem parameter to allowed-service-points endpoint #1447

Merged
merged 19 commits into from
Apr 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion descriptors/ModuleDescriptor-template.json
Original file line number Diff line number Diff line change
Expand Up @@ -649,7 +649,7 @@
},
{
"id": "allowed-service-points",
"version": "1.0",
"version": "1.1",
"handlers": [
{
"methods": [
Expand Down
6 changes: 5 additions & 1 deletion ramls/circulation.raml
Original file line number Diff line number Diff line change
Expand Up @@ -343,6 +343,10 @@ resourceTypes:
description: "Instance ID"
pattern: "^[a-fA-F0-9]{8}-[a-fA-F0-9]{4}-[1-5][a-fA-F0-9]{3}-[89abAB][a-fA-F0-9]{3}-[a-fA-F0-9]{12}$"
required: false
useStubItem:
description: "When true, allows to apply circulation rules based on patron group only"
type: boolean
required: false
responses:
200:
description: "List of allowed service points was retrieved successfully"
Expand All @@ -364,4 +368,4 @@ resourceTypes:
description: "Internal server error"
body:
text/plain:
example: "Internal server error"
example: "Internal server error"
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ public class AllowedServicePointsRequest {
private String requesterId;
private String instanceId;
private String itemId;
private String requestId;
private boolean useStubItem;

public boolean isForTitleLevelRequest() {
return instanceId != null;
Expand All @@ -27,7 +29,6 @@ public boolean isForTitleLevelRequest() {
public boolean isForItemLevelRequest() {
return itemId != null;
}
private String requestId;

public AllowedServicePointsRequest updateWithRequestInformation(Request request) {
log.debug("updateWithRequestInformation:: parameters request: {}", request);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import java.util.Collection;
import java.util.Map;
import java.util.Set;
import java.util.UUID;
import java.util.concurrent.CompletableFuture;
import java.util.function.BinaryOperator;
import java.util.stream.Collectors;
Expand Down Expand Up @@ -95,6 +96,15 @@ public CompletableFuture<Result<Map<RequestPolicy, Set<Item>>>> lookupRequestPol
.thenCompose(r -> r.after(this::lookupRequestPolicies));
}

public CompletableFuture<Result<RequestPolicy>> lookupRequestPolicy(User user) {
OleksandrVidinieiev marked this conversation as resolved.
Show resolved Hide resolved
// 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(),
UUID.randomUUID().toString(), UUID.randomUUID().toString())
.thenCompose(r -> r.after(this::lookupRequestPolicy))
.thenApply(result -> result.map(RequestPolicy::from));
}

private BinaryOperator<Set<Item>> itemsMergeOperator() {
return (items1, items2) -> Stream.concat(items1.stream(), items2.stream())
.collect(Collectors.toSet());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,52 +67,39 @@ private static Result<AllowedServicePointsRequest> buildRequest(RoutingContext r
.map(String::toUpperCase)
.map(Request.Operation::valueOf)
.orElse(null);

AllowedServicePointsRequest request = new AllowedServicePointsRequest(operation,
queryParams.get("requesterId"), queryParams.get("instanceId"), queryParams.get("itemId"),
queryParams.get("requestId"));

return validateAllowedServicePointsRequest(request);
}

private static Result<AllowedServicePointsRequest> validateAllowedServicePointsRequest(
AllowedServicePointsRequest allowedServicePointsRequest) {

log.debug("validateAllowedServicePointsRequest:: parameters allowedServicePointsRequest: {}",
allowedServicePointsRequest);

Request.Operation operation = allowedServicePointsRequest.getOperation();
String requesterId = allowedServicePointsRequest.getRequesterId();
String instanceId = allowedServicePointsRequest.getInstanceId();
String itemId = allowedServicePointsRequest.getItemId();
String requestId = allowedServicePointsRequest.getRequestId();
String requesterId = queryParams.get("requesterId");
String instanceId = queryParams.get("instanceId");
String itemId = queryParams.get("itemId");
String requestId = queryParams.get("requestId");
String useStubItem = queryParams.get("useStubItem");

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

// Checking UUID validity

if (requesterId != null && !isUuid(requesterId)) {
log.warn("Requester ID is not a valid UUID: {}", requesterId);
log.warn("validateAllowedServicePointsRequest:: requester ID is not a valid UUID: {}", requesterId);
errors.add(String.format("Requester ID is not a valid UUID: %s.", requesterId));
}

if (instanceId != null && !isUuid(instanceId)) {
log.warn("Instance ID is not a valid UUID: {}", requesterId);
log.warn("validateAllowedServicePointsRequest:: instance ID is not a valid UUID: {}",
requesterId);
errors.add(String.format("Instance ID is not a valid UUID: %s.", instanceId));
}

if (itemId != null && !isUuid(itemId)) {
log.warn("Item ID is not a valid UUID: {}", itemId);
log.warn("validateAllowedServicePointsRequest:: item ID is not a valid UUID: {}", itemId);
errors.add(String.format("Item ID is not a valid UUID: %s.", itemId));
}

if (requestId != null && !isUuid(requestId)) {
log.warn("Request ID is not a valid UUID: {}", requestId);
log.warn("validateAllowedServicePointsRequest:: request ID is not a valid UUID: {}",
requestId);
errors.add(String.format("Request ID is not a valid UUID: %s.", requestId));
}

if (useStubItem != null && !"true".equals(useStubItem) && !"false".equals(useStubItem)) {
log.warn("validateAllowedServicePointsRequest:: useStubItem is not a valid boolean: {}",
useStubItem);
errors.add(String.format("useStubItem is not a valid boolean: %s.", useStubItem));
}
// Checking parameter combinations

boolean allowedCombinationOfParametersDetected = false;

if (operation == Request.Operation.CREATE && requesterId != null && instanceId != null &&
Expand All @@ -121,40 +108,36 @@ private static Result<AllowedServicePointsRequest> validateAllowedServicePointsR
log.info("validateAllowedServicePointsRequest:: TLR request creation case");
allowedCombinationOfParametersDetected = true;
}

if (operation == Request.Operation.CREATE && requesterId != null && instanceId == null &&
itemId != null && requestId == null) {

log.info("validateAllowedServicePointsRequest:: ILR request creation case");
allowedCombinationOfParametersDetected = true;
}

if (operation == Request.Operation.REPLACE && requesterId == null && instanceId == null &&
itemId == null && requestId != null) {

log.info("validateAllowedServicePointsRequest:: request replacement case");
allowedCombinationOfParametersDetected = true;
}

if (operation == Request.Operation.MOVE && requesterId == null && instanceId == null &&
itemId != null && requestId != null) {

log.info("validateAllowedServicePointsRequest:: request movement case");
allowedCombinationOfParametersDetected = true;
}

if (!allowedCombinationOfParametersDetected) {
String errorMessage = "Invalid combination of query parameters";
errors.add(errorMessage);
}

if (!errors.isEmpty()) {
String errorMessage = String.join(" ", errors);
log.error("validateRequest:: allowed service points request failed: {}", errorMessage);
return failed(new BadRequestFailure(errorMessage));
}

return succeeded(allowedServicePointsRequest);
return succeeded(new AllowedServicePointsRequest(operation, requesterId, instanceId, itemId,
requestId, Boolean.parseBoolean(useStubItem)));
}

private static JsonObject toJson(Map<RequestType, Set<AllowedServicePoint>> allowedServicePoints) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import java.util.Arrays;
import java.util.Collection;
import java.util.EnumMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
Expand Down Expand Up @@ -154,6 +155,12 @@ private CompletableFuture<Result<User>> fetchUser(AllowedServicePointsRequest re
? this::extractAllowedServicePointsIgnoringItemStatus
: this::extractAllowedServicePointsConsideringItemStatus;

if (request.isUseStubItem()) {
return requestPolicyRepository.lookupRequestPolicy(user)
.thenCompose(r -> r.after(policy -> extractAllowedServicePointsIgnoringItemStatus(
policy, new HashSet<>())));
}

return requestPolicyRepository.lookupRequestPolicies(items, user)
.thenCompose(r -> r.after(policies -> allOf(policies, mappingFunction)))
.thenApply(r -> r.map(this::combineAllowedServicePoints));
Expand Down
Loading