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:
@@ -39,7 +39,8 @@ export async function createWorkspaceAction(
|
|||||||
return { ok: false, error: "Şirket adı zorunlu." };
|
return { ok: false, error: "Şirket adı zorunlu." };
|
||||||
}
|
}
|
||||||
|
|
||||||
let teamId: string;
|
let teamId: string | null = null;
|
||||||
|
const admin = createAdminClient();
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const session = await createSessionClient();
|
const session = await createSessionClient();
|
||||||
@@ -48,7 +49,6 @@ export async function createWorkspaceAction(
|
|||||||
const team = await session.teams.create(ID.unique(), companyName, ["owner"]);
|
const team = await session.teams.create(ID.unique(), companyName, ["owner"]);
|
||||||
teamId = team.$id;
|
teamId = team.$id;
|
||||||
|
|
||||||
const admin = createAdminClient();
|
|
||||||
await admin.tablesDB.createRow(
|
await admin.tablesDB.createRow(
|
||||||
DATABASE_ID,
|
DATABASE_ID,
|
||||||
TABLES.tenantSettings,
|
TABLES.tenantSettings,
|
||||||
@@ -58,7 +58,6 @@ export async function createWorkspaceAction(
|
|||||||
companyName,
|
companyName,
|
||||||
companyTaxId,
|
companyTaxId,
|
||||||
companyPhone,
|
companyPhone,
|
||||||
createdBy: user.$id,
|
|
||||||
},
|
},
|
||||||
[
|
[
|
||||||
Permission.read(Role.team(teamId)),
|
Permission.read(Role.team(teamId)),
|
||||||
@@ -75,6 +74,13 @@ export async function createWorkspaceAction(
|
|||||||
|
|
||||||
await setActiveTenantCookie(teamId);
|
await setActiveTenantCookie(teamId);
|
||||||
} catch (e) {
|
} 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) };
|
return { ok: false, error: appwriteError(e) };
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user