1212
1313from flask import current_app
1414from invenio_accounts .models import LoginInformation , SessionActivity , User , userrole
15+ from invenio_circulation .pidstore .pids import CIRCULATION_LOAN_PID_TYPE
1516from invenio_circulation .proxies import current_circulation
1617from invenio_db import db
1718from invenio_oauthclient .models import RemoteAccount , RemoteToken , UserIdentity
1819from invenio_search .engine import search as inv_search
1920from invenio_userprofiles .models import UserProfile
2021
22+ from invenio_app_ils .acquisition .api import ORDER_PID_TYPE
2123from invenio_app_ils .acquisition .proxies import current_ils_acq
2224from invenio_app_ils .circulation .search import (
2325 get_active_loans_by_patron_pid ,
2426 get_loans_by_patron_pid ,
2527)
28+ from invenio_app_ils .document_requests .api import DOCUMENT_REQUEST_PID_TYPE
2629from invenio_app_ils .errors import AnonymizationActiveLoansError , PatronNotFoundError
30+ from invenio_app_ils .ill .api import BORROWING_REQUEST_PID_TYPE
2731from invenio_app_ils .ill .proxies import current_ils_ill
2832from invenio_app_ils .notifications .models import NotificationsLogs
2933from invenio_app_ils .patrons .api import get_patron_or_unknown_dump
@@ -106,9 +110,32 @@ def anonymize_patron_data(patron_pid, force=False):
106110 cls = current_app .config ["ILS_PATRON_ANONYMOUS_CLASS" ]
107111 anonymous_patron_fields = cls ().dumps_loader ()
108112
113+ Loan = current_circulation .loan_record_cls
114+ BorrowingRequest = current_ils_ill .borrowing_request_record_cls
115+ DocumentRequest = current_app_ils .document_request_record_cls
116+ Order = current_ils_acq .order_record_cls
117+
118+ anonymized_records = {
119+ CIRCULATION_LOAN_PID_TYPE : {
120+ "indexer" : current_circulation .loan_indexer (),
121+ "records" : [],
122+ },
123+ BORROWING_REQUEST_PID_TYPE : {
124+ "indexer" : current_ils_ill .borrowing_request_indexer_cls (),
125+ "records" : [],
126+ },
127+ DOCUMENT_REQUEST_PID_TYPE : {
128+ "indexer" : current_app_ils .document_request_indexer ,
129+ "records" : [],
130+ },
131+ ORDER_PID_TYPE : {
132+ "indexer" : current_ils_acq .order_indexer ,
133+ "records" : [],
134+ },
135+ }
136+
109137 patron_loans = get_loans_by_patron_pid (patron_pid ).scan ()
110138
111- Loan = current_circulation .loan_record_cls
112139 indices = 0
113140 for hit in patron_loans :
114141 loan = Loan .get_record_by_pid (hit .pid )
@@ -131,30 +158,27 @@ def anonymize_patron_data(patron_pid, force=False):
131158 loan ["patron_pid" ] = anonymous_patron_fields ["pid" ]
132159 loan ["patron" ] = anonymous_patron_fields
133160 loan .commit ()
134- current_circulation . loan_indexer (). index (loan )
161+ anonymized_records [ CIRCULATION_LOAN_PID_TYPE ][ "records" ]. append (loan )
135162 indices += 1
136163
137164 BorrowingRequestsSearch = current_ils_ill .borrowing_request_search_cls
138165 patron_borrowing_requests = (
139166 BorrowingRequestsSearch ().search_by_patron_pid (patron_pid ).scan ()
140167 )
141168
142- BorrowingRequest = current_ils_ill .borrowing_request_record_cls
143- indexer = current_ils_ill .borrowing_request_indexer_cls ()
144169 for hit in patron_borrowing_requests :
145170 borrowing_request = BorrowingRequest .get_record_by_pid (hit .pid )
146171 borrowing_request ["patron" ] = anonymous_patron_fields
147172 borrowing_request ["patron_pid" ] = anonymous_patron_fields ["pid" ]
148173 borrowing_request .commit ()
149- indexer . index (borrowing_request )
174+ anonymized_records [ BORROWING_REQUEST_PID_TYPE ][ "records" ]. append (borrowing_request )
150175 indices += 1
151176
152177 DocumentRequestSearch = current_app_ils .document_request_search_cls
153178 patron_document_requests = (
154179 DocumentRequestSearch ().search_by_patron_pid (patron_pid ).scan ()
155180 )
156181
157- DocumentRequest = current_app_ils .document_request_record_cls
158182 for hit in patron_document_requests :
159183 document_request = DocumentRequest .get_record_by_pid (hit .pid )
160184 if document_request ["state" ] == "PENDING" :
@@ -163,19 +187,18 @@ def anonymize_patron_data(patron_pid, force=False):
163187 document_request ["patron" ] = anonymous_patron_fields
164188 document_request ["patron_pid" ] = anonymous_patron_fields ["pid" ]
165189 document_request .commit ()
166- current_app_ils . document_request_indexer . index (document_request )
190+ anonymized_records [ ORDER_PID_TYPE ][ "records" ]. append (document_request )
167191 indices += 1
168192
169193 patron_acquisitions = OrderSearch ().search_by_patron_pid (patron_pid ).scan ()
170194
171- Order = current_ils_acq .order_record_cls
172195 for hit in patron_acquisitions :
173196 acquisition = Order .get_record_by_pid (hit .pid )
174197 for line in acquisition ["order_lines" ]:
175198 if line .get ("patron_pid" ) == patron_pid :
176199 line ["patron_pid" ] = anonymous_patron_fields ["pid" ]
177200 acquisition .commit ()
178- current_ils_acq . order_indexer . index (acquisition )
201+ anonymized_records [ Order . _pid_type ][ "records" ]. append (acquisition )
179202 indices += 1
180203
181204 # delete rows from db
@@ -185,6 +208,13 @@ def anonymize_patron_data(patron_pid, force=False):
185208 notifications = anonymize_patron_in_notification_logs (patron_pid )
186209
187210 db .session .commit ()
211+
212+ # index all after committing to DB, to ensure that no errors occurred.
213+ for value in anonymized_records .values ():
214+ indexer , records = value ["indexer" ], value ["records" ]
215+ for record in records :
216+ indexer .index (record )
217+
188218 if patron :
189219 try :
190220 patron_indexer = current_app_ils .patron_indexer
0 commit comments