diff --git a/social_protection/signals/__init__.py b/social_protection/signals/__init__.py index 66588b4..7d35ed5 100644 --- a/social_protection/signals/__init__.py +++ b/social_protection/signals/__init__.py @@ -4,6 +4,7 @@ from core.signals import bind_service_signal from social_protection.services import BenefitPlanService, BeneficiaryService, GroupBeneficiaryService from social_protection.signals.on_benefit_plan_data_upload import on_benefit_plan_data_upload +from social_protection.signals.on_confirm_enrollment_of_individual import on_confirm_enrollment_of_individual from social_protection.signals.on_validation_import_valid_items import on_task_complete_validation_import_valid_items from tasks_management.services import on_task_complete_service_handler @@ -36,3 +37,8 @@ def bind_service_signals(): on_task_complete_validation_import_valid_items, bind_type=ServiceSignalBindType.AFTER ) + bind_service_signal( + 'individual_service.select_individuals_to_benefit_plan', + on_confirm_enrollment_of_individual, + bind_type=ServiceSignalBindType.AFTER + ) diff --git a/social_protection/signals/on_confirm_enrollment_of_individual.py b/social_protection/signals/on_confirm_enrollment_of_individual.py new file mode 100644 index 0000000..99f22b0 --- /dev/null +++ b/social_protection/signals/on_confirm_enrollment_of_individual.py @@ -0,0 +1,26 @@ +import logging + +from django.core.exceptions import ValidationError +from social_protection.models import Beneficiary + +logger = logging.getLogger(__name__) + + +def on_confirm_enrollment_of_individual(**kwargs): + result = kwargs.get('result', None) + benefit_plan_id = result['benefit_plan_id'] + status = result['status'] + user = result['user'] + individuals_to_upload = result['individuals_not_assigned_to_selected_programme'] + for individual in individuals_to_upload: + # Create a new Beneficiary instance + beneficiary = Beneficiary( + individual=individual, + benefit_plan_id=benefit_plan_id, + status=status, + json_ext=individual.json_ext + ) + try: + beneficiary.save(username=user.username) + except ValidationError as e: + logger.error(f"Validation error occurred: {e}")