Skip to content

CIRC-2681 cap hold-shelf expiry at due day - #1717

Merged
nielserik merged 8 commits into
masterfrom
CIRC-2681-cap-holdshelf-expiry-at-due-date
Nov 18, 2026
Merged

nielserik merged 8 commits into
masterfrom
CIRC-2681-cap-holdshelf-expiry-at-due-date

Conversation

@nielserik

@nielserik nielserik commented Nov 17, 2026 •

Copy link
Copy Markdown
Contributor

-- with for-use-at-location

Purpose

When putting a book on hold-shelf in a reading room, the calculated expiration date of the hold can be set to after the due date of the loan, which should not be possible.

The purpose of this PR is to cap the expiration day at the due day.

If the loan is subsequently renewed, and the due date thus extended, the standard expiration date should be used (the hold date + the configured expiry period, adjusted for closing days as configured).

This only applies to reading room loans ("for use at location").

Approach

Cap the default expiration date if necessary in HoldByBarcodeResource. Extend a capped expiration date if necessary in RenewalResource.

 -- with for-use-at-location
@nielserik
nielserik requested a balanced review from Copilot November 17, 2026 12:35

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

Unresolved hold-expiration defects can incorrectly extend holds, block renewals when no expiry period is configured, and turn expected validation or lookup failures into HTTP 500 errors.

5 open issues
What changed in this PR

This PR updates reading-room (“for use at location”) loans to cap hold-shelf expiration at the loan’s due day and recalculate it after renewal.

Changes:

  • Extracts shared expiration calculation and adds a due-day cap.
  • Recalculates expiration when renewing a held reading-room loan.
  • Adds regression coverage for capping and renewal readjustment.
File Description
src/​test/​java/​api/​loans/​LoansForUseAtLocationTests.java Adds a hold-expiration cap and renewal regression test.
src/​main/​java/​org/​folio/​circulation/​resources/​renewal/​RenewalResource.java Recalculates hold-shelf expiration before storing renewed loans.
src/​main/​java/​org/​folio/​circulation/​resources/​foruseatlocation/​HoldByBarcodeResource.java Extracts the shared expiration calculator and applies the due-day cap.
src/​main/​java/​org/​folio/​circulation/​domain/​Loan.java Adds held-status and expiration accessors plus an expiration updater.

Review effort: Balanced

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔵 Needs a closer look

Renewal recalculates shelf expiry from the current time instead of the original hold date, incorrectly extending the hold period even for holds that were never capped.

1 open issue
1 previously missed issue

In code that hasn't changed since last review

Medium severity Renewal incorrectly resets the hold’s shelf-expiry period

src/​main/​java/​org/​folio/​circulation/​resources/​renewal/​RenewalResource.java:252

Renewal restarts the shelf-expiry period because the shared calculator uses the current time rather than the original hold date, extending even holds that were never capped. Pass the persisted forUseAtLocation.statusDate as the calculation base on renewal, retaining the current time for newly placed holds. Advance the mocked clock before renewal in the regression test so it detects this drift.

4 resolved since last review

Review effort: Balanced

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

Inconsistent timestamps can restore expiration a day late when a calendar lookup crosses tenant midnight, while the test’s extra renewal leaves first-renewal restoration unverified.

2 open issues
1 previously missed issue

In code that hasn't changed since last review

Medium severity Reuse the hold timestamp for renewal expiration

src/​main/​java/​org/​folio/​circulation/​resources/​renewal/​RenewalResource.java:252

The initial expiration uses a timestamp captured before the calendar lookup, but Loan.changeStatusOfUsageAtLocation records a fresh statusDate afterward. If the lookup crosses tenant midnight, renewal uses the later date and can restore an expiration a day beyond the original standard expiration. Persist and reuse the same captured hold timestamp for both calculations, and add a regression test that advances the clock across midnight during the lookup.

Review effort: Balanced

@nielserik
nielserik requested review from a team and a balanced review from Copilot November 17, 2026 16:30

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔵 Needs a closer look

Human assessment is needed because a critical renewal error-handling path can turn calendar failures into unhandled exceptions, leaving requests without an HTTP response.

No open issues 🎉

2 resolved since last review

Review effort: Balanced

@roman-barannyk

roman-barannyk commented Nov 18, 2026 •

Copy link
Copy Markdown
Contributor

🔴 Important (Should Fix)

  • Expiry in the past: Expiry can land in the past. If the loan is already overdue when it goes on hold, the cap sets the expiry before the hold date. Either clamp it to max(cap, hold date), document that behavior, and add a test.
  • Uncapped due-date changes: Other due-date changes aren’t re-capped. ChangeDueDateResource and recall shortening can pull the due date earlier than the stored expiry. This may be out of scope, so check it against the ticket.
  • Renewal failure on service-point lookup: Renewal can now fail on a service-point lookup. The new getServicePointById(loan.getCheckoutServicePointId()) call in RenewalResource.java (~245-262) runs inside the renewal. If that service point is missing, a held use-at-location loan can’t be renewed. It should log and leave the stored expiry alone instead.

