emails: Send onboarding_zulip_guide only for unique organization type.

Only schedule the onboarding_zulip_guide email if the user is not
also a member of any other Zulip organization of the same type on
the server.
This commit is contained in:
Lauryn Menard 2023-07-19 20:47:05 +02:00
parent 3ad2f5e85b
commit 2c68a1b395
2 changed files with 60 additions and 59 deletions

View File

@ -839,10 +839,11 @@ def enqueue_welcome_emails(user: UserProfile, realm_creation: bool = False) -> N
return
from_name, from_address = welcome_sender_information()
other_account_count = (
realm_org_types_for_other_accounts = set(
UserProfile.objects.filter(delivery_email__iexact=user.delivery_email)
.exclude(id=user.id)
.count()
.values_list("realm__org_type", flat=True)
.distinct()
)
unsubscribe_link = one_click_unsubscribe_link(user, "welcome")
realm_url = user.realm.url
@ -851,7 +852,9 @@ def enqueue_welcome_emails(user: UserProfile, realm_creation: bool = False) -> N
# to determine how long to delay sending the email based on when the user signed up.
onboarding_email_schedule = get_onboarding_email_schedule(user)
if other_account_count == 0:
# If the user has another account with the same email in another
# organization, then we don't schedule the followup_day2 email.
if len(realm_org_types_for_other_accounts) == 0:
onboarding_zulip_topics_context = common_context(user)
onboarding_zulip_topics_context.update(
@ -871,8 +874,14 @@ def enqueue_welcome_emails(user: UserProfile, realm_creation: bool = False) -> N
delay=onboarding_email_schedule["onboarding_zulip_topics"],
)
# We only send the onboarding_zulip_guide email for a subset of Realm.ORG_TYPES
onboarding_zulip_guide_url, organization_type_reference = get_org_type_zulip_guide(user.realm)
# If the user has an account in another organization of the same
# type, then we don't schedule the onboarding_zulip_guide email.
if user.realm.org_type not in realm_org_types_for_other_accounts:
# We send the onboarding_zulip_guide email for a subset of Realm.ORG_TYPES
onboarding_zulip_guide_url, organization_type_reference = get_org_type_zulip_guide(
user.realm
)
# Only send follow_zulip_guide to "/for/communities/" guide if user is realm admin.
# TODO: Remove this condition and related tests when guide is updated;
@ -886,7 +895,7 @@ def enqueue_welcome_emails(user: UserProfile, realm_creation: bool = False) -> N
if onboarding_zulip_guide_url is not None:
onboarding_zulip_guide_context = common_context(user)
onboarding_zulip_guide_context.update(
# We use the same unsubscribe link in both onboarding_zulip_topics
# We use the same unsubscribe link in both followup_day2
# and onboarding_zulip_guide as these links do not expire.
unsubscribe_link=unsubscribe_link,
organization_type=organization_type_reference,

View File

@ -462,7 +462,12 @@ class TestFollowupEmails(ZulipTestCase):
"zerver/emails/account_registered",
)
def test_followup_emails_for_regular_realms(self) -> None:
def test_emails_for_new_organization_with_existing_account(self) -> None:
# The initial account registered email is sent, and its content is for
# creating a new organization. Neither of the additional onboarding
# emails are scheduled because the user has a user account with the same
# email in another realm and that other realm has the same organization
# type as the new realm.
cordelia = self.example_user("cordelia")
send_account_registered_email(self.example_user("cordelia"), realm_creation=True)
enqueue_welcome_emails(self.example_user("cordelia"), realm_creation=True)
@ -470,61 +475,48 @@ class TestFollowupEmails(ZulipTestCase):
"scheduled_timestamp"
)
assert scheduled_emails is not None
self.assert_length(scheduled_emails, 3)
self.assert_length(scheduled_emails, 1)
self.assertEqual(
orjson.loads(scheduled_emails[0].data)["template_prefix"],
"zerver/emails/account_registered",
)
self.assertEqual(
orjson.loads(scheduled_emails[1].data)["template_prefix"],
"zerver/emails/onboarding_zulip_guide",
)
self.assertEqual(
orjson.loads(scheduled_emails[2].data)["template_prefix"],
"zerver/emails/onboarding_team_to_zulip",
)
deliver_scheduled_emails(scheduled_emails[0])
from django.core.mail import outbox
self.assert_length(outbox, 1)
message = outbox[0]
self.assert_length(mail.outbox, 1)
message = mail.outbox[0]
self.assertIn("you have created a new Zulip organization", message.body)
self.assertNotIn("demo org", message.body)
def test_followup_emails_for_demo_realms(self) -> None:
def test_emails_for_new_demo_organization_with_existing_account(self) -> None:
# The initial account registered email is sent, and its content is for
# creating a new demo organization. Neither of the additional onboarding
# emails are scheduled because the user has a user account with the same
# email in another realm and that other realm has the same organization
# type as the new realm.
cordelia = self.example_user("cordelia")
cordelia.realm.demo_organization_scheduled_deletion_date = timezone_now() + timedelta(
days=30
)
cordelia.realm.save()
send_account_registered_email(self.example_user("cordelia"), realm_creation=True)
enqueue_welcome_emails(self.example_user("cordelia"), realm_creation=True)
scheduled_emails = ScheduledEmail.objects.filter(users=cordelia).order_by(
"scheduled_timestamp"
)
assert scheduled_emails is not None
self.assert_length(scheduled_emails, 3)
self.assert_length(scheduled_emails, 1)
self.assertEqual(
orjson.loads(scheduled_emails[0].data)["template_prefix"],
"zerver/emails/account_registered",
)
self.assertEqual(
orjson.loads(scheduled_emails[1].data)["template_prefix"],
"zerver/emails/onboarding_zulip_guide",
)
self.assertEqual(
orjson.loads(scheduled_emails[2].data)["template_prefix"],
"zerver/emails/onboarding_team_to_zulip",
)
deliver_scheduled_emails(scheduled_emails[0])
from django.core.mail import outbox
self.assert_length(outbox, 1)
message = outbox[0]
self.assert_length(mail.outbox, 1)
message = mail.outbox[0]
self.assertIn("you have created a new demo Zulip organization", message.body)
def test_onboarding_zulip_guide_with_invalid_org_type(self) -> None: