Skip to content

Commit

Permalink
CIRC-1970, skip account for reminders without fee (#1377)
Browse files Browse the repository at this point in the history
- Support reminders with a fee of zero, but don't create an account then,
  and apply loan notice context instead of fee-fine charge context.
  • Loading branch information
nielserik authored Nov 21, 2023
1 parent 7666b2f commit 86a5a85
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import static java.util.Objects.isNull;
import static org.folio.circulation.domain.ItemStatus.CLAIMED_RETURNED;
import static org.folio.circulation.domain.ItemStatus.DECLARED_LOST;
import static org.folio.circulation.domain.notice.TemplateContextUtil.createLoanNoticeContext;
import static org.folio.circulation.domain.representations.ContributorsToNamesMapper.mapContributorNamesToJson;
import static org.folio.circulation.support.results.Result.*;
import static org.folio.circulation.support.results.ResultBinding.mapResult;
Expand Down Expand Up @@ -131,6 +132,10 @@ private CompletableFuture<Result<ScheduledNoticeContext>> updateLoan(ScheduledNo

private CompletableFuture<Result<ScheduledNoticeContext>> buildAccountObject(ScheduledNoticeContext context) {
Loan loan = context.getLoan();
OverdueFinePolicyRemindersPolicy.ReminderSequenceEntry reminder = context.getLoan().getNextReminder();
if (isNoticeIrrelevant(context) || reminder.hasZeroFee()) {
return ofAsync(() -> context);
}
Item item = context.getLoan().getItem();
ReminderFeeAccount reminderFeeAccount = new ReminderFeeAccount()
.with(ReminderFeeAccount.FEE_FINE_ID, ACCOUNT_FEE_FINE_ID_VALUE)
Expand Down Expand Up @@ -165,15 +170,17 @@ private CompletableFuture<Result<FeeFineOwner>> lookupFeeFineOwner(ScheduledNoti
}

private CompletableFuture<Result<ScheduledNoticeContext>> persistAccount(ScheduledNoticeContext context) {
if (isNoticeIrrelevant(context)) {
OverdueFinePolicyRemindersPolicy.ReminderSequenceEntry reminder = context.getLoan().getNextReminder();
if (isNoticeIrrelevant(context) || reminder.hasZeroFee()) {
return ofAsync(() -> context);
}
return accountsStorageClient.post(context.getAccount().toJson())
.thenApply(r -> Result.succeeded(context));
}

private CompletableFuture<Result<ScheduledNoticeContext>> createFeeFineAction(ScheduledNoticeContext context) {
if (isNoticeIrrelevant(context)) {
OverdueFinePolicyRemindersPolicy.ReminderSequenceEntry reminder = context.getLoan().getNextReminder();
if (isNoticeIrrelevant(context) || reminder.hasZeroFee()) {
return ofAsync(() -> context);
}
Account account = context.getAccount();
Expand Down Expand Up @@ -223,7 +230,12 @@ protected boolean isNoticeIrrelevant(ScheduledNoticeContext context) {

@Override
protected JsonObject buildNoticeContextJson(ScheduledNoticeContext context) {
return createFeeFineChargeNoticeContext(context.getAccount(), context.getLoan(), context.getChargeAction());
Loan loan = context.getLoan();
if (loan.getNextReminder().hasZeroFee()) {
return createLoanNoticeContext(loan);
} else {
return createFeeFineChargeNoticeContext(context.getAccount(), loan, context.getChargeAction());
}
}

static class ReminderFeeAccount {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -135,5 +135,9 @@ private static String normalizeTimeUnit (String timeUnitId) {
return (capitalized.endsWith("s") ? capitalized : capitalized + "s");
}

public boolean hasZeroFee () {
return reminderFee.doubleValue() == 0.0;
}

}
}
9 changes: 5 additions & 4 deletions src/test/java/api/loans/ReminderFeeTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ void checkOutWithReminderFeePolicyWillScheduleFirstReminder() {
}

@Test
void willSendThreeRemindersAndCreateThreeAccountsThenStop() {
void willSendThreeRemindersAndCreateTwoAccountsThenStop() {

final IndividualResource response = checkOutFixture.checkOutByBarcode(
new CheckOutByBarcodeRequestBuilder()
Expand Down Expand Up @@ -137,7 +137,8 @@ void willSendThreeRemindersAndCreateThreeAccountsThenStop() {
verifyNumberOfSentNotices(2);
verifyNumberOfPublishedEvents(NOTICE, 2);
verifyNumberOfPublishedEvents(NOTICE_ERROR, 0);
waitAtMost(1, SECONDS).until(accountsClient::getAll, hasSize(2));
// Second reminder has zero fee, don't create account
waitAtMost(1, SECONDS).until(accountsClient::getAll, hasSize(1));

ZonedDateTime thirdRunTime = dueDate.plusMinutes(6);
scheduledNoticeProcessingClient.runScheduledDigitalRemindersProcessing(thirdRunTime);
Expand All @@ -146,7 +147,7 @@ void willSendThreeRemindersAndCreateThreeAccountsThenStop() {
verifyNumberOfSentNotices(3);
verifyNumberOfPublishedEvents(NOTICE, 3);
verifyNumberOfPublishedEvents(NOTICE_ERROR, 0);
waitAtMost(1, SECONDS).until(accountsClient::getAll, hasSize(3));
waitAtMost(1, SECONDS).until(accountsClient::getAll, hasSize(2));

ZonedDateTime fourthRunTime = dueDate.plusMinutes(8);
scheduledNoticeProcessingClient.runScheduledDigitalRemindersProcessing(fourthRunTime);
Expand All @@ -155,7 +156,7 @@ void willSendThreeRemindersAndCreateThreeAccountsThenStop() {
verifyNumberOfSentNotices(3);
verifyNumberOfPublishedEvents(NOTICE, 3);
verifyNumberOfPublishedEvents(NOTICE_ERROR, 0);
waitAtMost(1, SECONDS).until(accountsClient::getAll, hasSize(3));
waitAtMost(1, SECONDS).until(accountsClient::getAll, hasSize(2));
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ public IndividualResource reminderFeesPolicy() {
.withAddedReminderEntry(
1,"minute",1.50,
"Email",FIRST_REMINDER_TEMPLATE_ID.toString())
.withAddedReminderEntry(1, "minute", 2.00,
.withAddedReminderEntry(1, "minute", 0.00,
"Email", SECOND_REMINDER_TEMPLATE_ID.toString())
.withAddedReminderEntry(1,"minute", 2.15,
"Email", THIRD_REMINDER_TEMPLATE_ID.toString());
Expand Down

0 comments on commit 86a5a85

Please sign in to comment.