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 2141 #2

Merged
merged 2 commits into from
Sep 17, 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
4 changes: 4 additions & 0 deletions ramls/request.json
Original file line number Diff line number Diff line change
Expand Up @@ -423,6 +423,10 @@
"description": "Request fields used for search",
"type": "object",
"$ref": "request-search-index.json"
},
"itemLocationCode": {
"description": "Allow specifying item location when creating title-level requests",
"type": "string"
}
},
"additionalProperties": false,
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/org/folio/circulation/domain/Campus.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,10 @@ public static Campus unknown() {
}

public static Campus unknown(String id) {
return new Campus(id, null);
return new Campus(id, null, null);
}

String id;
String name;
String code;
}
3 changes: 2 additions & 1 deletion src/main/java/org/folio/circulation/domain/Institution.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,10 @@ public static Institution unknown() {
}

public static Institution unknown(String id) {
return new Institution(id, null);
return new Institution(id, null, null);
}

String id;
String name;
String code;
}
3 changes: 2 additions & 1 deletion src/main/java/org/folio/circulation/domain/Library.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,10 @@ public static Library unknown() {
}

public static Library unknown(String id) {
return new Library(id, null);
return new Library(id, null, null);
}

String id;
String name;
String code;
}
5 changes: 5 additions & 0 deletions src/main/java/org/folio/circulation/domain/Request.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import static org.folio.circulation.domain.representations.RequestProperties.CANCELLATION_REASON_ID;
import static org.folio.circulation.domain.representations.RequestProperties.CANCELLATION_REASON_NAME;
import static org.folio.circulation.domain.representations.RequestProperties.CANCELLATION_REASON_PUBLIC_DESCRIPTION;
import static org.folio.circulation.domain.representations.RequestProperties.ITEM_LOCATION_CODE;
import static org.folio.circulation.domain.representations.RequestProperties.HOLDINGS_RECORD_ID;
import static org.folio.circulation.domain.representations.RequestProperties.HOLD_SHELF_EXPIRATION_DATE;
import static org.folio.circulation.domain.representations.RequestProperties.INSTANCE_ID;
Expand Down Expand Up @@ -379,6 +380,10 @@ public String getPatronComments() {
return getProperty(requestRepresentation, "patronComments");
}

public String geItemLocationCode() {
return getProperty(requestRepresentation, ITEM_LOCATION_CODE);
}

public Request truncateRequestExpirationDateToTheEndOfTheDay(ZoneId zone) {
ZonedDateTime requestExpirationDate = getRequestExpirationDate();
if (requestExpirationDate != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,5 @@ private RequestProperties() { }
public static final String REQUESTER_ID = "requesterId";
public static final String FULFILLMENT_PREFERENCE = "fulfillmentPreference";
public static final String PICKUP_SERVICE_POINT_ID = "pickupServicePointId";
public static final String ITEM_LOCATION_CODE = "itemLocationCode";
}
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,9 @@ public CompletableFuture<Result<MultipleRecords<Location>>> fetchLocations(
new LocationMapper()::toDomain);

return fetcher.findByIds(locationIds)
.thenCompose(this::loadLibrariesForLocations);
.thenCompose(this::loadLibrariesForLocations)
.thenCompose(this::loadCampusesForLocations)
.thenCompose(this::loadInstitutionsForLocations);
}

