Skip to content

Commit 358fff9

Browse files
JakobMiesnerkpsherva
authored andcommitted
circulation: self-checkout of MISSING items marks them as CAN_CIRCULATE
1 parent 7769b2f commit 358fff9

File tree

3 files changed

+51
-9
lines changed

3 files changed

+51
-9
lines changed

invenio_app_ils/circulation/api.py

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# -*- coding: utf-8 -*-
22
#
3-
# Copyright (C) 2018-2020 CERN.
3+
# Copyright (C) 2018-2025 CERN.
44
#
55
# invenio-app-ils is free software; you can redistribute it and/or modify it
66
# under the terms of the MIT License; see LICENSE file for more details.
@@ -78,9 +78,9 @@ def _validate_delivery(delivery):
7878
)
7979

8080

81-
def _set_item_to_can_circulate(item_pid):
81+
def _set_item_to_can_circulate(item_pid_value):
8282
"""Change the item status to CAN_CIRCULATE."""
83-
item = Item.get_record_by_pid(item_pid["value"])
83+
item = Item.get_record_by_pid(item_pid_value)
8484
if item["status"] != "CAN_CIRCULATE":
8585
item["status"] = "CAN_CIRCULATE"
8686
item.commit()
@@ -245,7 +245,7 @@ def checkout_loan(
245245
_validate_delivery(optional_delivery)
246246

247247
if force:
248-
_set_item_to_can_circulate(item_pid)
248+
_set_item_to_can_circulate(item_pid["value"])
249249

250250
return _checkout_loan(
251251
item_pid,
@@ -256,14 +256,19 @@ def checkout_loan(
256256
)
257257

258258

259-
def _ensure_item_loanable_via_self_checkout(item_pid):
259+
def _ensure_item_loanable_via_self_checkout(item_pid_value):
260260
"""Self-checkout: return loanable item or raise when not loanable.
261261
262262
Implements the self-checkout rules to loan an item.
263263
"""
264-
item = current_app_ils.item_record_cls.get_record_by_pid(item_pid)
264+
item = current_app_ils.item_record_cls.get_record_by_pid(item_pid_value)
265265
item_dict = item.replace_refs()
266266

267+
if item_dict["status"] == "MISSING":
268+
_set_item_to_can_circulate(item_pid_value)
269+
item = current_app_ils.item_record_cls.get_record_by_pid(item_pid_value)
270+
item_dict = item.replace_refs()
271+
267272
if item_dict["status"] != "CAN_CIRCULATE":
268273
raise ItemCannotCirculateError()
269274

tests/api/circulation/test_loan_checkout.py

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# -*- coding: utf-8 -*-
22
#
3-
# Copyright (C) 2018-2020 CERN.
3+
# Copyright (C) 2018-2025 CERN.
44
#
55
# invenio-app-ils is free software; you can redistribute it and/or modify it
66
# under the terms of the MIT License; see LICENSE file for more details.
@@ -249,15 +249,29 @@ def test_self_checkout_search(app, client, json_headers, users, testdata):
249249
assert res.status_code == 400
250250

251251
# test that an error is returned when the item cannot circulate
252-
missing_item_barcode = "123456789-1"
252+
in_binding_item_barcode = "123456789-74"
253253
url = url_for("invenio_app_ils_circulation.loan_self_checkout")
254-
res = client.get(f"{url}?barcode={missing_item_barcode}", headers=json_headers)
254+
res = client.get(f"{url}?barcode={in_binding_item_barcode}", headers=json_headers)
255255
assert res.status_code == 400
256256
# assert that the payload will contain the key error with a msg
257257
response = res.get_json()
258258
assert LoanSelfCheckoutItemInvalidStatus.description in response["message"]
259259
assert LoanSelfCheckoutItemInvalidStatus.supportCode in response["supportCode"]
260260

261+
# test that no error is returned when the item is marked as missing
262+
missing_item_barcode = "123456789-1"
263+
missing_item_pid = "itemid-1"
264+
url = url_for("invenio_app_ils_circulation.loan_self_checkout")
265+
res = client.get(f"{url}?barcode={missing_item_barcode}", headers=json_headers)
266+
assert res.status_code == 200
267+
268+
# assert item is no longer marked as missing
269+
response = res.get_json()
270+
item_pid = response["metadata"]["pid"]
271+
assert item_pid == missing_item_pid
272+
item = Item.get_record_by_pid(item_pid)
273+
assert item["status"] == "CAN_CIRCULATE"
274+
261275
# create a loan on the same patron, and another one on another patron
262276
user_login(client, "librarian", users)
263277
url = url_for("invenio_app_ils_circulation.loan_checkout")
@@ -326,6 +340,7 @@ def _create_request(patron, document_pid):
326340
current_search.flush_and_refresh(index="*")
327341

328342
def _self_checkout(patron, item_pid, document_pid):
343+
url = url_for("invenio_app_ils_circulation.loan_self_checkout")
329344
params = deepcopy(NEW_LOAN)
330345
params["document_pid"] = document_pid
331346
params["item_pid"] = dict(type="pitmid", value=item_pid)
@@ -394,3 +409,14 @@ def _self_checkout(patron, item_pid, document_pid):
394409
assert res.status_code == 202
395410
response = res.get_json()
396411
assert response["metadata"]["delivery"]["method"] == "SELF-CHECKOUT"
412+
413+
# test self-checkout with item marked as missing raises no error and marks the item as CAN_CIRCULATE
414+
missing_item_pid = "itemid-1"
415+
app.config["ILS_SELF_CHECKOUT_ENABLED"] = True
416+
patron2 = user_login(client, "patron2", users)
417+
res = _self_checkout(patron2, missing_item_pid, "docid-1")
418+
assert res.status_code == 202
419+
response = res.get_json()
420+
assert response["metadata"]["delivery"]["method"] == "SELF-CHECKOUT"
421+
item = Item.get_record_by_pid(missing_item_pid)
422+
assert item["status"] == "CAN_CIRCULATE"

tests/data/items.json

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -289,5 +289,16 @@
289289
"medium": "NOT_SPECIFIED",
290290
"status": "CAN_CIRCULATE",
291291
"document": {}
292+
},
293+
{
294+
"pid": "itemid-74",
295+
"created_by": { "type": "script", "value": "demo" },
296+
"barcode": "123456789-74",
297+
"document_pid": "docid-17",
298+
"internal_location_pid": "ilocid-1",
299+
"circulation_restriction": "NO_RESTRICTION",
300+
"medium": "NOT_SPECIFIED",
301+
"status": "IN_BINDING",
302+
"document": {}
292303
}
293304
]

0 commit comments

Comments
 (0)