Skip to content

Commit

Permalink
communications: Send emails to admins when employer left the company
Browse files Browse the repository at this point in the history
  • Loading branch information
tonial committed Jan 6, 2025
1 parent 0f75cc3 commit 37c46c3
Show file tree
Hide file tree
Showing 4 changed files with 107 additions and 8 deletions.
12 changes: 8 additions & 4 deletions itou/communications/dispatch/email.py
Original file line number Diff line number Diff line change
@@ -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

Expand All @@ -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]
Expand Down
2 changes: 1 addition & 1 deletion itou/templates/layout/base_email_text_body.txt
Original file line number Diff line number Diff line change
@@ -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 %}
Expand Down
43 changes: 41 additions & 2 deletions tests/communications/test_dispatch.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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
)


Expand Down
58 changes: 57 additions & 1 deletion tests/job_applications/__snapshots__/test_transfer.ambr
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# serializer version: 1
# name: test_model_fields
dict({
'num_queries': 14,
'num_queries': 15,
'queries': list([
dict({
'origin': list([
Expand Down Expand Up @@ -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]',
Expand Down

0 comments on commit 37c46c3

Please sign in to comment.