From eee41af884049aab2aac9a5b00e62221b1bcfcb5 Mon Sep 17 00:00:00 2001
From: Stein Elgethun <stein.elgethun@usit.uio.no>
Date: Mon, 3 Jan 2022 16:54:53 +0100
Subject: [PATCH] Send mail to sponsor when guest finishes registration

Issue: greg-112
---
 gregui/api/views/invitation.py            |  6 +++--
 gregui/mailutils.py                       |  7 ++++++
 gregui/tests/api/views/test_invitation.py | 29 ++++++++++++++++-------
 gregui/tests/test_mailutils.py            |  9 +++++++
 4 files changed, 40 insertions(+), 11 deletions(-)

diff --git a/gregui/api/views/invitation.py b/gregui/api/views/invitation.py
index a2459c14..b6931572 100644
--- a/gregui/api/views/invitation.py
+++ b/gregui/api/views/invitation.py
@@ -21,7 +21,7 @@ from greg.permissions import IsSponsor
 from greg.utils import get_default_invitation_expire_date_from_now
 from gregui.api.serializers.guest import GuestRegisterSerializer
 from gregui.api.serializers.invitation import InviteGuestSerializer
-from gregui.mailutils import send_invite_mail
+from gregui.mailutils import send_confirmation_mail_from_link, send_invite_mail
 from gregui.models import GregUserProfile
 
 logger = structlog.getLogger(__name__)
@@ -328,7 +328,9 @@ class InvitedGuestView(GenericAPIView):
             # Expire the invite link
             invite_link.expire = timezone.now()
             invite_link.save()
-            # TODO: Send an email to the sponsor?
+
+            # Send an email to the sponsor
+            send_confirmation_mail_from_link(invite_link)
         return Response(status=status.HTTP_200_OK)
 
     @staticmethod
diff --git a/gregui/mailutils.py b/gregui/mailutils.py
index fb308345..e1902da2 100644
--- a/gregui/mailutils.py
+++ b/gregui/mailutils.py
@@ -82,3 +82,10 @@ def send_invite_mail(link: InvitationLink) -> Optional[str]:
     return send_registration_mail(
         email_address.value, f"{sponsor.first_name} {sponsor.last_name}"
     )
+
+
+def send_confirmation_mail_from_link(link: InvitationLink) -> Optional[str]:
+    email_address = link.invitation.role.sponsor.work_email
+    guest = link.invitation.role.person
+    guest_name = f"{guest.first_name} {guest.last_name}"
+    return send_confirmation_mail(email_address, guest_name)
diff --git a/gregui/tests/api/views/test_invitation.py b/gregui/tests/api/views/test_invitation.py
index 755cdc3d..4b6e1744 100644
--- a/gregui/tests/api/views/test_invitation.py
+++ b/gregui/tests/api/views/test_invitation.py
@@ -129,7 +129,9 @@ def test_get_invited_info_expired_link(
 
 
 @pytest.mark.django_db
-def test_invited_guest_can_post_information(client: APIClient, invited_person):
+def test_invited_guest_can_post_information(
+    client: APIClient, invited_person, confirmation_template
+):
     person, invitation_link = invited_person
     # get a session
     client.post(
@@ -217,7 +219,9 @@ def test_post_invited_info_invalid_national_id_number(client, invited_person):
 
 
 @pytest.mark.django_db
-def test_post_invited_info_valid_national_id_number(client, invited_person):
+def test_post_invited_info_valid_national_id_number(
+    client, invited_person, confirmation_template
+):
     person, invitation_link = invited_person
     fnr = "11120618212"
     data = {
@@ -243,7 +247,7 @@ def test_post_invited_info_valid_national_id_number(client, invited_person):
 
 
 @pytest.mark.django_db
-def test_email_update(client, invited_person):
+def test_email_update(client, invited_person, confirmation_template):
     person, invitation_link = invited_person
     email_test = person.private_email.value
     url = reverse("gregui-v1:invited-info")
@@ -264,7 +268,7 @@ def test_email_update(client, invited_person):
 
 
 @pytest.mark.django_db
-def test_register_passport(client, invited_person):
+def test_register_passport(client, invited_person, confirmation_template):
     person, invitation_link = invited_person
     passport_information = "EN-123456789"
     data = {"person": {"mobile_phone": "+4797543992", "passport": passport_information}}
@@ -350,6 +354,7 @@ def test_name_update_allowed_if_feide_identity_is_not_present(
     create_invitation,
     create_invitation_link,
     invitation_valid_date,
+    confirmation_template,
 ):
     # This person does not have a Feide ID, so he will be viewed as someone whose name was entered manually
     # by the sponsor, and in it that case the guest can update it
@@ -427,7 +432,9 @@ def test_post_info_fail_unknown_field(client, invited_person_verified_nin):
 
 
 @pytest.mark.django_db
-def test_date_of_birth_stored(client, invited_person_verified_nin):
+def test_date_of_birth_stored(
+    client, invited_person_verified_nin, confirmation_template
+):
     _, invitation_link = invited_person_verified_nin
 
     session = client.session
@@ -468,7 +475,9 @@ def test_invalid_date_of_birth_rejected(client, invited_person_verified_nin):
 
 
 @pytest.mark.django_db
-def test_saves_consents(client, invited_person, consent_type_foo, consent_type_bar):
+def test_saves_consents(
+    client, invited_person, consent_type_foo, consent_type_bar, confirmation_template
+):
     person, invitation_link = invited_person
     fnr = "11120618212"
 
@@ -502,7 +511,7 @@ def test_saves_consents(client, invited_person, consent_type_foo, consent_type_b
 
 
 @pytest.mark.django_db
-def test_post_invited_info_valid_dnumber(client, invited_person):
+def test_post_invited_info_valid_dnumber(client, invited_person, confirmation_template):
     person, invitation_link = invited_person
     d_number = "53097248016"
     data = {
@@ -528,7 +537,7 @@ def test_post_invited_info_valid_dnumber(client, invited_person):
 
 
 @pytest.mark.django_db
-def test_gender_stored(client, invited_person_verified_nin):
+def test_gender_stored(client, invited_person_verified_nin, confirmation_template):
     _, invitation_link = invited_person_verified_nin
 
     session = client.session
@@ -553,7 +562,9 @@ def test_gender_stored(client, invited_person_verified_nin):
 
 
 @pytest.mark.django_db
-def test_gender_blank_allowed(client, invited_person_verified_nin):
+def test_gender_blank_allowed(
+    client, invited_person_verified_nin, confirmation_template
+):
     _, invitation_link = invited_person_verified_nin
 
     session = client.session
diff --git a/gregui/tests/test_mailutils.py b/gregui/tests/test_mailutils.py
index 04bf6dcd..6e1f0482 100644
--- a/gregui/tests/test_mailutils.py
+++ b/gregui/tests/test_mailutils.py
@@ -76,3 +76,12 @@ def test_send_invite_mail_no_mail(invited_person):
     assert len(mail.outbox) == 0
     assert mailutils.send_invite_mail(link) is None
     assert len(mail.outbox) == 0
+
+
+@pytest.mark.django_db
+def test_confirmation_mail_from_link(invited_person, confirmation_template):
+    mail.outbox = []
+    _, link = invited_person
+    assert len(mail.outbox) == 0
+    assert mailutils.send_confirmation_mail_from_link(link)
+    assert len(mail.outbox) == 1
-- 
GitLab