Commit eebb38cd authored by crazywoola's avatar crazywoola

feat: add db transaction

parent 653259c8
...@@ -258,23 +258,20 @@ class TenantService: ...@@ -258,23 +258,20 @@ 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"""
with db.session.begin(): tenant_account_join = TenantAccountJoin.query.filter_by(account_id=account.id, tenant_id=tenant_id).first()
try: if not tenant_account_join:
tenant_account_join = TenantAccountJoin.query.filter_by(account_id=account.id, tenant_id=tenant_id).first() raise AccountNotLinkTenantError("Tenant not found or account is not a member of the tenant.")
TenantAccountJoin.query.filter_by(account_id=account.id).update({'current': False}) else:
with db.session.begin():
# Check if the tenant exists and the account is a member of the tenant try:
if not tenant_account_join: TenantAccountJoin.query.filter_by(account_id=account.id).update({'current': False})
raise AccountNotLinkTenantError("Tenant not found or account is not a member of the tenant.")
else:
tenant_account_join.current = True tenant_account_join.current = True
db.session.commit() db.session.commit()
# Set the current tenant for the account
# Set the current tenant for the account account.current_tenant_id = tenant_account_join.tenant_id
account.current_tenant_id = tenant_account_join.tenant_id except exc.SQLAlchemyError:
except exc.SQLAlchemyError: db.session.rollback()
db.session.rollback() raise
raise
@staticmethod @staticmethod
def get_tenant_members(tenant: Tenant) -> List[Account]: def get_tenant_members(tenant: Tenant) -> List[Account]:
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment