From 8a8408a8202d1c8bc631aa1e2d35dbdcda3a825e Mon Sep 17 00:00:00 2001 From: rikuke <33894149+rikuke@users.noreply.github.com> Date: Tue, 10 Sep 2024 11:06:19 +0300 Subject: [PATCH] Hl 1455 (#3291) * feat: clear error_from_ahjo on success * feat: no update request if errors in decision req --- .../applications/api/v1/ahjo_integration_views.py | 5 +++++ .../applications/services/ahjo_integration.py | 10 ++++++++-- .../applications/tests/test_ahjo_integration.py | 15 ++++++++++++++- 3 files changed, 27 insertions(+), 3 deletions(-) diff --git a/backend/benefit/applications/api/v1/ahjo_integration_views.py b/backend/benefit/applications/api/v1/ahjo_integration_views.py index e9eca62348..694e1a0ab8 100644 --- a/backend/benefit/applications/api/v1/ahjo_integration_views.py +++ b/backend/benefit/applications/api/v1/ahjo_integration_views.py @@ -203,6 +203,11 @@ def handle_success_callback( ) -> Response: try: with transaction.atomic(): + # Clear any previous error messages before creating a new success status + latest_status = application.ahjo_status.latest() + if latest_status.error_from_ahjo is not None: + latest_status.error_from_ahjo = None + latest_status.save() info = self.cb_info_message(application, callback_data, request_type) if request_type == AhjoRequestType.OPEN_CASE: self._handle_open_case_success(application, callback_data) diff --git a/backend/benefit/applications/services/ahjo_integration.py b/backend/benefit/applications/services/ahjo_integration.py index 83f6684eb8..df51f39096 100644 --- a/backend/benefit/applications/services/ahjo_integration.py +++ b/backend/benefit/applications/services/ahjo_integration.py @@ -506,10 +506,16 @@ def update_application_summary_record_in_ahjo( application: Application, ahjo_token: AhjoToken ) -> Union[Tuple[Application, str], None]: """Update the application summary pdf in Ahjo. - Should be done just before the decision proposal is sent. + Should be done about the same time proposal is sent. """ - ahjo_request = AhjoUpdateRecordsRequest(application) + if application.ahjo_status.latest().error_from_ahjo: + # If there are errors from Ahjo, do not send the update request + raise ValueError( + f"Application {application.id} has errors \ +in Ahjo status {application.ahjo_status.latest().status}, not sending {ahjo_request}." + ) + ahjo_client = AhjoApiClient(ahjo_token, ahjo_request) pdf_summary = generate_application_attachment( diff --git a/backend/benefit/applications/tests/test_ahjo_integration.py b/backend/benefit/applications/tests/test_ahjo_integration.py index a859e30655..c1907bb0a1 100644 --- a/backend/benefit/applications/tests/test_ahjo_integration.py +++ b/backend/benefit/applications/tests/test_ahjo_integration.py @@ -432,22 +432,26 @@ def ahjo_callback_payload(): @pytest.mark.parametrize( - "request_type, ahjo_status", + "request_type, previous_ahjo_status, ahjo_status", [ ( AhjoRequestType.OPEN_CASE, + AhjoStatusEnum.REQUEST_TO_OPEN_CASE_SENT, AhjoStatusEnum.CASE_OPENED, ), ( AhjoRequestType.UPDATE_APPLICATION, + AhjoStatusEnum.UPDATE_REQUEST_SENT, AhjoStatusEnum.UPDATE_REQUEST_RECEIVED, ), ( AhjoRequestType.DELETE_APPLICATION, + AhjoStatusEnum.DELETE_REQUEST_SENT, AhjoStatusEnum.DELETE_REQUEST_RECEIVED, ), ( AhjoRequestType.SEND_DECISION_PROPOSAL, + AhjoStatusEnum.DECISION_PROPOSAL_SENT, AhjoStatusEnum.DECISION_PROPOSAL_ACCEPTED, ), ], @@ -459,6 +463,7 @@ def test_ahjo_callback_success( decided_application, settings, request_type, + previous_ahjo_status, ahjo_status, ahjo_callback_payload, ): @@ -473,6 +478,14 @@ def test_ahjo_callback_success( ahjo_callback_payload["message"] = AhjoCallBackStatus.SUCCESS ahjo_callback_payload["records"][0]["hashValue"] = attachment_hash_value + status = AhjoStatus.objects.create( + application=decided_application, + status=previous_ahjo_status, + ) + # make sure the previous status is created earlier than the new status + status.created_at = timezone.now() - timedelta(days=5) + status.save() + if request_type in [ AhjoRequestType.SEND_DECISION_PROPOSAL, AhjoRequestType.DELETE_APPLICATION,