From 3f0c5153553f1ac680d6239e4ba83f9d26c5ad5b Mon Sep 17 00:00:00 2001 From: crazywoola <100913391+crazywoola@users.noreply.github.com> Date: Fri, 2 Feb 2024 21:44:35 +0800 Subject: [PATCH] fix: switch tenant (#2363) --- api/services/account_service.py | 19 +++++++++---------- 1 file changed, 9 insertions(+), 10 deletions(-) diff --git a/api/services/account_service.py b/api/services/account_service.py index 3d82af2966..0999d700b6 100644 --- a/api/services/account_service.py +++ b/api/services/account_service.py @@ -258,20 +258,19 @@ class TenantService: def switch_tenant(account: Account, tenant_id: int = None) -> None: """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() if not tenant_account_join: raise AccountNotLinkTenantError("Tenant not found or account is not a member of the tenant.") else: - with db.session.begin(): - try: - TenantAccountJoin.query.filter_by(account_id=account.id).update({'current': False}) - tenant_account_join.current = True - db.session.commit() - # Set the current tenant for the account - account.current_tenant_id = tenant_account_join.tenant_id - except exc.SQLAlchemyError: - db.session.rollback() - raise + TenantAccountJoin.query.filter(TenantAccountJoin.account_id == account.id, TenantAccountJoin.tenant_id != tenant_id).update({'current': False}) + tenant_account_join.current = True + db.session.commit() + # Set the current tenant for the account + account.current_tenant_id = tenant_account_join.tenant_id @staticmethod def get_tenant_members(tenant: Tenant) -> List[Account]: