From bed859218d78d185d2aaf41bb219ae9129cd5c33 Mon Sep 17 00:00:00 2001 From: Sampo Tawast <5328394+sirtawast@users.noreply.github.com> Date: Thu, 22 Aug 2024 09:11:34 +0300 Subject: [PATCH] fix: reset a few values when apprenticeship or pay subsidy type is changed (#3218) --- .../benefit/applications/api/v1/serializers/application.py | 7 ++++++- frontend/benefit/applicant/src/hooks/useFormActions.tsx | 5 ++++- frontend/benefit/handler/src/hooks/useFormActions.tsx | 3 +-- 3 files changed, 11 insertions(+), 4 deletions(-) diff --git a/backend/benefit/applications/api/v1/serializers/application.py b/backend/benefit/applications/api/v1/serializers/application.py index 4243848a53..d0d1b2831f 100755 --- a/backend/benefit/applications/api/v1/serializers/application.py +++ b/backend/benefit/applications/api/v1/serializers/application.py @@ -1291,18 +1291,23 @@ def _base_update(self, instance, validated_data): employee_data = validated_data.pop("employee", None) approve_terms = validated_data.pop("approve_terms", None) decision_proposal_data = validated_data.pop("decision_proposal", None) + apprenticeship_program = validated_data.get("apprenticeship_program", None) pre_update_status = instance.status application = super().update(instance, validated_data) + if de_minimis_data is not None: # if it is a patch request that didn't have de_minimis_data_set, do nothing self._update_de_minimis_aid(application, de_minimis_data) if employee_data is not None: self._update_or_create_employee(application, employee_data) - if decision_proposal_data is not None: AhjoDecisionProposalDraft.objects.update_or_create( application=application, defaults=decision_proposal_data ) + if apprenticeship_program is None or apprenticeship_program is False: + application.training_compensations.all().delete() + if application.pay_subsidy_granted == PaySubsidyGranted.NOT_GRANTED: + application.pay_subsidies.all().delete() if instance.status != pre_update_status: self.handle_status_transition( diff --git a/frontend/benefit/applicant/src/hooks/useFormActions.tsx b/frontend/benefit/applicant/src/hooks/useFormActions.tsx index a25537d8d6..6dfdd88318 100644 --- a/frontend/benefit/applicant/src/hooks/useFormActions.tsx +++ b/frontend/benefit/applicant/src/hooks/useFormActions.tsx @@ -145,7 +145,10 @@ const useFormActions = (application: Partial): FormActions => { endDate: endDate ? convertToBackendDateFormat(parseDate(endDate)) : undefined, - apprenticeshipProgram, + apprenticeshipProgram: + paySubsidyGranted !== PAY_SUBSIDY_GRANTED.NOT_GRANTED + ? apprenticeshipProgram + : null, }; // Use context on first step, otherwise pass data from backend diff --git a/frontend/benefit/handler/src/hooks/useFormActions.tsx b/frontend/benefit/handler/src/hooks/useFormActions.tsx index c8e129076b..1ad2b64bfc 100644 --- a/frontend/benefit/handler/src/hooks/useFormActions.tsx +++ b/frontend/benefit/handler/src/hooks/useFormActions.tsx @@ -215,8 +215,7 @@ const useFormActions = ( values: Partial ): TrainingCompensation[] => { if ( - values.apprenticeshipProgram === - initialApplication?.apprenticeshipProgram && + values.apprenticeshipProgram && values.paySubsidyGranted !== PAY_SUBSIDY_GRANTED.NOT_GRANTED ) { // Return the training compensation values as they are