diff --git a/itou/communications/dispatch/email.py b/itou/communications/dispatch/email.py index 6bdb4c424f..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 @@ -26,12 +27,15 @@ def send(self): 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.user.is_prescriber and self.structure + and (self.user.is_prescriber or self.user.is_employer) ): - memberships = ( - PrescriberMembership.objects.active().filter(organization=self.structure).select_related("user") - ) + 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] diff --git a/itou/templates/layout/base_email_text_body.txt b/itou/templates/layout/base_email_text_body.txt index 3f19f2c9bd..e3911d0f52 100644 --- a/itou/templates/layout/base_email_text_body.txt +++ b/itou/templates/layout/base_email_text_body.txt @@ -1,7 +1,7 @@ {% autoescape off %} {% if forward_from_user|default:False %} -Vous recevez cet e-mail parce que l'utilisateur {{ forward_from_user.get_full_name }} ({{ forward_from_user.email}}) ne fait plus partie de votre organisation. +Vous recevez cet e-mail parce que l'utilisateur {{ forward_from_user.get_full_name }} ({{ forward_from_user.email}}) ne fait plus partie de votre {{ forward_from_user.is_employer|yesno:"structure,organisation" }}. ----- {% endif %} diff --git a/tests/communications/test_dispatch.py b/tests/communications/test_dispatch.py index 53d4184818..b2ef33d721 100644 --- a/tests/communications/test_dispatch.py +++ b/tests/communications/test_dispatch.py @@ -14,6 +14,7 @@ WithStructureMixin, ) from itou.communications.models import NotificationRecord, NotificationSettings +from tests.companies.factories import CompanyMembershipFactory from tests.prescribers.factories import PrescriberMembershipFactory from tests.users.factories import EmployerFactory, JobSeekerFactory, PrescriberFactory @@ -246,11 +247,49 @@ def test_method_send_for_prescriber_that_left_his_org( assert set(mailoutbox[0].to + mailoutbox[1].to) == {admin_1.email, admin_2.email} assert ( f"Vous recevez cet e-mail parce que l'utilisateur {self.user.get_full_name()} ({self.user.email})" - " ne fait plus partie de votre organisation" in mailoutbox[0].body + " ne fait plus partie de votre organisation." in mailoutbox[0].body ) assert ( f"Vous recevez cet e-mail parce que l'utilisateur {self.user.get_full_name()} ({self.user.email})" - " ne fait plus partie de votre organisation" in mailoutbox[1].body + " ne fait plus partie de votre organisation." in mailoutbox[1].body + ) + + def test_method_send_for_employer_that_left_his_company( + self, email_notification, django_capture_on_commit_callbacks, mailoutbox, caplog + ): + user = EmployerFactory(with_company=True) + company = user.companymembership_set.first().company + user.companymembership_set.update(is_active=False) + + admin_1 = CompanyMembershipFactory( + user=EmployerFactory(), + company=company, + is_admin=True, + ).user + admin_2 = CompanyMembershipFactory( + user=EmployerFactory(), + company=company, + is_admin=True, + ).user + CompanyMembershipFactory( + user=EmployerFactory(), + company=company, + is_admin=False, + ) + + with django_capture_on_commit_callbacks(execute=True): + email_notification(user, company).send() + + assert caplog.messages == ["Send email copy to admin, admin_count=2"] + assert len(mailoutbox) == 2 + assert set(mailoutbox[0].to + mailoutbox[1].to) == {admin_1.email, admin_2.email} + assert ( + f"Vous recevez cet e-mail parce que l'utilisateur {user.get_full_name()} ({user.email})" + " ne fait plus partie de votre structure." in mailoutbox[0].body + ) + assert ( + f"Vous recevez cet e-mail parce que l'utilisateur {user.get_full_name()} ({user.email})" + " ne fait plus partie de votre structure." in mailoutbox[1].body ) diff --git a/tests/job_applications/__snapshots__/test_transfer.ambr b/tests/job_applications/__snapshots__/test_transfer.ambr index 19ceb88153..1d4296ac85 100644 --- a/tests/job_applications/__snapshots__/test_transfer.ambr +++ b/tests/job_applications/__snapshots__/test_transfer.ambr @@ -1,7 +1,7 @@ # serializer version: 1 # name: test_model_fields dict({ - 'num_queries': 14, + 'num_queries': 15, 'queries': list([ dict({ 'origin': list([ @@ -147,6 +147,62 @@ ORDER BY RANDOM() ASC ''', }), + dict({ + 'origin': list([ + 'JobApplicationTransferredForEmployerNotification.send[communications/dispatch/email.py]', + 'JobApplication.transfer[job_applications/models.py]', + ]), + 'sql': ''' + SELECT "companies_companymembership"."id", + "companies_companymembership"."user_id", + "companies_companymembership"."joined_at", + "companies_companymembership"."is_admin", + "companies_companymembership"."is_active", + "companies_companymembership"."created_at", + "companies_companymembership"."updated_at", + "companies_companymembership"."company_id", + "companies_companymembership"."updated_by_id", + "companies_companymembership"."notifications", + "users_user"."id", + "users_user"."password", + "users_user"."last_login", + "users_user"."is_superuser", + "users_user"."username", + "users_user"."first_name", + "users_user"."last_name", + "users_user"."is_staff", + "users_user"."is_active", + "users_user"."date_joined", + "users_user"."address_line_1", + "users_user"."address_line_2", + "users_user"."post_code", + "users_user"."city", + "users_user"."department", + "users_user"."coords", + "users_user"."geocoding_score", + "users_user"."geocoding_updated_at", + "users_user"."ban_api_resolved_address", + "users_user"."insee_city_id", + "users_user"."title", + "users_user"."email", + "users_user"."phone", + "users_user"."kind", + "users_user"."identity_provider", + "users_user"."has_completed_welcoming_tour", + "users_user"."created_by_id", + "users_user"."external_data_source_history", + "users_user"."last_checked_at", + "users_user"."public_id", + "users_user"."address_filled_at", + "users_user"."first_login" + FROM "companies_companymembership" + INNER JOIN "users_user" ON ("companies_companymembership"."user_id" = "users_user"."id") + WHERE ("users_user"."is_active" + AND "companies_companymembership"."is_active" + AND "companies_companymembership"."company_id" = %s) + ORDER BY RANDOM() ASC + ''', + }), dict({ 'origin': list([ 'JobApplicationTransferredForEmployerNotification.should_send[communications/dispatch/base.py]',