Skip to content
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
8 changes: 4 additions & 4 deletions api/odl.py
Original file line number Diff line number Diff line change
Expand Up @@ -694,10 +694,10 @@ def _recalculate_holds_in_license_pool(self, licensepool: LicensePool) -> None:
for hold in ready_for_checkout:
if hold.position != 0:
hold.position = 0
# And start the reservation period.
hold.end = utc_now() + datetime.timedelta(
days=default_reservation_period
)
# And start the reservation period which ends end of day.
hold.end = (
utc_now() + datetime.timedelta(days=default_reservation_period)
).replace(hour=23, minute=59, second=59, microsecond=999999)

# Update the rest of the queue.
for idx, hold in enumerate(waiting):
Expand Down
2 changes: 1 addition & 1 deletion core/scripts.py
Original file line number Diff line number Diff line change
Expand Up @@ -2745,7 +2745,7 @@ class LoanNotificationsScript(Script):
are expiring"""

# Days before on which to send out a notification
LOAN_EXPIRATION_DAYS = [5, 1]
LOAN_EXPIRATION_DAYS = [3, 1]
BATCH_SIZE = 100

def do_run(self):
Expand Down
9 changes: 5 additions & 4 deletions core/util/notifications.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,8 +116,8 @@ def send_loan_expiry_message(
edition = loan.license_pool.presentation_edition
identifier = loan.license_pool.identifier
library_short_name = loan.library.short_name
title = f"Only {days_to_expiry} {'days' if days_to_expiry != 1 else 'day'} left on your loan!"
body = f"Your loan on {edition.title} is expiring soon"
title = f'"{edition.title}" laina-aika on päättymässä!'
body = f'"{edition.title}" laina-aika päättyy {days_to_expiry} päivän päästä.'
data = dict(
title=title,
body=body,
Expand Down Expand Up @@ -196,8 +196,9 @@ def send_holds_notifications(cls, holds: list[Hold]) -> list[str]:
loans_api = f"{url}/{hold.patron.library.short_name}/loans"
work: Work = hold.work
identifier: Identifier = hold.license_pool.identifier
title = "Your hold is available!"
body = f'Your hold on "{work.title}" is available!'
title = f'Varauksesi "{work.title}" on lainattavissa!'
body = f"Varaus on lainattava viimeistään {hold.end.strftime('%-d.%-m.%Y') if hold.end else '3 päivän päästä'}."
print(body)
data = dict(
title=title,
body=body,
Expand Down
22 changes: 10 additions & 12 deletions tests/api/test_odl.py
Original file line number Diff line number Diff line change
Expand Up @@ -1137,9 +1137,10 @@ def test_update_licensepool_and_hold_queue(

# And the first hold changed.
assert 0 == hold.position
assert hold.end - utc_now() - datetime.timedelta(days=3) < datetime.timedelta(
hours=1
)
# assert hold.end - utc_now() - datetime.timedelta(days=3) < datetime.timedelta(
# hours=1
# )
assert hold.end.hour == 23 and hold.end.minute == 59

# The later hold also moved one position.
assert 1 == later_hold.position
Expand All @@ -1158,9 +1159,10 @@ def test_update_licensepool_and_hold_queue(
assert 2 == opds2_with_odl_api_fixture.pool.licenses_reserved
assert 2 == opds2_with_odl_api_fixture.pool.patrons_in_hold_queue
assert 0 == later_hold.position
assert later_hold.end - utc_now() - datetime.timedelta(
days=3
) < datetime.timedelta(hours=1)
# assert later_hold.end - utc_now() - datetime.timedelta(
# days=3
# ) < datetime.timedelta(hours=1)
assert later_hold.end.hour == 23 and later_hold.end.minute == 59

# Now there are no more holds. If we add another license,
# it ends up being available.
Expand Down Expand Up @@ -1217,12 +1219,8 @@ def test_update_licensepool_and_hold_queue(
assert 0 == holds[0].position
assert 0 == holds[1].position
assert 1 == holds[2].position
assert holds[0].end - utc_now() - datetime.timedelta(
days=3
) < datetime.timedelta(hours=1)
assert holds[1].end - utc_now() - datetime.timedelta(
days=3
) < datetime.timedelta(hours=1)
assert holds[0].end.hour == 23 and holds[0].end.minute == 59
assert holds[1].end.hour == 23 and holds[1].end.minute == 59

# If there are more licenses that change than holds, some of them become available.
with opds2_with_odl_api_fixture.mock_http.patch():
Expand Down
21 changes: 12 additions & 9 deletions tests/core/util/test_notifications.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import logging
import re
from collections.abc import Generator
from datetime import datetime
from datetime import datetime, timedelta
from typing import Any
from unittest import mock
from unittest.mock import MagicMock
Expand All @@ -11,6 +11,7 @@
import pytz
from firebase_admin.exceptions import FirebaseError
from firebase_admin.messaging import UnregisteredError
from freezegun import freeze_time
from google.auth import credentials
from requests_mock import Mocker

Expand Down Expand Up @@ -105,12 +106,12 @@ def test_send_loan_notification(self, push_notf_fixture: PushNotificationsFixtur
{
"token": "atoken",
"notification": messaging.Notification(
title="Only 1 day left on your loan!",
body=f"Your loan on {work.presentation_edition.title} is expiring soon",
title=f'"{work.presentation_edition.title}" laina-aika on päättymässä!',
body=f'"{work.presentation_edition.title}" laina-aika päättyy 1 päivän päästä.',
),
"data": dict(
title="Only 1 day left on your loan!",
body=f"Your loan on {work.presentation_edition.title} is expiring soon",
title=f'"{work.presentation_edition.title}" laina-aika on päättymässä!',
body=f'"{work.presentation_edition.title}" laina-aika päättyy 1 päivän päästä.',
event_type=NotificationConstants.LOAN_EXPIRY_TYPE,
loans_endpoint="http://localhost/default/loans",
external_identifier=patron.external_identifier,
Expand Down Expand Up @@ -254,6 +255,7 @@ def test_send_activity_sync(self, push_notf_fixture: PushNotificationsFixture):

assert messaging.send.call_count == 4

@freeze_time()
def test_holds_notification(self, push_notf_fixture: PushNotificationsFixture):
db = push_notf_fixture.db
# Only patron1 will get an identifier
Expand All @@ -276,8 +278,9 @@ def test_holds_notification(self, push_notf_fixture: PushNotificationsFixture):
assert p1 is not None
p2 = work2.active_license_pool()
assert p2 is not None
hold1, _ = p1.on_hold_to(patron1, position=0)
hold2, _ = p2.on_hold_to(patron2, position=0)
in_three_days = utc_now() + timedelta(days=3)
hold1, _ = p1.on_hold_to(patron1, position=0, end=in_three_days)
hold2, _ = p2.on_hold_to(patron2, position=0, end=in_three_days)

with mock.patch("core.util.notifications.messaging") as mock_messaging:
# Mock the notification method to return the kwargs passed to it
Expand All @@ -298,8 +301,8 @@ def assert_message_call(
include_auth_id: bool = True,
) -> None:
data = dict(
title="Your hold is available!",
body=f'Your hold on "{work.title}" is available!',
title=f'Varauksesi "{work.title}" on lainattavissa!',
body=f'Varaus on lainattava viimeistään {in_three_days.strftime("%-d.%-m.%Y")}.',
event_type=NotificationConstants.HOLD_AVAILABLE_TYPE,
loans_endpoint=loans_api,
identifier=hold.license_pool.identifier.identifier,
Expand Down
Loading