diff --git a/backend/benefit/applications/management/commands/send_ahjo_requests.py b/backend/benefit/applications/management/commands/send_ahjo_requests.py index a6046cee2c..eb9a70eda9 100644 --- a/backend/benefit/applications/management/commands/send_ahjo_requests.py +++ b/backend/benefit/applications/management/commands/send_ahjo_requests.py @@ -70,7 +70,7 @@ def get_applications_for_request( ApplicationStatus.ACCEPTED, ApplicationStatus.REJECTED, ], - AhjoStatusEnum.SUBMITTED_BUT_NOT_SENT_TO_AHJO, + [AhjoStatusEnum.SUBMITTED_BUT_NOT_SENT_TO_AHJO], True, ) elif request_type == AhjoRequestType.SEND_DECISION_PROPOSAL: @@ -82,13 +82,13 @@ def get_applications_for_request( elif request_type == AhjoRequestType.UPDATE_APPLICATION: applications = Application.objects.get_by_statuses( [ApplicationStatus.ACCEPTED, ApplicationStatus.REJECTED], - AhjoStatusEnum.DECISION_PROPOSAL_ACCEPTED, + [AhjoStatusEnum.DECISION_PROPOSAL_ACCEPTED], False, ) elif request_type == AhjoRequestType.GET_DECISION_DETAILS: applications = Application.objects.get_by_statuses( [ApplicationStatus.ACCEPTED, ApplicationStatus.REJECTED], - AhjoStatusEnum.SIGNED_IN_AHJO, + [AhjoStatusEnum.SIGNED_IN_AHJO], False, ) elif request_type == AhjoRequestType.DELETE_APPLICATION: diff --git a/backend/benefit/applications/models.py b/backend/benefit/applications/models.py index 55e9e38b2f..3d311e3848 100755 --- a/backend/benefit/applications/models.py +++ b/backend/benefit/applications/models.py @@ -171,12 +171,17 @@ def with_non_downloaded_attachments(self): def get_by_statuses( self, application_statuses: List[ApplicationStatus], - ahjo_status: AhjoStatusEnum, + ahjo_statuses: List[AhjoStatusEnum], has_no_case_id: bool, ): """ Query applications by their latest AhjoStatus relation and their current ApplicationStatus and if they have a case id in Ahjo or not. + The latest ahjo status can be one of several as for example in the case of update + records request, where the previous status can be either: + AhjoStatusEnum.DECISION_PROPOSAL_ACCEPTED, + or + AhjoStatusEnum.NEW_RECORDS_RECEIVED """ # Subquery to get the latest AhjoStatus id for each application latest_ahjo_status_subquery = ( @@ -204,7 +209,7 @@ def get_by_statuses( .filter( status__in=application_statuses, ahjo_status__id=F("latest_ahjo_status_id"), - ahjo_status__status=ahjo_status, + ahjo_status__status__in=ahjo_statuses, ahjo_case_id__isnull=has_no_case_id, ) .prefetch_related(attachments_prefetch, "calculation", "company") diff --git a/backend/benefit/applications/services/ahjo_client.py b/backend/benefit/applications/services/ahjo_client.py index 6993ebc04e..e59cfc8de8 100644 --- a/backend/benefit/applications/services/ahjo_client.py +++ b/backend/benefit/applications/services/ahjo_client.py @@ -79,6 +79,7 @@ class AhjoAddRecordsRequest(AhjoRequest): request_type = AhjoRequestType.ADD_RECORDS request_method = "POST" + result_status = AhjoStatusEnum.NEW_RECORDS_REQUEST_SENT @dataclass diff --git a/backend/benefit/applications/services/ahjo_integration.py b/backend/benefit/applications/services/ahjo_integration.py index df51f39096..bc073789c5 100644 --- a/backend/benefit/applications/services/ahjo_integration.py +++ b/backend/benefit/applications/services/ahjo_integration.py @@ -545,9 +545,11 @@ def send_new_attachment_records_to_ahjo( data = prepare_attachment_records_payload(attachments, application) - application, response_text = ahjo_client.send_request_to_ahjo(data) + result, response_text = ahjo_client.send_request_to_ahjo(data) - return application, response_text + if result: + create_status_for_application(application, ahjo_request.result_status) + return result, response_text def send_decision_proposal_to_ahjo( diff --git a/backend/benefit/applications/tests/conftest.py b/backend/benefit/applications/tests/conftest.py index f02665d904..ba4b48ee3d 100755 --- a/backend/benefit/applications/tests/conftest.py +++ b/backend/benefit/applications/tests/conftest.py @@ -519,7 +519,7 @@ def multiple_applications_with_ahjo_case_id( ): with factory.Faker.override_default_locale("fi_FI"): applications = DecidedApplicationFactory.create_batch( - 5, company=mock_get_organisation_roles_and_create_company + 10, company=mock_get_organisation_roles_and_create_company ) for a in applications: diff --git a/backend/benefit/applications/tests/test_ahjo_integration.py b/backend/benefit/applications/tests/test_ahjo_integration.py index 45f256e32e..3d870b44d9 100644 --- a/backend/benefit/applications/tests/test_ahjo_integration.py +++ b/backend/benefit/applications/tests/test_ahjo_integration.py @@ -772,18 +772,26 @@ def test_generate_ahjo_secret_decision_text_xml(decided_application): def test_get_applications_for_ahjo_update( multiple_applications_with_ahjo_case_id, ): - for a in multiple_applications_with_ahjo_case_id: + for a in multiple_applications_with_ahjo_case_id[:5]: AhjoStatus.objects.create( application=a, status=AhjoStatusEnum.DECISION_PROPOSAL_ACCEPTED, ) + for a in multiple_applications_with_ahjo_case_id[5:]: + AhjoStatus.objects.create( + application=a, + status=AhjoStatusEnum.NEW_RECORDS_RECEIVED, + ) applications_for_ahjo_update = Application.objects.get_by_statuses( [ ApplicationStatus.ACCEPTED, ApplicationStatus.REJECTED, ], - AhjoStatusEnum.DECISION_PROPOSAL_ACCEPTED, + [ + AhjoStatusEnum.DECISION_PROPOSAL_ACCEPTED, + AhjoStatusEnum.NEW_RECORDS_RECEIVED, + ], False, ) @@ -844,7 +852,7 @@ def test_get_applications_for_open_case( ApplicationStatus.ACCEPTED, ApplicationStatus.REJECTED, ], - AhjoStatusEnum.SUBMITTED_BUT_NOT_SENT_TO_AHJO, + [AhjoStatusEnum.SUBMITTED_BUT_NOT_SENT_TO_AHJO], True, )