Commit 653259c8 authored by crazywoola's avatar crazywoola

feat: add db transaction

parent 7e87a528
...@@ -24,6 +24,7 @@ from services.errors.account import (AccountAlreadyInTenantError, AccountLoginEr ...@@ -24,6 +24,7 @@ from services.errors.account import (AccountAlreadyInTenantError, AccountLoginEr
from sqlalchemy import func from sqlalchemy import func
from tasks.mail_invite_member_task import send_invite_member_mail_task from tasks.mail_invite_member_task import send_invite_member_mail_task
from werkzeug.exceptions import Forbidden from werkzeug.exceptions import Forbidden
from sqlalchemy import exc
def _create_tenant_for_account(account) -> Tenant: def _create_tenant_for_account(account) -> Tenant:
...@@ -257,18 +258,23 @@ class TenantService: ...@@ -257,18 +258,23 @@ 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"""
tenant_account_join = TenantAccountJoin.query.filter_by(account_id=account.id, tenant_id=tenant_id).first() with db.session.begin():
TenantAccountJoin.query.filter_by(account_id=account.id).update({'current': False}) try:
tenant_account_join = TenantAccountJoin.query.filter_by(account_id=account.id, tenant_id=tenant_id).first()
# Check if the tenant exists and the account is a member of the tenant TenantAccountJoin.query.filter_by(account_id=account.id).update({'current': False})
if not tenant_account_join:
raise AccountNotLinkTenantError("Tenant not found or account is not a member of the tenant.") # Check if the tenant exists and the account is a member of the tenant
else: if not tenant_account_join:
tenant_account_join.current = True raise AccountNotLinkTenantError("Tenant not found or account is not a member of the tenant.")
db.session.commit() else:
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]:
......
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