import type { Metadata } from "next"; import { redirect } from "next/navigation"; import { listLeads } from "@/lib/appwrite/lead-queries"; import { createAdminClient } from "@/lib/appwrite/server"; import { requireTenant } from "@/lib/appwrite/tenant-guard"; import { LeadsBoard } from "./components/leads-board"; import type { LeadRow } from "./components/types"; export const metadata: Metadata = { title: "İşletmem — Müşteri Adayları", }; export default async function LeadsPage() { let ctx; try { ctx = await requireTenant(); } catch { redirect("/onboarding"); } const { teams } = createAdminClient(); const [leads, membershipsResult] = await Promise.all([ listLeads(ctx.tenantId), teams.listMemberships(ctx.tenantId).catch(() => ({ memberships: [] })), ]); const memberMap = new Map( membershipsResult.memberships .filter((m) => m.confirm) .map((m) => [m.userId, m.userName || m.userEmail]), ); const members = membershipsResult.memberships .filter((m) => m.confirm) .map((m) => ({ id: m.userId, name: m.userName || m.userEmail, email: m.userEmail })); const leadRows: LeadRow[] = leads.map((l) => ({ id: l.$id, name: l.name, contactName: l.contactName ?? "", email: l.email ?? "", phone: l.phone ?? "", source: l.source ?? "other", status: l.status ?? "cold", estimatedValue: l.estimatedValue ?? null, currency: l.currency ?? "TRY", notes: l.notes ?? "", assigneeId: l.assigneeId ?? "", assigneeName: l.assigneeId ? (memberMap.get(l.assigneeId) ?? "") : "", lastContactAt: l.lastContactAt ?? null, nextFollowUpAt: l.nextFollowUpAt ?? null, calendarEventId: l.calendarEventId ?? null, customerId: l.customerId ?? null, createdAt: l.$createdAt, })); return (
{ctx.settings?.companyName ?? "Çalışma alanı"}
Müşteri adaylarını takip edin, ısıtın ve müşteriye dönüştürün.