private CompletableFuture<Result<Location>> loadLibrary(Location location) {
Expand Down Expand Up @@ -208,6 +210,19 @@ public CompletableFuture<Result<Map<String, Library>>> getLibraries(
.thenApply(mapResult(records -> records.toMap(Library::getId)));
}

private CompletableFuture<Result<MultipleRecords<Location>>> loadCampusesForLocations(
Result<MultipleRecords<Location>> multipleRecordsResult) {

log.debug("loadCampusesForLocations:: parameters multipleRecordsResult: {}",
() -> resultAsString(multipleRecordsResult));

return multipleRecordsResult.combineAfter(
locations -> getCampuses(locations.getRecords()), (locations, campuses) ->
locations.mapRecords(location -> location.withCampus(
campuses.getOrDefault(location.getCampusId(), Campus.unknown(location.getCampusId())))));

}

public CompletableFuture<Result<Map<String, Campus>>> getCampuses(
Collection<Location> locations) {

Expand All @@ -223,6 +238,19 @@ public CompletableFuture<Result<Map<String, Campus>>> getCampuses(
.thenApply(mapResult(records -> records.toMap(Campus::getId)));
}

private CompletableFuture<Result<MultipleRecords<Location>>> loadInstitutionsForLocations(
Result<MultipleRecords<Location>> multipleRecordsResult) {

log.debug("loadInstitutionsForLocations:: parameters multipleRecordsResult: {}",
() -> resultAsString(multipleRecordsResult));

return multipleRecordsResult.combineAfter(
locations -> getInstitutions(locations.getRecords()), (locations, institutions) ->
locations.mapRecords(location -> location.withInstitution(
institutions.getOrDefault(location.getInstitutionId(), Institution.unknown(location.getInstitutionId())))));

}

public CompletableFuture<Result<Map<String, Institution>>> getInstitutions(
Collection<Location> locations) {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import static java.util.stream.Collectors.toList;
import static java.util.stream.Collectors.toSet;
import static org.folio.circulation.domain.RequestType.PAGE;
import static org.folio.circulation.domain.representations.RequestProperties.ITEM_LOCATION_CODE;
import static org.folio.circulation.domain.representations.RequestProperties.INSTANCE_ID;
import static org.folio.circulation.support.ValidationErrorFailure.failedValidation;
import static org.folio.circulation.support.results.Result.of;
Expand Down Expand Up @@ -69,7 +70,27 @@ private Result<List<Item>> refusePageRequestWhenNoAvailablePageableItemsExist(Re
return failedValidation(message, INSTANCE_ID, request.getInstanceId());
}

return of(() -> availablePageableItems);
List<Item> finalAvailablePageableItems;
if (request.geItemLocationCode() != null) {
finalAvailablePageableItems = availablePageableItems.stream()
.filter(item -> request.geItemLocationCode().equals(item.getLocation().getCode()) ||
request.geItemLocationCode().equals(item.getLocation().getLibrary().getCode()) ||
request.geItemLocationCode().equals(item.getLocation().getCampus().getCode()) ||
request.geItemLocationCode().equals(item.getLocation().getInstitution().getCode())
)
.toList();
if (finalAvailablePageableItems.isEmpty()) {
String message = "Cannot create page TLR for this instance ID - no pageable available " +
"items found in forced location";
log.info("{}. Instance ID: {}, Forced location code {}",
message, request.getInstanceId(), request.geItemLocationCode());
return failedValidation(message, ITEM_LOCATION_CODE, request.geItemLocationCode());
}
} else {
finalAvailablePageableItems = availablePageableItems;
}

return of(() -> finalAvailablePageableItems);
}

private static Item pickClosestItem(Collection<Location> requestedLocations, List<Item> availableItems) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
public class CampusMapper {
public Campus toDomain(JsonObject representation) {
return new Campus(getProperty(representation, "id"),
getProperty(representation, "name"));
getProperty(representation, "name"),
getProperty(representation, "code"));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
public class InstitutionMapper {
public Institution toDomain(JsonObject representation) {
return new Institution(getProperty(representation, "id"),
getProperty(representation, "name"));
getProperty(representation, "name"),
getProperty(representation, "code"));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
public class LibraryMapper {
public Library toDomain(JsonObject representation) {
return new Library(getProperty(representation, "id"),
getProperty(representation, "name"));
getProperty(representation, "name"),
getProperty(representation, "code"));
}
}
58 changes: 58 additions & 0 deletions src/test/java/api/requests/RequestsAPICreationTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -631,6 +631,64 @@ void canCreateTitleLevelRequestWhenTlrEnabled() {
assertThat(publishedEvents.filterToList(byEventType("LOAN_DUE_DATE_CHANGED")), hasSize(0));
}

@ParameterizedTest
@CsvSource(value = {
"NU/JC/DL/3F",
"DLRC",
"JC",
"NU"
})
void createTitleLevelRequestWhenTlrEnabledSetLocation(String locationCode) {
UUID patronId = usersFixture.charlotte().getId();
final UUID pickupServicePointId = servicePointsFixture.cd1().getId();

final var items = itemsFixture.createMultipleItemsForTheSameInstance(2);
UUID instanceId = items.get(0).getInstanceId();

configurationsFixture.enableTlrFeature();

IndividualResource requestResource = requestsClient.create(new RequestBuilder()
.page()
.withNoHoldingsRecordId()
.withNoItemId()
.titleRequestLevel()
.withInstanceId(instanceId)
.withPickupServicePointId(pickupServicePointId)
.withRequesterId(patronId)
.withItemLocationCode(locationCode));

JsonObject request = requestResource.getJson();
assertThat(request.getString("requestLevel"), is("Title"));
}

@Test
void createTitleLevelRequestWhenTlrEnabledSetLocationNoItems() {
UUID patronId = usersFixture.charlotte().getId();
final UUID pickupServicePointId = servicePointsFixture.cd1().getId();

final var items = itemsFixture.createMultipleItemsForTheSameInstance(2);
UUID instanceId = items.get(0).getInstanceId();

configurationsFixture.enableTlrFeature();

Response response = requestsClient.attemptCreate(
new RequestBuilder()
.page()
.withNoHoldingsRecordId()
.withNoItemId()
.titleRequestLevel()
.withInstanceId(instanceId)
.withPickupServicePointId(pickupServicePointId)
.withRequesterId(patronId)
.withItemLocationCode("DoesNotExist")
.create());

assertThat(response.getStatusCode(), is(422));
assertThat(response.getJson(), hasErrorWith(
hasMessage("Cannot create page TLR for this instance ID - no pageable " +
"available items found in forced location")));
}

@Test
void cannotCreateRequestWithNonExistentRequestLevelWhenTlrEnabled() {
UUID patronId = usersFixture.charlotte().getId();
Expand Down
7 changes: 6 additions & 1 deletion src/test/java/api/support/builders/RequestBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import static api.support.utl.DateTimeUtils.getLocalDatePropertyForDateWithTime;
import static java.time.ZoneOffset.UTC;
import static java.util.stream.Collectors.toList;
import static org.folio.circulation.domain.representations.RequestProperties.ITEM_LOCATION_CODE;
import static org.folio.circulation.support.json.JsonPropertyFetcher.getDateTimeProperty;
import static org.folio.circulation.support.json.JsonPropertyFetcher.getIntegerProperty;
import static org.folio.circulation.support.json.JsonPropertyFetcher.getLocalDateProperty;
Expand Down Expand Up @@ -63,6 +64,7 @@ public class RequestBuilder extends JsonBuilder implements Builder {
private final Tags tags;
private final String patronComments;
private final BlockOverrides blockOverrides;
private final String itemLocationCode;

public RequestBuilder() {
this(UUID.randomUUID(),
Expand All @@ -89,6 +91,7 @@ public RequestBuilder() {
null,
null,
null,
null,
null);
}

Expand Down Expand Up @@ -120,7 +123,8 @@ public static RequestBuilder from(IndividualResource response) {
getUUIDProperty(representation, "pickupServicePointId"),
new Tags((toStream(representation.getJsonObject("tags"), "tagList").collect(toList()))),
getProperty(representation, "patronComments"),
null
null,
getProperty(representation, ITEM_LOCATION_CODE)
);
}

Expand Down Expand Up @@ -149,6 +153,7 @@ public JsonObject create() {
put(request, "cancelledDate", formatDateTimeOptional(cancelledDate));
put(request, "pickupServicePointId", this.pickupServicePointId);
put(request, "patronComments", this.patronComments);
put(request, ITEM_LOCATION_CODE, this.itemLocationCode);

if (itemSummary != null) {
final JsonObject itemRepresentation = new JsonObject();
Expand Down