fix(api): ensure enterprise workspace join occurs on account registration failure

This commit is contained in:
L1nSn0w 2026-03-04 12:13:31 +08:00
parent d94af41f07
commit bf5a327156
2 changed files with 9 additions and 13 deletions

View File

@ -297,18 +297,15 @@ class AccountService:
email=email, name=name, interface_language=interface_language, password=password
)
workspace_creation_error: Exception | None = None
try:
TenantService.create_owner_tenant_if_not_exist(account=account)
except Exception as e: # noqa: BLE001
workspace_creation_error = e
except Exception:
# Enterprise-only side-effect should run independently from personal workspace creation.
_try_join_enterprise_default_workspace(str(account.id))
raise
# Enterprise-only side-effect should run independently from personal workspace creation.
_try_join_enterprise_default_workspace(str(account.id))
if workspace_creation_error is not None:
raise workspace_creation_error
return account
@staticmethod
@ -1363,7 +1360,6 @@ class RegisterService:
db.session.begin_nested()
"""Register account"""
try:
workspace_creation_error: Exception | None = None
account = AccountService.create_account(
email=email,
name=name,
@ -1387,15 +1383,13 @@ class RegisterService:
TenantService.create_tenant_member(tenant, account, role="owner")
account.current_tenant = tenant
tenant_was_created.send(tenant)
except Exception as e: # noqa: BLE001
workspace_creation_error = e
except Exception:
_try_join_enterprise_default_workspace(str(account.id))
raise
db.session.commit()
_try_join_enterprise_default_workspace(str(account.id))
if workspace_creation_error is not None:
raise workspace_creation_error
except WorkSpaceNotAllowedCreateError:
db.session.rollback()
logger.exception("Register failed")

View File

@ -1178,6 +1178,7 @@ class TestRegisterService:
)
mock_join_default_workspace.assert_called_once_with(str(mock_account.id))
mock_db_dependencies["db"].session.commit.assert_not_called()
def test_register_still_calls_default_workspace_join_when_workspace_limit_exceeded(
self, mock_db_dependencies, mock_external_service_dependencies, monkeypatch
@ -1216,6 +1217,7 @@ class TestRegisterService:
)
mock_join_default_workspace.assert_called_once_with(str(mock_account.id))
mock_db_dependencies["db"].session.commit.assert_not_called()
def test_register_with_oauth(self, mock_db_dependencies, mock_external_service_dependencies):
"""Test account registration with OAuth integration."""