diff --git a/backend/benefit/applications/api/v1/talpa_integration_views.py b/backend/benefit/applications/api/v1/talpa_integration_views.py index 7baf4c5cbf..c1bc66019f 100644 --- a/backend/benefit/applications/api/v1/talpa_integration_views.py +++ b/backend/benefit/applications/api/v1/talpa_integration_views.py @@ -3,6 +3,7 @@ from django.conf import settings from django.contrib.auth.models import AnonymousUser +from django.core.exceptions import MultipleObjectsReturned, ObjectDoesNotExist from django.db import transaction from django.utils import timezone from rest_framework import status @@ -170,12 +171,24 @@ def do_status_updates_based_on_instalments( Always set the application as archived after the first instalment is succesfully sent to talpa. """ for application in applications: - instalment = application.calculation.instalments.get( - status=InstalmentStatus.ACCEPTED, - due_date__lte=timezone.now().date(), - ) - instalment.status = instalment_status - instalment.save() + try: + instalment = application.calculation.instalments.get( + status=InstalmentStatus.ACCEPTED, + due_date__lte=timezone.now().date(), + ) + instalment.status = instalment_status + if is_success: + instalment.amount_paid = instalment.amount_after_recoveries + instalment.save() + except ObjectDoesNotExist: + LOGGER.error( + f"Valid payable Instalment not found for application {application.application_number}" + ) + except MultipleObjectsReturned: + LOGGER.error( + f"Multiple payable Instalments found for application \ +{application.application_number}, there should be only one" + ) if is_success: # after 1st instalment is sent to talpa, diff --git a/backend/benefit/applications/tests/test_talpa_integration.py b/backend/benefit/applications/tests/test_talpa_integration.py index 83c05d7d00..df63668834 100644 --- a/backend/benefit/applications/tests/test_talpa_integration.py +++ b/backend/benefit/applications/tests/test_talpa_integration.py @@ -253,6 +253,7 @@ def test_talpa_callback_success( for instalment in instalments: assert instalment.status == InstalmentStatus.PAID + assert instalment.amount_paid == instalment.amount_after_recoveries if number_of_instalments == 1: assert ( @@ -269,12 +270,14 @@ def test_talpa_callback_success( instalment_1 = decided_application.calculation.instalments.get( instalment_number=1 ) + assert instalment_1.amount_paid == instalment.amount_after_recoveries assert instalment_1.status == InstalmentStatus.PAID instalment_2 = decided_application.calculation.instalments.get( instalment_number=2 ) assert instalment_2.status == InstalmentStatus.WAITING + assert instalment_2.amount_paid is None assert ( decided_application.talpa_status