Skip to content

Commit

Permalink
fix: HL 1465 improve Ahjo callback error logging (#3325)
Browse files Browse the repository at this point in the history
* fix: request error logging when no failureDetails

* feat: store request id in ahjoStatus on failure
  • Loading branch information
rikuke authored Sep 18, 2024
1 parent c0dc2e7 commit cadaa1b
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 9 deletions.
15 changes: 9 additions & 6 deletions backend/benefit/applications/api/v1/ahjo_integration_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
AhjoStatus as AhjoStatusEnum,
ApplicationBatchStatus,
ApplicationStatus,
DEFAULT_AHJO_CALLBACK_ERROR_MESSAGE,
)
from applications.models import AhjoStatus, Application, ApplicationBatch, Attachment
from common.permissions import BFIsHandler, SafeListPermission
Expand Down Expand Up @@ -169,11 +170,6 @@ 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
)

if callback_data["message"] == AhjoCallBackStatus.SUCCESS:
return self.handle_success_callback(
Expand Down Expand Up @@ -258,7 +254,14 @@ def handle_failure_callback(
self, application: Application, callback_data: dict
) -> Response:
latest_status = application.ahjo_status.latest()
latest_status.error_from_ahjo = callback_data.get("failureDetails", None)

latest_status.ahjo_request_id = callback_data.get(
"requestId", "no request id received"
)

latest_status.error_from_ahjo = callback_data.get(
"failureDetails", DEFAULT_AHJO_CALLBACK_ERROR_MESSAGE
)
latest_status.save()
self._log_failure_details(application, callback_data)
return Response(
Expand Down
8 changes: 8 additions & 0 deletions backend/benefit/applications/enums.py
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,14 @@ class AhjoDecisionDetails:
decision_date: datetime


DEFAULT_AHJO_CALLBACK_ERROR_MESSAGE = [
{
"id": "NO_ID",
"context": "Received an error but no failure details in the callback.",
"message": "Ahjo-pyynnössä tapahtui virhe, mutta Ahjo ei palauttanut tarkempia tietoja.",
}
]

# Call gettext on some of the enums so that "makemessages" command can find them when used dynamically in templates
_("granted")
_("granted_aged")
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# Generated by Django 4.2.11 on 2024-09-13 11:17

from django.db import migrations, models


class Migration(migrations.Migration):
dependencies = [
("applications", "0082_ahjodecisionproposaldraft_decision_maker_id_and_more"),
]

operations = [
migrations.AddField(
model_name="ahjostatus",
name="ahjo_request_id",
field=models.CharField(blank=True, max_length=64, null=True),
),
]
6 changes: 6 additions & 0 deletions backend/benefit/applications/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -1173,6 +1173,12 @@ class AhjoStatus(TimeStampedModel):
blank=True,
)

ahjo_request_id = models.CharField(
max_length=64,
null=True,
blank=True,
)

def __str__(self):
return self.status

Expand Down
28 changes: 25 additions & 3 deletions backend/benefit/applications/tests/test_ahjo_integration.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
ApplicationTalpaStatus,
AttachmentType,
BenefitType,
DEFAULT_AHJO_CALLBACK_ERROR_MESSAGE,
)
from applications.models import (
AhjoDecisionText,
Expand Down Expand Up @@ -589,10 +590,26 @@ def test_subscribe_to_decisions_callback_success(
}
],
),
(
AhjoRequestType.SEND_DECISION_PROPOSAL,
AhjoStatusEnum.DECISION_PROPOSAL_SENT,
[
{
"id": "INVALID_RECORD_TYPE",
"message": "Asiakirjan tyyppi ei ole sallittu.",
"context": "(Tähän tulisi tietoa virheen esiintymispaikasta jos mahdollista antaa.)",
}
],
),
(
AhjoRequestType.SEND_DECISION_PROPOSAL,
AhjoStatusEnum.DECISION_PROPOSAL_SENT,
None,
),
],
)
@pytest.mark.django_db
def test_ahjo_open_case_callback_failure(
def test_ahjo_callback_failure(
ahjo_client,
ahjo_user_token,
decided_application,
Expand All @@ -611,7 +628,8 @@ def test_ahjo_open_case_callback_failure(
status=last_ahjo_status,
)

ahjo_callback_payload["failureDetails"] = failure_details
if failure_details:
ahjo_callback_payload["failureDetails"] = failure_details

url = reverse(
"ahjo_callback_url",
Expand All @@ -633,7 +651,11 @@ def test_ahjo_open_case_callback_failure(

latest_status = decided_application.ahjo_status.latest()
assert latest_status.status == last_ahjo_status
assert latest_status.error_from_ahjo == ahjo_callback_payload["failureDetails"]
if not failure_details:
assert latest_status.error_from_ahjo == DEFAULT_AHJO_CALLBACK_ERROR_MESSAGE
else:
assert latest_status.error_from_ahjo == ahjo_callback_payload["failureDetails"]
assert latest_status.ahjo_request_id == ahjo_callback_payload["requestId"]


@pytest.mark.parametrize(
Expand Down

0 comments on commit cadaa1b

Please sign in to comment.