Skip to content

Commit

Permalink
circulation: fix item availability
Browse files Browse the repository at this point in the history
  • Loading branch information
KonstantinaStoikou committed Nov 27, 2020
1 parent 38f5a6b commit a5c2d21
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 9 deletions.
11 changes: 10 additions & 1 deletion invenio_app_ils/documents/jsonresolvers/document_circulation.py
Expand Up @@ -14,6 +14,7 @@
from invenio_app_ils.circulation.search import (get_loan_next_available_date,
get_loans_aggregated_by_states,
get_overdue_loans_by_doc_pid)
from invenio_app_ils.ill.search import BorrowingRequestsSearch
from invenio_app_ils.items.search import get_items_aggregated_by_statuses

lt_es7 = ES_VERSION[0] < 7
Expand Down Expand Up @@ -82,8 +83,16 @@ def circulation_resolver(document_pid):
if bucket["key"] in "FOR_REFERENCE_ONLY":
items_for_reference_count = bucket["doc_count"]

active_ill_loans = (
BorrowingRequestsSearch()
.get_active_ill_loans_for_document(document_pid)
.count()
)

active_ils_loans_count = active_loans_count - active_ill_loans

available_items_for_loan_count = (
items_count - active_loans_count - unavailable_items_count
items_count - active_ils_loans_count - unavailable_items_count
)

circulation = {
Expand Down
31 changes: 23 additions & 8 deletions invenio_app_ils/ill/search.py
Expand Up @@ -7,6 +7,8 @@

"""ILL search APIs."""

from elasticsearch_dsl import Q
from invenio_circulation.search.api import search_by_pid
from invenio_search.api import RecordsSearch

from invenio_app_ils.errors import MissingRequiredParameterError
Expand Down Expand Up @@ -36,10 +38,7 @@ def search_by_document_pid(self, document_pid=None):
search = self

if document_pid:
search = search.filter(
"term",
document_pid=document_pid
)
search = search.filter("term", document_pid=document_pid)
else:
raise MissingRequiredParameterError(
description="document_pid is required"
Expand All @@ -51,10 +50,7 @@ def search_by_patron_pid(self, patron_pid=None):
search = self

if patron_pid:
search = search.filter(
"term",
patron_pid=patron_pid
)
search = search.filter("term", patron_pid=patron_pid)
else:
raise MissingRequiredParameterError(
description="patron_pid is required"
Expand All @@ -73,3 +69,22 @@ def search_by_library_pid(self, library_pid=None):
)

return search

def get_active_ill_loans_for_document(self, document_pid=None):
"""Retrieve ILL loans with the given document_pid."""
search = self

if document_pid:
search = search.query(
"bool",
filter=[
Q("term", status="ON_LOAN"),
Q("term", document_pid=document_pid),
],
)

else:
raise MissingRequiredParameterError(
description="document_pid is required"
)
return search

0 comments on commit a5c2d21

Please sign in to comment.