feat: onboarding'de diğer KovakSoft uygulamalarından workspace içe aktarma
This commit is contained in:
@@ -1,21 +1,30 @@
|
||||
"use client";
|
||||
|
||||
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 { Card, CardContent, CardDescription, CardHeader, CardTitle } from "@/components/ui/card";
|
||||
import { Input } from "@/components/ui/input";
|
||||
import { Label } from "@/components/ui/label";
|
||||
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";
|
||||
|
||||
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(
|
||||
createWorkspaceAction,
|
||||
initialWorkspaceState,
|
||||
);
|
||||
const [, importFormAction, isImporting] = useActionState(
|
||||
importWorkspaceAction,
|
||||
initialWorkspaceState,
|
||||
);
|
||||
|
||||
return (
|
||||
<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">
|
||||
<Logo size={22} />
|
||||
</div>
|
||||
<span className="text-xl font-semibold">İşletmem</span>
|
||||
<span className="text-xl font-semibold">Kovak Emlak CRM</span>
|
||||
</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>
|
||||
<CardHeader className="text-center">
|
||||
<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"}
|
||||
</CardTitle>
|
||||
<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>
|
||||
</CardHeader>
|
||||
<CardContent>
|
||||
<form action={formAction} className="flex flex-col gap-5">
|
||||
<div className="grid gap-3">
|
||||
<Label htmlFor="companyName">Şirket adı *</Label>
|
||||
<Label htmlFor="companyName">Ofis / şirket adı *</Label>
|
||||
<Input
|
||||
id="companyName"
|
||||
name="companyName"
|
||||
type="text"
|
||||
placeholder="KovakSoft Yazılım Ltd."
|
||||
placeholder="Kovak Emlak Ofisi"
|
||||
autoComplete="organization"
|
||||
required
|
||||
autoFocus
|
||||
/>
|
||||
</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">
|
||||
<Label htmlFor="companyPhone">
|
||||
Telefon <span className="text-muted-foreground text-xs">(opsiyonel)</span>
|
||||
|
||||
@@ -2,12 +2,12 @@ import type { Metadata } from "next";
|
||||
import { redirect } from "next/navigation";
|
||||
|
||||
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";
|
||||
|
||||
export const metadata: Metadata = {
|
||||
title: "İşletmem — Çalışma alanı oluştur",
|
||||
description: "İşletmem için ilk çalışma alanınızı kurun.",
|
||||
title: "Kovak Emlak CRM — Çalışma alanı oluştur",
|
||||
description: "Kovak Emlak CRM için ilk çalışma alanınızı kurun.",
|
||||
};
|
||||
|
||||
export default async function OnboardingPage() {
|
||||
@@ -15,14 +15,17 @@ export default async function OnboardingPage() {
|
||||
if (!user) redirect("/sign-in");
|
||||
|
||||
const teams = await getUserTeams();
|
||||
if (teams && teams.total > 0) {
|
||||
redirect("/dashboard");
|
||||
}
|
||||
if (teams && teams.total > 0) redirect("/dashboard");
|
||||
|
||||
const crossAppTeams = await getCrossAppTeams();
|
||||
|
||||
return (
|
||||
<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">
|
||||
<CreateWorkspaceForm userName={user.name?.split(" ")[0]} />
|
||||
<CreateWorkspaceForm
|
||||
userName={user.name?.split(" ")[0]}
|
||||
crossAppTeams={crossAppTeams}
|
||||
/>
|
||||
</div>
|
||||
</div>
|
||||
);
|
||||
|
||||
@@ -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