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-2181: Missing owner throws NPE on age to lost with actual cost #1513

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package org.folio.circulation.services.agedtolost;

import static java.util.concurrent.CompletableFuture.completedFuture;
import static java.util.stream.Collectors.toSet;
import static org.folio.circulation.domain.FeeFine.LOST_ITEM_FEE_TYPE;
import static org.folio.circulation.domain.FeeFine.LOST_ITEM_PROCESSING_FEE_TYPE;
Expand Down Expand Up @@ -36,6 +37,7 @@
import org.apache.logging.log4j.Logger;
import org.folio.circulation.StoreLoanAndItem;
import org.folio.circulation.domain.FeeFine;
import org.folio.circulation.domain.FeeFineAction;
import org.folio.circulation.domain.FeeFineOwner;
import org.folio.circulation.domain.Loan;
import org.folio.circulation.domain.MultipleRecords;
Expand Down Expand Up @@ -136,7 +138,6 @@ private CompletableFuture<Result<Void>> chargeLostFees(
LoanToChargeFees loanToChargeFees) {

return ofAsync(() -> loanToChargeFees)
.thenCompose(r -> r.after(actualCostRecordService::createIfNecessaryForAgedToLostItem))
.thenCompose(r -> r.after(this::chargeLostFeesForLoan))
.thenCompose(r -> r.after(eventPublisher::publishClosedLoanEvent))
.thenApply(r -> r.mapFailure(failure -> handleFailure(loanToChargeFees, failure.toString())))
Expand All @@ -149,18 +150,27 @@ private static Result<Void> handleFailure(LoanToChargeFees loan, String errorMes
}

private CompletableFuture<Result<Loan>> chargeLostFeesForLoan(LoanToChargeFees loanToChargeFees) {
Loan loan = loanToChargeFees.getLoan();

// we can close loans that have no fee to charge
// and billed immediately
if (loanToChargeFees.shouldCloseLoan()) {
log.info("No age to lost fees/fines to charge immediately, closing loan [{}]",
loanToChargeFees.getLoan().getId());

log.info("No age to lost fees/fines to charge immediately, closing loan [{}]", loan.getId());
return closeLoanAsLostAndPaid(loanToChargeFees);
}

Loan loan = loanToChargeFees.getLoan();
return createAccountsForLoan(loanToChargeFees)
.after(feeFineFacade::createAccounts)
if (loanToChargeFees.hasNoFeeFineOwner()) {
log.warn("No fee/fine owner present for primary service point {}, skipping loan {}",
loanToChargeFees.getPrimaryServicePointId(), loan.getId());

return completedFuture(failed(singleValidationError(
"No fee/fine owner found for item's permanent location",
"servicePointId", loanToChargeFees.getPrimaryServicePointId())));
}

return actualCostRecordService.createIfNecessaryForAgedToLostItem(loanToChargeFees)
.thenApply(r -> r.next(this::createAccountsForLoan))
.thenCompose(r -> r.after(feeFineFacade::createAccounts))
.thenCompose(r -> r.after(actions ->
feeFineScheduledNoticeService.scheduleNoticesForAgedLostFeeFineCharged(loan, actions)))
.thenCompose(r -> r.after(notUsed -> updateLoanBillingInfo(loanToChargeFees)));
Expand Down Expand Up @@ -279,14 +289,6 @@ private Result<CqlQuery> loanFetchQuery() {
}

private Result<LoanToChargeFees> validateCanCreateAccountForLoan(LoanToChargeFees loanToChargeFees) {
if (loanToChargeFees.hasNoFeeFineOwner()) {
log.warn("No fee/fine owner present for service point {}, skipping loan {}",
loanToChargeFees.getPrimaryServicePointId(), loanToChargeFees.getLoan().getId());

return failed(singleValidationError("No fee/fine owner found for item's effective location",
"servicePointId", loanToChargeFees.getPrimaryServicePointId()));
}

final LostItemPolicy lostItemPolicy = loanToChargeFees.getLoan().getLostItemPolicy();

if (lostItemPolicy.getSetCostFee().isChargeable() && loanToChargeFees.hasNoLostItemFee()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -298,6 +298,28 @@ void cannotChargeFeesWhenNoItemFeeType() {
assertThatPublishedLoanLogRecordEventsAreValid(loanAfter.getJson());
}

@Test
void cannotChargeActualCostWithoutOwner() {
feeFineOwnerFixture.cleanUp(); // remove owner

var policy = lostItemFeePoliciesFixture
.ageToLostAfterOneMinutePolicy()
.withActualCost(19.99)
.withLostItemProcessingFee(9.99);
useLostItemPolicy(lostItemFeePoliciesFixture.create(policy).getId());
val item = itemsFixture.basedUponNod(ItemBuilder::withRandomBarcode);
val loanBefore = checkOutFixture.checkOutByBarcode(item, usersFixture.steve());

ageToLostFixture.ageToLostAndChargeFees();

IndividualResource loanAfter = loansFixture.getLoanById(loanBefore.getId());
assertThat(loanAfter, hasNoLostItemFee());
assertThat(loanAfter, hasNoLostItemProcessingFee());
assertThat(accountsClient.getAll(), hasSize(0));
assertThat(scheduledNoticesClient.getAll(), hasSize(0));
assertThatPublishedLoanLogRecordEventsAreValid(loanAfter.getJson());
}

@Test
void shouldNotChargeFeesWhenDelayedBillingPeriodHasNotPassed() {
val lostItemFeePolicy = lostItemFeePoliciesFixture
Expand Down