Skip to content

Commit eca2e34

Browse files
authored
30309 - Activity Log - Changes to support payment method from and to changes (bcgov#3495)
1 parent 4a2d99c commit eca2e34

File tree

3 files changed

+49
-25
lines changed

3 files changed

+49
-25
lines changed

auth-api/src/auth_api/services/activity_log.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,10 @@ def _twofactor_reset(activity: ActivityLogModel) -> str:
148148
def _payment_info_change(activity: ActivityLogModel) -> str:
149149
"""User X updated the account payment information to [payment method]."""
150150
payment_information = activity.item_value.replace("_", " ")
151+
if "|" in payment_information:
152+
from_payment_method, to_payment_method = payment_information.split("|")
153+
if from_payment_method and to_payment_method:
154+
return f"Updated the account payment information from {from_payment_method} to {to_payment_method}"
151155
return f"Updated the account payment information to {payment_information}"
152156

153157
@staticmethod

auth-api/src/auth_api/services/org.py

Lines changed: 44 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,39 @@ def create_membership(access_type, org, user_id, **kwargs):
214214
# Add the user to account_holders group
215215
KeycloakService.join_account_holders_group()
216216

217+
@staticmethod
218+
def _get_payment_method_descriptions(current_payment_method: str, new_payment_method: str) -> str:
219+
"""Get payment method descriptions for activity logging."""
220+
valid_payment_methods = [item.value for item in PaymentMethod]
221+
new_method_description = (
222+
PaymentMethod(new_payment_method).name if new_payment_method in valid_payment_methods else ""
223+
)
224+
if not current_payment_method:
225+
return new_method_description
226+
current_method_description = (
227+
PaymentMethod(current_payment_method).name if current_payment_method in valid_payment_methods else ""
228+
)
229+
return f"{current_method_description}|{new_method_description}"
230+
231+
@staticmethod
232+
def _handle_pay_http_error_raise_business_exception(http_error: HTTPError) -> None:
233+
"""Handle HTTP error by extracting error info and raising BusinessException."""
234+
error_payload = http_error.response.json()
235+
error_code = next(
236+
(error_payload[key] for key in ["error", "code"] if key in error_payload),
237+
Error.PAYMENT_ACCOUNT_UPSERT_FAILED,
238+
)
239+
error_details = next(
240+
(
241+
error_payload[key]
242+
for key in ["error_description", "message", "description", "type"]
243+
if key in error_payload
244+
),
245+
"",
246+
)
247+
current_app.logger.error(f"Account create payment Error: {http_error}")
248+
raise BusinessException(error_code, error_details) from http_error
249+
217250
@staticmethod
218251
@user_context
219252
def _create_payment_settings(
@@ -229,19 +262,23 @@ def _create_payment_settings(
229262
pay_url = current_app.config.get("PAY_API_URL")
230263
pay_request = Org._build_payment_request(org_model, payment_info, payment_method, mailing_address, **kwargs)
231264
error_code = None
265+
current_payment_method = None
266+
token = RestService.get_service_account_token()
232267

233268
if is_new_org:
234269
response = RestService.post(
235270
endpoint=f"{pay_url}/accounts",
236271
data=pay_request,
237-
token=RestService.get_service_account_token(),
272+
token=token,
238273
raise_for_status=True,
239274
)
240275
else:
276+
response = RestService.get(endpoint=f"{pay_url}/accounts/{org_model.id}", token=token)
277+
current_payment_method = response.json().get("paymentMethod")
241278
response = RestService.put(
242279
endpoint=f"{pay_url}/accounts/{org_model.id}",
243280
data=pay_request,
244-
token=RestService.get_service_account_token(),
281+
token=token,
245282
raise_for_status=True,
246283
)
247284

@@ -252,42 +289,25 @@ def _create_payment_settings(
252289
payment_account_status = PaymentAccountStatus.PENDING
253290
case _:
254291
payment_account_status = PaymentAccountStatus.FAILED
255-
error_payload = getattr(response, "json", lambda: {})()
256-
error_code = error_payload.get("error", "UNKNOWN_ERROR")
292+
error_code = getattr(response, "json", lambda: {})().get("error", "UNKNOWN_ERROR")
257293
current_app.logger.error(f"Account create payment Error: {response.text}")
258294

259295
if payment_account_status != PaymentAccountStatus.FAILED and payment_method:
260-
payment_method_description = (
261-
PaymentMethod(payment_method).name
262-
if payment_method in [item.value for item in PaymentMethod]
263-
else ""
296+
payment_method_descriptions = Org._get_payment_method_descriptions(
297+
current_payment_method, payment_method
264298
)
265299
ActivityLogPublisher.publish_activity(
266300
Activity(
267301
org_model.id,
268302
ActivityAction.PAYMENT_INFO_CHANGE.value,
269303
name=org_model.name,
270-
value=payment_method_description,
304+
value=payment_method_descriptions,
271305
)
272306
)
273307
return payment_account_status, error_code
274308

275309
except HTTPError as http_error:
276-
error_payload = http_error.response.json()
277-
error_code = next(
278-
(error_payload[key] for key in ["error", "code"] if key in error_payload),
279-
Error.PAYMENT_ACCOUNT_UPSERT_FAILED,
280-
)
281-
error_details = next(
282-
(
283-
error_payload[key]
284-
for key in ["error_description", "message", "description", "type"]
285-
if key in error_payload
286-
),
287-
"",
288-
)
289-
current_app.logger.error(f"Account create payment Error: {http_error}")
290-
raise BusinessException(error_code, error_details) from http_error
310+
return Org._handle_pay_http_error_raise_business_exception(http_error)
291311

292312
@staticmethod
293313
def _build_payment_request(org_model: OrgModel, payment_info: dict, payment_method: str, mailing_address, **kwargs):

auth-api/tests/unit/services/test_org.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -212,7 +212,7 @@ def test_update_basic_org_assert_pay_request_activity(session, keycloak_mock, mo
212212
org_id=ANY,
213213
name=ANY,
214214
id=ANY,
215-
value=PaymentMethod.ONLINE_BANKING.value,
215+
value=f"{PaymentMethod.EJV.value}|{PaymentMethod.ONLINE_BANKING.value}",
216216
)
217217
)
218218

0 commit comments

Comments
 (0)