Skip to content

Commit

Permalink
feat: HL 1440 & HL 1441 set archived status & talpa_status for applic…
Browse files Browse the repository at this point in the history
…ations (#3236)

* fix: archived status after talpa / ahjo

* fix: archived and talpa_status after csv download
  • Loading branch information
rikuke authored Sep 10, 2024
1 parent 8a8408a commit e1d80ac
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ def post(self, request, *args, **kwargs):
return Response(
{"error": "Application not found"}, status=status.HTTP_404_NOT_FOUND
)

# TODO how to check the success of the callback if it has no message property?
if request_type == AhjoRequestType.SEND_DECISION_PROPOSAL:
return self.handle_success_callback(
request, application, callback_data, request_type
Expand Down Expand Up @@ -299,6 +299,7 @@ def _handle_open_case_success(self, application: Application, callback_data: dic
def _handle_delete_callback_success(self, application):
# do anything that needs to be done when Ahjo sends a delete callback
application.status = ApplicationStatus.CANCELLED
application.archived = True
application.save()

if application.batch and application.batch.auto_generated_by_ahjo:
Expand Down
24 changes: 22 additions & 2 deletions backend/benefit/applications/api/v1/application_batch_views.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import logging

from django.db import transaction
from django.forms import ValidationError
from django.http import HttpResponse
Expand All @@ -17,7 +19,11 @@
ApplicationBatchListSerializer,
ApplicationBatchSerializer,
)
from applications.enums import ApplicationBatchStatus, ApplicationStatus
from applications.enums import (
ApplicationBatchStatus,
ApplicationStatus,
ApplicationTalpaStatus,
)
from applications.exceptions import (
BatchCompletionDecisionDateError,
BatchCompletionRequiredFieldsError,
Expand All @@ -30,6 +36,8 @@
from common.permissions import BFIsHandler
from shared.audit_log.viewsets import AuditLoggingModelViewSet

LOGGER = logging.getLogger(__name__)


class ApplicationBatchFilter(filters.FilterSet):
status = filters.MultipleChoiceFilter(
Expand Down Expand Up @@ -213,7 +221,19 @@ def talpa_export_batch(self, request, *args, **kwargs) -> HttpResponse:
# for easier testing in the test environment do not update the batches as sent_to_talpa
# remove this when TALPA integration is ready for production
if not skip_update:
approved_batches.all().update(status=ApplicationBatchStatus.SENT_TO_TALPA)
try:
# Update all approved batches to SENT_TO_TALPA status in a single query
approved_batches.update(status=ApplicationBatchStatus.SENT_TO_TALPA)
# Update all applications in the approved batches to SUCCESSFULLY_SENT_TO_TALPA status and archived=True
for a in applications:
a.talpa_status = ApplicationTalpaStatus.SUCCESSFULLY_SENT_TO_TALPA
a.archived = True
a.save()

except Exception as e:
LOGGER.error(
f"An error occurred while updating batches after Talpa csv download: {e}"
)
return response

@action(methods=["PATCH"], detail=False)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,8 @@ def _handle_successful_applications(
successful_applications = self._get_applications(application_numbers)
if successful_applications:
successful_applications.update(
talpa_status=ApplicationTalpaStatus.SUCCESSFULLY_SENT_TO_TALPA
talpa_status=ApplicationTalpaStatus.SUCCESSFULLY_SENT_TO_TALPA,
archived=True,
)

for application in successful_applications:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -532,6 +532,7 @@ def test_ahjo_callback_success(

assert decided_application.status == ApplicationStatus.CANCELLED
assert decided_application.batch.status == ApplicationBatchStatus.CANCELLED
assert decided_application.archived is True

assert decided_application.ahjo_status.latest().status == ahjo_status

Expand Down
21 changes: 19 additions & 2 deletions backend/benefit/applications/tests/test_application_batch_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,12 @@
from rest_framework.reverse import reverse

from applications.api.v1.serializers.application import ApplicationBatchSerializer
from applications.enums import AhjoDecision, ApplicationBatchStatus, ApplicationStatus
from applications.enums import (
AhjoDecision,
ApplicationBatchStatus,
ApplicationStatus,
ApplicationTalpaStatus,
)
from applications.exceptions import BatchTooManyDraftsError
from applications.models import Application, ApplicationBatch
from applications.tests.conftest import * # noqa
Expand Down Expand Up @@ -844,14 +849,26 @@ def test_application_batches_talpa_export(anonymous_client, application_batch):
app_batch_2.status = ApplicationBatchStatus.DECIDED_ACCEPTED
fill_as_valid_batch_completion_and_save(app_batch_2)

url = reverse("v1:applicationbatch-talpa-export-batch")

# Export accepted batches then change it status
response = anonymous_client.get(reverse("v1:applicationbatch-talpa-export-batch"))
response = anonymous_client.get(f"{url}?skip_update=0")

application_batch.refresh_from_db()
app_batch_2.refresh_from_db()
assert application_batch.status == ApplicationBatchStatus.SENT_TO_TALPA
assert app_batch_2.status == ApplicationBatchStatus.SENT_TO_TALPA

applications = Application.objects.filter(
batch__in=[application_batch, app_batch_2]
)
for application in applications:
assert (
application.talpa_status
== ApplicationTalpaStatus.SUCCESSFULLY_SENT_TO_TALPA
)
assert application.archived is True

assert isinstance(response, HttpResponse)
assert response.headers["Content-Type"] == "text/csv"
assert response.status_code == 200
2 changes: 2 additions & 0 deletions backend/benefit/applications/tests/test_talpa_integration.py
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,8 @@ def test_talpa_callback_success(talpa_client, decided_application):
== ApplicationTalpaStatus.SUCCESSFULLY_SENT_TO_TALPA
)

assert decided_application.archived is True


@pytest.mark.django_db
def test_talpa_callback_rejected_application(
Expand Down

0 comments on commit e1d80ac

Please sign in to comment.