fix: switch tenant (#2363)

This commit is contained in:
crazywoola 2024-02-02 21:44:35 +08:00 committed by GitHub
parent f95839c785
commit 3f0c515355
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -258,20 +258,19 @@ class TenantService:
def switch_tenant(account: Account, tenant_id: int = None) -> None: def switch_tenant(account: Account, tenant_id: int = None) -> None:
"""Switch the current workspace for the account""" """Switch the current workspace for the account"""
# Ensure tenant_id is provided
if tenant_id is None:
raise ValueError("Tenant ID must be provided.")
tenant_account_join = TenantAccountJoin.query.filter_by(account_id=account.id, tenant_id=tenant_id).first() tenant_account_join = TenantAccountJoin.query.filter_by(account_id=account.id, tenant_id=tenant_id).first()
if not tenant_account_join: if not tenant_account_join:
raise AccountNotLinkTenantError("Tenant not found or account is not a member of the tenant.") raise AccountNotLinkTenantError("Tenant not found or account is not a member of the tenant.")
else: else:
with db.session.begin(): TenantAccountJoin.query.filter(TenantAccountJoin.account_id == account.id, TenantAccountJoin.tenant_id != tenant_id).update({'current': False})
try: tenant_account_join.current = True
TenantAccountJoin.query.filter_by(account_id=account.id).update({'current': False}) db.session.commit()
tenant_account_join.current = True # Set the current tenant for the account
db.session.commit() account.current_tenant_id = tenant_account_join.tenant_id
# Set the current tenant for the account
account.current_tenant_id = tenant_account_join.tenant_id
except exc.SQLAlchemyError:
db.session.rollback()
raise
@staticmethod @staticmethod
def get_tenant_members(tenant: Tenant) -> List[Account]: def get_tenant_members(tenant: Tenant) -> List[Account]: