From 19a0e2b11f1233625026fc3b72e1aab6870c4d20 Mon Sep 17 00:00:00 2001 From: kovakmedya Date: Thu, 30 Apr 2026 03:27:49 +0300 Subject: [PATCH] fix(onboarding): drop createdBy from tenant_settings write + atomic rollback Two fixes triggered by user-reported error 'Invalid document structure: Unknown attribute createdBy' during onboarding: 1) tenant_settings has no createdBy column by design (one row per tenant, creator metadata is redundant). Removed createdBy from the row payload. 2) Made the action atomic: if any step after teams.create fails (row write, prefs, cookie), delete the just-created team using the admin client. Without this, two failed attempts left two orphan teams; reload then redirected the user to /dashboard with no tenant_settings, trapping them. Already cleaned up the two orphan teams via Appwrite MCP. --- src/lib/appwrite/tenant-actions.ts | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/src/lib/appwrite/tenant-actions.ts b/src/lib/appwrite/tenant-actions.ts index d44311e..fdf5630 100644 --- a/src/lib/appwrite/tenant-actions.ts +++ b/src/lib/appwrite/tenant-actions.ts @@ -39,7 +39,8 @@ export async function createWorkspaceAction( return { ok: false, error: "Şirket adı zorunlu." }; } - let teamId: string; + let teamId: string | null = null; + const admin = createAdminClient(); try { const session = await createSessionClient(); @@ -48,7 +49,6 @@ export async function createWorkspaceAction( const team = await session.teams.create(ID.unique(), companyName, ["owner"]); teamId = team.$id; - const admin = createAdminClient(); await admin.tablesDB.createRow( DATABASE_ID, TABLES.tenantSettings, @@ -58,7 +58,6 @@ export async function createWorkspaceAction( companyName, companyTaxId, companyPhone, - createdBy: user.$id, }, [ Permission.read(Role.team(teamId)), @@ -75,6 +74,13 @@ export async function createWorkspaceAction( await setActiveTenantCookie(teamId); } catch (e) { + if (teamId) { + try { + await admin.teams.delete(teamId); + } catch { + // best-effort cleanup; original error is what we surface + } + } return { ok: false, error: appwriteError(e) }; }