Skip to content

Commit

Permalink
fix: query handled applications for open case (#3051)
Browse files Browse the repository at this point in the history
  • Loading branch information
rikuke authored Jun 4, 2024
1 parent 2893b93 commit 62f45d7
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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
Expand Down
8 changes: 6 additions & 2 deletions backend/benefit/applications/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 = (
Expand Down Expand Up @@ -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")
)
Expand Down
10 changes: 10 additions & 0 deletions backend/benefit/applications/tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"):
Expand Down
19 changes: 15 additions & 4 deletions backend/benefit/applications/tests/test_ahjo_integration.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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
Expand Down

0 comments on commit 62f45d7

Please sign in to comment.