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.
This commit is contained in:
kovakmedya
2026-04-30 03:27:49 +03:00
parent 30cbb8f1be
commit 19a0e2b11f
+9 -3
View File
@@ -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) };
}