feat: onboarding'de diğer KovakSoft uygulamalarından workspace içe aktarma
This commit is contained in:
@@ -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">
|
||||||
@@ -26,6 +35,51 @@ export function CreateWorkspaceForm({ userName }: { userName?: string }) {
|
|||||||
<span className="text-xl font-semibold">İşletmem</span>
|
<span className="text-xl font-semibold">İşletmem</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">
|
||||||
|
|||||||
@@ -2,7 +2,7 @@ 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 = {
|
||||||
@@ -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>
|
||||||
);
|
);
|
||||||
|
|||||||
@@ -89,6 +89,59 @@ 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,
|
||||||
|
companyName: team.name,
|
||||||
|
plan: "free",
|
||||||
|
planStartedAt: new Date().toISOString(),
|
||||||
|
defaultVatRate: 20,
|
||||||
|
invoicePrefix: "INV",
|
||||||
|
invoiceCounter: 0,
|
||||||
|
},
|
||||||
|
[
|
||||||
|
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();
|
||||||
|
|||||||
@@ -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;
|
||||||
|
|||||||
Reference in New Issue
Block a user