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

circulation: fix transaction end_date. #1532

Merged
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
12 changes: 12 additions & 0 deletions rero_ils/modules/items/api/circulation.py
Original file line number Diff line number Diff line change
Expand Up @@ -407,6 +407,18 @@ def checkout(self, current_loan, **kwargs):
dbcommit=True,
reindex=True
)

# If 'end_date' is specified, we need to check if the selected date is
# not a closed date. If it's a closed date, then we need to update the
# value to the next open day.
if 'end_date' in action_params:
library = Library.get_record_by_pid(self.library_pid)
if not library.is_open(action_params['end_date'], True):
new_end_date = library.next_open(action_params['end_date'])
new_end_date = new_end_date.astimezone()\
.replace(microsecond=0).isoformat()
action_params['end_date'] = new_end_date

loan = current_circulation.circulation.trigger(
current_loan,
**dict(action_params, trigger='checkout')
Expand Down
45 changes: 43 additions & 2 deletions tests/api/circulation/test_actions_views_checkout.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,9 @@
# along with this program. If not, see <http://www.gnu.org/licenses/>.

"""Tests REST checkout API methods in the item api_views."""
from datetime import datetime, timedelta


import ciso8601
from invenio_accounts.testutils import login_user_via_session
from utils import postdata

Expand Down Expand Up @@ -103,7 +104,7 @@ def test_checkout(
)
assert res.status_code == 200

# test WITH loan PID
# test WITH loan PID & WITH SPECIFIED END-DATE
item, patron_pid, loan = item_on_shelf_martigny_patron_and_loan_pending
assert item.status == ItemStatus.ON_SHELF
params['item_pid'] = item.pid
Expand All @@ -114,3 +115,43 @@ def test_checkout(
params
)
assert res.status_code == 200

# TEST CHECKOUT WITH SPECIFIED END-DATE
# 0) do a check-in for the item
# 1) Ensure than Saturday is a closed day for the loc_public_martigny
# 2) Try a checkout
# 3) Ensure than checkout response return a transaction end_date == next
# business open day
res, _ = postdata(
client,
'api_item.checkin',
dict(
item_pid=item.pid,
transaction_library_pid=lib_martigny.pid,
transaction_user_pid=librarian_martigny_no_email.pid
)
)
assert res.status_code == 200

now = datetime.now()
delta = timedelta((12 - now.weekday()) % 7)
next_saturday = now + delta
assert not lib_martigny.is_open(next_saturday, True)

params = dict(
item_pid=item.pid,
patron_pid=patron_martigny_no_email.pid,
transaction_user_pid=librarian_martigny_no_email.pid,
transaction_location_pid=loc_public_martigny.pid,
end_date=next_saturday.isoformat()
)
res, data = postdata(
client,
'api_item.checkout',
params
)
assert res.status_code == 200
transaction_end_date = data['action_applied']['checkout']['end_date']
transaction_end_date = ciso8601.parse_datetime(transaction_end_date)
next_open_date = lib_martigny.next_open(next_saturday)
assert next_open_date.date() == transaction_end_date.date()