forked from folio-org/mod-circulation
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCreateRequestService.java
More file actions
298 lines (248 loc) · 15.8 KB
/
CreateRequestService.java
File metadata and controls
298 lines (248 loc) · 15.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
package org.folio.circulation.domain;
import static java.util.concurrent.CompletableFuture.completedFuture;
import static org.folio.circulation.domain.EcsRequestPhase.INTERMEDIATE;
import static org.folio.circulation.domain.EcsRequestPhase.PRIMARY;
import static org.folio.circulation.domain.RequestLevel.TITLE;
import static org.folio.circulation.domain.representations.RequestProperties.INSTANCE_ID;
import static org.folio.circulation.domain.representations.RequestProperties.ITEM_ID;
import static org.folio.circulation.domain.representations.RequestProperties.REQUESTER_ID;
import static org.folio.circulation.domain.representations.logs.LogEventType.REQUEST_CREATED;
import static org.folio.circulation.domain.representations.logs.LogEventType.REQUEST_CREATED_THROUGH_OVERRIDE;
import static org.folio.circulation.domain.representations.logs.RequestUpdateLogEventMapper.mapToRequestLogEventJson;
import static org.folio.circulation.resources.handlers.error.CirculationErrorType.ATTEMPT_HOLD_OR_RECALL_TLR_FOR_AVAILABLE_ITEM;
import static org.folio.circulation.resources.handlers.error.CirculationErrorType.ATTEMPT_TO_CREATE_TLR_LINKED_TO_AN_ITEM;
import static org.folio.circulation.resources.handlers.error.CirculationErrorType.INSTANCE_DOES_NOT_EXIST;
import static org.folio.circulation.resources.handlers.error.CirculationErrorType.INVALID_INSTANCE_ID;
import static org.folio.circulation.resources.handlers.error.CirculationErrorType.INVALID_ITEM_ID;
import static org.folio.circulation.resources.handlers.error.CirculationErrorType.INVALID_USER_OR_PATRON_GROUP_ID;
import static org.folio.circulation.resources.handlers.error.CirculationErrorType.ITEM_ALREADY_LOANED_TO_SAME_USER;
import static org.folio.circulation.resources.handlers.error.CirculationErrorType.ITEM_ALREADY_REQUESTED_BY_SAME_USER;
import static org.folio.circulation.resources.handlers.error.CirculationErrorType.ITEM_DOES_NOT_EXIST;
import static org.folio.circulation.resources.handlers.error.CirculationErrorType.ONE_OF_INSTANCES_ITEMS_HAS_OPEN_LOAN;
import static org.folio.circulation.resources.handlers.error.CirculationErrorType.REQUESTING_DISALLOWED;
import static org.folio.circulation.resources.handlers.error.CirculationErrorType.REQUESTING_DISALLOWED_BY_POLICY;
import static org.folio.circulation.resources.handlers.error.CirculationErrorType.REQUEST_NOT_ALLOWED_FOR_PATRON_TITLE_COMBINATION;
import static org.folio.circulation.resources.handlers.error.CirculationErrorType.TLR_RECALL_WITHOUT_OPEN_LOAN_OR_RECALLABLE_ITEM;
import static org.folio.circulation.resources.handlers.error.CirculationErrorType.USER_IS_INACTIVE;
import static org.folio.circulation.support.ErrorCode.HOLD_AND_RECALL_TLR_NOT_ALLOWED_PAGEABLE_AVAILABLE_ITEM_FOUND;
import static org.folio.circulation.support.ValidationErrorFailure.failedValidation;
import static org.folio.circulation.support.results.MappingFunctions.when;
import static org.folio.circulation.support.results.Result.of;
import static org.folio.circulation.support.results.Result.ofAsync;
import static org.folio.circulation.support.results.Result.succeeded;
import static org.folio.circulation.support.utils.LogUtil.logResult;
import java.lang.invoke.MethodHandles;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CompletableFuture;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.folio.circulation.domain.representations.logs.LogEventType;
import org.folio.circulation.domain.validation.RequestLoanValidator;
import org.folio.circulation.resources.RequestBlockValidators;
import org.folio.circulation.resources.RequestNoticeSender;
import org.folio.circulation.resources.handlers.error.CirculationErrorHandler;
import org.folio.circulation.services.EventPublisher;
import org.folio.circulation.services.ItemForTlrService;
import org.folio.circulation.support.ErrorCode;
import org.folio.circulation.support.ValidationErrorFailure;
import org.folio.circulation.support.request.RequestRelatedRepositories;
import org.folio.circulation.support.results.Result;
public class CreateRequestService {
private static final Logger log = LogManager.getLogger(MethodHandles.lookup().lookupClass());
private final RequestRelatedRepositories repositories;
private final UpdateUponRequest updateUponRequest;
private final RequestLoanValidator requestLoanValidator;
private final RequestNoticeSender requestNoticeSender;
private final RequestBlockValidators requestBlockValidators;
private final EventPublisher eventPublisher;
private final CirculationErrorHandler errorHandler;
public CreateRequestService(RequestRelatedRepositories repositories,
UpdateUponRequest updateUponRequest, RequestLoanValidator requestLoanValidator,
RequestNoticeSender requestNoticeSender, RequestBlockValidators requestBlockValidators,
EventPublisher eventPublisher, CirculationErrorHandler errorHandler) {
this.repositories = repositories;
this.updateUponRequest = updateUponRequest;
this.requestLoanValidator = requestLoanValidator;
this.requestNoticeSender = requestNoticeSender;
this.requestBlockValidators = requestBlockValidators;
this.eventPublisher = eventPublisher;
this.errorHandler = errorHandler;
}
public CompletableFuture<Result<RequestAndRelatedRecords>> createRequest(
RequestAndRelatedRecords requestAndRelatedRecords) {
log.debug("createRequest:: parameters requestAndRelatedRecords: {}", () -> requestAndRelatedRecords);
final var requestRepository = repositories.getRequestRepository();
final var settingsRepository = repositories.getSettingsRepository();
final var automatedBlocksValidator = requestBlockValidators.getAutomatedPatronBlocksValidator();
final var manualBlocksValidator = requestBlockValidators.getManualPatronBlocksValidator();
final Result<RequestAndRelatedRecords> result = succeeded(requestAndRelatedRecords);
return result.next(RequestServiceUtility::refuseWhenInvalidUserAndPatronGroup)
.mapFailure(err -> errorHandler.handleValidationError(err, INVALID_USER_OR_PATRON_GROUP_ID, result))
.next(RequestServiceUtility::refuseWhenUserIsInactive)
.mapFailure(err -> errorHandler.handleValidationError(err, USER_IS_INACTIVE, result))
.next(RequestServiceUtility::refuseWhenAlreadyRequested)
.mapFailure(err -> errorHandler.handleValidationError(err, ITEM_ALREADY_REQUESTED_BY_SAME_USER, result))
.after(automatedBlocksValidator::validate)
.thenApply(r -> errorHandler.handleValidationResult(r, automatedBlocksValidator.getErrorType(), result))
.thenCompose(r -> r.after(manualBlocksValidator::validate))
.thenApply(r -> errorHandler.handleValidationResult(r, manualBlocksValidator.getErrorType(), result))
.thenComposeAsync(r -> r.after(when(this::shouldCheckInstance, this::checkInstance, this::doNothing)))
.thenComposeAsync(r -> r.after(when(this::shouldCheckItem, this::checkItem, this::doNothing)))
.thenComposeAsync(r -> r.after(this::checkPolicy))
.thenApply(r -> r.next(this::refuseHoldOrRecallTlrWhenPageableItemExists))
.thenComposeAsync(r -> r.combineAfter(settingsRepository::lookupTimeZoneSettings,
RequestAndRelatedRecords::withTimeZone))
.thenApply(r -> r.next(errorHandler::failWithValidationErrors))
.thenComposeAsync(r -> r.after(updateUponRequest.updateItem::onRequestCreateOrUpdate))
.thenComposeAsync(r -> r.after(updateUponRequest.updateLoan::onRequestCreateOrUpdate))
.thenComposeAsync(r -> r.after(requestRepository::create))
.thenComposeAsync(r -> r.after(updateUponRequest.updateRequestQueue::onCreate))
.thenApplyAsync(r -> {
r.after(t -> eventPublisher.publishLogRecord(mapToRequestLogEventJson(t.getRequest()), getLogEventType()));
return r.next(requestNoticeSender::sendNoticeOnRequestCreated);
}).thenApply(r -> logResult(r, "createRequest"));
}
private Result<RequestAndRelatedRecords> refuseHoldOrRecallTlrWhenPageableItemExists(
RequestAndRelatedRecords requestAndRelatedRecords) {
Request request = requestAndRelatedRecords.getRequest();
if (request.isTitleLevel() && (request.isHold() || request.isRecall())) {
log.info("refuseHoldOrRecallTlrWhenPageableItemExists:: request is title-level Hold or Recall");
List<Item> availablePageableItems = ItemForTlrService.using(repositories)
.findAvailablePageableItems(requestAndRelatedRecords.getRequest());
return failValidationWhenPageableItemsExist(requestAndRelatedRecords, availablePageableItems)
.mapFailure(err -> errorHandler.handleValidationError(err,
ATTEMPT_HOLD_OR_RECALL_TLR_FOR_AVAILABLE_ITEM, requestAndRelatedRecords));
}
return of(() -> requestAndRelatedRecords);
}
private Result<RequestAndRelatedRecords> failValidationWhenPageableItemsExist(
RequestAndRelatedRecords requestAndRelatedRecords, List<Item> availablePageableItems) {
if (availablePageableItems.isEmpty()) {
log.info("failValidationWhenPageableItemsExist:: no available pageable items found");
return succeeded(requestAndRelatedRecords);
}
String availablePageableItemId = availablePageableItems.stream()
.map(Item::getItemId)
.findAny()
.orElse("");
return failedValidationHoldAndRecallNotAllowed(requestAndRelatedRecords.getRequest(),
availablePageableItemId);
}
private Result<RequestAndRelatedRecords> failedValidationHoldAndRecallNotAllowed(Request request,
String availableItemId) {
String errorMessage = "Hold/Recall title level request not allowed: pageable available item " +
"found for instance";
log.warn("failedValidationHoldAndRecallNotAllowed:: {}. Pageable available item: {}",
errorMessage, availableItemId);
return failedValidation(errorMessage, Map.of(ITEM_ID, availableItemId, INSTANCE_ID,
request.getInstanceId()), HOLD_AND_RECALL_TLR_NOT_ALLOWED_PAGEABLE_AVAILABLE_ITEM_FOUND);
}
private CompletableFuture<Result<RequestAndRelatedRecords>> checkInstance(
RequestAndRelatedRecords records) {
return completedFuture(succeeded(records)
.next(RequestServiceUtility::refuseWhenInstanceDoesNotExist)
.mapFailure(err ->
errorHandler.handleValidationError(err, INSTANCE_DOES_NOT_EXIST, records)));
}
private CompletableFuture<Result<RequestAndRelatedRecords>> checkItem(
RequestAndRelatedRecords records) {
Request request = records.getRequest();
RequestLevel requestLevel = request.getRequestLevel();
log.info("checkItem:: request level: {}", requestLevel);
log.debug("checkItem:: accumulated errors: {}", errorHandler::getErrors);
if (records.isTlrFeatureEnabled() && requestLevel == TITLE) {
log.info("checkItem:: checking title-level request");
if (errorHandler.hasAny(ATTEMPT_TO_CREATE_TLR_LINKED_TO_AN_ITEM, ATTEMPT_HOLD_OR_RECALL_TLR_FOR_AVAILABLE_ITEM)) {
log.warn("checkItem:: error(s) incompatible with TLR check detected, check aborted");
return ofAsync(() -> records);
}
Result<RequestAndRelatedRecords> result = succeeded(records);
String itemId = request.getItemId();
if (itemId != null) {
log.info("checkItem:: request contains itemId: {}", itemId);
result = result
.next(RequestServiceUtility::refuseWhenItemDoesNotExist)
.mapFailure(err -> errorHandler.handleValidationError(err, ITEM_DOES_NOT_EXIST, records));
}
if (errorHandler.hasNone(INVALID_INSTANCE_ID, INSTANCE_DOES_NOT_EXIST)) {
log.info("checkItem:: checking if user already has an item of requested instance on loan");
return result
.after(requestLoanValidator::refuseWhenUserHasAlreadyBeenLoanedOneOfInstancesItems)
.thenApply(r -> errorHandler.handleValidationResult(r, ONE_OF_INSTANCES_ITEMS_HAS_OPEN_LOAN, records));
}
return completedFuture(result);
}
return succeeded(records)
.next(RequestServiceUtility::refuseWhenItemDoesNotExist)
.mapFailure(err -> errorHandler.handleValidationError(err, ITEM_DOES_NOT_EXIST, records))
.next(RequestServiceUtility::refuseWhenRequestTypeIsNotAllowedForItem)
.mapFailure(err -> errorHandler.handleValidationError(err, REQUESTING_DISALLOWED, records))
.after(requestLoanValidator::refuseWhenUserHasAlreadyBeenLoanedItem)
.thenApply(r -> errorHandler.handleValidationResult(r, ITEM_ALREADY_LOANED_TO_SAME_USER, records));
}
private CompletableFuture<Result<RequestAndRelatedRecords>> checkPolicy(
RequestAndRelatedRecords records) {
log.debug("checkPolicy:: accumulated errors: {}", errorHandler::getErrors);
if (errorHandler.hasAny(INVALID_INSTANCE_ID, INSTANCE_DOES_NOT_EXIST, INVALID_ITEM_ID,
ITEM_DOES_NOT_EXIST, INVALID_USER_OR_PATRON_GROUP_ID,
TLR_RECALL_WITHOUT_OPEN_LOAN_OR_RECALLABLE_ITEM)) {
log.warn("checkPolicy:: error(s) incompatible with check detected, check aborted");
return ofAsync(() -> records);
}
final Request request = records.getRequest();
boolean tlrFeatureEnabled = request.getTlrSettingsConfiguration().isTitleLevelRequestsFeatureEnabled();
if (tlrFeatureEnabled && request.isTitleLevel() && request.isHold()) {
EcsRequestPhase ecsRequestPhase = request.getEcsRequestPhase();
if (ecsRequestPhase == PRIMARY || ecsRequestPhase == INTERMEDIATE) {
log.warn("checkPolicy:: ECS TLR Hold with phase {} detected, skipping policy check", ecsRequestPhase);
return ofAsync(() -> records);
}
log.info("checkPolicy:: checking policy for title-level hold");
return completedFuture(checkPolicyForTitleLevelHold(records));
}
return repositories.getRequestPolicyRepository().lookupRequestPolicy(records)
.thenApply(r -> r.next(RequestServiceUtility::refuseWhenRequestCannotBeFulfilled)
.mapFailure(err -> errorHandler.handleValidationError(err, REQUESTING_DISALLOWED_BY_POLICY, r)));
}
private Result<RequestAndRelatedRecords> checkPolicyForTitleLevelHold(RequestAndRelatedRecords records) {
final Request request = records.getRequest();
if (request.getTlrSettingsConfiguration().isTlrHoldShouldFollowCirculationRules() &&
noneOfInstanceItemsAreAllowedForHold(request)) {
log.warn("checkPolicyForTlrHold:: none of the items of requested instance are allowed for " +
"Hold requests according to circulation rules: requesterId={}, instanceId={}",
request.getRequesterId(), request.getInstanceId());
return ValidationErrorFailure.<RequestAndRelatedRecords>failedValidation(
"Hold requests are not allowed for this patron and title combination",
Map.of(REQUESTER_ID, request.getRequesterId(), INSTANCE_ID, request.getInstanceId()),
ErrorCode.REQUEST_NOT_ALLOWED_FOR_PATRON_TITLE_COMBINATION)
.mapFailure(err -> errorHandler.handleValidationError(err,
REQUEST_NOT_ALLOWED_FOR_PATRON_TITLE_COMBINATION, records));
}
return succeeded(records);
}
private static boolean noneOfInstanceItemsAreAllowedForHold(Request request) {
return request.getInstanceItemsRequestPolicies()
.values()
.stream()
.noneMatch(policy -> policy.allowsType(RequestType.HOLD));
}
private CompletableFuture<Result<Boolean>> shouldCheckInstance(RequestAndRelatedRecords records) {
log.debug("shouldCheckInstance:: accumulated errors: {}", errorHandler::getErrors);
return ofAsync(() -> errorHandler.hasNone(INVALID_INSTANCE_ID));
}
private CompletableFuture<Result<Boolean>> shouldCheckItem(RequestAndRelatedRecords records) {
log.debug("shouldCheckItem:: accumulated errors: {}", errorHandler::getErrors);
return ofAsync(() -> errorHandler.hasNone(INVALID_INSTANCE_ID, INSTANCE_DOES_NOT_EXIST,
INVALID_ITEM_ID));
}
private CompletableFuture<Result<RequestAndRelatedRecords>> doNothing(
RequestAndRelatedRecords records) {
return ofAsync(() -> records);
}
private LogEventType getLogEventType() {
return requestBlockValidators.isOverrideRequested()
? REQUEST_CREATED_THROUGH_OVERRIDE
: REQUEST_CREATED;
}
}