feat: leads module — kanban board, aktiviteler, takvim entegrasyonu, müşteriye dönüştür

This commit is contained in:
kovakmedya
2026-05-01 03:26:31 +03:00
parent 9e1355a137
commit ea3d6f6045
13 changed files with 1590 additions and 0 deletions
@@ -0,0 +1,24 @@
"use server";
import { listLeadActivities } from "@/lib/appwrite/lead-queries";
import { requireTenant } from "@/lib/appwrite/tenant-guard";
import type { ActivityRow } from "./types";
export async function listLeadActivitiesForClient(leadId: string): Promise<ActivityRow[]> {
try {
const ctx = await requireTenant();
const rows = await listLeadActivities(ctx.tenantId, leadId);
return rows.map((a) => ({
id: a.$id,
leadId: a.leadId,
type: a.type,
content: a.content,
calendarEventId: a.calendarEventId ?? null,
occurredAt: a.occurredAt ?? null,
createdAt: a.$createdAt,
createdByName: "",
}));
} catch {
return [];
}
}
@@ -0,0 +1,139 @@
"use client";
import { useSortable } from "@dnd-kit/sortable";
import { CSS } from "@dnd-kit/utilities";
import { Calendar, GripVertical, MoreHorizontal, Pencil, Phone, Trash2, UserCircle } from "lucide-react";
import { Badge } from "@/components/ui/badge";
import { Button } from "@/components/ui/button";
import {
DropdownMenu,
DropdownMenuContent,
DropdownMenuItem,
DropdownMenuSeparator,
DropdownMenuTrigger,
} from "@/components/ui/dropdown-menu";
import { cn } from "@/lib/utils";
import { formatCurrency, formatDate } from "@/lib/format";
import { LEAD_SOURCE_LABEL, LEAD_STATUS_CONFIG, type LeadRow } from "./types";
type Props = {
lead: LeadRow;
onEdit: (lead: LeadRow) => void;
onDetail: (lead: LeadRow) => void;
onDelete: (lead: LeadRow) => void;
isOverlay?: boolean;
};
export function LeadCard({ lead, onEdit, onDetail, onDelete, isOverlay }: Props) {
const { attributes, listeners, setNodeRef, transform, transition, isDragging } = useSortable({
id: lead.id,
data: { type: "lead", status: lead.status },
});
const style = { transform: CSS.Transform.toString(transform), transition };
const cfg = LEAD_STATUS_CONFIG[lead.status];
const isOverdue = lead.nextFollowUpAt && new Date(lead.nextFollowUpAt) < new Date();
return (
<div
ref={setNodeRef}
style={style}
className={cn(
"bg-card group cursor-pointer rounded-lg border p-3 shadow-sm transition-shadow hover:shadow-md",
isDragging && "opacity-30",
isOverlay && "rotate-2 shadow-xl",
)}
onClick={() => onDetail(lead)}
>
<div className="flex items-start gap-2">
<button
{...attributes}
{...listeners}
aria-label="Sürükle"
onClick={(e) => e.stopPropagation()}
className="text-muted-foreground hover:text-foreground mt-0.5 cursor-grab touch-none active:cursor-grabbing"
>
<GripVertical className="size-4" />
</button>
<div className="min-w-0 flex-1">
<div className="flex items-start justify-between gap-2">
<div className="min-w-0">
<h3 className="truncate text-sm font-semibold leading-snug">{lead.name}</h3>
{lead.contactName && (
<p className="text-muted-foreground truncate text-xs">{lead.contactName}</p>
)}
</div>
<DropdownMenu>
<DropdownMenuTrigger asChild>
<Button
variant="ghost"
size="icon"
className="size-7 shrink-0 opacity-0 group-hover:opacity-100"
onClick={(e) => e.stopPropagation()}
>
<MoreHorizontal className="size-3.5" />
</Button>
</DropdownMenuTrigger>
<DropdownMenuContent align="end">
<DropdownMenuItem onClick={(e) => { e.stopPropagation(); onDetail(lead); }}>
<UserCircle className="size-3.5" />
Detay & Aktiviteler
</DropdownMenuItem>
<DropdownMenuItem onClick={(e) => { e.stopPropagation(); onEdit(lead); }}>
<Pencil className="size-3.5" />
Düzenle
</DropdownMenuItem>
<DropdownMenuSeparator />
<DropdownMenuItem variant="destructive" onClick={(e) => { e.stopPropagation(); onDelete(lead); }}>
<Trash2 className="size-3.5" />
Sil
</DropdownMenuItem>
</DropdownMenuContent>
</DropdownMenu>
</div>
<div className="mt-2 flex flex-wrap items-center gap-1.5">
<Badge variant="outline" className={cn("border text-xs", cfg.color, cfg.bg, cfg.border)}>
{LEAD_SOURCE_LABEL[lead.source]}
</Badge>
{lead.estimatedValue != null && lead.estimatedValue > 0 && (
<Badge variant="secondary" className="text-xs font-medium">
{formatCurrency(lead.estimatedValue, lead.currency)}
</Badge>
)}
</div>
<div className="mt-2 flex flex-wrap items-center gap-2 text-xs">
{lead.phone && (
<span className="text-muted-foreground flex items-center gap-1">
<Phone className="size-3" />
{lead.phone}
</span>
)}
{lead.nextFollowUpAt && (
<span className={cn(
"flex items-center gap-1",
isOverdue ? "text-destructive font-medium" : "text-muted-foreground",
)}>
<Calendar className="size-3" />
{formatDate(lead.nextFollowUpAt)}
{isOverdue && " — gecikti"}
</span>
)}
</div>
{lead.assigneeName && (
<div className="mt-1.5 flex items-center gap-1">
<UserCircle className="text-muted-foreground size-3" />
<span className="text-muted-foreground text-xs">{lead.assigneeName}</span>
</div>
)}
</div>
</div>
</div>
);
}
@@ -0,0 +1,269 @@
"use client";
import { useActionState, useEffect, useState, useTransition } from "react";
import {
Calendar, CheckCircle2, ChevronDown, Loader2, Mail, MessageSquarePlus,
Phone, TrendingUp, UserCheck, X,
} from "lucide-react";
import { toast } from "sonner";
import { Badge } from "@/components/ui/badge";
import { Button } from "@/components/ui/button";
import { Input } from "@/components/ui/input";
import { Label } from "@/components/ui/label";
import {
Select, SelectContent, SelectItem, SelectTrigger, SelectValue,
} from "@/components/ui/select";
import {
Sheet, SheetContent, SheetHeader, SheetTitle,
} from "@/components/ui/sheet";
import { Textarea } from "@/components/ui/textarea";
import { cn } from "@/lib/utils";
import { formatCurrency, formatDateTime } from "@/lib/format";
import {
addLeadActivityAction,
scheduleFollowUpAction,
} from "@/lib/appwrite/lead-activity-actions";
import { convertLeadToCustomerAction } from "@/lib/appwrite/lead-actions";
import {
ACTIVITY_TYPE_CONFIG,
LEAD_SOURCE_LABEL,
LEAD_STATUS_CONFIG,
type ActivityRow,
type LeadActivityType,
type LeadRow,
} from "./types";
type Props = {
open: boolean;
onOpenChange: (v: boolean) => void;
lead: LeadRow | null;
activities: ActivityRow[];
onEdit: (lead: LeadRow) => void;
};
const ACTIVITY_TYPES: LeadActivityType[] = ["note", "call", "meeting", "email"];
export function LeadDetailSheet({ open, onOpenChange, lead, activities, onEdit }: Props) {
const [activityState, activityAction, activityPending] = useActionState(
addLeadActivityAction, { ok: false },
);
const [followUpState, followUpAction, followUpPending] = useActionState(
scheduleFollowUpAction, { ok: false },
);
const [convertBusy, startConvert] = useTransition();
const [tab, setTab] = useState<"activities" | "followup">("activities");
const [activityType, setActivityType] = useState<LeadActivityType>("note");
useEffect(() => {
if (activityState.ok) toast.success("Aktivite kaydedildi.");
else if (activityState.error) toast.error(activityState.error);
}, [activityState]);
useEffect(() => {
if (followUpState.ok) { toast.success("Takip takvime eklendi."); setTab("activities"); }
else if (followUpState.error) toast.error(followUpState.error);
}, [followUpState]);
const handleConvert = () => {
if (!lead) return;
startConvert(async () => {
const fd = new FormData();
fd.set("leadId", lead.id);
const result = await convertLeadToCustomerAction(fd);
if (result.ok) {
toast.success("Müşteriye dönüştürüldü.");
onOpenChange(false);
} else {
toast.error(result.error ?? "Dönüştürme başarısız.");
}
});
};
if (!lead) return null;
const cfg = LEAD_STATUS_CONFIG[lead.status];
return (
<Sheet open={open} onOpenChange={onOpenChange}>
<SheetContent className="flex w-full flex-col gap-0 overflow-hidden p-0 sm:max-w-lg">
<SheetHeader className="border-b px-6 py-4">
<div className="flex items-start justify-between gap-3">
<div className="min-w-0">
<SheetTitle className="truncate">{lead.name}</SheetTitle>
{lead.contactName && (
<p className="text-muted-foreground mt-0.5 text-sm">{lead.contactName}</p>
)}
</div>
<Badge variant="outline" className={cn("shrink-0 text-xs", cfg.color, cfg.bg, cfg.border)}>
{cfg.label}
</Badge>
</div>
</SheetHeader>
<div className="flex flex-1 flex-col overflow-hidden">
{/* Info strip */}
<div className="bg-muted/30 flex flex-wrap gap-3 border-b px-6 py-3 text-xs">
{lead.phone && (
<a href={`tel:${lead.phone}`} className="text-muted-foreground hover:text-foreground flex items-center gap-1.5">
<Phone className="size-3.5" /> {lead.phone}
</a>
)}
{lead.email && (
<a href={`mailto:${lead.email}`} className="text-muted-foreground hover:text-foreground flex items-center gap-1.5">
<Mail className="size-3.5" /> {lead.email}
</a>
)}
{lead.estimatedValue != null && lead.estimatedValue > 0 && (
<span className="flex items-center gap-1.5 font-medium">
<TrendingUp className="size-3.5" />
{formatCurrency(lead.estimatedValue, lead.currency)}
</span>
)}
<span className="text-muted-foreground">{LEAD_SOURCE_LABEL[lead.source]}</span>
</div>
{/* Tab bar */}
<div className="flex border-b">
{(["activities", "followup"] as const).map((t) => (
<button
key={t}
type="button"
onClick={() => setTab(t)}
className={cn(
"flex-1 px-4 py-2.5 text-sm font-medium transition-colors",
tab === t
? "border-b-2 border-primary text-foreground"
: "text-muted-foreground hover:text-foreground",
)}
>
{t === "activities" ? "Aktiviteler" : "Takip Planla"}
</button>
))}
</div>
<div className="flex-1 overflow-y-auto">
{tab === "activities" && (
<div className="space-y-4 px-6 py-4">
{/* Add activity form */}
<form action={activityAction} className="space-y-3 rounded-lg border bg-muted/20 p-3">
<input type="hidden" name="leadId" value={lead.id} />
<div className="flex items-center gap-2">
<Select
name="type"
value={activityType}
onValueChange={(v) => setActivityType(v as LeadActivityType)}
>
<SelectTrigger className="h-8 w-[140px] text-xs">
<SelectValue />
</SelectTrigger>
<SelectContent>
{ACTIVITY_TYPES.map((t) => (
<SelectItem key={t} value={t} className="text-xs">
{ACTIVITY_TYPE_CONFIG[t].icon} {ACTIVITY_TYPE_CONFIG[t].label}
</SelectItem>
))}
</SelectContent>
</Select>
<span className="text-muted-foreground text-xs">ekle</span>
</div>
<Textarea name="content" rows={2} placeholder="Notunuzu yazın…" className="text-sm" required />
<div className="flex justify-end">
<Button type="submit" size="sm" disabled={activityPending}>
{activityPending
? <Loader2 className="size-3.5 animate-spin" />
: <MessageSquarePlus className="size-3.5" />}
Kaydet
</Button>
</div>
</form>
{/* Timeline */}
<div className="space-y-3">
{activities.length === 0 && (
<p className="text-muted-foreground py-4 text-center text-sm">Henüz aktivite yok.</p>
)}
{activities.map((a) => {
const acfg = ACTIVITY_TYPE_CONFIG[a.type];
return (
<div key={a.id} className="flex gap-3">
<div className="bg-muted flex size-7 shrink-0 items-center justify-center rounded-full text-sm">
{acfg.icon}
</div>
<div className="min-w-0 flex-1">
<div className="flex items-center gap-2">
<span className="text-xs font-medium">{acfg.label}</span>
<span className="text-muted-foreground text-xs">
{formatDateTime(a.occurredAt ?? a.createdAt)}
</span>
</div>
<p className="mt-0.5 whitespace-pre-wrap text-sm">{a.content}</p>
</div>
</div>
);
})}
</div>
</div>
)}
{tab === "followup" && (
<form action={followUpAction} className="space-y-4 px-6 py-4">
<input type="hidden" name="leadId" value={lead.id} />
<div className="grid gap-2">
<Label htmlFor="followUpAt">Takip tarihi & saati</Label>
<Input
id="followUpAt"
name="followUpAt"
type="datetime-local"
min={new Date().toISOString().slice(0, 16)}
defaultValue={
lead.nextFollowUpAt
? new Date(lead.nextFollowUpAt).toISOString().slice(0, 16)
: ""
}
required
/>
</div>
<div className="grid gap-2">
<Label htmlFor="note">Not (isteğe bağlı)</Label>
<Textarea id="note" name="note" rows={2} placeholder="Görüşme konusu, hatırlatmalar…" />
</div>
<Button type="submit" className="w-full" disabled={followUpPending}>
{followUpPending
? <><Loader2 className="size-4 animate-spin" />Planlanıyor</>
: <><Calendar className="size-4" />Takvime ekle</>}
</Button>
{lead.nextFollowUpAt && (
<p className="text-muted-foreground text-center text-xs">
Mevcut takip: {formatDateTime(lead.nextFollowUpAt)}
</p>
)}
</form>
)}
</div>
{/* Footer actions */}
<div className="flex gap-2 border-t px-6 py-4">
<Button variant="outline" size="sm" onClick={() => onEdit(lead)} className="flex-1">
<ChevronDown className="size-3.5 rotate-90" />
Düzenle
</Button>
{lead.status !== "converted" && lead.status !== "lost" && (
<Button size="sm" onClick={handleConvert} disabled={convertBusy} className="flex-1 bg-green-600 hover:bg-green-700 text-white">
{convertBusy
? <Loader2 className="size-3.5 animate-spin" />
: <UserCheck className="size-3.5" />}
Müşteriye dönüştür
</Button>
)}
{lead.status === "converted" && (
<div className="flex flex-1 items-center justify-center gap-1.5 text-sm text-green-600">
<CheckCircle2 className="size-4" /> Müşteriye dönüştürüldü
</div>
)}
</div>
</div>
</SheetContent>
</Sheet>
);
}
@@ -0,0 +1,199 @@
"use client";
import { useActionState, useEffect, useState } from "react";
import { ChevronDown, Loader2, Save } from "lucide-react";
import { toast } from "sonner";
import { Badge } from "@/components/ui/badge";
import { Button } from "@/components/ui/button";
import { Checkbox } from "@/components/ui/checkbox";
import { Input } from "@/components/ui/input";
import { Label } from "@/components/ui/label";
import { Popover, PopoverContent, PopoverTrigger } from "@/components/ui/popover";
import {
Select, SelectContent, SelectItem, SelectTrigger, SelectValue,
} from "@/components/ui/select";
import {
Sheet, SheetContent, SheetDescription, SheetFooter, SheetHeader, SheetTitle,
} from "@/components/ui/sheet";
import { Textarea } from "@/components/ui/textarea";
import {
createLeadAction,
initialLeadState,
updateLeadAction,
} from "@/lib/appwrite/lead-actions";
import { LEAD_SOURCE_LABEL, LEAD_STATUS_CONFIG, type LeadRow, type MemberOption } from "./types";
type Props = {
open: boolean;
onOpenChange: (v: boolean) => void;
lead?: LeadRow | null;
defaultStatus?: string;
members: MemberOption[];
onCreated?: (leadId: string) => void;
};
export function LeadFormSheet({ open, onOpenChange, lead, defaultStatus, members, onCreated }: Props) {
const isEdit = Boolean(lead);
const action = isEdit ? updateLeadAction : createLeadAction;
const [state, formAction, isPending] = useActionState(action, initialLeadState);
const [assigneeOpen, setAssigneeOpen] = useState(false);
const [assigneeId, setAssigneeId] = useState(lead?.assigneeId ?? "");
useEffect(() => {
if (open) setAssigneeId(lead?.assigneeId ?? "");
}, [open, lead]);
useEffect(() => {
if (state.ok) {
toast.success(isEdit ? "Aday güncellendi." : "Aday eklendi.");
if (!isEdit && state.leadId) onCreated?.(state.leadId);
onOpenChange(false);
} else if (state.error) {
toast.error(state.error);
}
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [state]);
const selectedMember = members.find((m) => m.id === assigneeId);
return (
<Sheet open={open} onOpenChange={onOpenChange}>
<SheetContent className="flex w-full flex-col gap-0 p-0 sm:max-w-lg">
<SheetHeader className="border-b px-6 py-4">
<SheetTitle>{isEdit ? "Adayı düzenle" : "Yeni müşteri adayı"}</SheetTitle>
<SheetDescription>Müşteri adayının bilgilerini girin.</SheetDescription>
</SheetHeader>
<form action={formAction} className="flex flex-1 flex-col">
{isEdit && lead && <input type="hidden" name="id" value={lead.id} />}
<input type="hidden" name="assigneeId" value={assigneeId} />
<div className="flex-1 space-y-4 overflow-y-auto px-6 py-5">
{/* Ad / Şirket */}
<div className="grid gap-2">
<Label htmlFor="name">Şirket / Lead adı *</Label>
<Input id="name" name="name" defaultValue={lead?.name ?? ""} placeholder="Örn. ABC Yazılım A.Ş." required />
{state.fieldErrors?.name && <p className="text-destructive text-xs">{state.fieldErrors.name}</p>}
</div>
{/* İlgili kişi */}
<div className="grid gap-2">
<Label htmlFor="contactName">İlgili kişi</Label>
<Input id="contactName" name="contactName" defaultValue={lead?.contactName ?? ""} placeholder="Ad Soyad" />
</div>
{/* Tel + Email */}
<div className="grid gap-4 sm:grid-cols-2">
<div className="grid gap-2">
<Label htmlFor="phone">Telefon</Label>
<Input id="phone" name="phone" type="tel" defaultValue={lead?.phone ?? ""} placeholder="+90 5xx xxx xx xx" />
</div>
<div className="grid gap-2">
<Label htmlFor="email">E-posta</Label>
<Input id="email" name="email" type="email" defaultValue={lead?.email ?? ""} placeholder="ornek@sirket.com" />
{state.fieldErrors?.email && <p className="text-destructive text-xs">{state.fieldErrors.email}</p>}
</div>
</div>
{/* Kaynak + Durum */}
<div className="grid gap-4 sm:grid-cols-2">
<div className="grid gap-2">
<Label htmlFor="source">Kaynak</Label>
<Select name="source" defaultValue={lead?.source ?? "other"}>
<SelectTrigger id="source"><SelectValue /></SelectTrigger>
<SelectContent>
{(Object.entries(LEAD_SOURCE_LABEL) as [string, string][]).map(([v, l]) => (
<SelectItem key={v} value={v}>{l}</SelectItem>
))}
</SelectContent>
</Select>
</div>
<div className="grid gap-2">
<Label htmlFor="status">Durum</Label>
<Select name="status" defaultValue={lead?.status ?? defaultStatus ?? "cold"}>
<SelectTrigger id="status"><SelectValue /></SelectTrigger>
<SelectContent>
{(Object.entries(LEAD_STATUS_CONFIG) as [string, { label: string }][]).map(([v, c]) => (
<SelectItem key={v} value={v}>{c.label}</SelectItem>
))}
</SelectContent>
</Select>
</div>
</div>
{/* Tahmini değer + para birimi */}
<div className="grid gap-4 sm:grid-cols-3">
<div className="grid gap-2 sm:col-span-2">
<Label htmlFor="estimatedValue">Tahmini değer</Label>
<Input id="estimatedValue" name="estimatedValue" type="number" step="0.01" min="0"
defaultValue={lead?.estimatedValue ?? ""} placeholder="0.00" />
</div>
<div className="grid gap-2">
<Label htmlFor="currency">Para birimi</Label>
<Select name="currency" defaultValue={lead?.currency ?? "TRY"}>
<SelectTrigger id="currency"><SelectValue /></SelectTrigger>
<SelectContent>
<SelectItem value="TRY"> TRY</SelectItem>
<SelectItem value="USD">$ USD</SelectItem>
<SelectItem value="EUR"> EUR</SelectItem>
</SelectContent>
</Select>
</div>
</div>
{/* Sorumlu */}
{members.length > 0 && (
<div className="grid gap-2">
<Label>Sorumlu</Label>
<Popover open={assigneeOpen} onOpenChange={setAssigneeOpen}>
<PopoverTrigger asChild>
<Button type="button" variant="outline" className="w-full justify-between">
{selectedMember ? (
<Badge variant="secondary" className="font-normal">{selectedMember.name}</Badge>
) : (
<span className="text-muted-foreground text-sm font-normal">Personel seçin</span>
)}
<ChevronDown className="text-muted-foreground size-4" />
</Button>
</PopoverTrigger>
<PopoverContent className="w-64 p-1" align="start">
<label className="flex cursor-pointer items-center gap-3 rounded px-3 py-2 hover:bg-muted">
<Checkbox checked={!assigneeId} onCheckedChange={() => { setAssigneeId(""); setAssigneeOpen(false); }} />
<span className="text-muted-foreground text-sm">Atanmamış</span>
</label>
{members.map((m) => (
<label key={m.id} className="flex cursor-pointer items-center gap-3 rounded px-3 py-2 hover:bg-muted">
<Checkbox checked={assigneeId === m.id} onCheckedChange={() => { setAssigneeId(m.id); setAssigneeOpen(false); }} />
<div className="min-w-0">
<div className="truncate text-sm font-medium">{m.name}</div>
<div className="text-muted-foreground truncate text-xs">{m.email}</div>
</div>
</label>
))}
</PopoverContent>
</Popover>
</div>
)}
{/* Notlar */}
<div className="grid gap-2">
<Label htmlFor="notes">Notlar</Label>
<Textarea id="notes" name="notes" rows={3} defaultValue={lead?.notes ?? ""} placeholder="İlk görüşme notları, ihtiyaçlar vb." />
</div>
</div>
<SheetFooter className="border-t bg-muted/30 px-6 pt-4">
<div className="flex w-full justify-end gap-2">
<Button type="button" variant="outline" onClick={() => onOpenChange(false)} disabled={isPending}>Vazgeç</Button>
<Button type="submit" disabled={isPending}>
{isPending ? <><Loader2 className="size-4 animate-spin" />Kaydediliyor...</> : <><Save className="size-4" />{isEdit ? "Güncelle" : "Kaydet"}</>}
</Button>
</div>
</SheetFooter>
</form>
</SheetContent>
</Sheet>
);
}
@@ -0,0 +1,255 @@
"use client";
import { useMemo, useState, useTransition } from "react";
import {
DndContext,
type DragEndEvent,
DragOverlay,
type DragStartEvent,
PointerSensor,
closestCorners,
useDroppable,
useSensor,
useSensors,
} from "@dnd-kit/core";
import { SortableContext, verticalListSortingStrategy } from "@dnd-kit/sortable";
import { Loader2, Plus, Trash2 } from "lucide-react";
import { toast } from "sonner";
import { Badge } from "@/components/ui/badge";
import { Button } from "@/components/ui/button";
import {
Dialog, DialogContent, DialogDescription, DialogFooter, DialogHeader, DialogTitle,
} from "@/components/ui/dialog";
import { cn } from "@/lib/utils";
import { deleteLeadAction, moveLeadAction } from "@/lib/appwrite/lead-actions";
import { LeadCard } from "./lead-card";
import { LeadFormSheet } from "./lead-form-sheet";
import { LeadDetailSheet } from "./lead-detail-sheet";
import {
COLUMNS,
LEAD_STATUS_CONFIG,
type ActivityRow,
type LeadRow,
type LeadStatus,
type MemberOption,
} from "./types";
import { listLeadActivitiesForClient } from "./lead-activities-fetcher";
type Props = {
leads: LeadRow[];
members: MemberOption[];
currentUserId: string;
};
function Column({
status,
leads,
onAdd,
onEdit,
onDetail,
onDelete,
}: {
status: LeadStatus;
leads: LeadRow[];
onAdd: (status: LeadStatus) => void;
onEdit: (lead: LeadRow) => void;
onDetail: (lead: LeadRow) => void;
onDelete: (lead: LeadRow) => void;
}) {
const { setNodeRef, isOver } = useDroppable({
id: `col-${status}`,
data: { type: "column", status },
});
const cfg = LEAD_STATUS_CONFIG[status];
const totalValue = leads.reduce((s, l) => s + (l.estimatedValue ?? 0), 0);
return (
<div className="bg-muted/40 flex min-w-[260px] flex-col rounded-lg border">
<div className={cn("flex items-center justify-between border-b px-3 py-2.5", cfg.bg)}>
<div className="flex items-center gap-2">
<h2 className={cn("text-sm font-semibold", cfg.color)}>{LEAD_STATUS_CONFIG[status].label}</h2>
<Badge variant="secondary" className="rounded-full px-1.5 py-0.5 text-xs">{leads.length}</Badge>
</div>
<Button variant="ghost" size="icon" className="size-7" onClick={() => onAdd(status)} aria-label="Yeni aday">
<Plus className="size-3.5" />
</Button>
</div>
{totalValue > 0 && (
<div className="border-b px-3 py-1.5">
<span className="text-muted-foreground text-xs">
{new Intl.NumberFormat("tr-TR", { style: "currency", currency: "TRY", maximumFractionDigits: 0 }).format(totalValue)}
</span>
</div>
)}
<div
ref={setNodeRef}
className={cn("flex flex-1 flex-col gap-2 p-2 transition-colors", isOver && "bg-primary/5")}
>
<SortableContext items={leads.map((l) => l.id)} strategy={verticalListSortingStrategy}>
{leads.map((lead) => (
<LeadCard key={lead.id} lead={lead} onEdit={onEdit} onDetail={onDetail} onDelete={onDelete} />
))}
</SortableContext>
{leads.length === 0 && (
<p className="text-muted-foreground py-8 text-center text-xs">Boş</p>
)}
</div>
</div>
);
}
export function LeadsBoard({ leads: initialLeads, members, currentUserId: _uid }: Props) {
const [leads, setLeads] = useState<LeadRow[]>(initialLeads);
const [activeId, setActiveId] = useState<string | null>(null);
const [formOpen, setFormOpen] = useState(false);
const [formStatus, setFormStatus] = useState<LeadStatus>("cold");
const [editing, setEditing] = useState<LeadRow | null>(null);
const [detailLead, setDetailLead] = useState<LeadRow | null>(null);
const [detailActivities, setDetailActivities] = useState<ActivityRow[]>([]);
const [detailOpen, setDetailOpen] = useState(false);
const [deleting, setDeleting] = useState<LeadRow | null>(null);
const [busy, startTransition] = useTransition();
const sensors = useSensors(useSensor(PointerSensor, { activationConstraint: { distance: 6 } }));
const grouped = useMemo(() => {
const map = Object.fromEntries(COLUMNS.map((c) => [c.key, [] as LeadRow[]])) as Record<LeadStatus, LeadRow[]>;
for (const l of leads) map[l.status].push(l);
return map;
}, [leads]);
const activeLead = useMemo(() => leads.find((l) => l.id === activeId) ?? null, [leads, activeId]);
const onDragStart = (e: DragStartEvent) => setActiveId(String(e.active.id));
const onDragEnd = (e: DragEndEvent) => {
const { active, over } = e;
setActiveId(null);
if (!over) return;
const overData = over.data.current as { type?: string; status?: LeadStatus } | undefined;
let targetStatus: LeadStatus | undefined;
if (overData?.type === "column") targetStatus = overData.status;
else if (overData?.type === "lead") targetStatus = overData.status;
if (!targetStatus) return;
const src = leads.find((l) => l.id === active.id);
if (!src || src.status === targetStatus) return;
setLeads((prev) => prev.map((l) => l.id === src.id ? { ...l, status: targetStatus! } : l));
startTransition(async () => {
const result = await moveLeadAction(src.id, targetStatus!);
if (!result.ok) {
setLeads((prev) => prev.map((l) => l.id === src.id ? { ...l, status: src.status } : l));
toast.error(result.error ?? "Taşıma başarısız.");
}
});
};
const openDetail = async (lead: LeadRow) => {
setDetailLead(lead);
setDetailActivities([]);
setDetailOpen(true);
const acts = await listLeadActivitiesForClient(lead.id);
setDetailActivities(acts);
};
const handleDelete = () => {
if (!deleting) return;
startTransition(async () => {
const fd = new FormData();
fd.set("id", deleting.id);
const result = await deleteLeadAction(fd);
if (result.ok) {
toast.success("Aday silindi.");
setLeads((prev) => prev.filter((l) => l.id !== deleting.id));
setDeleting(null);
} else {
toast.error(result.error ?? "Silme başarısız.");
}
});
};
useMemo(() => setLeads(initialLeads), [initialLeads]);
return (
<>
<div className="flex items-center justify-between">
<div className="text-muted-foreground text-sm">
Toplam {leads.length} aday
{leads.filter((l) => l.status === "converted").length > 0 && (
<span className="ml-2 text-green-600">
· {leads.filter((l) => l.status === "converted").length} kazanıldı
</span>
)}
</div>
<Button onClick={() => { setEditing(null); setFormStatus("cold"); setFormOpen(true); }}>
<Plus className="size-4" />
Yeni aday
</Button>
</div>
<DndContext sensors={sensors} collisionDetection={closestCorners} onDragStart={onDragStart} onDragEnd={onDragEnd}>
<div className="flex gap-4 overflow-x-auto pb-4">
{COLUMNS.map((col) => (
<Column
key={col.key}
status={col.key}
leads={grouped[col.key]}
onAdd={(s) => { setEditing(null); setFormStatus(s); setFormOpen(true); }}
onEdit={(l) => { setEditing(l); setFormOpen(true); }}
onDetail={openDetail}
onDelete={(l) => setDeleting(l)}
/>
))}
</div>
<DragOverlay>
{activeLead && (
<LeadCard lead={activeLead} onEdit={() => {}} onDetail={() => {}} onDelete={() => {}} isOverlay />
)}
</DragOverlay>
</DndContext>
<LeadFormSheet
open={formOpen}
onOpenChange={(v) => { setFormOpen(v); if (!v) setEditing(null); }}
lead={editing}
defaultStatus={formStatus}
members={members}
onCreated={(id) => {
// Detail sheet will reload on next open; no-op here
}}
/>
<LeadDetailSheet
open={detailOpen}
onOpenChange={setDetailOpen}
lead={detailLead}
activities={detailActivities}
onEdit={(l) => { setDetailOpen(false); setEditing(l); setFormOpen(true); }}
/>
<Dialog open={Boolean(deleting)} onOpenChange={(v) => !v && setDeleting(null)}>
<DialogContent>
<DialogHeader>
<DialogTitle>Adayı sil</DialogTitle>
<DialogDescription>
<strong>{deleting?.name}</strong> adlı aday ve tüm aktiviteleri kalıcı olarak silinecek.
</DialogDescription>
</DialogHeader>
<DialogFooter>
<Button variant="outline" onClick={() => setDeleting(null)} disabled={busy}>Vazgeç</Button>
<Button variant="destructive" onClick={handleDelete} disabled={busy}>
{busy ? <Loader2 className="size-4 animate-spin" /> : <Trash2 className="size-4" />}
Sil
</Button>
</DialogFooter>
</DialogContent>
</Dialog>
</>
);
}
@@ -0,0 +1,72 @@
import type { LeadActivityType, LeadSource, LeadStatus } from "@/lib/appwrite/schema";
export type { LeadStatus, LeadSource, LeadActivityType };
export type LeadRow = {
id: string;
name: string;
contactName: string;
email: string;
phone: string;
source: LeadSource;
status: LeadStatus;
estimatedValue: number | null;
currency: string;
notes: string;
assigneeId: string;
assigneeName: string;
lastContactAt: string | null;
nextFollowUpAt: string | null;
calendarEventId: string | null;
customerId: string | null;
createdAt: string;
};
export type ActivityRow = {
id: string;
leadId: string;
type: LeadActivityType;
content: string;
calendarEventId: string | null;
occurredAt: string | null;
createdAt: string;
createdByName: string;
};
export type MemberOption = { id: string; name: string; email: string };
export const LEAD_STATUS_CONFIG: Record<
LeadStatus,
{ label: string; color: string; bg: string; border: string }
> = {
cold: { label: "Soğuk", color: "text-blue-600 dark:text-blue-400", bg: "bg-blue-500/10", border: "border-blue-500/30" },
warm: { label: "Ilık", color: "text-orange-600 dark:text-orange-400", bg: "bg-orange-500/10", border: "border-orange-500/30" },
hot: { label: "Sıcak", color: "text-red-600 dark:text-red-400", bg: "bg-red-500/10", border: "border-red-500/30" },
converted: { label: "Kazanıldı", color: "text-green-600 dark:text-green-400", bg: "bg-green-500/10", border: "border-green-500/30" },
lost: { label: "Kaybedildi",color: "text-muted-foreground", bg: "bg-muted/40", border: "border-border" },
};
export const LEAD_SOURCE_LABEL: Record<LeadSource, string> = {
website: "Website",
social: "Sosyal medya",
referral: "Referans",
cold_call: "Soğuk arama",
event: "Fuar / Etkinlik",
other: "Diğer",
};
export const ACTIVITY_TYPE_CONFIG: Record<LeadActivityType, { label: string; icon: string }> = {
note: { label: "Not", icon: "📝" },
call: { label: "Arama", icon: "📞" },
meeting: { label: "Toplantı", icon: "🤝" },
email: { label: "E-posta", icon: "✉️" },
status_change: { label: "Durum değişti", icon: "🔄" },
};
export const COLUMNS: { key: LeadStatus; title: string }[] = [
{ key: "cold", title: "Soğuk" },
{ key: "warm", title: "Ilık" },
{ key: "hot", title: "Sıcak" },
{ key: "converted", title: "Kazanıldı" },
{ key: "lost", title: "Kaybedildi" },
];
+76
View File
@@ -0,0 +1,76 @@
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 (
<div className="flex-1 space-y-4 px-6 pt-0">
<div className="flex flex-col gap-1">
<p className="text-muted-foreground text-sm">{ctx.settings?.companyName ?? "Çalışma alanı"}</p>
<h1 className="text-2xl font-bold tracking-tight">Müşteri Adayları</h1>
<p className="text-muted-foreground text-sm">
Müşteri adaylarını takip edin, ısıtın ve müşteriye dönüştürün.
</p>
</div>
<LeadsBoard
leads={leadRows}
members={members}
currentUserId={ctx.user.id}
/>
</div>
);
}
+6
View File
@@ -11,6 +11,7 @@ import {
Package, Package,
Receipt, Receipt,
Settings, Settings,
TrendingUp,
Users, Users,
Wallet, Wallet,
} from "lucide-react"; } from "lucide-react";
@@ -45,6 +46,11 @@ const navGroups = [
{ {
label: "İşletme", label: "İşletme",
items: [ items: [
{
title: "Müşteri Adayları",
url: "/leads",
icon: TrendingUp,
},
{ {
title: "Müşteriler", title: "Müşteriler",
url: "/customers", url: "/customers",
+299
View File
@@ -0,0 +1,299 @@
"use server";
import { revalidatePath } from "next/cache";
import { AppwriteException, ID, Permission, Role } from "node-appwrite";
import { z } from "zod";
import { logAudit } from "./audit";
import { DATABASE_ID, TABLES, type Lead, type LeadStatus } from "./schema";
import { createAdminClient } from "./server";
import { requireTenant } from "./tenant-guard";
import { leadSchema } from "@/lib/validation/leads";
export type LeadActionState = {
ok: boolean;
error?: string;
fieldErrors?: Record<string, string>;
leadId?: string;
};
export const initialLeadState: LeadActionState = { ok: false };
function appwriteError(e: unknown): string {
if (e instanceof AppwriteException) return e.message || "Beklenmeyen bir hata oluştu.";
return "Bağlantı hatası. Tekrar deneyin.";
}
function flattenErrors(err: z.ZodError): Record<string, string> {
const out: Record<string, string> = {};
for (const issue of err.issues) {
const key = issue.path.join(".");
if (key && !out[key]) out[key] = issue.message;
}
return out;
}
function teamRowPermissions(tenantId: string) {
return [
Permission.read(Role.team(tenantId)),
Permission.update(Role.team(tenantId)),
Permission.delete(Role.team(tenantId, "owner")),
Permission.delete(Role.team(tenantId, "admin")),
];
}
function pickFormFields(formData: FormData) {
return {
name: String(formData.get("name") ?? "").trim(),
contactName: String(formData.get("contactName") ?? "").trim(),
email: String(formData.get("email") ?? "").trim(),
phone: String(formData.get("phone") ?? "").trim(),
source: String(formData.get("source") ?? "other"),
status: String(formData.get("status") ?? "cold"),
estimatedValue: String(formData.get("estimatedValue") ?? ""),
currency: String(formData.get("currency") ?? "TRY"),
notes: String(formData.get("notes") ?? "").trim(),
assigneeId: String(formData.get("assigneeId") ?? ""),
};
}
export async function createLeadAction(
_prev: LeadActionState,
formData: FormData,
): Promise<LeadActionState> {
let ctx;
try {
ctx = await requireTenant();
} catch {
return { ok: false, error: "Yetkiniz yok." };
}
const parsed = leadSchema.safeParse(pickFormFields(formData));
if (!parsed.success) {
return { ok: false, error: "Form geçersiz.", fieldErrors: flattenErrors(parsed.error) };
}
try {
const { tablesDB } = createAdminClient();
const row = await tablesDB.createRow(
DATABASE_ID,
TABLES.leads,
ID.unique(),
{ tenantId: ctx.tenantId, createdBy: ctx.user.id, ...parsed.data },
teamRowPermissions(ctx.tenantId),
);
await logAudit({
tenantId: ctx.tenantId,
userId: ctx.user.id,
action: "create",
entityType: "lead",
entityId: row.$id,
changes: { name: parsed.data.name, status: parsed.data.status },
});
revalidatePath("/leads");
return { ok: true, leadId: row.$id };
} catch (e) {
return { ok: false, error: appwriteError(e) };
}
}
export async function updateLeadAction(
_prev: LeadActionState,
formData: FormData,
): Promise<LeadActionState> {
const id = String(formData.get("id") ?? "");
if (!id) return { ok: false, error: "ID eksik." };
let ctx;
try {
ctx = await requireTenant();
} catch {
return { ok: false, error: "Yetkiniz yok." };
}
const parsed = leadSchema.safeParse(pickFormFields(formData));
if (!parsed.success) {
return { ok: false, error: "Form geçersiz.", fieldErrors: flattenErrors(parsed.error) };
}
try {
const { tablesDB } = createAdminClient();
const existing = (await tablesDB.getRow(DATABASE_ID, TABLES.leads, id)) as unknown as Lead;
if (existing.tenantId !== ctx.tenantId) return { ok: false, error: "Erişim engellendi." };
await tablesDB.updateRow(DATABASE_ID, TABLES.leads, id, parsed.data);
await logAudit({
tenantId: ctx.tenantId,
userId: ctx.user.id,
action: "update",
entityType: "lead",
entityId: id,
changes: parsed.data,
});
revalidatePath("/leads");
return { ok: true, leadId: id };
} catch (e) {
return { ok: false, error: appwriteError(e) };
}
}
export async function moveLeadAction(
leadId: string,
status: LeadStatus,
): Promise<LeadActionState> {
let ctx;
try {
ctx = await requireTenant();
} catch {
return { ok: false, error: "Yetkiniz yok." };
}
try {
const { tablesDB } = createAdminClient();
const existing = (await tablesDB.getRow(DATABASE_ID, TABLES.leads, leadId)) as unknown as Lead;
if (existing.tenantId !== ctx.tenantId) return { ok: false, error: "Erişim engellendi." };
const prevStatus = existing.status;
await tablesDB.updateRow(DATABASE_ID, TABLES.leads, leadId, {
status,
lastContactAt: new Date().toISOString(),
});
// Auto-create a status_change activity
await tablesDB.createRow(
DATABASE_ID,
TABLES.leadActivities,
ID.unique(),
{
tenantId: ctx.tenantId,
createdBy: ctx.user.id,
leadId,
type: "status_change",
content: `${prevStatus ?? "cold"}${status}`,
occurredAt: new Date().toISOString(),
},
[
Permission.read(Role.team(ctx.tenantId)),
Permission.update(Role.team(ctx.tenantId)),
Permission.delete(Role.team(ctx.tenantId, "owner")),
],
);
revalidatePath("/leads");
return { ok: true };
} catch (e) {
return { ok: false, error: appwriteError(e) };
}
}
export async function deleteLeadAction(formData: FormData): Promise<LeadActionState> {
const id = String(formData.get("id") ?? "");
if (!id) return { ok: false, error: "ID eksik." };
let ctx;
try {
ctx = await requireTenant();
} catch {
return { ok: false, error: "Yetkiniz yok." };
}
try {
const { tablesDB } = createAdminClient();
const existing = (await tablesDB.getRow(DATABASE_ID, TABLES.leads, id)) as unknown as Lead;
if (existing.tenantId !== ctx.tenantId) return { ok: false, error: "Erişim engellendi." };
await tablesDB.deleteRow(DATABASE_ID, TABLES.leads, id);
await logAudit({
tenantId: ctx.tenantId,
userId: ctx.user.id,
action: "delete",
entityType: "lead",
entityId: id,
changes: { name: existing.name },
});
revalidatePath("/leads");
return { ok: true };
} catch (e) {
return { ok: false, error: appwriteError(e) };
}
}
export async function convertLeadToCustomerAction(formData: FormData): Promise<LeadActionState> {
const leadId = String(formData.get("leadId") ?? "");
if (!leadId) return { ok: false, error: "Lead ID eksik." };
let ctx;
try {
ctx = await requireTenant();
} catch {
return { ok: false, error: "Yetkiniz yok." };
}
try {
const { tablesDB } = createAdminClient();
const lead = (await tablesDB.getRow(DATABASE_ID, TABLES.leads, leadId)) as unknown as Lead;
if (lead.tenantId !== ctx.tenantId) return { ok: false, error: "Erişim engellendi." };
const permissions = teamRowPermissions(ctx.tenantId);
// Create customer from lead data
const customer = await tablesDB.createRow(
DATABASE_ID,
TABLES.customers,
ID.unique(),
{
tenantId: ctx.tenantId,
createdBy: ctx.user.id,
name: lead.contactName || lead.name,
email: lead.email,
phone: lead.phone,
notes: lead.notes,
status: "active",
},
permissions,
);
// Update lead: mark converted + link customerId
await tablesDB.updateRow(DATABASE_ID, TABLES.leads, leadId, {
status: "converted",
customerId: customer.$id,
lastContactAt: new Date().toISOString(),
});
await tablesDB.createRow(
DATABASE_ID,
TABLES.leadActivities,
ID.unique(),
{
tenantId: ctx.tenantId,
createdBy: ctx.user.id,
leadId,
type: "status_change",
content: `Müşteriye dönüştürüldü → ${lead.contactName || lead.name}`,
occurredAt: new Date().toISOString(),
},
permissions,
);
await logAudit({
tenantId: ctx.tenantId,
userId: ctx.user.id,
action: "create",
entityType: "customer_from_lead",
entityId: customer.$id,
changes: { leadId, customerId: customer.$id },
});
revalidatePath("/leads");
revalidatePath("/customers");
return { ok: true, leadId: customer.$id };
} catch (e) {
return { ok: false, error: appwriteError(e) };
}
}
+145
View File
@@ -0,0 +1,145 @@
"use server";
import { revalidatePath } from "next/cache";
import { ID, Permission, Role } from "node-appwrite";
import { DATABASE_ID, TABLES, type Lead, type LeadActivityType } from "./schema";
import { createAdminClient } from "./server";
import { requireTenant } from "./tenant-guard";
export type ActivityActionState = { ok: boolean; error?: string };
export async function addLeadActivityAction(
_prev: ActivityActionState,
formData: FormData,
): Promise<ActivityActionState> {
const leadId = String(formData.get("leadId") ?? "");
const type = String(formData.get("type") ?? "note") as LeadActivityType;
const content = String(formData.get("content") ?? "").trim();
const occurredAt = String(formData.get("occurredAt") ?? "") || new Date().toISOString();
if (!leadId || !content) return { ok: false, error: "Zorunlu alanlar eksik." };
let ctx;
try {
ctx = await requireTenant();
} catch {
return { ok: false, error: "Yetkiniz yok." };
}
try {
const { tablesDB } = createAdminClient();
const lead = (await tablesDB.getRow(DATABASE_ID, TABLES.leads, leadId)) as unknown as Lead;
if (lead.tenantId !== ctx.tenantId) return { ok: false, error: "Erişim engellendi." };
await tablesDB.createRow(
DATABASE_ID,
TABLES.leadActivities,
ID.unique(),
{
tenantId: ctx.tenantId,
createdBy: ctx.user.id,
leadId,
type,
content,
occurredAt,
},
[
Permission.read(Role.team(ctx.tenantId)),
Permission.update(Role.team(ctx.tenantId)),
Permission.delete(Role.team(ctx.tenantId, "owner")),
],
);
// Update lastContactAt on the lead
await tablesDB.updateRow(DATABASE_ID, TABLES.leads, leadId, {
lastContactAt: new Date().toISOString(),
});
revalidatePath("/leads");
return { ok: true };
} catch (e) {
return { ok: false, error: e instanceof Error ? e.message : "Hata oluştu." };
}
}
export async function scheduleFollowUpAction(
_prev: ActivityActionState,
formData: FormData,
): Promise<ActivityActionState> {
const leadId = String(formData.get("leadId") ?? "");
const followUpAt = String(formData.get("followUpAt") ?? "");
const note = String(formData.get("note") ?? "").trim();
if (!leadId || !followUpAt) return { ok: false, error: "Tarih seçin." };
let ctx;
try {
ctx = await requireTenant();
} catch {
return { ok: false, error: "Yetkiniz yok." };
}
try {
const { tablesDB } = createAdminClient();
const lead = (await tablesDB.getRow(DATABASE_ID, TABLES.leads, leadId)) as unknown as Lead;
if (lead.tenantId !== ctx.tenantId) return { ok: false, error: "Erişim engellendi." };
const followUpDate = new Date(followUpAt);
const endDate = new Date(followUpDate.getTime() + 60 * 60 * 1000); // +1h
const permissions = [
Permission.read(Role.team(ctx.tenantId)),
Permission.update(Role.team(ctx.tenantId)),
Permission.delete(Role.team(ctx.tenantId, "owner")),
];
// Create calendar event
const event = await tablesDB.createRow(
DATABASE_ID,
TABLES.calendarEvents,
ID.unique(),
{
tenantId: ctx.tenantId,
createdBy: ctx.user.id,
title: `Takip: ${lead.contactName || lead.name}`,
description: note || `Lead takip görüşmesi — ${lead.name}`,
start: followUpDate.toISOString(),
end: endDate.toISOString(),
allDay: false,
leadId,
color: "#f97316", // orange — lead events
},
permissions,
);
// Update lead nextFollowUpAt + calendarEventId
await tablesDB.updateRow(DATABASE_ID, TABLES.leads, leadId, {
nextFollowUpAt: followUpDate.toISOString(),
calendarEventId: event.$id,
});
// Log activity
await tablesDB.createRow(
DATABASE_ID,
TABLES.leadActivities,
ID.unique(),
{
tenantId: ctx.tenantId,
createdBy: ctx.user.id,
leadId,
type: "meeting",
content: `Takip planlandı: ${followUpDate.toLocaleString("tr-TR")}${note ? `${note}` : ""}`,
calendarEventId: event.$id,
occurredAt: new Date().toISOString(),
},
permissions,
);
revalidatePath("/leads");
revalidatePath("/calendar");
return { ok: true };
} catch (e) {
return { ok: false, error: e instanceof Error ? e.message : "Hata oluştu." };
}
}
+51
View File
@@ -0,0 +1,51 @@
import "server-only";
import { Query } from "node-appwrite";
import { createAdminClient } from "./server";
import { DATABASE_ID, TABLES, type Lead, type LeadActivity } from "./schema";
export async function listLeads(tenantId: string): Promise<Lead[]> {
try {
const { tablesDB } = createAdminClient();
const result = await tablesDB.listRows({
databaseId: DATABASE_ID,
tableId: TABLES.leads,
queries: [Query.equal("tenantId", tenantId), Query.orderDesc("$createdAt"), Query.limit(500)],
});
return result.rows as unknown as Lead[];
} catch {
return [];
}
}
export async function getLead(tenantId: string, leadId: string): Promise<Lead | null> {
try {
const { tablesDB } = createAdminClient();
const row = await tablesDB.getRow(DATABASE_ID, TABLES.leads, leadId);
const lead = row as unknown as Lead;
if (lead.tenantId !== tenantId) return null;
return lead;
} catch {
return null;
}
}
export async function listLeadActivities(tenantId: string, leadId: string): Promise<LeadActivity[]> {
try {
const { tablesDB } = createAdminClient();
const result = await tablesDB.listRows({
databaseId: DATABASE_ID,
tableId: TABLES.leadActivities,
queries: [
Query.equal("tenantId", tenantId),
Query.equal("leadId", leadId),
Query.orderDesc("$createdAt"),
Query.limit(100),
],
});
return result.rows as unknown as LeadActivity[];
} catch {
return [];
}
}
+36
View File
@@ -24,6 +24,8 @@ export const TABLES = {
creditCardStatements: "credit_card_statements", creditCardStatements: "credit_card_statements",
subscriptionPayments: "subscription_payments", subscriptionPayments: "subscription_payments",
savedCards: "saved_cards", savedCards: "saved_cards",
leads: "leads",
leadActivities: "lead_activities",
} as const; } as const;
export type TableId = (typeof TABLES)[keyof typeof TABLES]; export type TableId = (typeof TABLES)[keyof typeof TABLES];
@@ -120,9 +122,43 @@ export interface CalendarEvent extends Row {
end: string; end: string;
allDay?: boolean; allDay?: boolean;
customerId?: string; customerId?: string;
leadId?: string;
color?: string; color?: string;
} }
export type LeadStatus = "cold" | "warm" | "hot" | "converted" | "lost";
export type LeadSource = "website" | "social" | "referral" | "cold_call" | "event" | "other";
export type LeadActivityType = "note" | "call" | "meeting" | "email" | "status_change";
export interface Lead extends Row {
tenantId: string;
createdBy: string;
name: string;
contactName?: string;
email?: string;
phone?: string;
source?: LeadSource;
status?: LeadStatus;
estimatedValue?: number;
currency?: string;
notes?: string;
assigneeId?: string;
lastContactAt?: string;
nextFollowUpAt?: string;
calendarEventId?: string;
customerId?: string;
}
export interface LeadActivity extends Row {
tenantId: string;
createdBy: string;
leadId: string;
type: LeadActivityType;
content: string;
calendarEventId?: string;
occurredAt?: string;
}
export type TaskStatus = "backlog" | "todo" | "in_progress" | "done"; export type TaskStatus = "backlog" | "todo" | "in_progress" | "done";
export type TaskPriority = "low" | "medium" | "high" | "urgent"; export type TaskPriority = "low" | "medium" | "high" | "urgent";
+19
View File
@@ -0,0 +1,19 @@
import { z } from "zod";
export const leadSchema = z.object({
name: z.string().trim().min(1, "Lead adı zorunlu.").max(255),
contactName: z.string().trim().max(255).optional().transform((v) => v || undefined),
email: z.string().trim().email("Geçerli e-posta girin.").optional().or(z.literal("")).transform((v) => v || undefined),
phone: z.string().trim().max(50).optional().transform((v) => v || undefined),
source: z.enum(["website", "social", "referral", "cold_call", "event", "other"]).optional().default("other"),
status: z.enum(["cold", "warm", "hot", "converted", "lost"]).optional().default("cold"),
estimatedValue: z
.union([z.number(), z.string()])
.transform((v) => (typeof v === "string" ? (v === "" ? undefined : Number(v.replace(",", "."))) : v))
.pipe(z.number().nonnegative().optional()),
currency: z.enum(["TRY", "USD", "EUR"]).optional().default("TRY"),
notes: z.string().trim().optional().transform((v) => v || undefined),
assigneeId: z.string().optional().transform((v) => v || undefined),
});
export type LeadInput = z.infer<typeof leadSchema>;