feat: onboarding'de diğer KovakSoft uygulamalarından workspace içe aktarma
This commit is contained in:
@@ -86,6 +86,56 @@ export async function createWorkspaceAction(
|
||||
redirect("/dashboard");
|
||||
}
|
||||
|
||||
export async function importWorkspaceAction(
|
||||
_prev: WorkspaceState,
|
||||
formData: FormData,
|
||||
): Promise<WorkspaceState> {
|
||||
const teamId = String(formData.get("teamId") ?? "").trim();
|
||||
if (!teamId) return { ok: false, error: "Geçersiz istek." };
|
||||
|
||||
try {
|
||||
const { account, teams, tablesDB } = await createSessionClient();
|
||||
const user = await account.get();
|
||||
|
||||
const allTeams = await teams.list();
|
||||
const team = allTeams.teams.find((t) => t.$id === teamId);
|
||||
if (!team) return { ok: false, error: "Çalışma alanı bulunamadı." };
|
||||
|
||||
const existing = await tablesDB.listRows({
|
||||
databaseId: DATABASE_ID,
|
||||
tableId: TABLES.tenantSettings,
|
||||
queries: [Query.equal("tenantId", teamId), Query.limit(1)],
|
||||
});
|
||||
if (existing.total > 0) return { ok: false, error: "Bu çalışma alanı zaten mevcut." };
|
||||
|
||||
const admin = createAdminClient();
|
||||
await admin.tablesDB.createRow(
|
||||
DATABASE_ID,
|
||||
TABLES.tenantSettings,
|
||||
ID.unique(),
|
||||
{
|
||||
tenantId: teamId,
|
||||
officeName: team.name,
|
||||
createdBy: user.$id,
|
||||
defaultCurrency: "TRY",
|
||||
},
|
||||
[
|
||||
Permission.read(Role.team(teamId)),
|
||||
Permission.update(Role.team(teamId, "owner")),
|
||||
Permission.update(Role.team(teamId, "admin")),
|
||||
Permission.delete(Role.team(teamId, "owner")),
|
||||
],
|
||||
);
|
||||
|
||||
await account.updatePrefs({ ...(user.prefs ?? {}), activeTenant: teamId });
|
||||
await setActiveTenantCookie(teamId);
|
||||
} catch (e) {
|
||||
return { ok: false, error: appwriteError(e) };
|
||||
}
|
||||
|
||||
redirect("/dashboard");
|
||||
}
|
||||
|
||||
export async function setActiveTenantAction(tenantId: string) {
|
||||
try {
|
||||
const { account, teams, tablesDB } = await createSessionClient();
|
||||
|
||||
@@ -30,6 +30,29 @@ export async function getUserTeams() {
|
||||
}
|
||||
}
|
||||
|
||||
export async function getCrossAppTeams(): Promise<Array<{ $id: string; name: string }>> {
|
||||
try {
|
||||
const { teams, tablesDB } = await createSessionClient();
|
||||
const allTeams = await teams.list();
|
||||
|
||||
if (allTeams.teams.length === 0) return [];
|
||||
|
||||
const teamIds = allTeams.teams.map((t) => t.$id);
|
||||
const settings = await tablesDB.listRows({
|
||||
databaseId: DATABASE_ID,
|
||||
tableId: TABLES.tenantSettings,
|
||||
queries: [Query.equal("tenantId", teamIds), Query.limit(100)],
|
||||
});
|
||||
const thisAppIds = new Set(settings.rows.map((r) => r.tenantId as string));
|
||||
|
||||
return allTeams.teams
|
||||
.filter((t) => !thisAppIds.has(t.$id))
|
||||
.map((t) => ({ $id: t.$id, name: t.name }));
|
||||
} catch {
|
||||
return [];
|
||||
}
|
||||
}
|
||||
|
||||
export async function getActiveTenantId(): Promise<string | null> {
|
||||
const cookie = (await cookies()).get(ACTIVE_TENANT_COOKIE)?.value;
|
||||
if (cookie) return cookie;
|
||||
|
||||
Reference in New Issue
Block a user