Skip to content

Commit

Permalink
fix: db session error
Browse files Browse the repository at this point in the history
  • Loading branch information
GareArc committed Dec 21, 2024
1 parent 6d6d161 commit c811260
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 28 deletions.
5 changes: 2 additions & 3 deletions api/controllers/console/workspace/account.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
from services.account_service import AccountService
from services.errors.account import \
CurrentPasswordIncorrectError as ServiceCurrentPasswordIncorrectError
from services.errors.account import RateLimitExceededError


class AccountInitApi(Resource):
Expand Down Expand Up @@ -254,8 +253,8 @@ def get(self):
try:
token, code = AccountService.generate_account_deletion_verification_code(account)
AccountService.send_account_delete_verification_email(account, code)
except RateLimitExceededError:
return {"result": "fail", "error": "Rate limit exceeded."}, 429
except Exception as e:
return {"result": "fail", "error": str(e)}, 429

return {"result": "success", "data": token}

Expand Down
1 change: 0 additions & 1 deletion api/services/account_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,6 @@ def generate_account_deletion_verification_code(account: Account) -> tuple[str,
token = TokenManager.generate_token(
account=account, token_type="account_deletion", additional_data={"code": code}
)
logging.info(f"Account {account.id} generated account deletion verification code {code} with token {token}")
return token, code

@classmethod
Expand Down
50 changes: 26 additions & 24 deletions api/tasks/delete_account_task.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,36 +25,38 @@ def delete_account_task(account_id, reason: str):
tenant_account_joins = db.session.query(TenantAccountJoin).filter(TenantAccountJoin.account_id == account.id).all()
for ta in tenant_account_joins:
try:
with db.session.begin():
if ta.role == TenantAccountJoinRole.OWNER:
# dismiss all members of the tenant
members = db.session.query(TenantAccountJoin).filter(TenantAccountJoin.tenant_id == ta.tenant_id).delete()
logging.info(f"Dismissed {members} members from tenant {ta.tenant_id}.")
if ta.role == TenantAccountJoinRole.OWNER:
# dismiss all members of the tenant
members = db.session.query(TenantAccountJoin).filter(TenantAccountJoin.tenant_id == ta.tenant_id).delete()
logging.info(f"Dismissed {members} members from tenant {ta.tenant_id}.")

# delete the tenant
db.session.query(Tenant).filter(Tenant.id == ta.tenant_id).delete()
logging.info(f"Deleted tenant {ta.tenant_id}.")
# delete the tenant
db.session.query(Tenant).filter(Tenant.id == ta.tenant_id).delete()
logging.info(f"Deleted tenant {ta.tenant_id}.")

# delete subscription
try:
BillingService.delete_tenant_customer(ta.tenant_id)
except Exception as e:
logging.error(f"Failed to delete subscription for tenant {ta.tenant_id}: {e}.")
raise
else:
# remove the account from tenant
db.session.delete(ta)
logging.info(f"Removed account {account.email} from tenant {ta.tenant_id}.")
# delete subscription
try:
BillingService.delete_tenant_customer(ta.tenant_id)
except Exception as e:
logging.error(f"Failed to delete subscription for tenant {ta.tenant_id}: {e}.")
raise
else:
# remove the account from tenant
db.session.delete(ta)
logging.info(f"Removed account {account.email} from tenant {ta.tenant_id}.")

# delete the account
db.session.delete(account)
# delete the account
db.session.delete(account)

# prepare account deletion log
account_deletion_log = AccountDeletionLogService.create_account_deletion_log(account.email, reason)
db.session.add(account_deletion_log)
# prepare account deletion log
account_deletion_log = AccountDeletionLogService.create_account_deletion_log(account.email, reason)
db.session.add(account_deletion_log)

db.session.commit()

except Exception as e:
logging.error(f"Failed to delete account {account.email}.")
db.session.rollback()
logging.error(f"Failed to delete account {account.email}: {e}.")
send_deletion_fail_task.delay(account.interface_language, account.email)
return

Expand Down

0 comments on commit c811260

Please sign in to comment.