diff --git a/itou/communications/dispatch/base.py b/itou/communications/dispatch/base.py index 3b4fcfe919..105f171577 100644 --- a/itou/communications/dispatch/base.py +++ b/itou/communications/dispatch/base.py @@ -3,9 +3,10 @@ class BaseNotification: can_be_disabled = True - def __init__(self, user, structure=None, /, **kwargs): + def __init__(self, user, structure=None, forward_from_user=None, /, **kwargs): self.user = user self.structure = structure + self.forward_from_user = forward_from_user self.context = kwargs def __repr__(self): @@ -29,7 +30,11 @@ def should_send(self): return True def get_context(self): - return self.validate_context() + return self.validate_context() | { + "user": self.user, + "structure": self.structure, + "forward_from_user": self.forward_from_user, + } def validate_context(self): return self.context diff --git a/itou/communications/dispatch/email.py b/itou/communications/dispatch/email.py index e613806807..501f2dc1b5 100644 --- a/itou/communications/dispatch/email.py +++ b/itou/communications/dispatch/email.py @@ -1,5 +1,6 @@ import logging +from itou.companies.models import CompanyMembership from itou.prescribers.models import PrescriberMembership from itou.utils.emails import get_email_message @@ -13,15 +14,6 @@ class EmailNotification(BaseNotification): REQUIRED = BaseNotification.REQUIRED + ["subject_template", "body_template"] def build(self): - # TODO: Temporary log for analysis : remove by the end of November 2024 - if self.user.is_prescriber and self.structure: - memberships = ( - PrescriberMembership.objects.active().filter(organization=self.structure).select_related("user") - ) - members = [m.user for m in memberships] - if self.user not in members: - admin_count = len([m for m in memberships if m.is_admin]) - logger.info("Estimate new email sent to admin_count=%d", admin_count) return get_email_message( [self.user.email], self.get_context(), @@ -30,5 +22,26 @@ def build(self): ) def send(self): + if ( + # If it is already a forwarded notification, do not check if the user is still a member of the organization + not self.forward_from_user + # Don't use should_send() if the user left the org because we don't want to use his settings + and self.is_applicable() + and self.structure + and (self.user.is_prescriber or self.user.is_employer) + ): + if self.user.is_prescriber: + memberships = ( + PrescriberMembership.objects.active().filter(organization=self.structure).select_related("user") + ) + elif self.user.is_employer: + memberships = CompanyMembership.objects.active().filter(company=self.structure).select_related("user") + members = [m.user for m in memberships] + if self.user not in members: + admins = [m.user for m in memberships if m.is_admin] + logger.info("Send email copy to admin, admin_count=%d", len(admins)) + for admin in admins: + self.__class__(admin, self.structure, self.user, **self.context).send() + return if self.should_send(): - return self.build().send() + self.build().send() diff --git a/itou/templates/apply/includes/job_application_sender_info.html b/itou/templates/apply/includes/job_application_sender_info.html index 965aafce19..1dc3b67089 100644 --- a/itou/templates/apply/includes/job_application_sender_info.html +++ b/itou/templates/apply/includes/job_application_sender_info.html @@ -1,6 +1,21 @@ {% load format_filters %} {% load matomo %} +{% if job_application_sender_left_org %} +
L’émetteur de cette candidature ne fait plus partie de l’organisation émettrice
+