Skip to content

Commit

Permalink
patron_transaction_events: add item informations
Browse files Browse the repository at this point in the history
* Adds document and item pid to patron_transaction_events ES.
* Adds patron barcode to patron_transactions and
  patron_transaction_events.
* Adds item filter to patron_transaction_events.

Co-Authored-by: Peter Weber <peter.weber@rero.ch>
  • Loading branch information
rerowep committed Apr 10, 2024
1 parent 20f7c0d commit ca0d8c9
Show file tree
Hide file tree
Showing 8 changed files with 73 additions and 12 deletions.
2 changes: 1 addition & 1 deletion rero_ils/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -2278,6 +2278,7 @@ def _(x):
)
),
filters={
'item': and_term_filter('item.pid'),
'patron_type': and_term_filter('patron_type.pid'),
'transaction_library': and_term_filter('library.pid'),
'transaction_date': range_filter(
Expand All @@ -2286,7 +2287,6 @@ def _(x):
start_date_math='/d',
end_date_math='/d'
),

},
post_filters={
'owning_library': {
Expand Down
8 changes: 6 additions & 2 deletions rero_ils/modules/patron_transaction_events/listener.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,15 +42,19 @@ def enrich_patron_transaction_event_data(sender, json=None, record=None,
# Add information about the patron related to this event
if patron := parent.patron:
json['patron'] = {'pid': patron.pid, 'type': 'ptrn'}
if barcode := patron.patron.get('barcode'):
json['patron']['barcode'] = barcode[0]
if ptty_pid := patron.patron_type_pid:
json['patron_type'] = {'pid': ptty_pid, 'type': 'ptty'}

# Add information about the owning library related to the parent loan
# (if exists) :: useful for faceting filter
if (loan := parent.loan) and (item := loan.item):
json['owning_library'] = {'pid': item.library_pid, 'type': 'lib'}
json['owning_location'] = {'pid': item.location_pid, 'type': 'loc'}

json['item'] = {'pid': parent.item_pid, 'type': 'item'}
if barcode := item.get('barcode'):
json['item']['barcode'] = barcode
# Add additional information
json['organisation'] = {'pid': parent.organisation_pid, 'type': 'org'}
json['category'] = parent['type']
json['document'] = {'pid': parent.document_pid, 'type': 'doc'}
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,9 @@
"pid": {
"type": "keyword"
},
"barcode": {
"type": "keyword"
},
"type": {
"type": "keyword"
}
Expand Down Expand Up @@ -117,6 +120,37 @@
}
}
},
"document": {
"properties": {
"pid": {
"type": "keyword"
},
"type": {
"type": "keyword"
}
}
},
"item": {
"properties": {
"pid": {
"type": "keyword"
},
"barcode": {
"type": "keyword"
},
"call_numbers": {
"type": "text",
"fields": {
"raw": {
"type": "keyword"
}
}
},
"type": {
"type": "keyword"
}
}
},
"_created": {
"type": "date"
},
Expand Down
6 changes: 6 additions & 0 deletions rero_ils/modules/patron_transactions/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,12 @@ def document_pid(self):
if loan := self.loan:
return loan.document_pid

@property
def item_pid(self):
"""Get the `Item` pid related to this transaction."""
if loan := self.loan:
return loan.item_pid

@property
def library_pid(self):
"""Get the `Library` pid related to this transaction."""
Expand Down
4 changes: 4 additions & 0 deletions rero_ils/modules/patron_transactions/listener.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,10 @@ def enrich_patron_transaction_data(sender, json=None, record=None, index=None,

if not isinstance(record, PatronTransaction):
record = PatronTransaction.get_record_by_pid(record.get('pid'))

if barcode := record.patron.patron.get('barcode'):
json['patron']['barcode'] = barcode[0]

if loan := record.loan:
json['document'] = {'pid': record.document_pid, 'type': 'doc'}
json['library'] = {'pid': record.library_pid, 'type': 'lib'}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@
},
"pid": {
"type": "keyword"
},
"barcode": {
"type": "keyword"
}
}
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
from utils import VerifyRecordPermissionPatch, get_json, postdata, \
to_relative_url

from rero_ils.modules.notifications.models import NotificationType
from rero_ils.modules.patron_transaction_events.api import \
PatronTransactionEvent

Expand Down Expand Up @@ -74,12 +75,20 @@ def test_patron_transaction_events_get(
data = get_json(res)
result = data['hits']['hits'][0]['metadata']
# delete dynamically added keys (listener)
del result['organisation']
del result['patron']
del result['category']
del result['owning_library']
del result['owning_location']
del result['patron_type']
assert result.pop('organisation') == {'pid': 'org1', 'type': 'org'}
assert result.pop('patron') == {
'barcode': '4098124352',
'pid': 'ptrn6',
'type': 'ptrn'
}
assert result.pop('category') == NotificationType.OVERDUE
assert result.pop('owning_library') == {'pid': 'lib1', 'type': 'lib'}
assert result.pop('owning_location') == {'pid': 'loc1', 'type': 'loc'}
assert result.pop('patron_type') == {'pid': 'ptty1', 'type': 'ptty'}
assert result.pop('document') == {'pid': 'doc1', 'type': 'doc'}
item_data = result.pop('item')
assert item_data.pop('barcode').startswith('f-')
assert item_data == {'pid': 'item8', 'type': 'item'}

assert result == patron_event.replace_refs()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,9 +79,10 @@ def test_patron_transactions_get(
assert res.status_code == 200
data = get_json(res)
result = data['hits']['hits'][0]['metadata']
del result['document']
del result['library']
del result['item']
assert result.pop('document') == {'pid': 'doc1', 'type': 'doc'}
assert result.pop('library') == {'pid': 'lib1', 'type': 'lib'}
assert result.pop('item') == {'pid': 'item8', 'type': 'item'}
del result['patron']['barcode']
assert result == transaction.replace_refs()

# Check for `rero+json` mime type response
Expand Down

0 comments on commit ca0d8c9

Please sign in to comment.