Skip to content

Commit ff22fe4

Browse files
authored
28703 ppr search cc payment (#2164)
* PPR API search cc payment changes. Signed-off-by: Doug Lovett <doug@diamante.ca> * MHR API search by org name bug fix. Signed-off-by: Doug Lovett <doug@diamante.ca> --------- Signed-off-by: Doug Lovett <doug@diamante.ca>
1 parent 3ce8240 commit ff22fe4

11 files changed

Lines changed: 213 additions & 89 deletions

File tree

mhr-api/src/database/postgres_views/mhr_search_owner_bus_vw.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
AND og.registration_id = p.registration_id
3636
AND p.owner_group_id = og.id
3737
AND p.party_type IN ('EXECUTOR', 'TRUSTEE', 'ADMINISTRATOR', 'TRUST', 'OWNER_BUS')
38+
AND p.business_name IS NOT NULL
3839
AND r.mhr_number = rl.mhr_number
3940
AND r.mhr_number = rd.mhr_number
4041
AND rl.id = l.registration_id

mhr-api/src/database/postgres_views/mhr_search_owner_ind_vw.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
AND og.registration_id = p.registration_id
3838
AND p.party_type IN ('EXECUTOR', 'TRUSTEE', 'ADMINISTRATOR', 'TRUST', 'OWNER_IND')
3939
AND p.owner_group_id = og.id
40+
AND p.last_name IS NOT NULL
4041
AND r.mhr_number = rl.mhr_number
4142
AND r.mhr_number = rd.mhr_number
4243
AND rl.id = l.registration_id

ppr-api/src/ppr_api/models/search_request.py

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
# Async search report status pending.
3636
REPORT_STATUS_PENDING = "PENDING"
3737
CHARACTER_SET_UNSUPPORTED = "The search name {} charcter set is not supported.\n"
38+
PAY_PENDING: int = 1000
3839

3940

4041
class SearchRequest(db.Model): # pylint: disable=too-many-instance-attributes
@@ -92,15 +93,17 @@ def json(self) -> dict:
9293
"maxResultsSize": search_utils.SEARCH_RESULTS_MAX_SIZE,
9394
"searchQuery": self.search_criteria,
9495
}
95-
if self.updated_selection:
96+
if self.search_result and self.search_result.is_payment_pending():
97+
result["paymentPending"] = True
98+
result["results"] = []
99+
elif self.updated_selection:
96100
result["results"] = self.updated_selection
97101
elif self.search_response:
98102
result["results"] = self.search_response
99103

100104
if self.pay_invoice_id and self.pay_path:
101105
payment = {"invoiceId": str(self.pay_invoice_id), "receipt": self.pay_path}
102106
result["payment"] = payment
103-
104107
return result
105108

106109
def save(self):
@@ -418,7 +421,11 @@ def find_all_by_account_id(
418421
# if api_result is null then the selections have not been finished
419422
search["inProgress"] = not row[11] and row[11] != [] and search["totalResultsSize"] > 0
420423
search["userId"] = str(row[12])
421-
if not search.get("inProgress") and (row[8] or model_utils.report_retry_elapsed(search_ts)):
424+
if row[13] and int(row[13]) == PAY_PENDING:
425+
search["paymentPending"] = True
426+
search["invoiceId"] = str(row[14]) if row[14] else ""
427+
search["reportAvailable"] = False
428+
elif not search.get("inProgress") and (row[8] or model_utils.report_retry_elapsed(search_ts)):
422429
search["reportAvailable"] = True
423430
else:
424431
search["reportAvailable"] = False

ppr-api/src/ppr_api/models/search_result.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
SCORE_LARGE_REPORT: int = 999
3535
LARGE_REPORT_REG_COUNT: int = 400
3636
LARGE_REPORT_JSON_SIZE: int = 1024000
37+
SCORE_PAY_PENDING: int = 1000
3738

3839

3940
class SearchResult(db.Model): # pylint: disable=too-many-instance-attributes
@@ -76,6 +77,10 @@ def json(self) -> dict:
7677
result["selected"] = self.search_select
7778
return result
7879

80+
def is_payment_pending(self):
81+
"""Evaluate if search results are waiting on payment completion."""
82+
return self.score and self.score == SCORE_PAY_PENDING
83+
7984
def save(self):
8085
"""Render a search results detail information to the local cache."""
8186
try:

ppr-api/src/ppr_api/models/search_utils.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -313,7 +313,7 @@
313313
ELSE '' END
314314
FROM users u WHERE u.username = sc.user_id FETCH FIRST 1 ROWS ONLY)
315315
END) AS username,
316-
sr.api_result, sc.user_id
316+
sr.api_result, sc.user_id, sr.score, sc.pay_invoice_id
317317
FROM search_requests sc, search_results sr
318318
WHERE sc.id = sr.search_id
319319
AND sc.account_id = '?'
@@ -347,7 +347,7 @@
347347
ELSE '' END
348348
FROM users u WHERE u.username = sc.user_id FETCH FIRST 1 ROWS ONLY)
349349
END) AS username,
350-
sr.api_result, sc.user_id
350+
sr.api_result, sc.user_id, sr.score, sc.pay_invoice_id
351351
FROM search_requests sc, search_results sr
352352
WHERE sc.id = sr.search_id
353353
AND sc.account_id = '?'
@@ -379,7 +379,7 @@
379379
ELSE '' END
380380
FROM users u WHERE u.username = sc.user_id FETCH FIRST 1 ROWS ONLY)
381381
END) AS username,
382-
sr.api_result, sc.user_id
382+
sr.api_result, sc.user_id, sr.score, sc.pay_invoice_id
383383
FROM search_requests sc, search_results sr
384384
WHERE sc.id = sr.search_id
385385
AND sc.account_id = '?'
@@ -412,7 +412,7 @@
412412
ELSE '' END
413413
FROM users u WHERE u.username = sc.user_id FETCH FIRST 1 ROWS ONLY)
414414
END) AS username,
415-
sr.api_result, sc.user_id
415+
sr.api_result, sc.user_id, sr.score, sc.pay_invoice_id
416416
FROM search_requests sc, search_results sr
417417
WHERE sc.id = sr.search_id
418418
AND sc.account_id = '?'

