Skip to content

Commit

Permalink
notification: fix loan notification candidates
Browse files Browse the repository at this point in the history
When we check about notification candidates on a loan, we need to
exclude the current loan from the current request list on the related
item.

Co-Authored-by: Renaud Michotte <[email protected]>
  • Loading branch information
zannkukai committed Sep 28, 2021
1 parent 3b595d2 commit f9fc093
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 6 deletions.
14 changes: 9 additions & 5 deletions rero_ils/modules/items/api/circulation.py
Original file line number Diff line number Diff line change
Expand Up @@ -1164,11 +1164,12 @@ def get_number_of_loans(self):
LoanState.ITEM_RETURNED,
]).source().count()

def get_requests(self, sort_by=None, count=False):
def get_requests(self, sort_by=None, count=False, pids=False):
"""Return sorted pending, item_on_transit, item_at_desk loans.
:param sort_by: the sort to appy. default sort is _created.
:param sort_by: the sort to result. default sort is _created.
:param count: if True, return the number of request.
:param pids: if True, return the only the pids.
:return a generator of corresponding request or a request counter.
"""

Expand All @@ -1177,8 +1178,6 @@ def _list_obj():
sort_term = sort_by or '_created'
if sort_term.startswith('-'):
(sort_term, order_by) = (sort_term[1:], 'desc')
print("sort_term", sort_term)
print("order_by", order_by)
es_query = query\
.params(preserve_order=True)\
.sort({sort_term: {'order': order_by}})
Expand All @@ -1191,7 +1190,12 @@ def _list_obj():
LoanState.ITEM_AT_DESK,
LoanState.ITEM_IN_TRANSIT_FOR_PICKUP
]).source(['pid'])
return query.count() if count else _list_obj()
if pids:
return [hit.pid for hit in query.scan()]
elif count:
return query.count()
else:
return _list_obj()

def get_first_loan_by_state(self, state=None):
"""Return the first loan with the given state and attached to item.
Expand Down
6 changes: 5 additions & 1 deletion rero_ils/modules/loans/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -761,7 +761,11 @@ def get_notification_candidates(self, trigger):

candidates = []
item = Item.get_record_by_pid(self.item_pid)
has_request = item.number_of_requests() > 0
# Get the number of requests on the related item and exclude myself
# from the result list.
requests = item.get_requests(pids=True)
requests = [loan_pid for loan_pid in requests if loan_pid != self.pid]
has_request = len(requests) > 0

# AVAILABILITY NOTIFICATION
# If loan (items) just arrived at the library desk we can create
Expand Down

0 comments on commit f9fc093

Please sign in to comment.