billing: Extract helper for getting manual license management plans.

Prep commit for expanding these checks for the number of licenses
set for the next billing period (renewal licenses) and for changing
guest users' roles.
This commit is contained in:
Lauryn Menard 2024-09-19 14:37:02 +02:00 committed by Tim Abbott
parent 137f4fccde
commit 73cb08265c
1 changed files with 23 additions and 14 deletions

View File

@ -2,16 +2,23 @@ from django.conf import settings
from django.utils.translation import gettext as _ 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 CustomerPlan, get_current_plan_by_realm
from zerver.actions.create_user import send_group_direct_message_to_admins 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
def generate_licenses_low_warning_message_if_required(realm: Realm) -> str | None: def get_plan_if_manual_license_management_enforced(realm: Realm) -> CustomerPlan | None:
plan = get_current_plan_by_realm(realm) plan = get_current_plan_by_realm(realm)
if plan is None or plan.automanage_licenses: if plan is None or plan.automanage_licenses or plan.customer.exempt_from_license_number_check:
return None
return plan
def generate_licenses_low_warning_message_if_required(realm: Realm) -> str | None:
plan = get_plan_if_manual_license_management_enforced(realm)
if plan is None:
return None return None
licenses_remaining = plan.licenses() - get_latest_seat_count(realm) licenses_remaining = plan.licenses() - get_latest_seat_count(realm)
@ -67,13 +74,9 @@ def send_user_unable_to_signup_group_direct_message_to_admins(
) )
def check_spare_licenses_available_for_adding_new_users( def check_spare_licenses_available(
realm: Realm, extra_non_guests_count: int = 0, extra_guests_count: int = 0 realm: Realm, plan: CustomerPlan, extra_non_guests_count: int = 0, extra_guests_count: int = 0
) -> None: ) -> None:
plan = get_current_plan_by_realm(realm)
if plan is None or plan.automanage_licenses or plan.customer.exempt_from_license_number_check:
return
if plan.licenses() < get_seat_count( if plan.licenses() < get_seat_count(
realm, extra_non_guests_count=extra_non_guests_count, extra_guests_count=extra_guests_count realm, extra_non_guests_count=extra_non_guests_count, extra_guests_count=extra_guests_count
): ):
@ -85,11 +88,15 @@ def check_spare_licenses_available_for_registering_new_user(
user_email_to_add: str, user_email_to_add: str,
role: int, role: int,
) -> None: ) -> None:
plan = get_plan_if_manual_license_management_enforced(realm)
if plan is None:
return
try: try:
if role == UserProfile.ROLE_GUEST: if role == UserProfile.ROLE_GUEST:
check_spare_licenses_available_for_adding_new_users(realm, extra_guests_count=1) check_spare_licenses_available(realm, plan, extra_guests_count=1)
else: else:
check_spare_licenses_available_for_adding_new_users(realm, extra_non_guests_count=1) check_spare_licenses_available(realm, plan, extra_non_guests_count=1)
except LicenseLimitError: except LicenseLimitError:
send_user_unable_to_signup_group_direct_message_to_admins(realm, user_email_to_add) send_user_unable_to_signup_group_direct_message_to_admins(realm, user_email_to_add)
raise raise
@ -99,10 +106,12 @@ def check_spare_licenses_available_for_inviting_new_users(
realm: Realm, extra_non_guests_count: int = 0, extra_guests_count: int = 0 realm: Realm, extra_non_guests_count: int = 0, extra_guests_count: int = 0
) -> None: ) -> None:
num_invites = extra_non_guests_count + extra_guests_count num_invites = extra_non_guests_count + extra_guests_count
plan = get_plan_if_manual_license_management_enforced(realm)
if plan is None:
return
try: try:
check_spare_licenses_available_for_adding_new_users( check_spare_licenses_available(realm, plan, extra_non_guests_count, extra_guests_count)
realm, extra_non_guests_count, extra_guests_count
)
except LicenseLimitError: except LicenseLimitError:
if num_invites == 1: if num_invites == 1:
message = _("All Zulip licenses for this organization are currently in use.") message = _("All Zulip licenses for this organization are currently in use.")