feat: onboarding'de diğer KovakSoft uygulamalarından workspace içe aktarma

This commit is contained in:
egecankomur
2026-05-06 23:04:26 +03:00
parent 9f462c2f1f
commit 95a7cbaf0d
4 changed files with 144 additions and 27 deletions
@@ -1,21 +1,30 @@
"use client"; "use client";
import { useActionState } from "react"; import { useActionState } from "react";
import { Building2, Loader2, ShieldCheck } from "lucide-react"; import { Building2, Loader2, ShieldCheck, ArrowRight } from "lucide-react";
import { Button } from "@/components/ui/button"; import { Button } from "@/components/ui/button";
import { Card, CardContent, CardDescription, CardHeader, CardTitle } from "@/components/ui/card"; import { Card, CardContent, CardDescription, CardHeader, CardTitle } from "@/components/ui/card";
import { Input } from "@/components/ui/input"; import { Input } from "@/components/ui/input";
import { Label } from "@/components/ui/label"; import { Label } from "@/components/ui/label";
import { Logo } from "@/components/logo"; import { Logo } from "@/components/logo";
import { createWorkspaceAction } from "@/lib/appwrite/tenant-actions"; import { createWorkspaceAction, importWorkspaceAction } from "@/lib/appwrite/tenant-actions";
import { initialWorkspaceState } from "@/lib/appwrite/tenant-types"; import { initialWorkspaceState } from "@/lib/appwrite/tenant-types";
export function CreateWorkspaceForm({ userName }: { userName?: string }) { interface Props {
userName?: string;
crossAppTeams?: Array<{ $id: string; name: string }>;
}
export function CreateWorkspaceForm({ userName, crossAppTeams = [] }: Props) {
const [state, formAction, isPending] = useActionState( const [state, formAction, isPending] = useActionState(
createWorkspaceAction, createWorkspaceAction,
initialWorkspaceState, initialWorkspaceState,
); );
const [, importFormAction, isImporting] = useActionState(
importWorkspaceAction,
initialWorkspaceState,
);
return ( return (
<div className="flex flex-col gap-6"> <div className="flex flex-col gap-6">
@@ -23,9 +32,54 @@ export function CreateWorkspaceForm({ userName }: { userName?: string }) {
<div className="bg-primary text-primary-foreground flex size-9 items-center justify-center rounded-md"> <div className="bg-primary text-primary-foreground flex size-9 items-center justify-center rounded-md">
<Logo size={22} /> <Logo size={22} />
</div> </div>
<span className="text-xl font-semibold">İşletmem</span> <span className="text-xl font-semibold">Kovak Emlak CRM</span>
</div> </div>
{/* Cross-app import section */}
{crossAppTeams.length > 0 && (
<Card className="border-primary/20 bg-primary/5">
<CardHeader className="pb-3">
<CardTitle className="text-base">Mevcut şirketinizi ekleyin</CardTitle>
<CardDescription className="text-xs">
Diğer bir KovakSoft uygulamasında kayıtlı şirketinizi tek tıkla buraya ekleyebilirsiniz.
</CardDescription>
</CardHeader>
<CardContent className="flex flex-col gap-2">
{crossAppTeams.map((team) => (
<form key={team.$id} action={importFormAction}>
<input type="hidden" name="teamId" value={team.$id} />
<button
type="submit"
disabled={isImporting}
className="flex w-full items-center justify-between rounded-lg border bg-background px-4 py-3 text-sm font-medium transition-colors hover:bg-accent hover:text-accent-foreground disabled:opacity-60"
>
<div className="flex items-center gap-2.5">
<Building2 className="size-4 text-muted-foreground" />
<span>{team.name}</span>
</div>
{isImporting ? (
<Loader2 className="size-4 animate-spin text-muted-foreground" />
) : (
<ArrowRight className="size-4 text-muted-foreground" />
)}
</button>
</form>
))}
</CardContent>
</Card>
)}
{crossAppTeams.length > 0 && (
<div className="relative">
<div className="absolute inset-0 flex items-center">
<span className="w-full border-t" />
</div>
<div className="relative flex justify-center text-xs uppercase">
<span className="bg-muted px-2 text-muted-foreground">veya yeni oluştur</span>
</div>
</div>
)}
<Card> <Card>
<CardHeader className="text-center"> <CardHeader className="text-center">
<div className="bg-primary/10 text-primary mx-auto mb-2 flex size-12 items-center justify-center rounded-full"> <div className="bg-primary/10 text-primary mx-auto mb-2 flex size-12 items-center justify-center rounded-full">
@@ -35,37 +89,24 @@ export function CreateWorkspaceForm({ userName }: { userName?: string }) {
{userName ? `Hoş geldiniz, ${userName}` : "Çalışma alanı oluştur"} {userName ? `Hoş geldiniz, ${userName}` : "Çalışma alanı oluştur"}
</CardTitle> </CardTitle>
<CardDescription> <CardDescription>
Şirketinizin bilgilerini girin birkaç saniyede çalışma alanınız hazır. Ofis bilgilerini girin birkaç saniyede çalışma alanınız hazır.
</CardDescription> </CardDescription>
</CardHeader> </CardHeader>
<CardContent> <CardContent>
<form action={formAction} className="flex flex-col gap-5"> <form action={formAction} className="flex flex-col gap-5">
<div className="grid gap-3"> <div className="grid gap-3">
<Label htmlFor="companyName">Şirket adı *</Label> <Label htmlFor="companyName">Ofis / şirket adı *</Label>
<Input <Input
id="companyName" id="companyName"
name="companyName" name="companyName"
type="text" type="text"
placeholder="KovakSoft Yazılım Ltd." placeholder="Kovak Emlak Ofisi"
autoComplete="organization" autoComplete="organization"
required required
autoFocus autoFocus
/> />
</div> </div>
<div className="grid gap-3">
<Label htmlFor="companyTaxId">
Vergi numarası <span className="text-muted-foreground text-xs">(opsiyonel)</span>
</Label>
<Input
id="companyTaxId"
name="companyTaxId"
type="text"
placeholder="1234567890"
inputMode="numeric"
/>
</div>
<div className="grid gap-3"> <div className="grid gap-3">
<Label htmlFor="companyPhone"> <Label htmlFor="companyPhone">
Telefon <span className="text-muted-foreground text-xs">(opsiyonel)</span> Telefon <span className="text-muted-foreground text-xs">(opsiyonel)</span>
+10 -7
View File
@@ -2,12 +2,12 @@ import type { Metadata } from "next";
import { redirect } from "next/navigation"; import { redirect } from "next/navigation";
import { getCurrentUser } from "@/lib/appwrite/server"; import { getCurrentUser } from "@/lib/appwrite/server";
import { getUserTeams } from "@/lib/appwrite/tenant"; import { getUserTeams, getCrossAppTeams } from "@/lib/appwrite/tenant";
import { CreateWorkspaceForm } from "./components/create-workspace-form"; import { CreateWorkspaceForm } from "./components/create-workspace-form";
export const metadata: Metadata = { export const metadata: Metadata = {
title: "İşletmem — Çalışma alanı oluştur", title: "Kovak Emlak CRM — Çalışma alanı oluştur",
description: "İşletmem için ilk çalışma alanınızı kurun.", description: "Kovak Emlak CRM için ilk çalışma alanınızı kurun.",
}; };
export default async function OnboardingPage() { export default async function OnboardingPage() {
@@ -15,14 +15,17 @@ export default async function OnboardingPage() {
if (!user) redirect("/sign-in"); if (!user) redirect("/sign-in");
const teams = await getUserTeams(); const teams = await getUserTeams();
if (teams && teams.total > 0) { if (teams && teams.total > 0) redirect("/dashboard");
redirect("/dashboard");
} const crossAppTeams = await getCrossAppTeams();
return ( return (
<div className="bg-muted flex min-h-svh flex-col items-center justify-center p-6 md:p-10"> <div className="bg-muted flex min-h-svh flex-col items-center justify-center p-6 md:p-10">
<div className="w-full max-w-md"> <div className="w-full max-w-md">
<CreateWorkspaceForm userName={user.name?.split(" ")[0]} /> <CreateWorkspaceForm
userName={user.name?.split(" ")[0]}
crossAppTeams={crossAppTeams}
/>
</div> </div>
</div> </div>
); );
+50
View File
@@ -86,6 +86,56 @@ export async function createWorkspaceAction(
redirect("/dashboard"); 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) { export async function setActiveTenantAction(tenantId: string) {
try { try {
const { account, teams, tablesDB } = await createSessionClient(); const { account, teams, tablesDB } = await createSessionClient();
+23
View File
@@ -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> { export async function getActiveTenantId(): Promise<string | null> {
const cookie = (await cookies()).get(ACTIVE_TENANT_COOKIE)?.value; const cookie = (await cookies()).get(ACTIVE_TENANT_COOKIE)?.value;
if (cookie) return cookie; if (cookie) return cookie;