|
33 | 33 | TimeFrame, |
34 | 34 | YearsTimeFrameChoices, |
35 | 35 | ) |
36 | | -from eap.tasks import send_new_eap_registration_email |
| 36 | +from eap.tasks import ( |
| 37 | + send_approved_email, |
| 38 | + send_eap_resubmission_email, |
| 39 | + send_feedback_email, |
| 40 | + send_feedback_email_for_resubmitted_eap, |
| 41 | + send_new_eap_registration_email, |
| 42 | + send_new_eap_submission_email, |
| 43 | + send_pending_pfa_email, |
| 44 | + send_technical_validation_email, |
| 45 | +) |
37 | 46 | from eap.utils import ( |
38 | 47 | has_country_permission, |
39 | 48 | is_user_ifrc_admin, |
@@ -906,3 +915,86 @@ def validate_review_checklist_file(self, file): |
906 | 915 | validate_file_type(file) |
907 | 916 |
|
908 | 917 | return file |
| 918 | + |
| 919 | + def update(self, instance: EAPRegistration, validated_data: dict[str, typing.Any]) -> EAPRegistration: |
| 920 | + old_status = instance.get_status_enum |
| 921 | + updated_instance = super().update(instance, validated_data) |
| 922 | + new_status = updated_instance.get_status_enum |
| 923 | + |
| 924 | + if old_status == new_status: |
| 925 | + return updated_instance |
| 926 | + |
| 927 | + eap_registration_id = updated_instance.id |
| 928 | + assert updated_instance.get_eap_type_enum is not None, "EAP type must not be None" |
| 929 | + |
| 930 | + if updated_instance.get_eap_type_enum == EAPType.SIMPLIFIED_EAP: |
| 931 | + eap_count = SimplifiedEAP.objects.filter(eap_registration=updated_instance).count() |
| 932 | + else: |
| 933 | + eap_count = FullEAP.objects.filter(eap_registration=updated_instance).count() |
| 934 | + |
| 935 | + if (old_status, new_status) == ( |
| 936 | + EAPRegistration.Status.UNDER_DEVELOPMENT, |
| 937 | + EAPRegistration.Status.UNDER_REVIEW, |
| 938 | + ): |
| 939 | + transaction.on_commit(lambda: send_new_eap_submission_email.delay(eap_registration_id)) |
| 940 | + |
| 941 | + elif (old_status, new_status) == ( |
| 942 | + EAPRegistration.Status.UNDER_REVIEW, |
| 943 | + EAPRegistration.Status.NS_ADDRESSING_COMMENTS, |
| 944 | + ): |
| 945 | + """ |
| 946 | + NOTE: |
| 947 | + At the transition (UNDER_REVIEW -> NS_ADDRESSING_COMMENTS), the EAP snapshot |
| 948 | + is generated inside `_validate_status()` BEFORE we reach this `update()` method. |
| 949 | +
|
| 950 | + That snapshot operation: |
| 951 | + - Locks the reviewed EAP (previous version) |
| 952 | + - Creates a new snapshot (incremented version) |
| 953 | + - Updates latest_simplified_eap or latest_full_eap to the new version |
| 954 | +
|
| 955 | + Email logic based on eap_count: |
| 956 | + - If eap_count == 2 (i.e., first snapshot already exists and this is the first IFRC feedback cycle) |
| 957 | + - Send the first feedback email |
| 958 | + - Else (eap_count > 2), indicating subsequent feedback cycles: |
| 959 | + - Send the resubmitted feedback email |
| 960 | +
|
| 961 | + Therefore: |
| 962 | + - version == 2 always corresponds to the first IFRC feedback cycle |
| 963 | + - Any later versions (>= 3) correspond to resubmitted cycles |
| 964 | + """ |
| 965 | + |
| 966 | + if eap_count == 2: |
| 967 | + transaction.on_commit(lambda: send_feedback_email.delay(eap_registration_id)) |
| 968 | + elif eap_count > 2: |
| 969 | + transaction.on_commit(lambda: send_feedback_email_for_resubmitted_eap.delay(eap_registration_id)) |
| 970 | + |
| 971 | + elif (old_status, new_status) == ( |
| 972 | + EAPRegistration.Status.NS_ADDRESSING_COMMENTS, |
| 973 | + EAPRegistration.Status.UNDER_REVIEW, |
| 974 | + ): |
| 975 | + transaction.on_commit(lambda: send_eap_resubmission_email.delay(eap_registration_id)) |
| 976 | + elif (old_status, new_status) == ( |
| 977 | + EAPRegistration.Status.TECHNICALLY_VALIDATED, |
| 978 | + EAPRegistration.Status.NS_ADDRESSING_COMMENTS, |
| 979 | + ): |
| 980 | + transaction.on_commit(lambda: send_feedback_email_for_resubmitted_eap.delay(eap_registration_id)) |
| 981 | + |
| 982 | + elif (old_status, new_status) == ( |
| 983 | + EAPRegistration.Status.UNDER_REVIEW, |
| 984 | + EAPRegistration.Status.TECHNICALLY_VALIDATED, |
| 985 | + ): |
| 986 | + transaction.on_commit(lambda: send_technical_validation_email.delay(eap_registration_id)) |
| 987 | + |
| 988 | + elif (old_status, new_status) == ( |
| 989 | + EAPRegistration.Status.TECHNICALLY_VALIDATED, |
| 990 | + EAPRegistration.Status.PENDING_PFA, |
| 991 | + ): |
| 992 | + transaction.on_commit(lambda: send_pending_pfa_email.delay(eap_registration_id)) |
| 993 | + |
| 994 | + elif (old_status, new_status) == ( |
| 995 | + EAPRegistration.Status.PENDING_PFA, |
| 996 | + EAPRegistration.Status.APPROVED, |
| 997 | + ): |
| 998 | + transaction.on_commit(lambda: send_approved_email.delay(eap_registration_id)) |
| 999 | + |
| 1000 | + return updated_instance |
0 commit comments