registration: Send a group DM to admins when no spare licenses left.

Earlier, when adding a new user failed due to no spare licenses
available, a message was sent to the "New user announcements"
stream.

We plan to disable the stream by default as a part of improving
onboarding experience.

Now, we send a group DM to admins when adding a new user fails
due to no spare licenses available. It makes it independent of
the "New user announcements" setting. These warning messages
are important and shouldn't be missed.
This commit is contained in:
Prakhar Pratyush 2024-04-02 10:08:38 +05:30 committed by Tim Abbott
parent 2abc25ac4c
commit e3f8c62e34
2 changed files with 12 additions and 8 deletions

View File

@ -5,7 +5,7 @@ from django.utils.translation import gettext as _
from corporate.lib.stripe import LicenseLimitError, get_latest_seat_count, get_seat_count from corporate.lib.stripe import LicenseLimitError, get_latest_seat_count, get_seat_count
from corporate.models import get_current_plan_by_realm from corporate.models import get_current_plan_by_realm
from zerver.actions.create_user import send_message_to_signup_notification_stream from zerver.actions.create_user import send_group_direct_message_to_admins
from zerver.lib.exceptions import InvitationError from zerver.lib.exceptions import InvitationError
from zerver.models import Realm, UserProfile from zerver.models import Realm, UserProfile
from zerver.models.users import get_system_bot from zerver.models.users import get_system_bot
@ -51,7 +51,7 @@ def generate_licenses_low_warning_message_if_required(realm: Realm) -> Optional[
}[licenses_remaining].format(**format_kwargs) }[licenses_remaining].format(**format_kwargs)
def send_user_unable_to_signup_message_to_signup_notification_stream( def send_user_unable_to_signup_group_direct_message_to_admins(
realm: Realm, user_email: str realm: Realm, user_email: str
) -> None: ) -> None:
message = _( message = _(
@ -64,7 +64,7 @@ def send_user_unable_to_signup_message_to_signup_notification_stream(
deactivate_user_help_page_link="/help/deactivate-or-reactivate-a-user", deactivate_user_help_page_link="/help/deactivate-or-reactivate-a-user",
) )
send_message_to_signup_notification_stream( send_group_direct_message_to_admins(
get_system_bot(settings.NOTIFICATION_BOT, realm.id), realm, message get_system_bot(settings.NOTIFICATION_BOT, realm.id), realm, message
) )
@ -93,7 +93,7 @@ def check_spare_licenses_available_for_registering_new_user(
else: else:
check_spare_licenses_available_for_adding_new_users(realm, extra_non_guests_count=1) check_spare_licenses_available_for_adding_new_users(realm, extra_non_guests_count=1)
except LicenseLimitError: except LicenseLimitError:
send_user_unable_to_signup_message_to_signup_notification_stream(realm, user_email_to_add) send_user_unable_to_signup_group_direct_message_to_admins(realm, user_email_to_add)
raise raise

View File

@ -78,6 +78,7 @@ from zerver.models import (
UserProfile, UserProfile,
) )
from zerver.models.realms import get_realm from zerver.models.realms import get_realm
from zerver.models.recipients import get_huddle_user_ids
from zerver.models.streams import get_stream from zerver.models.streams import get_stream
from zerver.models.users import get_system_bot, get_user, get_user_by_delivery_email from zerver.models.users import get_system_bot, get_user, get_user_by_delivery_email
from zerver.views.auth import redirect_and_log_into_subdomain, start_two_factor_auth from zerver.views.auth import redirect_and_log_into_subdomain, start_two_factor_auth
@ -2868,9 +2869,9 @@ class UserSignUpTest(ZulipTestCase):
def test_signup_to_realm_on_manual_license_plan(self) -> None: def test_signup_to_realm_on_manual_license_plan(self) -> None:
realm = get_realm("zulip") realm = get_realm("zulip")
denmark_stream = get_stream("Denmark", realm) admin_user_ids = set(realm.get_human_admin_users().values_list("id", flat=True))
realm.signup_announcements_stream = denmark_stream notification_bot = get_system_bot(settings.NOTIFICATION_BOT, realm.id)
realm.save(update_fields=["signup_announcements_stream"]) expected_group_direct_message_user_ids = admin_user_ids | {notification_bot.id}
_, ledger = self.subscribe_realm_to_monthly_plan_on_manual_license_management(realm, 5, 5) _, ledger = self.subscribe_realm_to_monthly_plan_on_manual_license_management(realm, 5, 5)
@ -2886,7 +2887,10 @@ class UserSignUpTest(ZulipTestCase):
f"A new member ({self.nonreg_email('test')}) was unable to join your organization because all Zulip", f"A new member ({self.nonreg_email('test')}) was unable to join your organization because all Zulip",
last_message.content, last_message.content,
) )
self.assertEqual(last_message.recipient.type_id, denmark_stream.id) self.assertEqual(
set(get_huddle_user_ids(last_message.recipient)),
expected_group_direct_message_user_ids,
)
ledger.licenses_at_next_renewal = 50 ledger.licenses_at_next_renewal = 50
ledger.save(update_fields=["licenses_at_next_renewal"]) ledger.save(update_fields=["licenses_at_next_renewal"])