Skip to content

Commit

Permalink
feat: handle update record callback (#2854)
Browse files Browse the repository at this point in the history
  • Loading branch information
rikuke authored Mar 4, 2024
1 parent 2241549 commit 03eaae2
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 8 deletions.
21 changes: 19 additions & 2 deletions backend/benefit/applications/api/v1/ahjo_integration_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,16 @@ def handle_success_callback(
elif request_type == AhjoRequestType.SEND_DECISION_PROPOSAL:
ahjo_status = AhjoStatusEnum.DECISION_PROPOSAL_ACCEPTED
info = "Decision proposal was sent to Ahjo"
elif request_type == AhjoRequestType.UPDATE_APPLICATION:
self._handle_update_records_success(application, callback_data)
ahjo_status = AhjoStatusEnum.UPDATE_REQUEST_RECEIVED
info = f"Updated application records were sent to Ahjo with request id: {callback_data['requestId']}"
else:
raise AhjoCallbackError(
f"Unknown request type {request_type} in the Ahjo callback"
)
AhjoStatus.objects.create(application=application, status=ahjo_status)

audit_logging.log(
request.user,
"",
Expand All @@ -174,10 +183,18 @@ def handle_failure_callback(
status=status.HTTP_400_BAD_REQUEST,
)

def _handle_update_records_success(
self, application: Application, callback_data: dict
):
cb_records = callback_data.get("records", [])
self._save_version_series_id(application, cb_records)

def _handle_open_case_success(self, application: Application, callback_data: dict):
"""Update the application with the case id (diaarinumero) and case guid from the Ahjo callback data."""
application.ahjo_case_guid = callback_data["caseGuid"]
application.ahjo_case_id = callback_data["caseId"]
if callback_data["caseGuid"]:
application.ahjo_case_guid = callback_data["caseGuid"]
if callback_data["caseId"]:
application.ahjo_case_id = callback_data["caseId"]
cb_records = callback_data.get("records", [])

self._save_version_series_id(application, cb_records)
Expand Down
26 changes: 20 additions & 6 deletions backend/benefit/applications/tests/test_ahjo_integration.py
Original file line number Diff line number Diff line change
Expand Up @@ -372,6 +372,13 @@ def test_get_attachment_unauthorized_ip_not_allowed(
assert response.status_code == 403


def _get_callback_url(request_type: AhjoRequestType, decided_application: Application):
return reverse(
"ahjo_callback_url",
kwargs={"request_type": request_type, "uuid": decided_application.id},
)


@pytest.fixture
def ahjo_callback_payload():
return {
Expand All @@ -397,6 +404,10 @@ def ahjo_callback_payload():
AhjoRequestType.OPEN_CASE,
AhjoStatusEnum.CASE_OPENED,
),
(
AhjoRequestType.UPDATE_APPLICATION,
AhjoStatusEnum.UPDATE_REQUEST_RECEIVED,
),
(
AhjoRequestType.DELETE_APPLICATION,
AhjoStatusEnum.DELETE_REQUEST_RECEIVED,
Expand Down Expand Up @@ -424,18 +435,14 @@ def test_ahjo_callback_success(
ahjo_callback_payload["message"] = AhjoCallBackStatus.SUCCESS
ahjo_callback_payload["records"][0]["hashValue"] = attachment_hash_value

url = reverse(
"ahjo_callback_url",
kwargs={"request_type": request_type, "uuid": decided_application.id},
)
url = _get_callback_url(request_type, decided_application)
response = ahjo_client.post(url, **auth_headers, data=ahjo_callback_payload)

decided_application.refresh_from_db()
attachment.refresh_from_db()
assert response.status_code == 200
assert response.data == {"message": "Callback received"}
if request_type == AhjoRequestType.OPEN_CASE:
attachment.refresh_from_db()

assert decided_application.ahjo_case_id == ahjo_callback_payload["caseId"]
assert (
str(decided_application.ahjo_case_guid) == ahjo_callback_payload["caseGuid"]
Expand All @@ -444,6 +451,11 @@ def test_ahjo_callback_success(
attachment.ahjo_version_series_id
== ahjo_callback_payload["records"][0]["versionSeriesId"]
)
if request_type == AhjoRequestType.UPDATE_APPLICATION:
assert (
attachment.ahjo_version_series_id
== ahjo_callback_payload["records"][0]["versionSeriesId"]
)
assert decided_application.ahjo_status.latest().status == ahjo_status


Expand Down Expand Up @@ -480,6 +492,7 @@ def test_ahjo_open_case_callback_failure(
"request_type",
[
(AhjoRequestType.OPEN_CASE,),
(AhjoRequestType.UPDATE_APPLICATION,),
(AhjoRequestType.DELETE_APPLICATION,),
],
)
Expand All @@ -505,6 +518,7 @@ def test_ahjo_callback_unauthorized_wrong_or_missing_credentials(
"request_type",
[
(AhjoRequestType.OPEN_CASE,),
(AhjoRequestType.UPDATE_APPLICATION,),
(AhjoRequestType.DELETE_APPLICATION,),
],
)
Expand Down

0 comments on commit 03eaae2

Please sign in to comment.