From 62f45d737d90cc6af27af08280da42ecb488a547 Mon Sep 17 00:00:00 2001 From: rikuke <33894149+rikuke@users.noreply.github.com> Date: Tue, 4 Jun 2024 09:33:22 +0300 Subject: [PATCH] fix: query handled applications for open case (#3051) --- .../management/commands/send_ahjo_requests.py | 9 ++++++++- backend/benefit/applications/models.py | 8 ++++++-- .../benefit/applications/tests/conftest.py | 10 ++++++++++ .../tests/test_ahjo_integration.py | 19 +++++++++++++++---- 4 files changed, 39 insertions(+), 7 deletions(-) diff --git a/backend/benefit/applications/management/commands/send_ahjo_requests.py b/backend/benefit/applications/management/commands/send_ahjo_requests.py index b9b482ee92..03603130c7 100644 --- a/backend/benefit/applications/management/commands/send_ahjo_requests.py +++ b/backend/benefit/applications/management/commands/send_ahjo_requests.py @@ -62,8 +62,13 @@ def get_applications_for_request( ) -> QuerySet[Application]: if request_type == AhjoRequestType.OPEN_CASE: applications = Application.objects.get_by_statuses( - [ApplicationStatus.HANDLING], + [ + ApplicationStatus.HANDLING, + ApplicationStatus.ACCEPTED, + ApplicationStatus.REJECTED, + ], AhjoStatusEnum.SUBMITTED_BUT_NOT_SENT_TO_AHJO, + True, ) elif request_type == AhjoRequestType.SEND_DECISION_PROPOSAL: applications = Application.objects.get_for_ahjo_decision() @@ -75,11 +80,13 @@ def get_applications_for_request( applications = Application.objects.get_by_statuses( [ApplicationStatus.ACCEPTED, ApplicationStatus.REJECTED], 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, + False, ) return applications diff --git a/backend/benefit/applications/models.py b/backend/benefit/applications/models.py index 01a6d5073b..f83ae0618f 100755 --- a/backend/benefit/applications/models.py +++ b/backend/benefit/applications/models.py @@ -167,11 +167,14 @@ def with_non_downloaded_attachments(self): return qs.prefetch_related(attachments_prefetch) def get_by_statuses( - self, application_statuses: List[ApplicationStatus], ahjo_status: AhjoStatusEnum + self, + application_statuses: List[ApplicationStatus], + ahjo_status: AhjoStatusEnum, + has_no_case_id: bool, ): """ Query applications by their latest AhjoStatus relation - and their current ApplicationStatus. + and their current ApplicationStatus and if they have a case id in Ahjo or not. """ # Subquery to get the latest AhjoStatus id for each application latest_ahjo_status_subquery = ( @@ -201,6 +204,7 @@ def get_by_statuses( status__in=application_statuses, ahjo_status__id=F("latest_ahjo_status_id"), ahjo_status__status=ahjo_status, + ahjo_case_id__isnull=has_no_case_id, ) .prefetch_related(attachments_prefetch, "calculation", "company") ) diff --git a/backend/benefit/applications/tests/conftest.py b/backend/benefit/applications/tests/conftest.py index c255e766c8..0c56fde628 100755 --- a/backend/benefit/applications/tests/conftest.py +++ b/backend/benefit/applications/tests/conftest.py @@ -102,6 +102,16 @@ def multiple_handling_applications(mock_get_organisation_roles_and_create_compan ) +@pytest.fixture +def multiple_decided_applications_for_open_case( + mock_get_organisation_roles_and_create_company, +): + with factory.Faker.override_default_locale("fi_FI"): + return DecidedApplicationFactory.create_batch( + 5, company=mock_get_organisation_roles_and_create_company + ) + + @pytest.fixture def application_batch(): with factory.Faker.override_default_locale("fi_FI"): diff --git a/backend/benefit/applications/tests/test_ahjo_integration.py b/backend/benefit/applications/tests/test_ahjo_integration.py index 6f460f3e92..87fb320038 100644 --- a/backend/benefit/applications/tests/test_ahjo_integration.py +++ b/backend/benefit/applications/tests/test_ahjo_integration.py @@ -714,10 +714,15 @@ def test_generate_ahjo_secret_decision_text_xml(decided_application): @pytest.mark.django_db def test_get_applications_for_open_case( - multiple_decided_applications, multiple_handling_applications + multiple_decided_applications, + multiple_decided_applications_for_open_case, + multiple_handling_applications, ): now = timezone.now() - for application in multiple_handling_applications: + applications_for_open_case = ( + multiple_decided_applications_for_open_case + multiple_handling_applications + ) + for application in applications_for_open_case: status = AhjoStatus.objects.create( application=application, status=AhjoStatusEnum.SUBMITTED_BUT_NOT_SENT_TO_AHJO, @@ -737,11 +742,17 @@ def test_get_applications_for_open_case( ahjo_status.save() applications_for_open_case = Application.objects.get_by_statuses( - [ApplicationStatus.HANDLING], AhjoStatusEnum.SUBMITTED_BUT_NOT_SENT_TO_AHJO + [ + ApplicationStatus.HANDLING, + ApplicationStatus.ACCEPTED, + ApplicationStatus.REJECTED, + ], + AhjoStatusEnum.SUBMITTED_BUT_NOT_SENT_TO_AHJO, + True, ) # only handled_applications should be returned as their last AhjoStatus is SUBMITTED_BUT_NOT_SENT_TO_AHJO # and their application status is HANDLING - assert applications_for_open_case.count() == len(multiple_handling_applications) + assert applications_for_open_case.count() == len(applications_for_open_case) @pytest.mark.django_db