🟡 Minor

  • Null handling / Potential NPE: calculateExpirationDateForUseAtLocation returns completedFuture(null) when the expiry is null. No current caller reaches that branch, but it would throw an NPE if chained.
  • Redundant check: The !expiration.equals(currentExpiration) guard in RenewalResource is redundant.
  • Styling and async consistency: The new block in RenewalResource has odd indentation and spacing (context,servicePointRepository) and uses thenCompose where neighboring code uses thenComposeAsync.
  • Legacy data NPE risk: A held loan with no statusDate (legacy data) would NPE on renewal.
  • Test assertion weakness: willCapHoldShelfExpirationAtDueDateButReadjustAtRenewal never asserts which day the due date is, so it only discriminates if the due date isn’t the 31st.
  • Test gaps: Missing test cases for:
    • A hold that needs no cap
    • An overdue loan
    • Renewing a non-held loan
    • A policy with no hold-shelf expiry period

@sonarqubecloud

Copy link
Copy Markdown

@nielserik

Copy link
Copy Markdown
Contributor Author

🔴 Important (Should Fix)

* **Expiry in the past:** Expiry can land in the past. If the loan is already overdue when it goes on hold, the cap sets the expiry before the hold date. Either clamp it to `max(cap, hold date)`, document that behavior, and add a test.

In known real life reading room procedures, the expiry cannot land in the past. The reason is that overdue reading room loans are closed by the librarian (i.e. by the end of the day) after which the books are returned from the reading room shelf. Thus, an overdue loan cannot subsequently (i.e. next day) be picked up by the patron and therefore cannot be put on hold.

We can of course speculate that something went wrong and the overdue loan was not closed (or renewed) as it should be, and that the patron somehow managed to pick it up in the reading room even so, and subsequently also got it held. It has not been a requirement so far to technically prevent this scenario, so the librarian would need to resolve it somehow if it ever occurred. But it should not be resolved by pretending that the patron is supposed to be able to keep the item on hold into any date in the future. If anything, having the date in the past would be a better indication of a problem.

* **Uncapped due-date changes:** Other due-date changes aren’t re-capped. `ChangeDueDateResource` and recall shortening can pull the due date earlier than the stored expiry. This may be out of scope, so check it against the ticket.

We checked the ticket as preparation for the work. It only deals with renewals. We are aware of other due date change scenarios and might discuss with the sponsor (NLS)

* **Renewal failure on service-point lookup:** Renewal can now fail on a service-point lookup. The new `getServicePointById(loan.getCheckoutServicePointId())` call in `RenewalResource.java` (~245-262) runs inside the renewal. If that service point is missing, a held use-at-location loan can’t be renewed. It should log and leave the stored expiry alone instead.

Addressed.

🟡 Minor

* **Null handling / Potential NPE:** `calculateExpirationDateForUseAtLocation` returns `completedFuture(null)` when the expiry is null. No current caller reaches that branch, but it would throw an NPE if chained.

Addressed.

* **Redundant check:** The `!expiration.equals(currentExpiration)` guard in `RenewalResource` is redundant.

The redundant null check is remove. The equals check serves the purpose of not repeatedly updating a property with the value it already holds (best practice when possible I'd say).

* **Styling and async consistency:** The new block in `RenewalResource` has odd indentation and spacing (`context,servicePointRepository`) and uses `thenCompose` where neighboring code uses `thenComposeAsync`.

Addressed.

* **Legacy data NPE risk:** A held loan with no `statusDate` (legacy data) would NPE on renewal.

Addressed.

* **Test assertion weakness:** `willCapHoldShelfExpirationAtDueDateButReadjustAtRenewal` never asserts which day the due date is, so it only discriminates if the due date isn’t the 31st.

* **Test gaps:** Missing test cases for:
  
  * A hold that needs no cap
  * An overdue loan
  * Renewing a non-held loan
  * A policy with no hold-shelf expiry period

Tests and asserts added (though not for overdue loan ref. comments in top).

Also added an entry in NEWS, but see that the request for that was subsequently removed from the review. I remove the entry again?

@roman-barannyk

Copy link
Copy Markdown
Contributor

Thanks, @nielserik!

Regarding the NEWS entry, please keep it. I had removed that requirement thinking it wasn't necessary, but it's definitely better to have it in. Thanks for adding it!

@nielserik

Copy link
Copy Markdown
Contributor Author

Thanks @roman-barannyk

@nielserik
nielserik merged commit f3ac6ad into master Nov 18, 2026
16 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants