def do_change_plan_type(realm: Realm, plan_type: int) -> None:
    old_value = realm.plan_type
    realm.plan_type = plan_type
    realm.save(update_fields=['plan_type'])
    RealmAuditLog.objects.create(event_type=RealmAuditLog.REALM_PLAN_TYPE_CHANGED, realm=realm, event_time=timezone_now(), extra_data={'old_value': old_value, 'new_value': plan_type})
    if (plan_type == Realm.STANDARD):
        realm.max_invites = Realm.INVITES_STANDARD_REALM_DAILY_MAX
        realm.message_visibility_limit = None
        realm.upload_quota_gb = Realm.UPLOAD_QUOTA_STANDARD
    elif (plan_type == Realm.SELF_HOSTED):
        realm.max_invites = None
        realm.message_visibility_limit = None
        realm.upload_quota_gb = None
    elif (plan_type == Realm.STANDARD_FREE):
        realm.max_invites = Realm.INVITES_STANDARD_REALM_DAILY_MAX
        realm.message_visibility_limit = None
        realm.upload_quota_gb = Realm.UPLOAD_QUOTA_STANDARD
    elif (plan_type == Realm.LIMITED):
        realm.max_invites = settings.INVITES_DEFAULT_REALM_DAILY_MAX
        realm.message_visibility_limit = Realm.MESSAGE_VISIBILITY_LIMITED
        realm.upload_quota_gb = Realm.UPLOAD_QUOTA_LIMITED
    else:
        raise AssertionError('Invalid plan type')
    update_first_visible_message_id(realm)
    realm.save(update_fields=['_max_invites', 'message_visibility_limit', 'upload_quota_gb'])
    event = {'type': 'realm', 'op': 'update', 'property': 'plan_type', 'value': plan_type, 'extra_data': {'upload_quota': realm.upload_quota_bytes()}}
    send_event(realm, event, active_user_ids(realm.id))
def do_change_plan_type(realm: Realm, plan_type: int) -> None:
    old_value = realm.plan_type
    realm.plan_type = plan_type
    realm.save(update_fields=['plan_type'])
    RealmAuditLog.objects.create(event_type=RealmAuditLog.REALM_PLAN_TYPE_CHANGED, realm=realm, event_time=timezone_now(), extra_data={'old_value': old_value, 'new_value': plan_type})
    if (plan_type == Realm.STANDARD):
        realm.max_invites = Realm.INVITES_STANDARD_REALM_DAILY_MAX
        realm.message_visibility_limit = None
        realm.upload_quota_gb = Realm.UPLOAD_QUOTA_STANDARD
    elif (plan_type == Realm.SELF_HOSTED):
        realm.max_invites = None
        realm.message_visibility_limit = None
        realm.upload_quota_gb = None
    elif (plan_type == Realm.STANDARD_FREE):
        realm.max_invites = Realm.INVITES_STANDARD_REALM_DAILY_MAX
        realm.message_visibility_limit = None
        realm.upload_quota_gb = Realm.UPLOAD_QUOTA_STANDARD
    elif (plan_type == Realm.LIMITED):
        realm.max_invites = settings.INVITES_DEFAULT_REALM_DAILY_MAX
        realm.message_visibility_limit = Realm.MESSAGE_VISIBILITY_LIMITED
        realm.upload_quota_gb = Realm.UPLOAD_QUOTA_LIMITED
    else:
        raise AssertionError('Invalid plan type')
    update_first_visible_message_id(realm)
    realm.save(update_fields=['_max_invites', 'message_visibility_limit', 'upload_quota_gb'])
    event = {'type': 'realm', 'op': 'update', 'property': 'plan_type', 'value': plan_type, 'extra_data': {'upload_quota': realm.upload_quota_bytes()}}
    send_event(realm, event, active_user_ids(realm.id))