ppr-api/src/ppr_api/resources/cc_payment_utils.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@
5959
"11S": "11S: staff search credit card payment set up complete for invoice id={invoice_id}.",
6060
"13": "13: credit card payment update error no search found for invoice id={invoice_id}.",
6161
"14": "14: credit card payment update error search no pending payment for invoice id={invoice_id}.",
62+
"15": "15: search selection allowed for invoice id={invoice_id}.",
6263
}
6364

6465

ppr-api/src/ppr_api/resources/v1/callbacks.py

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
from ppr_api.callback.document_storage.storage_service import DocumentTypes, GoogleStorageService
2222
from ppr_api.callback.utils.exceptions import ReportDataException, ReportException, StorageException
2323
from ppr_api.exceptions import DatabaseException
24-
from ppr_api.models import Draft, FinancingStatement, MailReport, Registration
24+
from ppr_api.models import Draft, EventTracking, FinancingStatement, MailReport, Registration, SearchResult
2525
from ppr_api.models import utils as model_utils
2626
from ppr_api.reports import get_callback_pdf
2727
from ppr_api.reports.v2.report_utils import ReportTypes
@@ -39,6 +39,7 @@
3939
PAY_CANCELLED_MSG = "Payment status cancelled/deleted: draft reverted."
4040
PAY_NO_REG_MSG = "Change no financing statement found for base reg num={reg_num}."
4141
PAY_REG_SUCCESS_MSG = "Registration created fs id={fs_id} reg_id={reg_id} reg num={reg_num}."
42+
PAY_SEARCH_SUCCESS_MSG = "Search selection allowed for search id={search_id}."
4243
ALLOWED_PAY_STATUS = [
4344
StatusCodes.CANCELLED.value,
4445
StatusCodes.COMPLETED.value,
@@ -151,6 +152,11 @@ def post_payment_callback(invoice_id: str): # pylint: disable=too-many-return-s
151152
elif pay_status not in ALLOWED_PAY_STATUS:
152153
error_msg: str = PAY_STATUS_INVALID_MSG.format(pay_status=pay_status)
153154
return pay_callback_error("02", invoice_id, HTTPStatus.BAD_REQUEST, error_msg)
155+
156+
search_id: int = get_search_id(invoice_id)
157+
if search_id and search_id > 0:
158+
return pay_callback_search(search_id, invoice_id)
159+
154160
draft: Draft = Draft.find_by_invoice_id(invoice_id)
155161
if not draft:
156162
return pay_callback_error("03", invoice_id, HTTPStatus.NOT_FOUND)
@@ -171,6 +177,40 @@ def post_payment_callback(invoice_id: str): # pylint: disable=too-many-return-s
171177
return pay_callback_error("00", invoice_id, HTTPStatus.INTERNAL_SERVER_ERROR, str(default_err))
172178

173179

180+
def get_search_id(invoice_id: str):
181+
"""Try to get the search id if the payment is for a search request."""
182+
events = EventTracking.find_by_key_id(int(invoice_id))
183+
if not events:
184+
return None
185+
for event in events:
186+
if event.event_tracking_type == EventTracking.EventTrackingTypes.PPR_PAYMENT.value and event.message:
187+
message: str = str(event.message)
188+
if message.startswith("11"):
189+
tokens = message.split("*")
190+
if tokens and len(tokens) > 1:
191+
search_id = int(tokens[1])
192+
logger.info(f"get_search_id found id={search_id}")
193+
return search_id
194+
return None
195+
196+
197+
def pay_callback_search(search_id: int, invoice_id: str):
198+
"""Handle a payment complete notification for a search request."""
199+
search_result: SearchResult = SearchResult.find_by_search_id(search_id)
200+
if not search_result:
201+
return pay_callback_error("13", invoice_id, HTTPStatus.NOT_FOUND)
202+
if not search_result.is_payment_pending():
203+
return pay_callback_error("14", invoice_id, HTTPStatus.BAD_REQUEST, f"Search id={search_id}")
204+
logger.info(f"Request valid for invoice id={invoice_id} search id={search_id}, marking search as completed.")
205+
cc_payment_utils.track_event("09", invoice_id, HTTPStatus.OK, f"Search id={search_id}")
206+
search_result.score = None
207+
search_result.save()
208+
logger.info(f"Search pending payment status removed for {search_id}.")
209+
msg = PAY_SEARCH_SUCCESS_MSG.format(search_id=search_id)
210+
cc_payment_utils.track_event("15", invoice_id, HTTPStatus.OK, msg)
211+
return {}, HTTPStatus.OK
212+
213+
174214
def pay_callback_error(code: str, key_id: int, status_code, message: str = None):
175215
"""Return the payment event listener callback error response based on the code."""
176216
error: str = cc_payment_utils.track_event(code, key_id, status_code, message)

0 commit comments

Comments
 (0)