zilencer: Use early return pattern for readability.

This commit is contained in:
Tim Abbott 2024-02-21 10:45:43 -08:00
parent ea863bab5b
commit bd0b5d80ba
1 changed files with 60 additions and 58 deletions

View File

@ -967,68 +967,70 @@ def handle_customer_migration_from_server_to_realm(
event_time = timezone_now()
remote_realm_audit_logs = []
if len(realm_uuids) == 1:
# Here, we have exactly one non-system-bot realm, and some
# sort of plan on the server; move it to the realm.
remote_realm = RemoteRealm.objects.get(uuid=realm_uuids[0], server=server)
remote_realm_customer = get_customer_by_remote_realm(remote_realm)
if len(realm_uuids) != 1:
return
# Migrate customer from server to remote realm if there is only one realm.
if remote_realm_customer is None:
# In this case the migration is easy, since we can just move the customer
# object directly.
server_customer.remote_realm = remote_realm
server_customer.remote_server = None
server_customer.save(update_fields=["remote_realm", "remote_server"])
# Here, we have exactly one non-system-bot realm, and some
# sort of plan on the server; move it to the realm.
remote_realm = RemoteRealm.objects.get(uuid=realm_uuids[0], server=server)
remote_realm_customer = get_customer_by_remote_realm(remote_realm)
# Migrate customer from server to remote realm if there is only one realm.
if remote_realm_customer is None:
# In this case the migration is easy, since we can just move the customer
# object directly.
server_customer.remote_realm = remote_realm
server_customer.remote_server = None
server_customer.save(update_fields=["remote_realm", "remote_server"])
else:
# If there's a Customer object for the realm already, things are harder,
# because it's an unusual state and there may be a plan already active
# for the realm, or there may have been.
# In the simplest case, where the realm doesn't have an active plan and the
# server's plan state can easily be moved, we proceed with the migrations.
remote_realm_plan = get_current_plan_by_customer(remote_realm_customer)
if (
remote_realm_plan is None
and server_plan.status != CustomerPlan.SWITCH_PLAN_TIER_AT_PLAN_END
):
# This is a simple case where we don't have to worry about the realm already
# having an active plan, or the server having a next plan scheduled that we'd need
# to figure out how to migrate correctly as well.
# Any other case is too complex to handle here, and should be handled manually,
# especially since that should be extremely rare.
server_plan.customer = remote_realm_customer
server_plan.save(update_fields=["customer"])
else:
# If there's a Customer object for the realm already, things are harder,
# because it's an unusual state and there may be a plan already active
# for the realm, or there may have been.
# In the simplest case, where the realm doesn't have an active plan and the
# server's plan state can easily be moved, we proceed with the migrations.
remote_realm_plan = get_current_plan_by_customer(remote_realm_customer)
if (
remote_realm_plan is None
and server_plan.status != CustomerPlan.SWITCH_PLAN_TIER_AT_PLAN_END
):
# This is a simple case where we don't have to worry about the realm already
# having an active plan, or the server having a next plan scheduled that we'd need
# to figure out how to migrate correctly as well.
# Any other case is too complex to handle here, and should be handled manually,
# especially since that should be extremely rare.
server_plan.customer = remote_realm_customer
server_plan.save(update_fields=["customer"])
else:
logger.warning(
"Failed to migrate customer from server (id: %s) to realm (id: %s): RemoteRealm customer already exists "
"and plans can't be migrated automatically.",
server.id,
remote_realm.id,
)
raise JsonableError(
_(
"Couldn't reconcile billing data between server and realm. Please contact {support_email}"
).format(support_email=FromAddress.SUPPORT)
)
# TODO: Might be better to call do_change_plan_type here.
remote_realm.plan_type = server.plan_type
remote_realm.save(update_fields=["plan_type"])
server.plan_type = RemoteZulipServer.PLAN_TYPE_SELF_MANAGED
server.save(update_fields=["plan_type"])
remote_realm_audit_logs.append(
RemoteRealmAuditLog(
server=server,
remote_realm=remote_realm,
event_type=RemoteRealmAuditLog.REMOTE_PLAN_TRANSFERRED_SERVER_TO_REALM,
event_time=event_time,
extra_data={
"attr_name": "plan_type",
"old_value": RemoteRealm.PLAN_TYPE_SELF_MANAGED,
"new_value": remote_realm.plan_type,
},
logger.warning(
"Failed to migrate customer from server (id: %s) to realm (id: %s): RemoteRealm customer already exists "
"and plans can't be migrated automatically.",
server.id,
remote_realm.id,
)
raise JsonableError(
_(
"Couldn't reconcile billing data between server and realm. Please contact {support_email}"
).format(support_email=FromAddress.SUPPORT)
)
# TODO: Might be better to call do_change_plan_type here.
remote_realm.plan_type = server.plan_type
remote_realm.save(update_fields=["plan_type"])
server.plan_type = RemoteZulipServer.PLAN_TYPE_SELF_MANAGED
server.save(update_fields=["plan_type"])
remote_realm_audit_logs.append(
RemoteRealmAuditLog(
server=server,
remote_realm=remote_realm,
event_type=RemoteRealmAuditLog.REMOTE_PLAN_TRANSFERRED_SERVER_TO_REALM,
event_time=event_time,
extra_data={
"attr_name": "plan_type",
"old_value": RemoteRealm.PLAN_TYPE_SELF_MANAGED,
"new_value": remote_realm.plan_type,
},
)
)
RemoteRealmAuditLog.objects.bulk_create(remote_realm_audit_logs)