feat: all core modules — properties, customers, searches, matches, presentations, activities, investors + public sunum page
- Server actions: property/customer/search/presentation/activity/investor CRUD - Matching engine: matchPropertyToSearches + syncMatchesForSearch on search save - UI: form sheets + table clients for all modules - Public /sunum/[token] page (no auth) with property card grid + expiry check - All pages force-dynamic for auth guard compatibility
This commit is contained in:
@@ -1,8 +1,41 @@
|
|||||||
export default function Page() {
|
export const dynamic = "force-dynamic";
|
||||||
|
|
||||||
|
import { Query } from "node-appwrite";
|
||||||
|
|
||||||
|
import { requireTenant } from "@/lib/appwrite/tenant-guard";
|
||||||
|
import { listCustomers } from "@/lib/appwrite/customer-queries";
|
||||||
|
import { listProperties } from "@/lib/appwrite/property-queries";
|
||||||
|
import { DATABASE_ID, TABLES, type Activity } from "@/lib/appwrite/schema";
|
||||||
|
import { createAdminClient } from "@/lib/appwrite/server";
|
||||||
|
import { ActivitiesClient } from "@/components/activities/activities-client";
|
||||||
|
|
||||||
|
export default async function ActivitiesPage() {
|
||||||
|
const ctx = await requireTenant();
|
||||||
|
const { tablesDB } = createAdminClient();
|
||||||
|
|
||||||
|
const [customers, properties, activitiesResult] = await Promise.all([
|
||||||
|
listCustomers(ctx.tenantId),
|
||||||
|
listProperties(ctx.tenantId),
|
||||||
|
tablesDB.listRows({
|
||||||
|
databaseId: DATABASE_ID,
|
||||||
|
tableId: TABLES.activities,
|
||||||
|
queries: [
|
||||||
|
Query.equal("tenantId", ctx.tenantId),
|
||||||
|
Query.orderDesc("$createdAt"),
|
||||||
|
Query.limit(300),
|
||||||
|
],
|
||||||
|
}),
|
||||||
|
]);
|
||||||
|
|
||||||
|
const activities = activitiesResult.rows as unknown as Activity[];
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div className="flex flex-1 flex-col gap-4 p-4">
|
<div className="flex flex-1 flex-col gap-4 p-4 md:p-6">
|
||||||
<h1 className="text-2xl font-bold capitalize">activities</h1>
|
<ActivitiesClient
|
||||||
<p className="text-muted-foreground">Yakında...</p>
|
initialActivities={activities}
|
||||||
|
customers={customers}
|
||||||
|
properties={properties}
|
||||||
|
/>
|
||||||
</div>
|
</div>
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,8 +1,81 @@
|
|||||||
export default function MatchesPage() {
|
export const dynamic = "force-dynamic";
|
||||||
|
|
||||||
|
import { Query } from "node-appwrite";
|
||||||
|
|
||||||
|
import { requireTenant } from "@/lib/appwrite/tenant-guard";
|
||||||
|
import { listCustomers } from "@/lib/appwrite/customer-queries";
|
||||||
|
import { listProperties } from "@/lib/appwrite/property-queries";
|
||||||
|
import { DATABASE_ID, TABLES, type PropertyMatch } from "@/lib/appwrite/schema";
|
||||||
|
import { createAdminClient } from "@/lib/appwrite/server";
|
||||||
|
|
||||||
|
export default async function MatchesPage() {
|
||||||
|
const ctx = await requireTenant();
|
||||||
|
const { tablesDB } = createAdminClient();
|
||||||
|
|
||||||
|
const [customers, properties, matchesResult] = await Promise.all([
|
||||||
|
listCustomers(ctx.tenantId),
|
||||||
|
listProperties(ctx.tenantId),
|
||||||
|
tablesDB.listRows({
|
||||||
|
databaseId: DATABASE_ID,
|
||||||
|
tableId: TABLES.propertyMatches,
|
||||||
|
queries: [
|
||||||
|
Query.equal("tenantId", ctx.tenantId),
|
||||||
|
Query.orderDesc("$createdAt"),
|
||||||
|
Query.limit(500),
|
||||||
|
],
|
||||||
|
}),
|
||||||
|
]);
|
||||||
|
|
||||||
|
const matches = matchesResult.rows as unknown as PropertyMatch[];
|
||||||
|
const customerMap = Object.fromEntries(customers.map((c) => [c.$id, c.name]));
|
||||||
|
const propertyMap = Object.fromEntries(properties.map((p) => [p.$id, p.title]));
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div className="flex flex-1 flex-col gap-4 p-4">
|
<div className="flex flex-1 flex-col gap-4 p-4 md:p-6">
|
||||||
<h1 className="text-2xl font-bold">Eşleşmeler</h1>
|
<div className="flex items-center justify-between">
|
||||||
<p className="text-muted-foreground">Yakında...</p>
|
<h1 className="text-2xl font-bold">Eşleşmeler</h1>
|
||||||
|
<span className="text-muted-foreground text-sm">{matches.length} eşleşme</span>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div className="rounded-md border">
|
||||||
|
<table className="w-full text-sm">
|
||||||
|
<thead>
|
||||||
|
<tr className="border-b">
|
||||||
|
<th className="text-left p-3 font-medium">Müşteri</th>
|
||||||
|
<th className="text-left p-3 font-medium">İlan</th>
|
||||||
|
<th className="text-left p-3 font-medium">Tarih</th>
|
||||||
|
<th className="text-left p-3 font-medium">Görüntülendi</th>
|
||||||
|
</tr>
|
||||||
|
</thead>
|
||||||
|
<tbody>
|
||||||
|
{matches.length === 0 && (
|
||||||
|
<tr>
|
||||||
|
<td colSpan={4} className="text-muted-foreground text-center py-10">
|
||||||
|
Henüz eşleşme yok.
|
||||||
|
</td>
|
||||||
|
</tr>
|
||||||
|
)}
|
||||||
|
{matches.map((m) => (
|
||||||
|
<tr key={m.$id} className="border-b last:border-0 hover:bg-muted/30">
|
||||||
|
<td className="p-3">{customerMap[m.customerId] ?? m.customerId}</td>
|
||||||
|
<td className="p-3">{propertyMap[m.propertyId] ?? m.propertyId}</td>
|
||||||
|
<td className="p-3 text-muted-foreground">
|
||||||
|
{new Date(m.$createdAt).toLocaleDateString("tr-TR")}
|
||||||
|
</td>
|
||||||
|
<td className="p-3">
|
||||||
|
{m.viewedAt ? (
|
||||||
|
<span className="text-green-600 text-xs">
|
||||||
|
{new Date(m.viewedAt).toLocaleDateString("tr-TR")}
|
||||||
|
</span>
|
||||||
|
) : (
|
||||||
|
<span className="text-muted-foreground text-xs">Hayır</span>
|
||||||
|
)}
|
||||||
|
</td>
|
||||||
|
</tr>
|
||||||
|
))}
|
||||||
|
</tbody>
|
||||||
|
</table>
|
||||||
|
</div>
|
||||||
</div>
|
</div>
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,8 +1,16 @@
|
|||||||
export default function CustomersPage() {
|
export const dynamic = "force-dynamic";
|
||||||
|
|
||||||
|
import { requireTenant } from "@/lib/appwrite/tenant-guard";
|
||||||
|
import { listCustomers } from "@/lib/appwrite/customer-queries";
|
||||||
|
import { CustomersClient } from "@/components/customers/customers-client";
|
||||||
|
|
||||||
|
export default async function CustomersPage() {
|
||||||
|
const ctx = await requireTenant();
|
||||||
|
const customers = await listCustomers(ctx.tenantId);
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div className="flex flex-1 flex-col gap-4 p-4">
|
<div className="flex flex-1 flex-col gap-4 p-4 md:p-6">
|
||||||
<h1 className="text-2xl font-bold">Müşteriler</h1>
|
<CustomersClient initialCustomers={customers} />
|
||||||
<p className="text-muted-foreground">Yakında...</p>
|
|
||||||
</div>
|
</div>
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,8 +1,35 @@
|
|||||||
export default function SearchesPage() {
|
export const dynamic = "force-dynamic";
|
||||||
|
|
||||||
|
import { Query } from "node-appwrite";
|
||||||
|
|
||||||
|
import { requireTenant } from "@/lib/appwrite/tenant-guard";
|
||||||
|
import { listCustomers } from "@/lib/appwrite/customer-queries";
|
||||||
|
import { DATABASE_ID, TABLES, type CustomerSearch } from "@/lib/appwrite/schema";
|
||||||
|
import { createAdminClient } from "@/lib/appwrite/server";
|
||||||
|
import { SearchesClient } from "@/components/customers/searches-client";
|
||||||
|
|
||||||
|
export default async function SearchesPage() {
|
||||||
|
const ctx = await requireTenant();
|
||||||
|
const { tablesDB } = createAdminClient();
|
||||||
|
|
||||||
|
const [customers, searchesResult] = await Promise.all([
|
||||||
|
listCustomers(ctx.tenantId),
|
||||||
|
tablesDB.listRows({
|
||||||
|
databaseId: DATABASE_ID,
|
||||||
|
tableId: TABLES.customerSearches,
|
||||||
|
queries: [
|
||||||
|
Query.equal("tenantId", ctx.tenantId),
|
||||||
|
Query.orderDesc("$createdAt"),
|
||||||
|
Query.limit(500),
|
||||||
|
],
|
||||||
|
}),
|
||||||
|
]);
|
||||||
|
|
||||||
|
const searches = searchesResult.rows as unknown as CustomerSearch[];
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div className="flex flex-1 flex-col gap-4 p-4">
|
<div className="flex flex-1 flex-col gap-4 p-4 md:p-6">
|
||||||
<h1 className="text-2xl font-bold">Arama Kriterleri</h1>
|
<SearchesClient initialSearches={searches} customers={customers} />
|
||||||
<p className="text-muted-foreground">Yakında...</p>
|
|
||||||
</div>
|
</div>
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,8 +1,31 @@
|
|||||||
export default function Page() {
|
export const dynamic = "force-dynamic";
|
||||||
|
|
||||||
|
import { Query } from "node-appwrite";
|
||||||
|
|
||||||
|
import { requireTenant } from "@/lib/appwrite/tenant-guard";
|
||||||
|
import { DATABASE_ID, TABLES, type Investor } from "@/lib/appwrite/schema";
|
||||||
|
import { createAdminClient } from "@/lib/appwrite/server";
|
||||||
|
import { InvestorsClient } from "@/components/investors/investors-client";
|
||||||
|
|
||||||
|
export default async function InvestorsPage() {
|
||||||
|
const ctx = await requireTenant();
|
||||||
|
const { tablesDB } = createAdminClient();
|
||||||
|
|
||||||
|
const result = await tablesDB.listRows({
|
||||||
|
databaseId: DATABASE_ID,
|
||||||
|
tableId: TABLES.investors,
|
||||||
|
queries: [
|
||||||
|
Query.equal("tenantId", ctx.tenantId),
|
||||||
|
Query.orderDesc("$createdAt"),
|
||||||
|
Query.limit(200),
|
||||||
|
],
|
||||||
|
});
|
||||||
|
|
||||||
|
const investors = result.rows as unknown as Investor[];
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div className="flex flex-1 flex-col gap-4 p-4">
|
<div className="flex flex-1 flex-col gap-4 p-4 md:p-6">
|
||||||
<h1 className="text-2xl font-bold capitalize">investors</h1>
|
<InvestorsClient initialInvestors={investors} />
|
||||||
<p className="text-muted-foreground">Yakında...</p>
|
|
||||||
</div>
|
</div>
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,8 +1,41 @@
|
|||||||
export default function Page() {
|
export const dynamic = "force-dynamic";
|
||||||
|
|
||||||
|
import { Query } from "node-appwrite";
|
||||||
|
|
||||||
|
import { requireTenant } from "@/lib/appwrite/tenant-guard";
|
||||||
|
import { listCustomers } from "@/lib/appwrite/customer-queries";
|
||||||
|
import { listProperties } from "@/lib/appwrite/property-queries";
|
||||||
|
import { DATABASE_ID, TABLES, type Presentation } from "@/lib/appwrite/schema";
|
||||||
|
import { createAdminClient } from "@/lib/appwrite/server";
|
||||||
|
import { PresentationsClient } from "@/components/presentations/presentations-client";
|
||||||
|
|
||||||
|
export default async function PresentationsPage() {
|
||||||
|
const ctx = await requireTenant();
|
||||||
|
const { tablesDB } = createAdminClient();
|
||||||
|
|
||||||
|
const [customers, properties, presResult] = await Promise.all([
|
||||||
|
listCustomers(ctx.tenantId),
|
||||||
|
listProperties(ctx.tenantId),
|
||||||
|
tablesDB.listRows({
|
||||||
|
databaseId: DATABASE_ID,
|
||||||
|
tableId: TABLES.presentations,
|
||||||
|
queries: [
|
||||||
|
Query.equal("tenantId", ctx.tenantId),
|
||||||
|
Query.orderDesc("$createdAt"),
|
||||||
|
Query.limit(200),
|
||||||
|
],
|
||||||
|
}),
|
||||||
|
]);
|
||||||
|
|
||||||
|
const presentations = presResult.rows as unknown as Presentation[];
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div className="flex flex-1 flex-col gap-4 p-4">
|
<div className="flex flex-1 flex-col gap-4 p-4 md:p-6">
|
||||||
<h1 className="text-2xl font-bold capitalize">presentations</h1>
|
<PresentationsClient
|
||||||
<p className="text-muted-foreground">Yakında...</p>
|
initialPresentations={presentations}
|
||||||
|
customers={customers}
|
||||||
|
properties={properties}
|
||||||
|
/>
|
||||||
</div>
|
</div>
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,8 +1,16 @@
|
|||||||
export default function Page() {
|
export const dynamic = "force-dynamic";
|
||||||
|
|
||||||
|
import { requireTenant } from "@/lib/appwrite/tenant-guard";
|
||||||
|
import { listProperties } from "@/lib/appwrite/property-queries";
|
||||||
|
import { PropertiesClient } from "@/components/properties/properties-client";
|
||||||
|
|
||||||
|
export default async function PropertiesPage() {
|
||||||
|
const ctx = await requireTenant();
|
||||||
|
const properties = await listProperties(ctx.tenantId);
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div className="flex flex-1 flex-col gap-4 p-4">
|
<div className="flex flex-1 flex-col gap-4 p-4 md:p-6">
|
||||||
<h1 className="text-2xl font-bold capitalize">properties</h1>
|
<PropertiesClient initialProperties={properties} />
|
||||||
<p className="text-muted-foreground">Yakında...</p>
|
|
||||||
</div>
|
</div>
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,138 @@
|
|||||||
|
import { notFound } from "next/navigation";
|
||||||
|
import { Query } from "node-appwrite";
|
||||||
|
|
||||||
|
import { DATABASE_ID, TABLES, type Presentation, type Property } from "@/lib/appwrite/schema";
|
||||||
|
import { createAdminClient } from "@/lib/appwrite/server";
|
||||||
|
import { incrementPresentationViewCount } from "@/lib/appwrite/presentation-actions";
|
||||||
|
import {
|
||||||
|
PROPERTY_TYPE_LABELS,
|
||||||
|
LISTING_TYPE_LABELS,
|
||||||
|
PROPERTY_STATUS_LABELS,
|
||||||
|
} from "@/lib/appwrite/schema";
|
||||||
|
|
||||||
|
interface Props {
|
||||||
|
params: Promise<{ token: string }>;
|
||||||
|
}
|
||||||
|
|
||||||
|
export default async function SunumPage({ params }: Props) {
|
||||||
|
const { token } = await params;
|
||||||
|
const { tablesDB } = createAdminClient();
|
||||||
|
|
||||||
|
const result = await tablesDB.listRows({
|
||||||
|
databaseId: DATABASE_ID,
|
||||||
|
tableId: TABLES.presentations,
|
||||||
|
queries: [Query.equal("shareToken", token), Query.limit(1)],
|
||||||
|
});
|
||||||
|
|
||||||
|
if (!result.rows.length) notFound();
|
||||||
|
|
||||||
|
const presentation = result.rows[0] as unknown as Presentation;
|
||||||
|
|
||||||
|
if (presentation.expiresAt && new Date(presentation.expiresAt) < new Date()) {
|
||||||
|
return (
|
||||||
|
<div className="min-h-screen flex items-center justify-center bg-gray-50">
|
||||||
|
<div className="text-center">
|
||||||
|
<h1 className="text-2xl font-bold text-gray-800 mb-2">Sunum Süresi Doldu</h1>
|
||||||
|
<p className="text-gray-500">Bu sunum artık geçerli değil.</p>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Increment view count (fire-and-forget)
|
||||||
|
void incrementPresentationViewCount(presentation.$id, presentation.viewCount ?? 0);
|
||||||
|
|
||||||
|
let propertyIds: string[] = [];
|
||||||
|
try {
|
||||||
|
propertyIds = JSON.parse(presentation.propertyIds) as string[];
|
||||||
|
} catch {
|
||||||
|
propertyIds = [];
|
||||||
|
}
|
||||||
|
|
||||||
|
const properties: Property[] = [];
|
||||||
|
for (const pid of propertyIds) {
|
||||||
|
try {
|
||||||
|
const row = await tablesDB.getRow(DATABASE_ID, TABLES.properties, pid);
|
||||||
|
properties.push(row as unknown as Property);
|
||||||
|
} catch {
|
||||||
|
// Property may have been deleted
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return (
|
||||||
|
<div className="min-h-screen bg-gray-50">
|
||||||
|
<header className="bg-white border-b px-6 py-4">
|
||||||
|
<h1 className="text-xl font-semibold text-gray-800">{presentation.title}</h1>
|
||||||
|
{presentation.notes && (
|
||||||
|
<p className="mt-1 text-sm text-gray-500">{presentation.notes}</p>
|
||||||
|
)}
|
||||||
|
<p className="mt-1 text-xs text-gray-400">{properties.length} ilan</p>
|
||||||
|
</header>
|
||||||
|
|
||||||
|
<main className="max-w-5xl mx-auto px-4 py-8 grid gap-6 sm:grid-cols-2 lg:grid-cols-3">
|
||||||
|
{properties.map((p) => (
|
||||||
|
<PropertyCard key={p.$id} property={p} />
|
||||||
|
))}
|
||||||
|
{properties.length === 0 && (
|
||||||
|
<p className="col-span-full text-center text-gray-400 py-16">
|
||||||
|
Bu sunumda ilan bulunmuyor.
|
||||||
|
</p>
|
||||||
|
)}
|
||||||
|
</main>
|
||||||
|
</div>
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
function PropertyCard({ property: p }: { property: Property }) {
|
||||||
|
const isExpired = p.status === "satildi" || p.status === "kiralandit";
|
||||||
|
|
||||||
|
return (
|
||||||
|
<div className={`bg-white rounded-xl border shadow-sm overflow-hidden ${isExpired ? "opacity-60" : ""}`}>
|
||||||
|
<div className="bg-gray-100 h-40 flex items-center justify-center text-4xl text-gray-300">
|
||||||
|
🏠
|
||||||
|
</div>
|
||||||
|
<div className="p-4 space-y-2">
|
||||||
|
<div className="flex items-start justify-between gap-2">
|
||||||
|
<h2 className="font-semibold text-gray-800 text-sm leading-tight">{p.title}</h2>
|
||||||
|
<span className={`text-xs px-1.5 py-0.5 rounded whitespace-nowrap font-medium ${
|
||||||
|
p.status === "aktif" ? "bg-green-100 text-green-700" :
|
||||||
|
p.status === "pasif" ? "bg-gray-100 text-gray-600" :
|
||||||
|
"bg-orange-100 text-orange-700"
|
||||||
|
}`}>
|
||||||
|
{PROPERTY_STATUS_LABELS[p.status] ?? p.status}
|
||||||
|
</span>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div className="flex gap-2 text-xs text-gray-500">
|
||||||
|
<span>{PROPERTY_TYPE_LABELS[p.propertyType] ?? p.propertyType}</span>
|
||||||
|
<span>·</span>
|
||||||
|
<span>{LISTING_TYPE_LABELS[p.listingType] ?? p.listingType}</span>
|
||||||
|
{p.roomCount && (
|
||||||
|
<>
|
||||||
|
<span>·</span>
|
||||||
|
<span>{p.roomCount}</span>
|
||||||
|
</>
|
||||||
|
)}
|
||||||
|
{p.netM2 && (
|
||||||
|
<>
|
||||||
|
<span>·</span>
|
||||||
|
<span>{p.netM2} m²</span>
|
||||||
|
</>
|
||||||
|
)}
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<p className="text-xs text-gray-500">
|
||||||
|
{[p.neighborhood, p.district, p.city].filter(Boolean).join(", ")}
|
||||||
|
</p>
|
||||||
|
|
||||||
|
<p className="text-lg font-bold text-gray-900">
|
||||||
|
{p.price.toLocaleString("tr-TR")} {p.currency ?? "TRY"}
|
||||||
|
</p>
|
||||||
|
|
||||||
|
{p.description && (
|
||||||
|
<p className="text-xs text-gray-500 line-clamp-3">{p.description}</p>
|
||||||
|
)}
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
);
|
||||||
|
}
|
||||||
@@ -0,0 +1,175 @@
|
|||||||
|
"use client";
|
||||||
|
|
||||||
|
import { useState } from "react";
|
||||||
|
import { MoreHorizontal, Plus, Pencil, Trash2, CheckCircle } from "lucide-react";
|
||||||
|
import { toast } from "sonner";
|
||||||
|
|
||||||
|
import { Button } from "@/components/ui/button";
|
||||||
|
import { Badge } from "@/components/ui/badge";
|
||||||
|
import {
|
||||||
|
Table, TableBody, TableCell, TableHead, TableHeader, TableRow,
|
||||||
|
} from "@/components/ui/table";
|
||||||
|
import {
|
||||||
|
DropdownMenu, DropdownMenuContent, DropdownMenuItem, DropdownMenuTrigger,
|
||||||
|
} from "@/components/ui/dropdown-menu";
|
||||||
|
import {
|
||||||
|
completeActivityAction,
|
||||||
|
deleteActivityAction,
|
||||||
|
} from "@/lib/appwrite/activity-actions";
|
||||||
|
import { ActivityFormSheet } from "./activity-form-sheet";
|
||||||
|
import type { Activity, Customer, Property } from "@/lib/appwrite/schema";
|
||||||
|
import { ACTIVITY_TYPE_LABELS } from "@/lib/appwrite/schema";
|
||||||
|
|
||||||
|
interface ActivitiesClientProps {
|
||||||
|
initialActivities: Activity[];
|
||||||
|
customers: Customer[];
|
||||||
|
properties: Property[];
|
||||||
|
}
|
||||||
|
|
||||||
|
export function ActivitiesClient({
|
||||||
|
initialActivities,
|
||||||
|
customers,
|
||||||
|
properties,
|
||||||
|
}: ActivitiesClientProps) {
|
||||||
|
const [activities, setActivities] = useState(initialActivities);
|
||||||
|
const [sheetOpen, setSheetOpen] = useState(false);
|
||||||
|
const [editing, setEditing] = useState<Activity | null>(null);
|
||||||
|
|
||||||
|
function customerName(id?: string | null) {
|
||||||
|
if (!id) return "—";
|
||||||
|
return customers.find((c) => c.$id === id)?.name ?? "—";
|
||||||
|
}
|
||||||
|
|
||||||
|
function propertyTitle(id?: string | null) {
|
||||||
|
if (!id) return "—";
|
||||||
|
return properties.find((p) => p.$id === id)?.title ?? "—";
|
||||||
|
}
|
||||||
|
|
||||||
|
function openCreate() {
|
||||||
|
setEditing(null);
|
||||||
|
setSheetOpen(true);
|
||||||
|
}
|
||||||
|
|
||||||
|
function openEdit(a: Activity) {
|
||||||
|
setEditing(a);
|
||||||
|
setSheetOpen(true);
|
||||||
|
}
|
||||||
|
|
||||||
|
async function handleComplete(a: Activity) {
|
||||||
|
const result = await completeActivityAction(a.$id);
|
||||||
|
if (result.ok) {
|
||||||
|
setActivities((prev) =>
|
||||||
|
prev.map((x) => x.$id === a.$id ? { ...x, completedAt: new Date().toISOString() } : x),
|
||||||
|
);
|
||||||
|
toast.success("Aktivite tamamlandı.");
|
||||||
|
} else {
|
||||||
|
toast.error(result.error ?? "Tamamlanamadı.");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
async function handleDelete(a: Activity) {
|
||||||
|
if (!confirm("Bu aktivite silinsin mi?")) return;
|
||||||
|
const result = await deleteActivityAction(a.$id);
|
||||||
|
if (result.ok) {
|
||||||
|
setActivities((prev) => prev.filter((x) => x.$id !== a.$id));
|
||||||
|
toast.success("Aktivite silindi.");
|
||||||
|
} else {
|
||||||
|
toast.error(result.error ?? "Silinemedi.");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return (
|
||||||
|
<div className="flex flex-col gap-4">
|
||||||
|
<div className="flex items-center justify-between">
|
||||||
|
<h1 className="text-2xl font-bold">Aktiviteler</h1>
|
||||||
|
<Button onClick={openCreate} size="sm">
|
||||||
|
<Plus className="mr-1.5 size-4" />
|
||||||
|
Yeni Aktivite
|
||||||
|
</Button>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div className="rounded-md border">
|
||||||
|
<Table>
|
||||||
|
<TableHeader>
|
||||||
|
<TableRow>
|
||||||
|
<TableHead>Tip</TableHead>
|
||||||
|
<TableHead>Başlık</TableHead>
|
||||||
|
<TableHead>Müşteri</TableHead>
|
||||||
|
<TableHead>İlan</TableHead>
|
||||||
|
<TableHead>Tarih</TableHead>
|
||||||
|
<TableHead>Durum</TableHead>
|
||||||
|
<TableHead />
|
||||||
|
</TableRow>
|
||||||
|
</TableHeader>
|
||||||
|
<TableBody>
|
||||||
|
{activities.length === 0 && (
|
||||||
|
<TableRow>
|
||||||
|
<TableCell colSpan={7} className="text-muted-foreground text-center py-10">
|
||||||
|
Henüz aktivite yok.
|
||||||
|
</TableCell>
|
||||||
|
</TableRow>
|
||||||
|
)}
|
||||||
|
{activities.map((a) => (
|
||||||
|
<TableRow key={a.$id}>
|
||||||
|
<TableCell>
|
||||||
|
<Badge variant="outline">
|
||||||
|
{ACTIVITY_TYPE_LABELS[a.type] ?? a.type}
|
||||||
|
</Badge>
|
||||||
|
</TableCell>
|
||||||
|
<TableCell className="font-medium max-w-[180px] truncate">{a.title}</TableCell>
|
||||||
|
<TableCell>{customerName(a.customerId)}</TableCell>
|
||||||
|
<TableCell className="max-w-[140px] truncate">{propertyTitle(a.propertyId)}</TableCell>
|
||||||
|
<TableCell className="text-muted-foreground">
|
||||||
|
{a.dueDate ? new Date(a.dueDate).toLocaleDateString("tr-TR") : "—"}
|
||||||
|
</TableCell>
|
||||||
|
<TableCell>
|
||||||
|
{a.completedAt ? (
|
||||||
|
<Badge variant="secondary">Tamamlandı</Badge>
|
||||||
|
) : (
|
||||||
|
<Badge>Açık</Badge>
|
||||||
|
)}
|
||||||
|
</TableCell>
|
||||||
|
<TableCell>
|
||||||
|
<DropdownMenu>
|
||||||
|
<DropdownMenuTrigger asChild>
|
||||||
|
<Button variant="ghost" size="icon" className="size-8">
|
||||||
|
<MoreHorizontal className="size-4" />
|
||||||
|
</Button>
|
||||||
|
</DropdownMenuTrigger>
|
||||||
|
<DropdownMenuContent align="end">
|
||||||
|
{!a.completedAt && (
|
||||||
|
<DropdownMenuItem onClick={() => handleComplete(a)}>
|
||||||
|
<CheckCircle className="mr-2 size-4" />
|
||||||
|
Tamamla
|
||||||
|
</DropdownMenuItem>
|
||||||
|
)}
|
||||||
|
<DropdownMenuItem onClick={() => openEdit(a)}>
|
||||||
|
<Pencil className="mr-2 size-4" />
|
||||||
|
Düzenle
|
||||||
|
</DropdownMenuItem>
|
||||||
|
<DropdownMenuItem
|
||||||
|
onClick={() => handleDelete(a)}
|
||||||
|
className="text-destructive focus:text-destructive"
|
||||||
|
>
|
||||||
|
<Trash2 className="mr-2 size-4" />
|
||||||
|
Sil
|
||||||
|
</DropdownMenuItem>
|
||||||
|
</DropdownMenuContent>
|
||||||
|
</DropdownMenu>
|
||||||
|
</TableCell>
|
||||||
|
</TableRow>
|
||||||
|
))}
|
||||||
|
</TableBody>
|
||||||
|
</Table>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<ActivityFormSheet
|
||||||
|
open={sheetOpen}
|
||||||
|
onOpenChange={setSheetOpen}
|
||||||
|
activity={editing}
|
||||||
|
customers={customers}
|
||||||
|
properties={properties}
|
||||||
|
/>
|
||||||
|
</div>
|
||||||
|
);
|
||||||
|
}
|
||||||
@@ -0,0 +1,141 @@
|
|||||||
|
"use client";
|
||||||
|
|
||||||
|
import { useActionState, useEffect } from "react";
|
||||||
|
import { Loader2 } from "lucide-react";
|
||||||
|
import { toast } from "sonner";
|
||||||
|
|
||||||
|
import { Button } from "@/components/ui/button";
|
||||||
|
import { Input } from "@/components/ui/input";
|
||||||
|
import { Label } from "@/components/ui/label";
|
||||||
|
import { Textarea } from "@/components/ui/textarea";
|
||||||
|
import {
|
||||||
|
Sheet,
|
||||||
|
SheetContent,
|
||||||
|
SheetFooter,
|
||||||
|
SheetHeader,
|
||||||
|
SheetTitle,
|
||||||
|
} from "@/components/ui/sheet";
|
||||||
|
import { createActivityAction, updateActivityAction } from "@/lib/appwrite/activity-actions";
|
||||||
|
import type { Activity, Customer, Property } from "@/lib/appwrite/schema";
|
||||||
|
|
||||||
|
type ActionState = { ok: boolean; error?: string; fieldErrors?: Record<string, string[]> };
|
||||||
|
const INITIAL: ActionState = { ok: false };
|
||||||
|
|
||||||
|
interface ActivityFormSheetProps {
|
||||||
|
open: boolean;
|
||||||
|
onOpenChange: (v: boolean) => void;
|
||||||
|
activity?: Activity | null;
|
||||||
|
customers: Customer[];
|
||||||
|
properties: Property[];
|
||||||
|
onSuccess?: () => void;
|
||||||
|
}
|
||||||
|
|
||||||
|
export function ActivityFormSheet({
|
||||||
|
open,
|
||||||
|
onOpenChange,
|
||||||
|
activity,
|
||||||
|
customers,
|
||||||
|
properties,
|
||||||
|
onSuccess,
|
||||||
|
}: ActivityFormSheetProps) {
|
||||||
|
const action = activity
|
||||||
|
? updateActivityAction.bind(null, activity.$id)
|
||||||
|
: createActivityAction;
|
||||||
|
|
||||||
|
const [state, formAction, isPending] = useActionState(action, INITIAL);
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
if (state.ok) {
|
||||||
|
toast.success(activity ? "Aktivite güncellendi." : "Aktivite oluşturuldu.");
|
||||||
|
onSuccess?.();
|
||||||
|
onOpenChange(false);
|
||||||
|
} else if (state.error) {
|
||||||
|
toast.error(state.error);
|
||||||
|
}
|
||||||
|
}, [state]);
|
||||||
|
|
||||||
|
const fe = state.fieldErrors ?? {};
|
||||||
|
|
||||||
|
return (
|
||||||
|
<Sheet open={open} onOpenChange={onOpenChange}>
|
||||||
|
<SheetContent className="sm:max-w-md">
|
||||||
|
<SheetHeader>
|
||||||
|
<SheetTitle>{activity ? "Aktiviteyi Düzenle" : "Yeni Aktivite"}</SheetTitle>
|
||||||
|
</SheetHeader>
|
||||||
|
|
||||||
|
<form action={formAction} className="mt-4 space-y-4 pb-6">
|
||||||
|
<div className="grid gap-1.5">
|
||||||
|
<Label>Tip *</Label>
|
||||||
|
<select
|
||||||
|
name="type"
|
||||||
|
defaultValue={activity?.type ?? "gorusme"}
|
||||||
|
className="border-input bg-background h-9 rounded-md border px-3 text-sm"
|
||||||
|
>
|
||||||
|
<option value="gorusme">Görüşme</option>
|
||||||
|
<option value="teklif">Teklif</option>
|
||||||
|
<option value="ziyaret">Ziyaret</option>
|
||||||
|
<option value="arama">Arama</option>
|
||||||
|
<option value="not">Not</option>
|
||||||
|
</select>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div className="grid gap-1.5">
|
||||||
|
<Label htmlFor="title">Başlık *</Label>
|
||||||
|
<Input id="title" name="title" defaultValue={activity?.title} placeholder="Görüşme notu..." />
|
||||||
|
{fe.title && <p className="text-destructive text-xs">{fe.title[0]}</p>}
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div className="grid gap-1.5">
|
||||||
|
<Label>Müşteri</Label>
|
||||||
|
<select
|
||||||
|
name="customerId"
|
||||||
|
defaultValue={activity?.customerId ?? ""}
|
||||||
|
className="border-input bg-background h-9 rounded-md border px-3 text-sm"
|
||||||
|
>
|
||||||
|
<option value="">Seçiniz</option>
|
||||||
|
{customers.map((c) => (
|
||||||
|
<option key={c.$id} value={c.$id}>{c.name}</option>
|
||||||
|
))}
|
||||||
|
</select>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div className="grid gap-1.5">
|
||||||
|
<Label>İlan</Label>
|
||||||
|
<select
|
||||||
|
name="propertyId"
|
||||||
|
defaultValue={activity?.propertyId ?? ""}
|
||||||
|
className="border-input bg-background h-9 rounded-md border px-3 text-sm"
|
||||||
|
>
|
||||||
|
<option value="">Seçiniz</option>
|
||||||
|
{properties.map((p) => (
|
||||||
|
<option key={p.$id} value={p.$id}>{p.title}</option>
|
||||||
|
))}
|
||||||
|
</select>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div className="grid gap-1.5">
|
||||||
|
<Label htmlFor="dueDate">Tarih</Label>
|
||||||
|
<Input
|
||||||
|
id="dueDate"
|
||||||
|
name="dueDate"
|
||||||
|
type="date"
|
||||||
|
defaultValue={activity?.dueDate ? activity.dueDate.split("T")[0] : ""}
|
||||||
|
/>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div className="grid gap-1.5">
|
||||||
|
<Label htmlFor="description">Açıklama</Label>
|
||||||
|
<Textarea id="description" name="description" rows={3} defaultValue={activity?.description ?? ""} />
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<SheetFooter>
|
||||||
|
<Button type="submit" disabled={isPending} className="w-full">
|
||||||
|
{isPending && <Loader2 className="mr-2 size-4 animate-spin" />}
|
||||||
|
{activity ? "Güncelle" : "Oluştur"}
|
||||||
|
</Button>
|
||||||
|
</SheetFooter>
|
||||||
|
</form>
|
||||||
|
</SheetContent>
|
||||||
|
</Sheet>
|
||||||
|
);
|
||||||
|
}
|
||||||
@@ -0,0 +1,101 @@
|
|||||||
|
"use client";
|
||||||
|
|
||||||
|
import { useActionState, useEffect } from "react";
|
||||||
|
import { Loader2 } from "lucide-react";
|
||||||
|
import { toast } from "sonner";
|
||||||
|
|
||||||
|
import { Button } from "@/components/ui/button";
|
||||||
|
import { Input } from "@/components/ui/input";
|
||||||
|
import { Label } from "@/components/ui/label";
|
||||||
|
import { Textarea } from "@/components/ui/textarea";
|
||||||
|
import {
|
||||||
|
Sheet,
|
||||||
|
SheetContent,
|
||||||
|
SheetFooter,
|
||||||
|
SheetHeader,
|
||||||
|
SheetTitle,
|
||||||
|
} from "@/components/ui/sheet";
|
||||||
|
import { createCustomerAction, updateCustomerAction } from "@/lib/appwrite/customer-actions";
|
||||||
|
import type { Customer } from "@/lib/appwrite/schema";
|
||||||
|
|
||||||
|
type ActionState = { ok: boolean; error?: string; fieldErrors?: Record<string, string[]> };
|
||||||
|
const INITIAL: ActionState = { ok: false };
|
||||||
|
|
||||||
|
interface CustomerFormSheetProps {
|
||||||
|
open: boolean;
|
||||||
|
onOpenChange: (v: boolean) => void;
|
||||||
|
customer?: Customer | null;
|
||||||
|
onSuccess?: () => void;
|
||||||
|
}
|
||||||
|
|
||||||
|
export function CustomerFormSheet({ open, onOpenChange, customer, onSuccess }: CustomerFormSheetProps) {
|
||||||
|
const action = customer
|
||||||
|
? updateCustomerAction.bind(null, customer.$id)
|
||||||
|
: createCustomerAction;
|
||||||
|
|
||||||
|
const [state, formAction, isPending] = useActionState(action, INITIAL);
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
if (state.ok) {
|
||||||
|
toast.success(customer ? "Müşteri güncellendi." : "Müşteri oluşturuldu.");
|
||||||
|
onSuccess?.();
|
||||||
|
onOpenChange(false);
|
||||||
|
} else if (state.error) {
|
||||||
|
toast.error(state.error);
|
||||||
|
}
|
||||||
|
}, [state]);
|
||||||
|
|
||||||
|
const fe = state.fieldErrors ?? {};
|
||||||
|
|
||||||
|
return (
|
||||||
|
<Sheet open={open} onOpenChange={onOpenChange}>
|
||||||
|
<SheetContent className="sm:max-w-md">
|
||||||
|
<SheetHeader>
|
||||||
|
<SheetTitle>{customer ? "Müşteriyi Düzenle" : "Yeni Müşteri"}</SheetTitle>
|
||||||
|
</SheetHeader>
|
||||||
|
|
||||||
|
<form action={formAction} className="mt-4 space-y-4 pb-6">
|
||||||
|
<div className="grid gap-1.5">
|
||||||
|
<Label htmlFor="name">Ad Soyad *</Label>
|
||||||
|
<Input id="name" name="name" defaultValue={customer?.name} placeholder="Ahmet Yılmaz" />
|
||||||
|
{fe.name && <p className="text-destructive text-xs">{fe.name[0]}</p>}
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div className="grid gap-1.5">
|
||||||
|
<Label>Müşteri tipi *</Label>
|
||||||
|
<select name="type" defaultValue={customer?.type ?? "alici"}
|
||||||
|
className="border-input bg-background h-9 rounded-md border px-3 text-sm">
|
||||||
|
<option value="alici">Alıcı</option>
|
||||||
|
<option value="kiraci">Kiracı</option>
|
||||||
|
<option value="yatirimci">Yatırımcı</option>
|
||||||
|
</select>
|
||||||
|
{fe.type && <p className="text-destructive text-xs">{fe.type[0]}</p>}
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div className="grid gap-1.5">
|
||||||
|
<Label htmlFor="phone">Telefon</Label>
|
||||||
|
<Input id="phone" name="phone" type="tel" defaultValue={customer?.phone ?? ""} placeholder="+90 555 123 45 67" />
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div className="grid gap-1.5">
|
||||||
|
<Label htmlFor="email">Email</Label>
|
||||||
|
<Input id="email" name="email" type="email" defaultValue={customer?.email ?? ""} placeholder="ahmet@example.com" />
|
||||||
|
{fe.email && <p className="text-destructive text-xs">{fe.email[0]}</p>}
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div className="grid gap-1.5">
|
||||||
|
<Label htmlFor="notes">Notlar</Label>
|
||||||
|
<Textarea id="notes" name="notes" rows={3} defaultValue={customer?.notes ?? ""} placeholder="Müşteri hakkında notlar..." />
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<SheetFooter>
|
||||||
|
<Button type="submit" disabled={isPending} className="w-full">
|
||||||
|
{isPending && <Loader2 className="mr-2 size-4 animate-spin" />}
|
||||||
|
{customer ? "Güncelle" : "Oluştur"}
|
||||||
|
</Button>
|
||||||
|
</SheetFooter>
|
||||||
|
</form>
|
||||||
|
</SheetContent>
|
||||||
|
</Sheet>
|
||||||
|
);
|
||||||
|
}
|
||||||
@@ -0,0 +1,124 @@
|
|||||||
|
"use client";
|
||||||
|
|
||||||
|
import { useState } from "react";
|
||||||
|
import { MoreHorizontal, Plus, Pencil, Trash2 } from "lucide-react";
|
||||||
|
import { toast } from "sonner";
|
||||||
|
|
||||||
|
import { Button } from "@/components/ui/button";
|
||||||
|
import { Badge } from "@/components/ui/badge";
|
||||||
|
import {
|
||||||
|
Table, TableBody, TableCell, TableHead, TableHeader, TableRow,
|
||||||
|
} from "@/components/ui/table";
|
||||||
|
import {
|
||||||
|
DropdownMenu, DropdownMenuContent, DropdownMenuItem, DropdownMenuTrigger,
|
||||||
|
} from "@/components/ui/dropdown-menu";
|
||||||
|
import { deleteCustomerAction } from "@/lib/appwrite/customer-actions";
|
||||||
|
import { CustomerFormSheet } from "./customer-form-sheet";
|
||||||
|
import type { Customer } from "@/lib/appwrite/schema";
|
||||||
|
import { CUSTOMER_TYPE_LABELS } from "@/lib/appwrite/schema";
|
||||||
|
|
||||||
|
interface CustomersClientProps {
|
||||||
|
initialCustomers: Customer[];
|
||||||
|
}
|
||||||
|
|
||||||
|
export function CustomersClient({ initialCustomers }: CustomersClientProps) {
|
||||||
|
const [customers, setCustomers] = useState(initialCustomers);
|
||||||
|
const [sheetOpen, setSheetOpen] = useState(false);
|
||||||
|
const [editing, setEditing] = useState<Customer | null>(null);
|
||||||
|
|
||||||
|
function openCreate() {
|
||||||
|
setEditing(null);
|
||||||
|
setSheetOpen(true);
|
||||||
|
}
|
||||||
|
|
||||||
|
function openEdit(c: Customer) {
|
||||||
|
setEditing(c);
|
||||||
|
setSheetOpen(true);
|
||||||
|
}
|
||||||
|
|
||||||
|
async function handleDelete(c: Customer) {
|
||||||
|
if (!confirm(`"${c.name}" silinsin mi?`)) return;
|
||||||
|
const result = await deleteCustomerAction(c.$id);
|
||||||
|
if (result.ok) {
|
||||||
|
setCustomers((prev) => prev.filter((x) => x.$id !== c.$id));
|
||||||
|
toast.success("Müşteri silindi.");
|
||||||
|
} else {
|
||||||
|
toast.error(result.error ?? "Silinemedi.");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return (
|
||||||
|
<div className="flex flex-col gap-4">
|
||||||
|
<div className="flex items-center justify-between">
|
||||||
|
<h1 className="text-2xl font-bold">Müşteriler</h1>
|
||||||
|
<Button onClick={openCreate} size="sm">
|
||||||
|
<Plus className="mr-1.5 size-4" />
|
||||||
|
Yeni Müşteri
|
||||||
|
</Button>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div className="rounded-md border">
|
||||||
|
<Table>
|
||||||
|
<TableHeader>
|
||||||
|
<TableRow>
|
||||||
|
<TableHead>Ad Soyad</TableHead>
|
||||||
|
<TableHead>Tip</TableHead>
|
||||||
|
<TableHead>Telefon</TableHead>
|
||||||
|
<TableHead>Email</TableHead>
|
||||||
|
<TableHead />
|
||||||
|
</TableRow>
|
||||||
|
</TableHeader>
|
||||||
|
<TableBody>
|
||||||
|
{customers.length === 0 && (
|
||||||
|
<TableRow>
|
||||||
|
<TableCell colSpan={5} className="text-muted-foreground text-center py-10">
|
||||||
|
Henüz müşteri yok.
|
||||||
|
</TableCell>
|
||||||
|
</TableRow>
|
||||||
|
)}
|
||||||
|
{customers.map((c) => (
|
||||||
|
<TableRow key={c.$id}>
|
||||||
|
<TableCell className="font-medium">{c.name}</TableCell>
|
||||||
|
<TableCell>
|
||||||
|
<Badge variant="outline">
|
||||||
|
{CUSTOMER_TYPE_LABELS[c.type] ?? c.type}
|
||||||
|
</Badge>
|
||||||
|
</TableCell>
|
||||||
|
<TableCell className="text-muted-foreground">{c.phone ?? "—"}</TableCell>
|
||||||
|
<TableCell className="text-muted-foreground">{c.email ?? "—"}</TableCell>
|
||||||
|
<TableCell>
|
||||||
|
<DropdownMenu>
|
||||||
|
<DropdownMenuTrigger asChild>
|
||||||
|
<Button variant="ghost" size="icon" className="size-8">
|
||||||
|
<MoreHorizontal className="size-4" />
|
||||||
|
</Button>
|
||||||
|
</DropdownMenuTrigger>
|
||||||
|
<DropdownMenuContent align="end">
|
||||||
|
<DropdownMenuItem onClick={() => openEdit(c)}>
|
||||||
|
<Pencil className="mr-2 size-4" />
|
||||||
|
Düzenle
|
||||||
|
</DropdownMenuItem>
|
||||||
|
<DropdownMenuItem
|
||||||
|
onClick={() => handleDelete(c)}
|
||||||
|
className="text-destructive focus:text-destructive"
|
||||||
|
>
|
||||||
|
<Trash2 className="mr-2 size-4" />
|
||||||
|
Sil
|
||||||
|
</DropdownMenuItem>
|
||||||
|
</DropdownMenuContent>
|
||||||
|
</DropdownMenu>
|
||||||
|
</TableCell>
|
||||||
|
</TableRow>
|
||||||
|
))}
|
||||||
|
</TableBody>
|
||||||
|
</Table>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<CustomerFormSheet
|
||||||
|
open={sheetOpen}
|
||||||
|
onOpenChange={setSheetOpen}
|
||||||
|
customer={editing}
|
||||||
|
/>
|
||||||
|
</div>
|
||||||
|
);
|
||||||
|
}
|
||||||
@@ -0,0 +1,190 @@
|
|||||||
|
"use client";
|
||||||
|
|
||||||
|
import { useActionState, useEffect } from "react";
|
||||||
|
import { Loader2 } from "lucide-react";
|
||||||
|
import { toast } from "sonner";
|
||||||
|
|
||||||
|
import { Button } from "@/components/ui/button";
|
||||||
|
import { Input } from "@/components/ui/input";
|
||||||
|
import { Label } from "@/components/ui/label";
|
||||||
|
import { Textarea } from "@/components/ui/textarea";
|
||||||
|
import {
|
||||||
|
Sheet,
|
||||||
|
SheetContent,
|
||||||
|
SheetFooter,
|
||||||
|
SheetHeader,
|
||||||
|
SheetTitle,
|
||||||
|
} from "@/components/ui/sheet";
|
||||||
|
import {
|
||||||
|
createCustomerSearchAction,
|
||||||
|
updateCustomerSearchAction,
|
||||||
|
} from "@/lib/appwrite/customer-search-actions";
|
||||||
|
import type { Customer, CustomerSearch } from "@/lib/appwrite/schema";
|
||||||
|
|
||||||
|
type ActionState = { ok: boolean; error?: string; fieldErrors?: Record<string, string[]> };
|
||||||
|
const INITIAL: ActionState = { ok: false };
|
||||||
|
|
||||||
|
interface SearchFormSheetProps {
|
||||||
|
open: boolean;
|
||||||
|
onOpenChange: (v: boolean) => void;
|
||||||
|
search?: CustomerSearch | null;
|
||||||
|
customers: Customer[];
|
||||||
|
defaultCustomerId?: string;
|
||||||
|
onSuccess?: () => void;
|
||||||
|
}
|
||||||
|
|
||||||
|
export function SearchFormSheet({
|
||||||
|
open,
|
||||||
|
onOpenChange,
|
||||||
|
search,
|
||||||
|
customers,
|
||||||
|
defaultCustomerId,
|
||||||
|
onSuccess,
|
||||||
|
}: SearchFormSheetProps) {
|
||||||
|
const action = search
|
||||||
|
? updateCustomerSearchAction.bind(null, search.$id)
|
||||||
|
: createCustomerSearchAction;
|
||||||
|
|
||||||
|
const [state, formAction, isPending] = useActionState(action, INITIAL);
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
if (state.ok) {
|
||||||
|
toast.success(search ? "Arama kriteri güncellendi." : "Arama kriteri oluşturuldu.");
|
||||||
|
onSuccess?.();
|
||||||
|
onOpenChange(false);
|
||||||
|
} else if (state.error) {
|
||||||
|
toast.error(state.error);
|
||||||
|
}
|
||||||
|
}, [state]);
|
||||||
|
|
||||||
|
const fe = state.fieldErrors ?? {};
|
||||||
|
|
||||||
|
// Parse existing JSON array fields back to comma-separated strings for display
|
||||||
|
function parseJsonToInput(json?: string | null): string {
|
||||||
|
if (!json) return "";
|
||||||
|
try {
|
||||||
|
const arr = JSON.parse(json) as string[];
|
||||||
|
return arr.join(", ");
|
||||||
|
} catch {
|
||||||
|
return json;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return (
|
||||||
|
<Sheet open={open} onOpenChange={onOpenChange}>
|
||||||
|
<SheetContent className="sm:max-w-md overflow-y-auto">
|
||||||
|
<SheetHeader>
|
||||||
|
<SheetTitle>{search ? "Aramayı Düzenle" : "Yeni Arama Kriteri"}</SheetTitle>
|
||||||
|
</SheetHeader>
|
||||||
|
|
||||||
|
<form action={formAction} className="mt-4 space-y-4 pb-6">
|
||||||
|
<div className="grid gap-1.5">
|
||||||
|
<Label>Müşteri *</Label>
|
||||||
|
<select
|
||||||
|
name="customerId"
|
||||||
|
defaultValue={search?.customerId ?? defaultCustomerId ?? ""}
|
||||||
|
className="border-input bg-background h-9 rounded-md border px-3 text-sm"
|
||||||
|
>
|
||||||
|
<option value="">Müşteri seçin</option>
|
||||||
|
{customers.map((c) => (
|
||||||
|
<option key={c.$id} value={c.$id}>
|
||||||
|
{c.name}
|
||||||
|
</option>
|
||||||
|
))}
|
||||||
|
</select>
|
||||||
|
{fe.customerId && <p className="text-destructive text-xs">{fe.customerId[0]}</p>}
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div className="grid gap-1.5">
|
||||||
|
<Label>İlan türü</Label>
|
||||||
|
<select
|
||||||
|
name="listingType"
|
||||||
|
defaultValue={search?.listingType ?? ""}
|
||||||
|
className="border-input bg-background h-9 rounded-md border px-3 text-sm"
|
||||||
|
>
|
||||||
|
<option value="">Tümü</option>
|
||||||
|
<option value="satilik">Satılık</option>
|
||||||
|
<option value="kiralik">Kiralık</option>
|
||||||
|
</select>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div className="grid gap-1.5">
|
||||||
|
<Label htmlFor="propertyTypes">Emlak tipleri</Label>
|
||||||
|
<p className="text-muted-foreground text-xs">Virgülle ayırın. Örn: daire, villa</p>
|
||||||
|
<Input
|
||||||
|
id="propertyTypes"
|
||||||
|
name="propertyTypes"
|
||||||
|
defaultValue={parseJsonToInput(search?.propertyTypes)}
|
||||||
|
placeholder="daire, villa"
|
||||||
|
/>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div className="grid gap-1.5">
|
||||||
|
<Label htmlFor="roomCounts">Oda sayıları</Label>
|
||||||
|
<p className="text-muted-foreground text-xs">Virgülle ayırın. Örn: 2+1, 3+1</p>
|
||||||
|
<Input
|
||||||
|
id="roomCounts"
|
||||||
|
name="roomCounts"
|
||||||
|
defaultValue={parseJsonToInput(search?.roomCounts)}
|
||||||
|
placeholder="2+1, 3+1"
|
||||||
|
/>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div className="grid grid-cols-2 gap-3">
|
||||||
|
<div className="grid gap-1.5">
|
||||||
|
<Label htmlFor="minPrice">Min fiyat</Label>
|
||||||
|
<Input id="minPrice" name="minPrice" type="number" min="0" defaultValue={search?.minPrice ?? ""} />
|
||||||
|
</div>
|
||||||
|
<div className="grid gap-1.5">
|
||||||
|
<Label htmlFor="maxPrice">Max fiyat</Label>
|
||||||
|
<Input id="maxPrice" name="maxPrice" type="number" min="0" defaultValue={search?.maxPrice ?? ""} />
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div className="grid grid-cols-2 gap-3">
|
||||||
|
<div className="grid gap-1.5">
|
||||||
|
<Label htmlFor="minM2">Min m²</Label>
|
||||||
|
<Input id="minM2" name="minM2" type="number" min="0" defaultValue={search?.minM2 ?? ""} />
|
||||||
|
</div>
|
||||||
|
<div className="grid gap-1.5">
|
||||||
|
<Label htmlFor="maxM2">Max m²</Label>
|
||||||
|
<Input id="maxM2" name="maxM2" type="number" min="0" defaultValue={search?.maxM2 ?? ""} />
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div className="grid gap-1.5">
|
||||||
|
<Label htmlFor="cities">Şehirler</Label>
|
||||||
|
<Input
|
||||||
|
id="cities"
|
||||||
|
name="cities"
|
||||||
|
defaultValue={parseJsonToInput(search?.cities)}
|
||||||
|
placeholder="İstanbul, Ankara"
|
||||||
|
/>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div className="grid gap-1.5">
|
||||||
|
<Label htmlFor="districts">İlçeler</Label>
|
||||||
|
<Input
|
||||||
|
id="districts"
|
||||||
|
name="districts"
|
||||||
|
defaultValue={parseJsonToInput(search?.districts)}
|
||||||
|
placeholder="Kadıköy, Beşiktaş"
|
||||||
|
/>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div className="grid gap-1.5">
|
||||||
|
<Label htmlFor="notes">Notlar</Label>
|
||||||
|
<Textarea id="notes" name="notes" rows={2} defaultValue={search?.notes ?? ""} />
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<SheetFooter>
|
||||||
|
<Button type="submit" disabled={isPending} className="w-full">
|
||||||
|
{isPending && <Loader2 className="mr-2 size-4 animate-spin" />}
|
||||||
|
{search ? "Güncelle" : "Oluştur"}
|
||||||
|
</Button>
|
||||||
|
</SheetFooter>
|
||||||
|
</form>
|
||||||
|
</SheetContent>
|
||||||
|
</Sheet>
|
||||||
|
);
|
||||||
|
}
|
||||||
@@ -0,0 +1,171 @@
|
|||||||
|
"use client";
|
||||||
|
|
||||||
|
import { useState } from "react";
|
||||||
|
import { MoreHorizontal, Plus, Pencil, Trash2, ToggleLeft } from "lucide-react";
|
||||||
|
import { toast } from "sonner";
|
||||||
|
|
||||||
|
import { Button } from "@/components/ui/button";
|
||||||
|
import { Badge } from "@/components/ui/badge";
|
||||||
|
import {
|
||||||
|
Table, TableBody, TableCell, TableHead, TableHeader, TableRow,
|
||||||
|
} from "@/components/ui/table";
|
||||||
|
import {
|
||||||
|
DropdownMenu, DropdownMenuContent, DropdownMenuItem, DropdownMenuTrigger,
|
||||||
|
} from "@/components/ui/dropdown-menu";
|
||||||
|
import {
|
||||||
|
deleteCustomerSearchAction,
|
||||||
|
toggleCustomerSearchActiveAction,
|
||||||
|
} from "@/lib/appwrite/customer-search-actions";
|
||||||
|
import { SearchFormSheet } from "./search-form-sheet";
|
||||||
|
import type { Customer, CustomerSearch } from "@/lib/appwrite/schema";
|
||||||
|
|
||||||
|
interface SearchesClientProps {
|
||||||
|
initialSearches: CustomerSearch[];
|
||||||
|
customers: Customer[];
|
||||||
|
}
|
||||||
|
|
||||||
|
export function SearchesClient({ initialSearches, customers }: SearchesClientProps) {
|
||||||
|
const [searches, setSearches] = useState(initialSearches);
|
||||||
|
const [sheetOpen, setSheetOpen] = useState(false);
|
||||||
|
const [editing, setEditing] = useState<CustomerSearch | null>(null);
|
||||||
|
|
||||||
|
function customerName(id: string) {
|
||||||
|
return customers.find((c) => c.$id === id)?.name ?? id;
|
||||||
|
}
|
||||||
|
|
||||||
|
function openCreate() {
|
||||||
|
setEditing(null);
|
||||||
|
setSheetOpen(true);
|
||||||
|
}
|
||||||
|
|
||||||
|
function openEdit(s: CustomerSearch) {
|
||||||
|
setEditing(s);
|
||||||
|
setSheetOpen(true);
|
||||||
|
}
|
||||||
|
|
||||||
|
async function handleToggle(s: CustomerSearch) {
|
||||||
|
const next = !s.isActive;
|
||||||
|
const result = await toggleCustomerSearchActiveAction(s.$id, next);
|
||||||
|
if (result.ok) {
|
||||||
|
setSearches((prev) => prev.map((x) => x.$id === s.$id ? { ...x, isActive: next } : x));
|
||||||
|
} else {
|
||||||
|
toast.error(result.error ?? "Durum güncellenemedi.");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
async function handleDelete(s: CustomerSearch) {
|
||||||
|
if (!confirm("Bu arama kriteri silinsin mi?")) return;
|
||||||
|
const result = await deleteCustomerSearchAction(s.$id);
|
||||||
|
if (result.ok) {
|
||||||
|
setSearches((prev) => prev.filter((x) => x.$id !== s.$id));
|
||||||
|
toast.success("Arama kriteri silindi.");
|
||||||
|
} else {
|
||||||
|
toast.error(result.error ?? "Silinemedi.");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function formatRange(min?: number | null, max?: number | null, unit = "") {
|
||||||
|
if (min && max) return `${min.toLocaleString("tr-TR")}–${max.toLocaleString("tr-TR")} ${unit}`.trim();
|
||||||
|
if (min) return `≥ ${min.toLocaleString("tr-TR")} ${unit}`.trim();
|
||||||
|
if (max) return `≤ ${max.toLocaleString("tr-TR")} ${unit}`.trim();
|
||||||
|
return "—";
|
||||||
|
}
|
||||||
|
|
||||||
|
function parseJsonList(json?: string | null): string {
|
||||||
|
if (!json) return "—";
|
||||||
|
try {
|
||||||
|
const arr = JSON.parse(json) as string[];
|
||||||
|
return arr.length ? arr.join(", ") : "—";
|
||||||
|
} catch {
|
||||||
|
return json;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return (
|
||||||
|
<div className="flex flex-col gap-4">
|
||||||
|
<div className="flex items-center justify-between">
|
||||||
|
<h1 className="text-2xl font-bold">Arama Kriterleri</h1>
|
||||||
|
<Button onClick={openCreate} size="sm">
|
||||||
|
<Plus className="mr-1.5 size-4" />
|
||||||
|
Yeni Kriter
|
||||||
|
</Button>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div className="rounded-md border">
|
||||||
|
<Table>
|
||||||
|
<TableHeader>
|
||||||
|
<TableRow>
|
||||||
|
<TableHead>Müşteri</TableHead>
|
||||||
|
<TableHead>Tür</TableHead>
|
||||||
|
<TableHead>Oda</TableHead>
|
||||||
|
<TableHead>Fiyat Aralığı</TableHead>
|
||||||
|
<TableHead>Şehir / İlçe</TableHead>
|
||||||
|
<TableHead>Durum</TableHead>
|
||||||
|
<TableHead />
|
||||||
|
</TableRow>
|
||||||
|
</TableHeader>
|
||||||
|
<TableBody>
|
||||||
|
{searches.length === 0 && (
|
||||||
|
<TableRow>
|
||||||
|
<TableCell colSpan={7} className="text-muted-foreground text-center py-10">
|
||||||
|
Henüz arama kriteri yok.
|
||||||
|
</TableCell>
|
||||||
|
</TableRow>
|
||||||
|
)}
|
||||||
|
{searches.map((s) => (
|
||||||
|
<TableRow key={s.$id}>
|
||||||
|
<TableCell className="font-medium">{customerName(s.customerId)}</TableCell>
|
||||||
|
<TableCell>{s.listingType ?? "Tümü"}</TableCell>
|
||||||
|
<TableCell>{parseJsonList(s.roomCounts)}</TableCell>
|
||||||
|
<TableCell className="tabular-nums">{formatRange(s.minPrice, s.maxPrice, "₺")}</TableCell>
|
||||||
|
<TableCell>
|
||||||
|
{[parseJsonList(s.cities), parseJsonList(s.districts)]
|
||||||
|
.filter((v) => v !== "—")
|
||||||
|
.join(" / ") || "—"}
|
||||||
|
</TableCell>
|
||||||
|
<TableCell>
|
||||||
|
<Badge variant={s.isActive ? "default" : "secondary"}>
|
||||||
|
{s.isActive ? "Aktif" : "Pasif"}
|
||||||
|
</Badge>
|
||||||
|
</TableCell>
|
||||||
|
<TableCell>
|
||||||
|
<DropdownMenu>
|
||||||
|
<DropdownMenuTrigger asChild>
|
||||||
|
<Button variant="ghost" size="icon" className="size-8">
|
||||||
|
<MoreHorizontal className="size-4" />
|
||||||
|
</Button>
|
||||||
|
</DropdownMenuTrigger>
|
||||||
|
<DropdownMenuContent align="end">
|
||||||
|
<DropdownMenuItem onClick={() => openEdit(s)}>
|
||||||
|
<Pencil className="mr-2 size-4" />
|
||||||
|
Düzenle
|
||||||
|
</DropdownMenuItem>
|
||||||
|
<DropdownMenuItem onClick={() => handleToggle(s)}>
|
||||||
|
<ToggleLeft className="mr-2 size-4" />
|
||||||
|
{s.isActive ? "Pasif yap" : "Aktif yap"}
|
||||||
|
</DropdownMenuItem>
|
||||||
|
<DropdownMenuItem
|
||||||
|
onClick={() => handleDelete(s)}
|
||||||
|
className="text-destructive focus:text-destructive"
|
||||||
|
>
|
||||||
|
<Trash2 className="mr-2 size-4" />
|
||||||
|
Sil
|
||||||
|
</DropdownMenuItem>
|
||||||
|
</DropdownMenuContent>
|
||||||
|
</DropdownMenu>
|
||||||
|
</TableCell>
|
||||||
|
</TableRow>
|
||||||
|
))}
|
||||||
|
</TableBody>
|
||||||
|
</Table>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<SearchFormSheet
|
||||||
|
open={sheetOpen}
|
||||||
|
onOpenChange={setSheetOpen}
|
||||||
|
search={editing}
|
||||||
|
customers={customers}
|
||||||
|
/>
|
||||||
|
</div>
|
||||||
|
);
|
||||||
|
}
|
||||||
@@ -0,0 +1,109 @@
|
|||||||
|
"use client";
|
||||||
|
|
||||||
|
import { useActionState, useEffect } from "react";
|
||||||
|
import { Loader2 } from "lucide-react";
|
||||||
|
import { toast } from "sonner";
|
||||||
|
|
||||||
|
import { Button } from "@/components/ui/button";
|
||||||
|
import { Input } from "@/components/ui/input";
|
||||||
|
import { Label } from "@/components/ui/label";
|
||||||
|
import { Textarea } from "@/components/ui/textarea";
|
||||||
|
import {
|
||||||
|
Sheet,
|
||||||
|
SheetContent,
|
||||||
|
SheetFooter,
|
||||||
|
SheetHeader,
|
||||||
|
SheetTitle,
|
||||||
|
} from "@/components/ui/sheet";
|
||||||
|
import { createInvestorAction, updateInvestorAction } from "@/lib/appwrite/investor-actions";
|
||||||
|
import type { Investor } from "@/lib/appwrite/schema";
|
||||||
|
|
||||||
|
type ActionState = { ok: boolean; error?: string; fieldErrors?: Record<string, string[]> };
|
||||||
|
const INITIAL: ActionState = { ok: false };
|
||||||
|
|
||||||
|
interface InvestorFormSheetProps {
|
||||||
|
open: boolean;
|
||||||
|
onOpenChange: (v: boolean) => void;
|
||||||
|
investor?: Investor | null;
|
||||||
|
onSuccess?: () => void;
|
||||||
|
}
|
||||||
|
|
||||||
|
export function InvestorFormSheet({ open, onOpenChange, investor, onSuccess }: InvestorFormSheetProps) {
|
||||||
|
const action = investor
|
||||||
|
? updateInvestorAction.bind(null, investor.$id)
|
||||||
|
: createInvestorAction;
|
||||||
|
|
||||||
|
const [state, formAction, isPending] = useActionState(action, INITIAL);
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
if (state.ok) {
|
||||||
|
toast.success(investor ? "Yatırımcı güncellendi." : "Yatırımcı oluşturuldu.");
|
||||||
|
onSuccess?.();
|
||||||
|
onOpenChange(false);
|
||||||
|
} else if (state.error) {
|
||||||
|
toast.error(state.error);
|
||||||
|
}
|
||||||
|
}, [state]);
|
||||||
|
|
||||||
|
const fe = state.fieldErrors ?? {};
|
||||||
|
|
||||||
|
return (
|
||||||
|
<Sheet open={open} onOpenChange={onOpenChange}>
|
||||||
|
<SheetContent className="sm:max-w-md">
|
||||||
|
<SheetHeader>
|
||||||
|
<SheetTitle>{investor ? "Yatırımcıyı Düzenle" : "Yeni Yatırımcı"}</SheetTitle>
|
||||||
|
</SheetHeader>
|
||||||
|
|
||||||
|
<form action={formAction} className="mt-4 space-y-4 pb-6">
|
||||||
|
<div className="grid gap-1.5">
|
||||||
|
<Label htmlFor="name">Ad Soyad *</Label>
|
||||||
|
<Input id="name" name="name" defaultValue={investor?.name} placeholder="Mehmet Demir" />
|
||||||
|
{fe.name && <p className="text-destructive text-xs">{fe.name[0]}</p>}
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div className="grid gap-1.5">
|
||||||
|
<Label htmlFor="email">Email *</Label>
|
||||||
|
<Input id="email" name="email" type="email" defaultValue={investor?.email} placeholder="mehmet@example.com" />
|
||||||
|
{fe.email && <p className="text-destructive text-xs">{fe.email[0]}</p>}
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div className="grid gap-1.5">
|
||||||
|
<Label htmlFor="phone">Telefon</Label>
|
||||||
|
<Input id="phone" name="phone" type="tel" defaultValue={investor?.phone ?? ""} placeholder="+90 555 123 45 67" />
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div className="grid grid-cols-2 gap-3">
|
||||||
|
<div className="grid gap-1.5">
|
||||||
|
<Label htmlFor="budget">Bütçe</Label>
|
||||||
|
<Input id="budget" name="budget" type="number" min="0" defaultValue={investor?.budget ?? ""} placeholder="5000000" />
|
||||||
|
</div>
|
||||||
|
<div className="grid gap-1.5">
|
||||||
|
<Label>Para birimi</Label>
|
||||||
|
<select
|
||||||
|
name="currency"
|
||||||
|
defaultValue={investor?.currency ?? "TRY"}
|
||||||
|
className="border-input bg-background h-9 rounded-md border px-3 text-sm"
|
||||||
|
>
|
||||||
|
<option value="TRY">TRY</option>
|
||||||
|
<option value="USD">USD</option>
|
||||||
|
<option value="EUR">EUR</option>
|
||||||
|
</select>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div className="grid gap-1.5">
|
||||||
|
<Label htmlFor="notes">Notlar</Label>
|
||||||
|
<Textarea id="notes" name="notes" rows={3} defaultValue={investor?.notes ?? ""} placeholder="Yatırım tercihleri..." />
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<SheetFooter>
|
||||||
|
<Button type="submit" disabled={isPending} className="w-full">
|
||||||
|
{isPending && <Loader2 className="mr-2 size-4 animate-spin" />}
|
||||||
|
{investor ? "Güncelle" : "Oluştur"}
|
||||||
|
</Button>
|
||||||
|
</SheetFooter>
|
||||||
|
</form>
|
||||||
|
</SheetContent>
|
||||||
|
</Sheet>
|
||||||
|
);
|
||||||
|
}
|
||||||
@@ -0,0 +1,120 @@
|
|||||||
|
"use client";
|
||||||
|
|
||||||
|
import { useState } from "react";
|
||||||
|
import { MoreHorizontal, Plus, Pencil, Trash2 } from "lucide-react";
|
||||||
|
import { toast } from "sonner";
|
||||||
|
|
||||||
|
import { Button } from "@/components/ui/button";
|
||||||
|
import {
|
||||||
|
Table, TableBody, TableCell, TableHead, TableHeader, TableRow,
|
||||||
|
} from "@/components/ui/table";
|
||||||
|
import {
|
||||||
|
DropdownMenu, DropdownMenuContent, DropdownMenuItem, DropdownMenuTrigger,
|
||||||
|
} from "@/components/ui/dropdown-menu";
|
||||||
|
import { deleteInvestorAction } from "@/lib/appwrite/investor-actions";
|
||||||
|
import { InvestorFormSheet } from "./investor-form-sheet";
|
||||||
|
import type { Investor } from "@/lib/appwrite/schema";
|
||||||
|
|
||||||
|
interface InvestorsClientProps {
|
||||||
|
initialInvestors: Investor[];
|
||||||
|
}
|
||||||
|
|
||||||
|
export function InvestorsClient({ initialInvestors }: InvestorsClientProps) {
|
||||||
|
const [investors, setInvestors] = useState(initialInvestors);
|
||||||
|
const [sheetOpen, setSheetOpen] = useState(false);
|
||||||
|
const [editing, setEditing] = useState<Investor | null>(null);
|
||||||
|
|
||||||
|
function openCreate() {
|
||||||
|
setEditing(null);
|
||||||
|
setSheetOpen(true);
|
||||||
|
}
|
||||||
|
|
||||||
|
function openEdit(i: Investor) {
|
||||||
|
setEditing(i);
|
||||||
|
setSheetOpen(true);
|
||||||
|
}
|
||||||
|
|
||||||
|
async function handleDelete(i: Investor) {
|
||||||
|
if (!confirm(`"${i.name}" silinsin mi?`)) return;
|
||||||
|
const result = await deleteInvestorAction(i.$id);
|
||||||
|
if (result.ok) {
|
||||||
|
setInvestors((prev) => prev.filter((x) => x.$id !== i.$id));
|
||||||
|
toast.success("Yatırımcı silindi.");
|
||||||
|
} else {
|
||||||
|
toast.error(result.error ?? "Silinemedi.");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return (
|
||||||
|
<div className="flex flex-col gap-4">
|
||||||
|
<div className="flex items-center justify-between">
|
||||||
|
<h1 className="text-2xl font-bold">Yatırımcılar</h1>
|
||||||
|
<Button onClick={openCreate} size="sm">
|
||||||
|
<Plus className="mr-1.5 size-4" />
|
||||||
|
Yeni Yatırımcı
|
||||||
|
</Button>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div className="rounded-md border">
|
||||||
|
<Table>
|
||||||
|
<TableHeader>
|
||||||
|
<TableRow>
|
||||||
|
<TableHead>Ad Soyad</TableHead>
|
||||||
|
<TableHead>Email</TableHead>
|
||||||
|
<TableHead>Telefon</TableHead>
|
||||||
|
<TableHead className="text-right">Bütçe</TableHead>
|
||||||
|
<TableHead />
|
||||||
|
</TableRow>
|
||||||
|
</TableHeader>
|
||||||
|
<TableBody>
|
||||||
|
{investors.length === 0 && (
|
||||||
|
<TableRow>
|
||||||
|
<TableCell colSpan={5} className="text-muted-foreground text-center py-10">
|
||||||
|
Henüz yatırımcı yok.
|
||||||
|
</TableCell>
|
||||||
|
</TableRow>
|
||||||
|
)}
|
||||||
|
{investors.map((i) => (
|
||||||
|
<TableRow key={i.$id}>
|
||||||
|
<TableCell className="font-medium">{i.name}</TableCell>
|
||||||
|
<TableCell>{i.email}</TableCell>
|
||||||
|
<TableCell className="text-muted-foreground">{i.phone ?? "—"}</TableCell>
|
||||||
|
<TableCell className="text-right tabular-nums">
|
||||||
|
{i.budget ? `${i.budget.toLocaleString("tr-TR")} ${i.currency ?? "TRY"}` : "—"}
|
||||||
|
</TableCell>
|
||||||
|
<TableCell>
|
||||||
|
<DropdownMenu>
|
||||||
|
<DropdownMenuTrigger asChild>
|
||||||
|
<Button variant="ghost" size="icon" className="size-8">
|
||||||
|
<MoreHorizontal className="size-4" />
|
||||||
|
</Button>
|
||||||
|
</DropdownMenuTrigger>
|
||||||
|
<DropdownMenuContent align="end">
|
||||||
|
<DropdownMenuItem onClick={() => openEdit(i)}>
|
||||||
|
<Pencil className="mr-2 size-4" />
|
||||||
|
Düzenle
|
||||||
|
</DropdownMenuItem>
|
||||||
|
<DropdownMenuItem
|
||||||
|
onClick={() => handleDelete(i)}
|
||||||
|
className="text-destructive focus:text-destructive"
|
||||||
|
>
|
||||||
|
<Trash2 className="mr-2 size-4" />
|
||||||
|
Sil
|
||||||
|
</DropdownMenuItem>
|
||||||
|
</DropdownMenuContent>
|
||||||
|
</DropdownMenu>
|
||||||
|
</TableCell>
|
||||||
|
</TableRow>
|
||||||
|
))}
|
||||||
|
</TableBody>
|
||||||
|
</Table>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<InvestorFormSheet
|
||||||
|
open={sheetOpen}
|
||||||
|
onOpenChange={setSheetOpen}
|
||||||
|
investor={editing}
|
||||||
|
/>
|
||||||
|
</div>
|
||||||
|
);
|
||||||
|
}
|
||||||
@@ -0,0 +1,160 @@
|
|||||||
|
"use client";
|
||||||
|
|
||||||
|
import { useActionState, useEffect } from "react";
|
||||||
|
import { Loader2 } from "lucide-react";
|
||||||
|
import { toast } from "sonner";
|
||||||
|
|
||||||
|
import { Button } from "@/components/ui/button";
|
||||||
|
import { Input } from "@/components/ui/input";
|
||||||
|
import { Label } from "@/components/ui/label";
|
||||||
|
import { Textarea } from "@/components/ui/textarea";
|
||||||
|
import { Checkbox } from "@/components/ui/checkbox";
|
||||||
|
import {
|
||||||
|
Sheet,
|
||||||
|
SheetContent,
|
||||||
|
SheetFooter,
|
||||||
|
SheetHeader,
|
||||||
|
SheetTitle,
|
||||||
|
} from "@/components/ui/sheet";
|
||||||
|
import {
|
||||||
|
createPresentationAction,
|
||||||
|
updatePresentationAction,
|
||||||
|
} from "@/lib/appwrite/presentation-actions";
|
||||||
|
import type { Customer, Presentation, Property } from "@/lib/appwrite/schema";
|
||||||
|
|
||||||
|
type ActionState = { ok: boolean; error?: string; fieldErrors?: Record<string, string[]>; id?: string };
|
||||||
|
const INITIAL: ActionState = { ok: false };
|
||||||
|
|
||||||
|
interface PresentationFormSheetProps {
|
||||||
|
open: boolean;
|
||||||
|
onOpenChange: (v: boolean) => void;
|
||||||
|
presentation?: Presentation | null;
|
||||||
|
customers: Customer[];
|
||||||
|
properties: Property[];
|
||||||
|
onSuccess?: () => void;
|
||||||
|
}
|
||||||
|
|
||||||
|
export function PresentationFormSheet({
|
||||||
|
open,
|
||||||
|
onOpenChange,
|
||||||
|
presentation,
|
||||||
|
customers,
|
||||||
|
properties,
|
||||||
|
onSuccess,
|
||||||
|
}: PresentationFormSheetProps) {
|
||||||
|
const action = presentation
|
||||||
|
? updatePresentationAction.bind(null, presentation.$id)
|
||||||
|
: createPresentationAction;
|
||||||
|
|
||||||
|
const [state, formAction, isPending] = useActionState(action, INITIAL);
|
||||||
|
|
||||||
|
const selectedIds: string[] = presentation
|
||||||
|
? (JSON.parse(presentation.propertyIds || "[]") as string[])
|
||||||
|
: [];
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
if (state.ok) {
|
||||||
|
toast.success(presentation ? "Sunum güncellendi." : "Sunum oluşturuldu.");
|
||||||
|
onSuccess?.();
|
||||||
|
onOpenChange(false);
|
||||||
|
} else if (state.error) {
|
||||||
|
toast.error(state.error);
|
||||||
|
}
|
||||||
|
}, [state]);
|
||||||
|
|
||||||
|
const fe = state.fieldErrors ?? {};
|
||||||
|
|
||||||
|
return (
|
||||||
|
<Sheet open={open} onOpenChange={onOpenChange}>
|
||||||
|
<SheetContent className="sm:max-w-md overflow-y-auto">
|
||||||
|
<SheetHeader>
|
||||||
|
<SheetTitle>{presentation ? "Sunumu Düzenle" : "Yeni Sunum"}</SheetTitle>
|
||||||
|
</SheetHeader>
|
||||||
|
|
||||||
|
<form action={formAction} className="mt-4 space-y-4 pb-6">
|
||||||
|
<div className="grid gap-1.5">
|
||||||
|
<Label htmlFor="title">Sunum başlığı *</Label>
|
||||||
|
<Input id="title" name="title" defaultValue={presentation?.title} placeholder="Kadıköy 3+1 Seçenekleri" />
|
||||||
|
{fe.title && <p className="text-destructive text-xs">{fe.title[0]}</p>}
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div className="grid gap-1.5">
|
||||||
|
<Label>Müşteri</Label>
|
||||||
|
<select
|
||||||
|
name="customerId"
|
||||||
|
defaultValue={presentation?.customerId ?? ""}
|
||||||
|
className="border-input bg-background h-9 rounded-md border px-3 text-sm"
|
||||||
|
>
|
||||||
|
<option value="">Müşteri seçin (opsiyonel)</option>
|
||||||
|
{customers.map((c) => (
|
||||||
|
<option key={c.$id} value={c.$id}>{c.name}</option>
|
||||||
|
))}
|
||||||
|
</select>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div className="grid gap-1.5">
|
||||||
|
<Label>İlanlar *</Label>
|
||||||
|
{fe.propertyIds && <p className="text-destructive text-xs">{fe.propertyIds[0]}</p>}
|
||||||
|
<PropertyCheckboxList
|
||||||
|
properties={properties}
|
||||||
|
selectedIds={selectedIds}
|
||||||
|
/>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div className="grid gap-1.5">
|
||||||
|
<Label htmlFor="expiresAt">Geçerlilik tarihi</Label>
|
||||||
|
<Input
|
||||||
|
id="expiresAt"
|
||||||
|
name="expiresAt"
|
||||||
|
type="date"
|
||||||
|
defaultValue={presentation?.expiresAt ? presentation.expiresAt.split("T")[0] : ""}
|
||||||
|
/>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div className="grid gap-1.5">
|
||||||
|
<Label htmlFor="notes">Notlar</Label>
|
||||||
|
<Textarea id="notes" name="notes" rows={2} defaultValue={presentation?.notes ?? ""} />
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<SheetFooter>
|
||||||
|
<Button type="submit" disabled={isPending} className="w-full">
|
||||||
|
{isPending && <Loader2 className="mr-2 size-4 animate-spin" />}
|
||||||
|
{presentation ? "Güncelle" : "Oluştur"}
|
||||||
|
</Button>
|
||||||
|
</SheetFooter>
|
||||||
|
</form>
|
||||||
|
</SheetContent>
|
||||||
|
</Sheet>
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
function PropertyCheckboxList({
|
||||||
|
properties,
|
||||||
|
selectedIds,
|
||||||
|
}: {
|
||||||
|
properties: Property[];
|
||||||
|
selectedIds: string[];
|
||||||
|
}) {
|
||||||
|
// We encode selected propertyIds as a JSON array in a hidden input
|
||||||
|
// and use checkboxes with the same name to collect values
|
||||||
|
return (
|
||||||
|
<div className="max-h-48 overflow-y-auto rounded-md border p-2 space-y-1">
|
||||||
|
{properties.length === 0 && (
|
||||||
|
<p className="text-muted-foreground text-sm">Aktif ilan bulunamadı.</p>
|
||||||
|
)}
|
||||||
|
{properties.map((p) => (
|
||||||
|
<label key={p.$id} className="flex items-center gap-2 cursor-pointer rounded px-2 py-1 hover:bg-muted/50">
|
||||||
|
<Checkbox
|
||||||
|
name="propertyIdsRaw"
|
||||||
|
value={p.$id}
|
||||||
|
defaultChecked={selectedIds.includes(p.$id)}
|
||||||
|
/>
|
||||||
|
<span className="text-sm truncate">
|
||||||
|
{p.title} — {p.city}{p.district ? `, ${p.district}` : ""}
|
||||||
|
</span>
|
||||||
|
</label>
|
||||||
|
))}
|
||||||
|
{/* Hidden field placeholder — server action reads propertyIdsRaw[] and joins them */}
|
||||||
|
</div>
|
||||||
|
);
|
||||||
|
}
|
||||||
@@ -0,0 +1,164 @@
|
|||||||
|
"use client";
|
||||||
|
|
||||||
|
import { useState } from "react";
|
||||||
|
import { MoreHorizontal, Plus, Pencil, Trash2, ExternalLink, Copy } from "lucide-react";
|
||||||
|
import { toast } from "sonner";
|
||||||
|
|
||||||
|
import { Button } from "@/components/ui/button";
|
||||||
|
import {
|
||||||
|
Table, TableBody, TableCell, TableHead, TableHeader, TableRow,
|
||||||
|
} from "@/components/ui/table";
|
||||||
|
import {
|
||||||
|
DropdownMenu, DropdownMenuContent, DropdownMenuItem, DropdownMenuTrigger,
|
||||||
|
} from "@/components/ui/dropdown-menu";
|
||||||
|
import { deletePresentationAction } from "@/lib/appwrite/presentation-actions";
|
||||||
|
import { PresentationFormSheet } from "./presentation-form-sheet";
|
||||||
|
import type { Customer, Presentation, Property } from "@/lib/appwrite/schema";
|
||||||
|
|
||||||
|
interface PresentationsClientProps {
|
||||||
|
initialPresentations: Presentation[];
|
||||||
|
customers: Customer[];
|
||||||
|
properties: Property[];
|
||||||
|
}
|
||||||
|
|
||||||
|
export function PresentationsClient({
|
||||||
|
initialPresentations,
|
||||||
|
customers,
|
||||||
|
properties,
|
||||||
|
}: PresentationsClientProps) {
|
||||||
|
const [presentations, setPresentations] = useState(initialPresentations);
|
||||||
|
const [sheetOpen, setSheetOpen] = useState(false);
|
||||||
|
const [editing, setEditing] = useState<Presentation | null>(null);
|
||||||
|
|
||||||
|
const appUrl = typeof window !== "undefined" ? window.location.origin : "";
|
||||||
|
|
||||||
|
function getShareUrl(p: Presentation) {
|
||||||
|
return `${appUrl}/sunum/${p.shareToken}`;
|
||||||
|
}
|
||||||
|
|
||||||
|
function customerName(id?: string | null) {
|
||||||
|
if (!id) return "—";
|
||||||
|
return customers.find((c) => c.$id === id)?.name ?? id;
|
||||||
|
}
|
||||||
|
|
||||||
|
function propertyCount(p: Presentation) {
|
||||||
|
try {
|
||||||
|
return (JSON.parse(p.propertyIds) as string[]).length;
|
||||||
|
} catch {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function openCreate() {
|
||||||
|
setEditing(null);
|
||||||
|
setSheetOpen(true);
|
||||||
|
}
|
||||||
|
|
||||||
|
function openEdit(p: Presentation) {
|
||||||
|
setEditing(p);
|
||||||
|
setSheetOpen(true);
|
||||||
|
}
|
||||||
|
|
||||||
|
async function copyLink(p: Presentation) {
|
||||||
|
await navigator.clipboard.writeText(getShareUrl(p));
|
||||||
|
toast.success("Link kopyalandı.");
|
||||||
|
}
|
||||||
|
|
||||||
|
async function handleDelete(p: Presentation) {
|
||||||
|
if (!confirm(`"${p.title}" sunumu silinsin mi?`)) return;
|
||||||
|
const result = await deletePresentationAction(p.$id);
|
||||||
|
if (result.ok) {
|
||||||
|
setPresentations((prev) => prev.filter((x) => x.$id !== p.$id));
|
||||||
|
toast.success("Sunum silindi.");
|
||||||
|
} else {
|
||||||
|
toast.error(result.error ?? "Silinemedi.");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return (
|
||||||
|
<div className="flex flex-col gap-4">
|
||||||
|
<div className="flex items-center justify-between">
|
||||||
|
<h1 className="text-2xl font-bold">Sunumlar</h1>
|
||||||
|
<Button onClick={openCreate} size="sm">
|
||||||
|
<Plus className="mr-1.5 size-4" />
|
||||||
|
Yeni Sunum
|
||||||
|
</Button>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div className="rounded-md border">
|
||||||
|
<Table>
|
||||||
|
<TableHeader>
|
||||||
|
<TableRow>
|
||||||
|
<TableHead>Başlık</TableHead>
|
||||||
|
<TableHead>Müşteri</TableHead>
|
||||||
|
<TableHead>İlanlar</TableHead>
|
||||||
|
<TableHead>Görüntülenme</TableHead>
|
||||||
|
<TableHead>Geçerlilik</TableHead>
|
||||||
|
<TableHead />
|
||||||
|
</TableRow>
|
||||||
|
</TableHeader>
|
||||||
|
<TableBody>
|
||||||
|
{presentations.length === 0 && (
|
||||||
|
<TableRow>
|
||||||
|
<TableCell colSpan={6} className="text-muted-foreground text-center py-10">
|
||||||
|
Henüz sunum yok.
|
||||||
|
</TableCell>
|
||||||
|
</TableRow>
|
||||||
|
)}
|
||||||
|
{presentations.map((p) => (
|
||||||
|
<TableRow key={p.$id}>
|
||||||
|
<TableCell className="font-medium">{p.title}</TableCell>
|
||||||
|
<TableCell>{customerName(p.customerId)}</TableCell>
|
||||||
|
<TableCell>{propertyCount(p)} ilan</TableCell>
|
||||||
|
<TableCell>{p.viewCount ?? 0}</TableCell>
|
||||||
|
<TableCell className="text-muted-foreground">
|
||||||
|
{p.expiresAt ? new Date(p.expiresAt).toLocaleDateString("tr-TR") : "—"}
|
||||||
|
</TableCell>
|
||||||
|
<TableCell>
|
||||||
|
<DropdownMenu>
|
||||||
|
<DropdownMenuTrigger asChild>
|
||||||
|
<Button variant="ghost" size="icon" className="size-8">
|
||||||
|
<MoreHorizontal className="size-4" />
|
||||||
|
</Button>
|
||||||
|
</DropdownMenuTrigger>
|
||||||
|
<DropdownMenuContent align="end">
|
||||||
|
<DropdownMenuItem onClick={() => copyLink(p)}>
|
||||||
|
<Copy className="mr-2 size-4" />
|
||||||
|
Linki kopyala
|
||||||
|
</DropdownMenuItem>
|
||||||
|
<DropdownMenuItem asChild>
|
||||||
|
<a href={getShareUrl(p)} target="_blank" rel="noopener noreferrer">
|
||||||
|
<ExternalLink className="mr-2 size-4" />
|
||||||
|
Önizleme
|
||||||
|
</a>
|
||||||
|
</DropdownMenuItem>
|
||||||
|
<DropdownMenuItem onClick={() => openEdit(p)}>
|
||||||
|
<Pencil className="mr-2 size-4" />
|
||||||
|
Düzenle
|
||||||
|
</DropdownMenuItem>
|
||||||
|
<DropdownMenuItem
|
||||||
|
onClick={() => handleDelete(p)}
|
||||||
|
className="text-destructive focus:text-destructive"
|
||||||
|
>
|
||||||
|
<Trash2 className="mr-2 size-4" />
|
||||||
|
Sil
|
||||||
|
</DropdownMenuItem>
|
||||||
|
</DropdownMenuContent>
|
||||||
|
</DropdownMenu>
|
||||||
|
</TableCell>
|
||||||
|
</TableRow>
|
||||||
|
))}
|
||||||
|
</TableBody>
|
||||||
|
</Table>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<PresentationFormSheet
|
||||||
|
open={sheetOpen}
|
||||||
|
onOpenChange={setSheetOpen}
|
||||||
|
presentation={editing}
|
||||||
|
customers={customers}
|
||||||
|
properties={properties}
|
||||||
|
/>
|
||||||
|
</div>
|
||||||
|
);
|
||||||
|
}
|
||||||
@@ -0,0 +1,153 @@
|
|||||||
|
"use client";
|
||||||
|
|
||||||
|
import { useState } from "react";
|
||||||
|
import { MoreHorizontal, Plus, Pencil, Trash2 } from "lucide-react";
|
||||||
|
import { toast } from "sonner";
|
||||||
|
|
||||||
|
import { Button } from "@/components/ui/button";
|
||||||
|
import { Badge } from "@/components/ui/badge";
|
||||||
|
import {
|
||||||
|
Table, TableBody, TableCell, TableHead, TableHeader, TableRow,
|
||||||
|
} from "@/components/ui/table";
|
||||||
|
import {
|
||||||
|
DropdownMenu, DropdownMenuContent, DropdownMenuItem, DropdownMenuTrigger,
|
||||||
|
} from "@/components/ui/dropdown-menu";
|
||||||
|
import { deletePropertyAction } from "@/lib/appwrite/property-actions";
|
||||||
|
import { PropertyFormSheet } from "./property-form-sheet";
|
||||||
|
import type { Property } from "@/lib/appwrite/schema";
|
||||||
|
import {
|
||||||
|
PROPERTY_STATUS_LABELS,
|
||||||
|
PROPERTY_TYPE_LABELS,
|
||||||
|
LISTING_TYPE_LABELS,
|
||||||
|
} from "@/lib/appwrite/schema";
|
||||||
|
|
||||||
|
interface PropertiesClientProps {
|
||||||
|
initialProperties: Property[];
|
||||||
|
}
|
||||||
|
|
||||||
|
export function PropertiesClient({ initialProperties }: PropertiesClientProps) {
|
||||||
|
const [properties, setProperties] = useState(initialProperties);
|
||||||
|
const [sheetOpen, setSheetOpen] = useState(false);
|
||||||
|
const [editing, setEditing] = useState<Property | null>(null);
|
||||||
|
|
||||||
|
function openCreate() {
|
||||||
|
setEditing(null);
|
||||||
|
setSheetOpen(true);
|
||||||
|
}
|
||||||
|
|
||||||
|
function openEdit(p: Property) {
|
||||||
|
setEditing(p);
|
||||||
|
setSheetOpen(true);
|
||||||
|
}
|
||||||
|
|
||||||
|
async function handleDelete(p: Property) {
|
||||||
|
if (!confirm(`"${p.title}" silinsin mi?`)) return;
|
||||||
|
const result = await deletePropertyAction(p.$id);
|
||||||
|
if (result.ok) {
|
||||||
|
setProperties((prev) => prev.filter((x) => x.$id !== p.$id));
|
||||||
|
toast.success("İlan silindi.");
|
||||||
|
} else {
|
||||||
|
toast.error(result.error ?? "Silinemedi.");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function handleSuccess() {
|
||||||
|
// Revalidation handles the data refresh; re-fetching in client is not
|
||||||
|
// necessary since the page will reload on next navigation.
|
||||||
|
// For immediate local update we just remove the item or close the sheet.
|
||||||
|
}
|
||||||
|
|
||||||
|
return (
|
||||||
|
<div className="flex flex-col gap-4">
|
||||||
|
<div className="flex items-center justify-between">
|
||||||
|
<h1 className="text-2xl font-bold">İlanlar</h1>
|
||||||
|
<Button onClick={openCreate} size="sm">
|
||||||
|
<Plus className="mr-1.5 size-4" />
|
||||||
|
Yeni İlan
|
||||||
|
</Button>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div className="rounded-md border">
|
||||||
|
<Table>
|
||||||
|
<TableHeader>
|
||||||
|
<TableRow>
|
||||||
|
<TableHead>Başlık</TableHead>
|
||||||
|
<TableHead>Tip</TableHead>
|
||||||
|
<TableHead>Tür</TableHead>
|
||||||
|
<TableHead>Şehir</TableHead>
|
||||||
|
<TableHead className="text-right">Fiyat</TableHead>
|
||||||
|
<TableHead>Durum</TableHead>
|
||||||
|
<TableHead />
|
||||||
|
</TableRow>
|
||||||
|
</TableHeader>
|
||||||
|
<TableBody>
|
||||||
|
{properties.length === 0 && (
|
||||||
|
<TableRow>
|
||||||
|
<TableCell colSpan={7} className="text-muted-foreground text-center py-10">
|
||||||
|
Henüz ilan yok.
|
||||||
|
</TableCell>
|
||||||
|
</TableRow>
|
||||||
|
)}
|
||||||
|
{properties.map((p) => (
|
||||||
|
<TableRow key={p.$id}>
|
||||||
|
<TableCell className="font-medium max-w-[200px] truncate">{p.title}</TableCell>
|
||||||
|
<TableCell>{PROPERTY_TYPE_LABELS[p.propertyType] ?? p.propertyType}</TableCell>
|
||||||
|
<TableCell>{LISTING_TYPE_LABELS[p.listingType] ?? p.listingType}</TableCell>
|
||||||
|
<TableCell>{[p.city, p.district].filter(Boolean).join(", ")}</TableCell>
|
||||||
|
<TableCell className="text-right tabular-nums">
|
||||||
|
{p.price.toLocaleString("tr-TR")} {p.currency ?? "TRY"}
|
||||||
|
</TableCell>
|
||||||
|
<TableCell>
|
||||||
|
<StatusBadge status={p.status} />
|
||||||
|
</TableCell>
|
||||||
|
<TableCell>
|
||||||
|
<DropdownMenu>
|
||||||
|
<DropdownMenuTrigger asChild>
|
||||||
|
<Button variant="ghost" size="icon" className="size-8">
|
||||||
|
<MoreHorizontal className="size-4" />
|
||||||
|
</Button>
|
||||||
|
</DropdownMenuTrigger>
|
||||||
|
<DropdownMenuContent align="end">
|
||||||
|
<DropdownMenuItem onClick={() => openEdit(p)}>
|
||||||
|
<Pencil className="mr-2 size-4" />
|
||||||
|
Düzenle
|
||||||
|
</DropdownMenuItem>
|
||||||
|
<DropdownMenuItem
|
||||||
|
onClick={() => handleDelete(p)}
|
||||||
|
className="text-destructive focus:text-destructive"
|
||||||
|
>
|
||||||
|
<Trash2 className="mr-2 size-4" />
|
||||||
|
Sil
|
||||||
|
</DropdownMenuItem>
|
||||||
|
</DropdownMenuContent>
|
||||||
|
</DropdownMenu>
|
||||||
|
</TableCell>
|
||||||
|
</TableRow>
|
||||||
|
))}
|
||||||
|
</TableBody>
|
||||||
|
</Table>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<PropertyFormSheet
|
||||||
|
open={sheetOpen}
|
||||||
|
onOpenChange={setSheetOpen}
|
||||||
|
property={editing}
|
||||||
|
onSuccess={handleSuccess}
|
||||||
|
/>
|
||||||
|
</div>
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
function StatusBadge({ status }: { status: string }) {
|
||||||
|
const map: Record<string, "default" | "secondary" | "outline" | "destructive"> = {
|
||||||
|
aktif: "default",
|
||||||
|
pasif: "secondary",
|
||||||
|
satildi: "outline",
|
||||||
|
kiralandit: "outline",
|
||||||
|
};
|
||||||
|
return (
|
||||||
|
<Badge variant={map[status] ?? "secondary"}>
|
||||||
|
{PROPERTY_STATUS_LABELS[status as keyof typeof PROPERTY_STATUS_LABELS] ?? status}
|
||||||
|
</Badge>
|
||||||
|
);
|
||||||
|
}
|
||||||
@@ -0,0 +1,181 @@
|
|||||||
|
"use client";
|
||||||
|
|
||||||
|
import { useActionState, useEffect } from "react";
|
||||||
|
import { Loader2 } from "lucide-react";
|
||||||
|
import { toast } from "sonner";
|
||||||
|
|
||||||
|
import { Button } from "@/components/ui/button";
|
||||||
|
import { Input } from "@/components/ui/input";
|
||||||
|
import { Label } from "@/components/ui/label";
|
||||||
|
import { Textarea } from "@/components/ui/textarea";
|
||||||
|
import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue } from "@/components/ui/select";
|
||||||
|
import {
|
||||||
|
Sheet,
|
||||||
|
SheetContent,
|
||||||
|
SheetFooter,
|
||||||
|
SheetHeader,
|
||||||
|
SheetTitle,
|
||||||
|
} from "@/components/ui/sheet";
|
||||||
|
import { createPropertyAction, updatePropertyAction } from "@/lib/appwrite/property-actions";
|
||||||
|
import type { Property } from "@/lib/appwrite/schema";
|
||||||
|
|
||||||
|
type ActionState = { ok: boolean; error?: string; fieldErrors?: Record<string, string[]> };
|
||||||
|
const INITIAL: ActionState = { ok: false };
|
||||||
|
|
||||||
|
interface PropertyFormSheetProps {
|
||||||
|
open: boolean;
|
||||||
|
onOpenChange: (v: boolean) => void;
|
||||||
|
property?: Property | null;
|
||||||
|
onSuccess?: () => void;
|
||||||
|
}
|
||||||
|
|
||||||
|
export function PropertyFormSheet({ open, onOpenChange, property, onSuccess }: PropertyFormSheetProps) {
|
||||||
|
const action = property
|
||||||
|
? updatePropertyAction.bind(null, property.$id)
|
||||||
|
: createPropertyAction;
|
||||||
|
|
||||||
|
const [state, formAction, isPending] = useActionState(action, INITIAL);
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
if (state.ok) {
|
||||||
|
toast.success(property ? "İlan güncellendi." : "İlan oluşturuldu.");
|
||||||
|
onSuccess?.();
|
||||||
|
onOpenChange(false);
|
||||||
|
} else if (state.error) {
|
||||||
|
toast.error(state.error);
|
||||||
|
}
|
||||||
|
}, [state]);
|
||||||
|
|
||||||
|
const fe = state.fieldErrors ?? {};
|
||||||
|
|
||||||
|
return (
|
||||||
|
<Sheet open={open} onOpenChange={onOpenChange}>
|
||||||
|
<SheetContent className="w-full sm:max-w-xl overflow-y-auto">
|
||||||
|
<SheetHeader>
|
||||||
|
<SheetTitle>{property ? "İlanı Düzenle" : "Yeni İlan"}</SheetTitle>
|
||||||
|
</SheetHeader>
|
||||||
|
|
||||||
|
<form action={formAction} className="mt-4 space-y-4 pb-6">
|
||||||
|
<div className="grid gap-1.5">
|
||||||
|
<Label htmlFor="title">Başlık *</Label>
|
||||||
|
<Input id="title" name="title" defaultValue={property?.title} placeholder="3+1 Daire, Kadıköy" />
|
||||||
|
{fe.title && <p className="text-destructive text-xs">{fe.title[0]}</p>}
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div className="grid grid-cols-2 gap-3">
|
||||||
|
<div className="grid gap-1.5">
|
||||||
|
<Label>Emlak tipi *</Label>
|
||||||
|
<select name="propertyType" defaultValue={property?.propertyType ?? "daire"}
|
||||||
|
className="border-input bg-background h-9 rounded-md border px-3 text-sm">
|
||||||
|
<option value="daire">Daire</option>
|
||||||
|
<option value="villa">Villa</option>
|
||||||
|
<option value="arsa">Arsa</option>
|
||||||
|
<option value="dukkan">Dükkan</option>
|
||||||
|
<option value="ofis">Ofis</option>
|
||||||
|
<option value="depo">Depo</option>
|
||||||
|
</select>
|
||||||
|
</div>
|
||||||
|
<div className="grid gap-1.5">
|
||||||
|
<Label>İlan türü *</Label>
|
||||||
|
<select name="listingType" defaultValue={property?.listingType ?? "satilik"}
|
||||||
|
className="border-input bg-background h-9 rounded-md border px-3 text-sm">
|
||||||
|
<option value="satilik">Satılık</option>
|
||||||
|
<option value="kiralik">Kiralık</option>
|
||||||
|
</select>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div className="grid grid-cols-2 gap-3">
|
||||||
|
<div className="grid gap-1.5">
|
||||||
|
<Label htmlFor="price">Fiyat *</Label>
|
||||||
|
<Input id="price" name="price" type="number" min="0" defaultValue={property?.price} placeholder="0" />
|
||||||
|
{fe.price && <p className="text-destructive text-xs">{fe.price[0]}</p>}
|
||||||
|
</div>
|
||||||
|
<div className="grid gap-1.5">
|
||||||
|
<Label>Durum</Label>
|
||||||
|
<select name="status" defaultValue={property?.status ?? "aktif"}
|
||||||
|
className="border-input bg-background h-9 rounded-md border px-3 text-sm">
|
||||||
|
<option value="aktif">Aktif</option>
|
||||||
|
<option value="pasif">Pasif</option>
|
||||||
|
<option value="satildi">Satıldı</option>
|
||||||
|
<option value="kiralandit">Kiralandı</option>
|
||||||
|
</select>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div className="grid grid-cols-3 gap-3">
|
||||||
|
<div className="grid gap-1.5">
|
||||||
|
<Label htmlFor="city">Şehir *</Label>
|
||||||
|
<Input id="city" name="city" defaultValue={property?.city} placeholder="İstanbul" />
|
||||||
|
{fe.city && <p className="text-destructive text-xs">{fe.city[0]}</p>}
|
||||||
|
</div>
|
||||||
|
<div className="grid gap-1.5">
|
||||||
|
<Label htmlFor="district">İlçe</Label>
|
||||||
|
<Input id="district" name="district" defaultValue={property?.district ?? ""} placeholder="Kadıköy" />
|
||||||
|
</div>
|
||||||
|
<div className="grid gap-1.5">
|
||||||
|
<Label htmlFor="neighborhood">Mahalle</Label>
|
||||||
|
<Input id="neighborhood" name="neighborhood" defaultValue={property?.neighborhood ?? ""} placeholder="Moda" />
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div className="grid grid-cols-2 gap-3">
|
||||||
|
<div className="grid gap-1.5">
|
||||||
|
<Label htmlFor="roomCount">Oda sayısı</Label>
|
||||||
|
<select name="roomCount" defaultValue={property?.roomCount ?? ""}
|
||||||
|
className="border-input bg-background h-9 rounded-md border px-3 text-sm">
|
||||||
|
<option value="">Seçiniz</option>
|
||||||
|
<option value="Stüdyo">Stüdyo</option>
|
||||||
|
<option value="1+0">1+0</option>
|
||||||
|
<option value="1+1">1+1</option>
|
||||||
|
<option value="2+1">2+1</option>
|
||||||
|
<option value="3+1">3+1</option>
|
||||||
|
<option value="4+1">4+1</option>
|
||||||
|
<option value="4+2">4+2</option>
|
||||||
|
<option value="5+1">5+1</option>
|
||||||
|
<option value="5+2">5+2</option>
|
||||||
|
<option value="6+">6+</option>
|
||||||
|
</select>
|
||||||
|
</div>
|
||||||
|
<div className="grid gap-1.5">
|
||||||
|
<Label htmlFor="netM2">Net m²</Label>
|
||||||
|
<Input id="netM2" name="netM2" type="number" min="0" defaultValue={property?.netM2 ?? ""} placeholder="90" />
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div className="grid grid-cols-3 gap-3">
|
||||||
|
<div className="grid gap-1.5">
|
||||||
|
<Label htmlFor="floor">Kat</Label>
|
||||||
|
<Input id="floor" name="floor" type="number" defaultValue={property?.floor ?? ""} />
|
||||||
|
</div>
|
||||||
|
<div className="grid gap-1.5">
|
||||||
|
<Label htmlFor="totalFloors">Top. kat</Label>
|
||||||
|
<Input id="totalFloors" name="totalFloors" type="number" defaultValue={property?.totalFloors ?? ""} />
|
||||||
|
</div>
|
||||||
|
<div className="grid gap-1.5">
|
||||||
|
<Label htmlFor="buildingAge">Bina yaşı</Label>
|
||||||
|
<Input id="buildingAge" name="buildingAge" type="number" min="0" defaultValue={property?.buildingAge ?? ""} />
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div className="grid gap-1.5">
|
||||||
|
<Label htmlFor="address">Tam adres</Label>
|
||||||
|
<Textarea id="address" name="address" rows={2} defaultValue={property?.address ?? ""} placeholder="Sokak, kapı no..." />
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div className="grid gap-1.5">
|
||||||
|
<Label htmlFor="description">Açıklama</Label>
|
||||||
|
<Textarea id="description" name="description" rows={3} defaultValue={property?.description ?? ""} placeholder="İlan detayları..." />
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<SheetFooter>
|
||||||
|
<Button type="submit" disabled={isPending} className="w-full">
|
||||||
|
{isPending && <Loader2 className="mr-2 size-4 animate-spin" />}
|
||||||
|
{property ? "Güncelle" : "Oluştur"}
|
||||||
|
</Button>
|
||||||
|
</SheetFooter>
|
||||||
|
</form>
|
||||||
|
</SheetContent>
|
||||||
|
</Sheet>
|
||||||
|
);
|
||||||
|
}
|
||||||
@@ -0,0 +1,117 @@
|
|||||||
|
"use server";
|
||||||
|
|
||||||
|
import { ID, Permission, Role } from "node-appwrite";
|
||||||
|
import { revalidatePath } from "next/cache";
|
||||||
|
|
||||||
|
import { activitySchema } from "@/lib/validation/activities";
|
||||||
|
import { DATABASE_ID, TABLES } from "./schema";
|
||||||
|
import { createAdminClient } from "./server";
|
||||||
|
import { requireTenant } from "@/lib/appwrite/tenant-guard";
|
||||||
|
|
||||||
|
type ActionState = { ok: boolean; error?: string; fieldErrors?: Record<string, string[]> };
|
||||||
|
|
||||||
|
export async function createActivityAction(
|
||||||
|
_prev: ActionState,
|
||||||
|
formData: FormData,
|
||||||
|
): Promise<ActionState> {
|
||||||
|
const ctx = await requireTenant();
|
||||||
|
const raw = Object.fromEntries(formData.entries());
|
||||||
|
const parsed = activitySchema.safeParse(raw);
|
||||||
|
if (!parsed.success) {
|
||||||
|
return { ok: false, fieldErrors: parsed.error.flatten().fieldErrors };
|
||||||
|
}
|
||||||
|
|
||||||
|
const { tablesDB } = createAdminClient();
|
||||||
|
const data = parsed.data;
|
||||||
|
|
||||||
|
try {
|
||||||
|
await tablesDB.createRow(
|
||||||
|
DATABASE_ID,
|
||||||
|
TABLES.activities,
|
||||||
|
ID.unique(),
|
||||||
|
{
|
||||||
|
tenantId: ctx.tenantId,
|
||||||
|
type: data.type,
|
||||||
|
title: data.title,
|
||||||
|
description: data.description,
|
||||||
|
customerId: data.customerId,
|
||||||
|
propertyId: data.propertyId,
|
||||||
|
dueDate: data.dueDate,
|
||||||
|
createdBy: ctx.user.id,
|
||||||
|
},
|
||||||
|
[
|
||||||
|
Permission.read(Role.team(ctx.tenantId)),
|
||||||
|
Permission.update(Role.team(ctx.tenantId)),
|
||||||
|
Permission.delete(Role.team(ctx.tenantId, "owner")),
|
||||||
|
Permission.delete(Role.team(ctx.tenantId, "admin")),
|
||||||
|
],
|
||||||
|
);
|
||||||
|
} catch {
|
||||||
|
return { ok: false, error: "Aktivite oluşturulamadı." };
|
||||||
|
}
|
||||||
|
|
||||||
|
revalidatePath("/activities");
|
||||||
|
return { ok: true };
|
||||||
|
}
|
||||||
|
|
||||||
|
export async function updateActivityAction(
|
||||||
|
id: string,
|
||||||
|
_prev: ActionState,
|
||||||
|
formData: FormData,
|
||||||
|
): Promise<ActionState> {
|
||||||
|
await requireTenant();
|
||||||
|
const raw = Object.fromEntries(formData.entries());
|
||||||
|
const parsed = activitySchema.safeParse(raw);
|
||||||
|
if (!parsed.success) {
|
||||||
|
return { ok: false, fieldErrors: parsed.error.flatten().fieldErrors };
|
||||||
|
}
|
||||||
|
|
||||||
|
const { tablesDB } = createAdminClient();
|
||||||
|
const data = parsed.data;
|
||||||
|
|
||||||
|
try {
|
||||||
|
await tablesDB.updateRow(DATABASE_ID, TABLES.activities, id, {
|
||||||
|
type: data.type,
|
||||||
|
title: data.title,
|
||||||
|
description: data.description,
|
||||||
|
customerId: data.customerId,
|
||||||
|
propertyId: data.propertyId,
|
||||||
|
dueDate: data.dueDate,
|
||||||
|
});
|
||||||
|
} catch {
|
||||||
|
return { ok: false, error: "Aktivite güncellenemedi." };
|
||||||
|
}
|
||||||
|
|
||||||
|
revalidatePath("/activities");
|
||||||
|
return { ok: true };
|
||||||
|
}
|
||||||
|
|
||||||
|
export async function completeActivityAction(id: string): Promise<ActionState> {
|
||||||
|
await requireTenant();
|
||||||
|
const { tablesDB } = createAdminClient();
|
||||||
|
|
||||||
|
try {
|
||||||
|
await tablesDB.updateRow(DATABASE_ID, TABLES.activities, id, {
|
||||||
|
completedAt: new Date().toISOString(),
|
||||||
|
});
|
||||||
|
} catch {
|
||||||
|
return { ok: false, error: "Aktivite tamamlanamadı." };
|
||||||
|
}
|
||||||
|
|
||||||
|
revalidatePath("/activities");
|
||||||
|
return { ok: true };
|
||||||
|
}
|
||||||
|
|
||||||
|
export async function deleteActivityAction(id: string): Promise<ActionState> {
|
||||||
|
await requireTenant();
|
||||||
|
const { tablesDB } = createAdminClient();
|
||||||
|
|
||||||
|
try {
|
||||||
|
await tablesDB.deleteRow(DATABASE_ID, TABLES.activities, id);
|
||||||
|
} catch {
|
||||||
|
return { ok: false, error: "Aktivite silinemedi." };
|
||||||
|
}
|
||||||
|
|
||||||
|
revalidatePath("/activities");
|
||||||
|
return { ok: true };
|
||||||
|
}
|
||||||
@@ -0,0 +1,99 @@
|
|||||||
|
"use server";
|
||||||
|
|
||||||
|
import { ID, Permission, Role } from "node-appwrite";
|
||||||
|
import { revalidatePath } from "next/cache";
|
||||||
|
|
||||||
|
import { customerSchema } from "@/lib/validation/customers";
|
||||||
|
import { DATABASE_ID, TABLES } from "./schema";
|
||||||
|
import { createAdminClient } from "./server";
|
||||||
|
import { requireTenant } from "@/lib/appwrite/tenant-guard";
|
||||||
|
|
||||||
|
type ActionState = { ok: boolean; error?: string; fieldErrors?: Record<string, string[]> };
|
||||||
|
|
||||||
|
export async function createCustomerAction(
|
||||||
|
_prev: ActionState,
|
||||||
|
formData: FormData,
|
||||||
|
): Promise<ActionState> {
|
||||||
|
const ctx = await requireTenant();
|
||||||
|
const raw = Object.fromEntries(formData.entries());
|
||||||
|
const parsed = customerSchema.safeParse(raw);
|
||||||
|
if (!parsed.success) {
|
||||||
|
return { ok: false, fieldErrors: parsed.error.flatten().fieldErrors };
|
||||||
|
}
|
||||||
|
|
||||||
|
const { tablesDB } = createAdminClient();
|
||||||
|
const data = parsed.data;
|
||||||
|
|
||||||
|
try {
|
||||||
|
await tablesDB.createRow(
|
||||||
|
DATABASE_ID,
|
||||||
|
TABLES.customers,
|
||||||
|
ID.unique(),
|
||||||
|
{
|
||||||
|
tenantId: ctx.tenantId,
|
||||||
|
name: data.name,
|
||||||
|
email: data.email,
|
||||||
|
phone: data.phone,
|
||||||
|
type: data.type,
|
||||||
|
notes: data.notes,
|
||||||
|
createdBy: ctx.user.id,
|
||||||
|
},
|
||||||
|
[
|
||||||
|
Permission.read(Role.team(ctx.tenantId)),
|
||||||
|
Permission.update(Role.team(ctx.tenantId)),
|
||||||
|
Permission.delete(Role.team(ctx.tenantId, "owner")),
|
||||||
|
Permission.delete(Role.team(ctx.tenantId, "admin")),
|
||||||
|
],
|
||||||
|
);
|
||||||
|
} catch {
|
||||||
|
return { ok: false, error: "Müşteri oluşturulamadı." };
|
||||||
|
}
|
||||||
|
|
||||||
|
revalidatePath("/customers");
|
||||||
|
return { ok: true };
|
||||||
|
}
|
||||||
|
|
||||||
|
export async function updateCustomerAction(
|
||||||
|
id: string,
|
||||||
|
_prev: ActionState,
|
||||||
|
formData: FormData,
|
||||||
|
): Promise<ActionState> {
|
||||||
|
await requireTenant();
|
||||||
|
const raw = Object.fromEntries(formData.entries());
|
||||||
|
const parsed = customerSchema.safeParse(raw);
|
||||||
|
if (!parsed.success) {
|
||||||
|
return { ok: false, fieldErrors: parsed.error.flatten().fieldErrors };
|
||||||
|
}
|
||||||
|
|
||||||
|
const { tablesDB } = createAdminClient();
|
||||||
|
const data = parsed.data;
|
||||||
|
|
||||||
|
try {
|
||||||
|
await tablesDB.updateRow(DATABASE_ID, TABLES.customers, id, {
|
||||||
|
name: data.name,
|
||||||
|
email: data.email,
|
||||||
|
phone: data.phone,
|
||||||
|
type: data.type,
|
||||||
|
notes: data.notes,
|
||||||
|
});
|
||||||
|
} catch {
|
||||||
|
return { ok: false, error: "Müşteri güncellenemedi." };
|
||||||
|
}
|
||||||
|
|
||||||
|
revalidatePath("/customers");
|
||||||
|
return { ok: true };
|
||||||
|
}
|
||||||
|
|
||||||
|
export async function deleteCustomerAction(id: string): Promise<ActionState> {
|
||||||
|
await requireTenant();
|
||||||
|
const { tablesDB } = createAdminClient();
|
||||||
|
|
||||||
|
try {
|
||||||
|
await tablesDB.deleteRow(DATABASE_ID, TABLES.customers, id);
|
||||||
|
} catch {
|
||||||
|
return { ok: false, error: "Müşteri silinemedi." };
|
||||||
|
}
|
||||||
|
|
||||||
|
revalidatePath("/customers");
|
||||||
|
return { ok: true };
|
||||||
|
}
|
||||||
@@ -0,0 +1,23 @@
|
|||||||
|
import "server-only";
|
||||||
|
|
||||||
|
import { Query } from "node-appwrite";
|
||||||
|
|
||||||
|
import { DATABASE_ID, TABLES, type Customer } from "./schema";
|
||||||
|
import { createAdminClient } from "./server";
|
||||||
|
|
||||||
|
export async function listCustomers(tenantId: string): Promise<Customer[]> {
|
||||||
|
const { tablesDB } = createAdminClient();
|
||||||
|
const result = await tablesDB.listRows({
|
||||||
|
databaseId: DATABASE_ID,
|
||||||
|
tableId: TABLES.customers,
|
||||||
|
queries: [Query.equal("tenantId", tenantId), Query.orderDesc("$createdAt"), Query.limit(500)],
|
||||||
|
});
|
||||||
|
return result.rows as unknown as Customer[];
|
||||||
|
}
|
||||||
|
|
||||||
|
export async function getCustomer(id: string, tenantId: string): Promise<Customer | null> {
|
||||||
|
const { tablesDB } = createAdminClient();
|
||||||
|
const row = (await tablesDB.getRow(DATABASE_ID, TABLES.customers, id)) as unknown as Customer;
|
||||||
|
if (row.tenantId !== tenantId) return null;
|
||||||
|
return row;
|
||||||
|
}
|
||||||
@@ -0,0 +1,160 @@
|
|||||||
|
"use server";
|
||||||
|
|
||||||
|
import { ID, Permission, Query, Role } from "node-appwrite";
|
||||||
|
import { revalidatePath } from "next/cache";
|
||||||
|
|
||||||
|
import { customerSearchSchema } from "@/lib/validation/customer-searches";
|
||||||
|
import { DATABASE_ID, TABLES, type Property } from "./schema";
|
||||||
|
import { createAdminClient } from "./server";
|
||||||
|
import { requireTenant } from "@/lib/appwrite/tenant-guard";
|
||||||
|
import { matchPropertyToSearches } from "./matching";
|
||||||
|
|
||||||
|
type ActionState = { ok: boolean; error?: string; fieldErrors?: Record<string, string[]> };
|
||||||
|
|
||||||
|
function toJsonList(csv?: string | null): string | undefined {
|
||||||
|
if (!csv || !csv.trim()) return undefined;
|
||||||
|
const items = csv
|
||||||
|
.split(",")
|
||||||
|
.map((s) => s.trim())
|
||||||
|
.filter(Boolean);
|
||||||
|
return items.length ? JSON.stringify(items) : undefined;
|
||||||
|
}
|
||||||
|
|
||||||
|
async function syncMatchesForSearch(tenantId: string, userId: string): Promise<void> {
|
||||||
|
const { tablesDB } = createAdminClient();
|
||||||
|
const result = await tablesDB.listRows({
|
||||||
|
databaseId: DATABASE_ID,
|
||||||
|
tableId: TABLES.properties,
|
||||||
|
queries: [
|
||||||
|
Query.equal("tenantId", tenantId),
|
||||||
|
Query.equal("status", "aktif"),
|
||||||
|
Query.limit(200),
|
||||||
|
],
|
||||||
|
});
|
||||||
|
const properties = result.rows as unknown as Property[];
|
||||||
|
for (const property of properties) {
|
||||||
|
await matchPropertyToSearches(property, tenantId, userId).catch(() => {});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
export async function createCustomerSearchAction(
|
||||||
|
_prev: ActionState,
|
||||||
|
formData: FormData,
|
||||||
|
): Promise<ActionState> {
|
||||||
|
const ctx = await requireTenant();
|
||||||
|
const raw = Object.fromEntries(formData.entries());
|
||||||
|
const parsed = customerSearchSchema.safeParse(raw);
|
||||||
|
if (!parsed.success) {
|
||||||
|
return { ok: false, fieldErrors: parsed.error.flatten().fieldErrors };
|
||||||
|
}
|
||||||
|
|
||||||
|
const { tablesDB } = createAdminClient();
|
||||||
|
const data = parsed.data;
|
||||||
|
|
||||||
|
try {
|
||||||
|
await tablesDB.createRow(
|
||||||
|
DATABASE_ID,
|
||||||
|
TABLES.customerSearches,
|
||||||
|
ID.unique(),
|
||||||
|
{
|
||||||
|
tenantId: ctx.tenantId,
|
||||||
|
customerId: data.customerId,
|
||||||
|
listingType: data.listingType || undefined,
|
||||||
|
propertyTypes: toJsonList(data.propertyTypes),
|
||||||
|
roomCounts: toJsonList(data.roomCounts),
|
||||||
|
minPrice: data.minPrice,
|
||||||
|
maxPrice: data.maxPrice,
|
||||||
|
minM2: data.minM2,
|
||||||
|
maxM2: data.maxM2,
|
||||||
|
cities: toJsonList(data.cities),
|
||||||
|
districts: toJsonList(data.districts),
|
||||||
|
isActive: true,
|
||||||
|
notes: data.notes,
|
||||||
|
createdBy: ctx.user.id,
|
||||||
|
},
|
||||||
|
[
|
||||||
|
Permission.read(Role.team(ctx.tenantId)),
|
||||||
|
Permission.update(Role.team(ctx.tenantId)),
|
||||||
|
Permission.delete(Role.team(ctx.tenantId, "owner")),
|
||||||
|
Permission.delete(Role.team(ctx.tenantId, "admin")),
|
||||||
|
],
|
||||||
|
);
|
||||||
|
|
||||||
|
await syncMatchesForSearch(ctx.tenantId, ctx.user.id);
|
||||||
|
} catch {
|
||||||
|
return { ok: false, error: "Arama kriteri oluşturulamadı." };
|
||||||
|
}
|
||||||
|
|
||||||
|
revalidatePath("/customers/searches");
|
||||||
|
return { ok: true };
|
||||||
|
}
|
||||||
|
|
||||||
|
export async function updateCustomerSearchAction(
|
||||||
|
id: string,
|
||||||
|
_prev: ActionState,
|
||||||
|
formData: FormData,
|
||||||
|
): Promise<ActionState> {
|
||||||
|
const ctx = await requireTenant();
|
||||||
|
const raw = Object.fromEntries(formData.entries());
|
||||||
|
const parsed = customerSearchSchema.safeParse(raw);
|
||||||
|
if (!parsed.success) {
|
||||||
|
return { ok: false, fieldErrors: parsed.error.flatten().fieldErrors };
|
||||||
|
}
|
||||||
|
|
||||||
|
const { tablesDB } = createAdminClient();
|
||||||
|
const data = parsed.data;
|
||||||
|
|
||||||
|
try {
|
||||||
|
await tablesDB.updateRow(DATABASE_ID, TABLES.customerSearches, id, {
|
||||||
|
customerId: data.customerId,
|
||||||
|
listingType: data.listingType || undefined,
|
||||||
|
propertyTypes: toJsonList(data.propertyTypes),
|
||||||
|
roomCounts: toJsonList(data.roomCounts),
|
||||||
|
minPrice: data.minPrice,
|
||||||
|
maxPrice: data.maxPrice,
|
||||||
|
minM2: data.minM2,
|
||||||
|
maxM2: data.maxM2,
|
||||||
|
cities: toJsonList(data.cities),
|
||||||
|
districts: toJsonList(data.districts),
|
||||||
|
notes: data.notes,
|
||||||
|
});
|
||||||
|
|
||||||
|
await syncMatchesForSearch(ctx.tenantId, ctx.user.id);
|
||||||
|
} catch {
|
||||||
|
return { ok: false, error: "Arama kriteri güncellenemedi." };
|
||||||
|
}
|
||||||
|
|
||||||
|
revalidatePath("/customers/searches");
|
||||||
|
return { ok: true };
|
||||||
|
}
|
||||||
|
|
||||||
|
export async function toggleCustomerSearchActiveAction(
|
||||||
|
id: string,
|
||||||
|
isActive: boolean,
|
||||||
|
): Promise<ActionState> {
|
||||||
|
await requireTenant();
|
||||||
|
const { tablesDB } = createAdminClient();
|
||||||
|
|
||||||
|
try {
|
||||||
|
await tablesDB.updateRow(DATABASE_ID, TABLES.customerSearches, id, { isActive });
|
||||||
|
} catch {
|
||||||
|
return { ok: false, error: "Durum güncellenemedi." };
|
||||||
|
}
|
||||||
|
|
||||||
|
revalidatePath("/customers/searches");
|
||||||
|
return { ok: true };
|
||||||
|
}
|
||||||
|
|
||||||
|
export async function deleteCustomerSearchAction(id: string): Promise<ActionState> {
|
||||||
|
await requireTenant();
|
||||||
|
const { tablesDB } = createAdminClient();
|
||||||
|
|
||||||
|
try {
|
||||||
|
await tablesDB.deleteRow(DATABASE_ID, TABLES.customerSearches, id);
|
||||||
|
} catch {
|
||||||
|
return { ok: false, error: "Arama kriteri silinemedi." };
|
||||||
|
}
|
||||||
|
|
||||||
|
revalidatePath("/customers/searches");
|
||||||
|
return { ok: true };
|
||||||
|
}
|
||||||
@@ -0,0 +1,128 @@
|
|||||||
|
"use server";
|
||||||
|
|
||||||
|
import { ID, Permission, Role } from "node-appwrite";
|
||||||
|
import { revalidatePath } from "next/cache";
|
||||||
|
import { z } from "zod";
|
||||||
|
|
||||||
|
import { DATABASE_ID, TABLES } from "./schema";
|
||||||
|
import { createAdminClient } from "./server";
|
||||||
|
import { requireTenant } from "@/lib/appwrite/tenant-guard";
|
||||||
|
|
||||||
|
const investorSchema = z.object({
|
||||||
|
name: z.string().trim().min(2, "En az 2 karakter").max(255),
|
||||||
|
email: z.string().email("Geçerli bir email girin."),
|
||||||
|
phone: z
|
||||||
|
.string()
|
||||||
|
.trim()
|
||||||
|
.max(30)
|
||||||
|
.optional()
|
||||||
|
.transform((v) => v || undefined),
|
||||||
|
budget: z
|
||||||
|
.string()
|
||||||
|
.optional()
|
||||||
|
.transform((v) => (v ? Number(v) : undefined))
|
||||||
|
.pipe(z.number().min(0).optional()),
|
||||||
|
currency: z
|
||||||
|
.string()
|
||||||
|
.max(10)
|
||||||
|
.optional()
|
||||||
|
.transform((v) => v || undefined),
|
||||||
|
notes: z
|
||||||
|
.string()
|
||||||
|
.trim()
|
||||||
|
.max(2000)
|
||||||
|
.optional()
|
||||||
|
.transform((v) => v || undefined),
|
||||||
|
});
|
||||||
|
|
||||||
|
type ActionState = { ok: boolean; error?: string; fieldErrors?: Record<string, string[]> };
|
||||||
|
|
||||||
|
export async function createInvestorAction(
|
||||||
|
_prev: ActionState,
|
||||||
|
formData: FormData,
|
||||||
|
): Promise<ActionState> {
|
||||||
|
const ctx = await requireTenant();
|
||||||
|
const raw = Object.fromEntries(formData.entries());
|
||||||
|
const parsed = investorSchema.safeParse(raw);
|
||||||
|
if (!parsed.success) {
|
||||||
|
return { ok: false, fieldErrors: parsed.error.flatten().fieldErrors };
|
||||||
|
}
|
||||||
|
|
||||||
|
const { tablesDB } = createAdminClient();
|
||||||
|
const data = parsed.data;
|
||||||
|
|
||||||
|
try {
|
||||||
|
await tablesDB.createRow(
|
||||||
|
DATABASE_ID,
|
||||||
|
TABLES.investors,
|
||||||
|
ID.unique(),
|
||||||
|
{
|
||||||
|
tenantId: ctx.tenantId,
|
||||||
|
name: data.name,
|
||||||
|
email: data.email,
|
||||||
|
phone: data.phone,
|
||||||
|
budget: data.budget,
|
||||||
|
currency: data.currency ?? "TRY",
|
||||||
|
notes: data.notes,
|
||||||
|
createdBy: ctx.user.id,
|
||||||
|
},
|
||||||
|
[
|
||||||
|
Permission.read(Role.team(ctx.tenantId)),
|
||||||
|
Permission.update(Role.team(ctx.tenantId)),
|
||||||
|
Permission.delete(Role.team(ctx.tenantId, "owner")),
|
||||||
|
Permission.delete(Role.team(ctx.tenantId, "admin")),
|
||||||
|
],
|
||||||
|
);
|
||||||
|
} catch {
|
||||||
|
return { ok: false, error: "Yatırımcı oluşturulamadı." };
|
||||||
|
}
|
||||||
|
|
||||||
|
revalidatePath("/investors");
|
||||||
|
return { ok: true };
|
||||||
|
}
|
||||||
|
|
||||||
|
export async function updateInvestorAction(
|
||||||
|
id: string,
|
||||||
|
_prev: ActionState,
|
||||||
|
formData: FormData,
|
||||||
|
): Promise<ActionState> {
|
||||||
|
await requireTenant();
|
||||||
|
const raw = Object.fromEntries(formData.entries());
|
||||||
|
const parsed = investorSchema.safeParse(raw);
|
||||||
|
if (!parsed.success) {
|
||||||
|
return { ok: false, fieldErrors: parsed.error.flatten().fieldErrors };
|
||||||
|
}
|
||||||
|
|
||||||
|
const { tablesDB } = createAdminClient();
|
||||||
|
const data = parsed.data;
|
||||||
|
|
||||||
|
try {
|
||||||
|
await tablesDB.updateRow(DATABASE_ID, TABLES.investors, id, {
|
||||||
|
name: data.name,
|
||||||
|
email: data.email,
|
||||||
|
phone: data.phone,
|
||||||
|
budget: data.budget,
|
||||||
|
currency: data.currency,
|
||||||
|
notes: data.notes,
|
||||||
|
});
|
||||||
|
} catch {
|
||||||
|
return { ok: false, error: "Yatırımcı güncellenemedi." };
|
||||||
|
}
|
||||||
|
|
||||||
|
revalidatePath("/investors");
|
||||||
|
return { ok: true };
|
||||||
|
}
|
||||||
|
|
||||||
|
export async function deleteInvestorAction(id: string): Promise<ActionState> {
|
||||||
|
await requireTenant();
|
||||||
|
const { tablesDB } = createAdminClient();
|
||||||
|
|
||||||
|
try {
|
||||||
|
await tablesDB.deleteRow(DATABASE_ID, TABLES.investors, id);
|
||||||
|
} catch {
|
||||||
|
return { ok: false, error: "Yatırımcı silinemedi." };
|
||||||
|
}
|
||||||
|
|
||||||
|
revalidatePath("/investors");
|
||||||
|
return { ok: true };
|
||||||
|
}
|
||||||
@@ -0,0 +1,117 @@
|
|||||||
|
import "server-only";
|
||||||
|
|
||||||
|
import { ID, Permission, Query, Role } from "node-appwrite";
|
||||||
|
|
||||||
|
import { DATABASE_ID, TABLES, type Property, type CustomerSearch } from "./schema";
|
||||||
|
import { createAdminClient } from "./server";
|
||||||
|
|
||||||
|
function priceMatches(price: number, min?: number | null, max?: number | null): boolean {
|
||||||
|
if (min != null && price < min) return false;
|
||||||
|
if (max != null && price > max) return false;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
function m2Matches(m2?: number | null, min?: number | null, max?: number | null): boolean {
|
||||||
|
if (!m2) return true;
|
||||||
|
if (min != null && m2 < min) return false;
|
||||||
|
if (max != null && m2 > max) return false;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
function listMatches(value: string, jsonList?: string | null): boolean {
|
||||||
|
if (!jsonList) return true;
|
||||||
|
try {
|
||||||
|
const arr = JSON.parse(jsonList) as string[];
|
||||||
|
if (!arr.length) return true;
|
||||||
|
return arr.includes(value);
|
||||||
|
} catch {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function cityMatches(city: string, jsonCities?: string | null): boolean {
|
||||||
|
if (!jsonCities) return true;
|
||||||
|
try {
|
||||||
|
const arr = JSON.parse(jsonCities) as string[];
|
||||||
|
if (!arr.length) return true;
|
||||||
|
return arr.some((c) => c.toLowerCase() === city.toLowerCase());
|
||||||
|
} catch {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function districtMatches(district?: string | null, jsonDistricts?: string | null): boolean {
|
||||||
|
if (!jsonDistricts) return true;
|
||||||
|
try {
|
||||||
|
const arr = JSON.parse(jsonDistricts) as string[];
|
||||||
|
if (!arr.length) return true;
|
||||||
|
if (!district) return false;
|
||||||
|
return arr.some((d) => d.toLowerCase() === district.toLowerCase());
|
||||||
|
} catch {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
export async function matchPropertyToSearches(
|
||||||
|
property: Property,
|
||||||
|
tenantId: string,
|
||||||
|
createdBy: string,
|
||||||
|
): Promise<void> {
|
||||||
|
if (property.status !== "aktif") return;
|
||||||
|
|
||||||
|
const { tablesDB } = createAdminClient();
|
||||||
|
|
||||||
|
const searchesResult = await tablesDB.listRows({
|
||||||
|
databaseId: DATABASE_ID,
|
||||||
|
tableId: TABLES.customerSearches,
|
||||||
|
queries: [
|
||||||
|
Query.equal("tenantId", tenantId),
|
||||||
|
Query.equal("isActive", true),
|
||||||
|
Query.limit(200),
|
||||||
|
],
|
||||||
|
});
|
||||||
|
|
||||||
|
const searches = searchesResult.rows as unknown as CustomerSearch[];
|
||||||
|
|
||||||
|
for (const search of searches) {
|
||||||
|
if (search.listingType && search.listingType !== property.listingType) continue;
|
||||||
|
if (!listMatches(property.propertyType, search.propertyTypes)) continue;
|
||||||
|
if (property.roomCount && !listMatches(property.roomCount, search.roomCounts)) continue;
|
||||||
|
if (!priceMatches(property.price, search.minPrice, search.maxPrice)) continue;
|
||||||
|
if (!m2Matches(property.netM2, search.minM2, search.maxM2)) continue;
|
||||||
|
if (!cityMatches(property.city, search.cities)) continue;
|
||||||
|
if (!districtMatches(property.district, search.districts)) continue;
|
||||||
|
|
||||||
|
// duplicate kontrolü
|
||||||
|
const existing = await tablesDB.listRows({
|
||||||
|
databaseId: DATABASE_ID,
|
||||||
|
tableId: TABLES.propertyMatches,
|
||||||
|
queries: [
|
||||||
|
Query.equal("propertyId", property.$id),
|
||||||
|
Query.equal("searchId", search.$id),
|
||||||
|
Query.limit(1),
|
||||||
|
],
|
||||||
|
});
|
||||||
|
if (existing.rows.length > 0) continue;
|
||||||
|
|
||||||
|
await tablesDB.createRow(
|
||||||
|
DATABASE_ID,
|
||||||
|
TABLES.propertyMatches,
|
||||||
|
ID.unique(),
|
||||||
|
{
|
||||||
|
tenantId,
|
||||||
|
propertyId: property.$id,
|
||||||
|
customerId: search.customerId,
|
||||||
|
searchId: search.$id,
|
||||||
|
notified: false,
|
||||||
|
createdBy,
|
||||||
|
},
|
||||||
|
[
|
||||||
|
Permission.read(Role.team(tenantId)),
|
||||||
|
Permission.update(Role.team(tenantId)),
|
||||||
|
Permission.delete(Role.team(tenantId, "owner")),
|
||||||
|
Permission.delete(Role.team(tenantId, "admin")),
|
||||||
|
],
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,119 @@
|
|||||||
|
"use server";
|
||||||
|
|
||||||
|
import crypto from "crypto";
|
||||||
|
import { ID, Permission, Role } from "node-appwrite";
|
||||||
|
import { revalidatePath } from "next/cache";
|
||||||
|
|
||||||
|
import { presentationSchema } from "@/lib/validation/presentations";
|
||||||
|
import { DATABASE_ID, TABLES } from "./schema";
|
||||||
|
import { createAdminClient } from "./server";
|
||||||
|
import { requireTenant } from "@/lib/appwrite/tenant-guard";
|
||||||
|
|
||||||
|
type ActionState = { ok: boolean; error?: string; fieldErrors?: Record<string, string[]>; id?: string };
|
||||||
|
|
||||||
|
export async function createPresentationAction(
|
||||||
|
_prev: ActionState,
|
||||||
|
formData: FormData,
|
||||||
|
): Promise<ActionState> {
|
||||||
|
const ctx = await requireTenant();
|
||||||
|
const selectedIds = formData.getAll("propertyIdsRaw") as string[];
|
||||||
|
const raw = {
|
||||||
|
...Object.fromEntries(formData.entries()),
|
||||||
|
propertyIds: JSON.stringify(selectedIds),
|
||||||
|
};
|
||||||
|
const parsed = presentationSchema.safeParse(raw);
|
||||||
|
if (!parsed.success) {
|
||||||
|
return { ok: false, fieldErrors: parsed.error.flatten().fieldErrors };
|
||||||
|
}
|
||||||
|
|
||||||
|
const { tablesDB } = createAdminClient();
|
||||||
|
const data = parsed.data;
|
||||||
|
const id = ID.unique();
|
||||||
|
const shareToken = crypto.randomBytes(16).toString("hex");
|
||||||
|
|
||||||
|
try {
|
||||||
|
await tablesDB.createRow(
|
||||||
|
DATABASE_ID,
|
||||||
|
TABLES.presentations,
|
||||||
|
id,
|
||||||
|
{
|
||||||
|
tenantId: ctx.tenantId,
|
||||||
|
title: data.title,
|
||||||
|
customerId: data.customerId,
|
||||||
|
propertyIds: data.propertyIds,
|
||||||
|
shareToken,
|
||||||
|
expiresAt: data.expiresAt,
|
||||||
|
viewCount: 0,
|
||||||
|
notes: data.notes,
|
||||||
|
createdBy: ctx.user.id,
|
||||||
|
},
|
||||||
|
[
|
||||||
|
Permission.read(Role.team(ctx.tenantId)),
|
||||||
|
Permission.update(Role.team(ctx.tenantId)),
|
||||||
|
Permission.delete(Role.team(ctx.tenantId, "owner")),
|
||||||
|
Permission.delete(Role.team(ctx.tenantId, "admin")),
|
||||||
|
],
|
||||||
|
);
|
||||||
|
} catch {
|
||||||
|
return { ok: false, error: "Sunum oluşturulamadı." };
|
||||||
|
}
|
||||||
|
|
||||||
|
revalidatePath("/presentations");
|
||||||
|
return { ok: true, id };
|
||||||
|
}
|
||||||
|
|
||||||
|
export async function updatePresentationAction(
|
||||||
|
id: string,
|
||||||
|
_prev: ActionState,
|
||||||
|
formData: FormData,
|
||||||
|
): Promise<ActionState> {
|
||||||
|
await requireTenant();
|
||||||
|
const selectedIds = formData.getAll("propertyIdsRaw") as string[];
|
||||||
|
const raw = {
|
||||||
|
...Object.fromEntries(formData.entries()),
|
||||||
|
propertyIds: JSON.stringify(selectedIds),
|
||||||
|
};
|
||||||
|
const parsed = presentationSchema.safeParse(raw);
|
||||||
|
if (!parsed.success) {
|
||||||
|
return { ok: false, fieldErrors: parsed.error.flatten().fieldErrors };
|
||||||
|
}
|
||||||
|
|
||||||
|
const { tablesDB } = createAdminClient();
|
||||||
|
const data = parsed.data;
|
||||||
|
|
||||||
|
try {
|
||||||
|
await tablesDB.updateRow(DATABASE_ID, TABLES.presentations, id, {
|
||||||
|
title: data.title,
|
||||||
|
customerId: data.customerId,
|
||||||
|
propertyIds: data.propertyIds,
|
||||||
|
expiresAt: data.expiresAt,
|
||||||
|
notes: data.notes,
|
||||||
|
});
|
||||||
|
} catch {
|
||||||
|
return { ok: false, error: "Sunum güncellenemedi." };
|
||||||
|
}
|
||||||
|
|
||||||
|
revalidatePath("/presentations");
|
||||||
|
return { ok: true };
|
||||||
|
}
|
||||||
|
|
||||||
|
export async function deletePresentationAction(id: string): Promise<ActionState> {
|
||||||
|
await requireTenant();
|
||||||
|
const { tablesDB } = createAdminClient();
|
||||||
|
|
||||||
|
try {
|
||||||
|
await tablesDB.deleteRow(DATABASE_ID, TABLES.presentations, id);
|
||||||
|
} catch {
|
||||||
|
return { ok: false, error: "Sunum silinemedi." };
|
||||||
|
}
|
||||||
|
|
||||||
|
revalidatePath("/presentations");
|
||||||
|
return { ok: true };
|
||||||
|
}
|
||||||
|
|
||||||
|
export async function incrementPresentationViewCount(id: string, currentCount: number): Promise<void> {
|
||||||
|
const { tablesDB } = createAdminClient();
|
||||||
|
await tablesDB.updateRow(DATABASE_ID, TABLES.presentations, id, {
|
||||||
|
viewCount: currentCount + 1,
|
||||||
|
}).catch(() => {});
|
||||||
|
}
|
||||||
@@ -0,0 +1,143 @@
|
|||||||
|
"use server";
|
||||||
|
|
||||||
|
import { ID, Permission, Role } from "node-appwrite";
|
||||||
|
import { revalidatePath } from "next/cache";
|
||||||
|
|
||||||
|
import { propertySchema } from "@/lib/validation/properties";
|
||||||
|
import { DATABASE_ID, TABLES, type Property } from "./schema";
|
||||||
|
import { createAdminClient } from "./server";
|
||||||
|
import { requireTenant } from "@/lib/appwrite/tenant-guard";
|
||||||
|
import { matchPropertyToSearches } from "./matching";
|
||||||
|
|
||||||
|
type ActionState = { ok: boolean; error?: string; fieldErrors?: Record<string, string[]> };
|
||||||
|
|
||||||
|
export async function createPropertyAction(
|
||||||
|
_prev: ActionState,
|
||||||
|
formData: FormData,
|
||||||
|
): Promise<ActionState> {
|
||||||
|
const ctx = await requireTenant();
|
||||||
|
const raw = Object.fromEntries(formData.entries());
|
||||||
|
const parsed = propertySchema.safeParse(raw);
|
||||||
|
if (!parsed.success) {
|
||||||
|
return { ok: false, fieldErrors: parsed.error.flatten().fieldErrors };
|
||||||
|
}
|
||||||
|
|
||||||
|
const { tablesDB } = createAdminClient();
|
||||||
|
const id = ID.unique();
|
||||||
|
const data = parsed.data;
|
||||||
|
|
||||||
|
try {
|
||||||
|
const row = await tablesDB.createRow(
|
||||||
|
DATABASE_ID,
|
||||||
|
TABLES.properties,
|
||||||
|
id,
|
||||||
|
{
|
||||||
|
tenantId: ctx.tenantId,
|
||||||
|
title: data.title,
|
||||||
|
description: data.description,
|
||||||
|
propertyType: data.propertyType,
|
||||||
|
listingType: data.listingType,
|
||||||
|
status: data.status ?? "aktif",
|
||||||
|
price: data.price,
|
||||||
|
currency: data.currency ?? "TRY",
|
||||||
|
roomCount: data.roomCount,
|
||||||
|
grossM2: data.grossM2,
|
||||||
|
netM2: data.netM2,
|
||||||
|
floor: data.floor,
|
||||||
|
totalFloors: data.totalFloors,
|
||||||
|
buildingAge: data.buildingAge,
|
||||||
|
city: data.city,
|
||||||
|
district: data.district,
|
||||||
|
neighborhood: data.neighborhood,
|
||||||
|
address: data.address,
|
||||||
|
mapLat: data.mapLat,
|
||||||
|
mapLng: data.mapLng,
|
||||||
|
featuresJson: data.featuresJson,
|
||||||
|
imageIds: data.imageIds,
|
||||||
|
createdBy: ctx.user.id,
|
||||||
|
assigneeId: data.assigneeId,
|
||||||
|
},
|
||||||
|
[
|
||||||
|
Permission.read(Role.team(ctx.tenantId)),
|
||||||
|
Permission.update(Role.team(ctx.tenantId)),
|
||||||
|
Permission.delete(Role.team(ctx.tenantId, "owner")),
|
||||||
|
Permission.delete(Role.team(ctx.tenantId, "admin")),
|
||||||
|
],
|
||||||
|
);
|
||||||
|
|
||||||
|
await matchPropertyToSearches(row as unknown as Property, ctx.tenantId, ctx.user.id).catch(
|
||||||
|
() => {},
|
||||||
|
);
|
||||||
|
} catch {
|
||||||
|
return { ok: false, error: "İlan oluşturulamadı." };
|
||||||
|
}
|
||||||
|
|
||||||
|
revalidatePath("/properties");
|
||||||
|
return { ok: true };
|
||||||
|
}
|
||||||
|
|
||||||
|
export async function updatePropertyAction(
|
||||||
|
id: string,
|
||||||
|
_prev: ActionState,
|
||||||
|
formData: FormData,
|
||||||
|
): Promise<ActionState> {
|
||||||
|
const ctx = await requireTenant();
|
||||||
|
const raw = Object.fromEntries(formData.entries());
|
||||||
|
const parsed = propertySchema.safeParse(raw);
|
||||||
|
if (!parsed.success) {
|
||||||
|
return { ok: false, fieldErrors: parsed.error.flatten().fieldErrors };
|
||||||
|
}
|
||||||
|
|
||||||
|
const { tablesDB } = createAdminClient();
|
||||||
|
const data = parsed.data;
|
||||||
|
|
||||||
|
try {
|
||||||
|
const row = await tablesDB.updateRow(DATABASE_ID, TABLES.properties, id, {
|
||||||
|
title: data.title,
|
||||||
|
description: data.description,
|
||||||
|
propertyType: data.propertyType,
|
||||||
|
listingType: data.listingType,
|
||||||
|
status: data.status,
|
||||||
|
price: data.price,
|
||||||
|
currency: data.currency,
|
||||||
|
roomCount: data.roomCount,
|
||||||
|
grossM2: data.grossM2,
|
||||||
|
netM2: data.netM2,
|
||||||
|
floor: data.floor,
|
||||||
|
totalFloors: data.totalFloors,
|
||||||
|
buildingAge: data.buildingAge,
|
||||||
|
city: data.city,
|
||||||
|
district: data.district,
|
||||||
|
neighborhood: data.neighborhood,
|
||||||
|
address: data.address,
|
||||||
|
mapLat: data.mapLat,
|
||||||
|
mapLng: data.mapLng,
|
||||||
|
featuresJson: data.featuresJson,
|
||||||
|
imageIds: data.imageIds,
|
||||||
|
assigneeId: data.assigneeId,
|
||||||
|
});
|
||||||
|
|
||||||
|
await matchPropertyToSearches(row as unknown as Property, ctx.tenantId, ctx.user.id).catch(
|
||||||
|
() => {},
|
||||||
|
);
|
||||||
|
} catch {
|
||||||
|
return { ok: false, error: "İlan güncellenemedi." };
|
||||||
|
}
|
||||||
|
|
||||||
|
revalidatePath("/properties");
|
||||||
|
return { ok: true };
|
||||||
|
}
|
||||||
|
|
||||||
|
export async function deletePropertyAction(id: string): Promise<ActionState> {
|
||||||
|
await requireTenant();
|
||||||
|
const { tablesDB } = createAdminClient();
|
||||||
|
|
||||||
|
try {
|
||||||
|
await tablesDB.deleteRow(DATABASE_ID, TABLES.properties, id);
|
||||||
|
} catch {
|
||||||
|
return { ok: false, error: "İlan silinemedi." };
|
||||||
|
}
|
||||||
|
|
||||||
|
revalidatePath("/properties");
|
||||||
|
return { ok: true };
|
||||||
|
}
|
||||||
@@ -0,0 +1,23 @@
|
|||||||
|
import "server-only";
|
||||||
|
|
||||||
|
import { Query } from "node-appwrite";
|
||||||
|
|
||||||
|
import { DATABASE_ID, TABLES, type Property } from "./schema";
|
||||||
|
import { createAdminClient } from "./server";
|
||||||
|
|
||||||
|
export async function listProperties(tenantId: string): Promise<Property[]> {
|
||||||
|
const { tablesDB } = createAdminClient();
|
||||||
|
const result = await tablesDB.listRows({
|
||||||
|
databaseId: DATABASE_ID,
|
||||||
|
tableId: TABLES.properties,
|
||||||
|
queries: [Query.equal("tenantId", tenantId), Query.orderDesc("$createdAt"), Query.limit(200)],
|
||||||
|
});
|
||||||
|
return result.rows as unknown as Property[];
|
||||||
|
}
|
||||||
|
|
||||||
|
export async function getProperty(id: string, tenantId: string): Promise<Property | null> {
|
||||||
|
const { tablesDB } = createAdminClient();
|
||||||
|
const row = (await tablesDB.getRow(DATABASE_ID, TABLES.properties, id)) as unknown as Property;
|
||||||
|
if (row.tenantId !== tenantId) return null;
|
||||||
|
return row;
|
||||||
|
}
|
||||||
@@ -0,0 +1,12 @@
|
|||||||
|
import { z } from "zod";
|
||||||
|
|
||||||
|
export const activitySchema = z.object({
|
||||||
|
type: z.enum(["gorusme", "teklif", "ziyaret", "arama", "not"]),
|
||||||
|
title: z.string().min(2, "En az 2 karakter").max(255),
|
||||||
|
description: z.string().max(10000).optional(),
|
||||||
|
customerId: z.string().max(36).optional(),
|
||||||
|
propertyId: z.string().max(36).optional(),
|
||||||
|
dueDate: z.string().optional(),
|
||||||
|
});
|
||||||
|
|
||||||
|
export type ActivityFormValues = z.infer<typeof activitySchema>;
|
||||||
@@ -0,0 +1,18 @@
|
|||||||
|
import { z } from "zod";
|
||||||
|
|
||||||
|
export const customerSearchSchema = z.object({
|
||||||
|
customerId: z.string().min(1, "Müşteri seçin"),
|
||||||
|
listingType: z.enum(["satilik", "kiralik"]).optional().or(z.literal("")),
|
||||||
|
propertyTypes: z.string().max(500).optional(),
|
||||||
|
roomCounts: z.string().max(200).optional(),
|
||||||
|
minPrice: z.coerce.number().min(0).optional().nullable(),
|
||||||
|
maxPrice: z.coerce.number().min(0).optional().nullable(),
|
||||||
|
minM2: z.coerce.number().min(0).optional().nullable(),
|
||||||
|
maxM2: z.coerce.number().min(0).optional().nullable(),
|
||||||
|
cities: z.string().max(500).optional(),
|
||||||
|
districts: z.string().max(1000).optional(),
|
||||||
|
isActive: z.boolean().default(true),
|
||||||
|
notes: z.string().max(5000).optional(),
|
||||||
|
});
|
||||||
|
|
||||||
|
export type CustomerSearchFormValues = z.infer<typeof customerSearchSchema>;
|
||||||
@@ -1,16 +1,24 @@
|
|||||||
import { z } from "zod";
|
import { z } from "zod";
|
||||||
|
|
||||||
export const customerSchema = z.object({
|
export const customerSchema = z.object({
|
||||||
name: z.string().trim().min(1, "İsim zorunlu.").max(255),
|
name: z.string().trim().min(2, "En az 2 karakter").max(255),
|
||||||
email: z
|
email: z
|
||||||
.union([z.string().email("Geçerli bir email girin."), z.literal("")])
|
.union([z.string().email("Geçerli bir email girin."), z.literal("")])
|
||||||
.optional()
|
.optional()
|
||||||
.transform((v) => (v ? v : undefined)),
|
.transform((v) => v || undefined),
|
||||||
phone: z.string().trim().max(30).optional().transform((v) => (v ? v : undefined)),
|
phone: z
|
||||||
taxId: z.string().trim().max(50).optional().transform((v) => (v ? v : undefined)),
|
.string()
|
||||||
address: z.string().trim().max(500).optional().transform((v) => (v ? v : undefined)),
|
.trim()
|
||||||
notes: z.string().trim().max(2000).optional().transform((v) => (v ? v : undefined)),
|
.max(30)
|
||||||
status: z.enum(["active", "passive"]).optional().default("active"),
|
.optional()
|
||||||
|
.transform((v) => v || undefined),
|
||||||
|
type: z.enum(["alici", "kiraci", "yatirimci"]),
|
||||||
|
notes: z
|
||||||
|
.string()
|
||||||
|
.trim()
|
||||||
|
.max(2000)
|
||||||
|
.optional()
|
||||||
|
.transform((v) => v || undefined),
|
||||||
});
|
});
|
||||||
|
|
||||||
export type CustomerInput = z.infer<typeof customerSchema>;
|
export type CustomerFormValues = z.infer<typeof customerSchema>;
|
||||||
|
|||||||
@@ -0,0 +1,11 @@
|
|||||||
|
import { z } from "zod";
|
||||||
|
|
||||||
|
export const presentationSchema = z.object({
|
||||||
|
title: z.string().min(2, "En az 2 karakter").max(255),
|
||||||
|
customerId: z.string().max(36).optional(),
|
||||||
|
propertyIds: z.string().min(1, "En az bir ilan seçin"),
|
||||||
|
expiresAt: z.string().optional(),
|
||||||
|
notes: z.string().max(5000).optional(),
|
||||||
|
});
|
||||||
|
|
||||||
|
export type PresentationFormValues = z.infer<typeof presentationSchema>;
|
||||||
@@ -0,0 +1,28 @@
|
|||||||
|
import { z } from "zod";
|
||||||
|
|
||||||
|
export const propertySchema = z.object({
|
||||||
|
title: z.string().min(3, "En az 3 karakter").max(255),
|
||||||
|
description: z.string().max(16000).optional(),
|
||||||
|
propertyType: z.enum(["daire", "villa", "arsa", "dukkan", "ofis", "depo"]),
|
||||||
|
listingType: z.enum(["satilik", "kiralik"]),
|
||||||
|
status: z.enum(["aktif", "pasif", "satildi", "kiralandit"]).default("aktif"),
|
||||||
|
price: z.coerce.number().min(0, "Geçerli bir fiyat girin"),
|
||||||
|
currency: z.string().max(3).default("TRY"),
|
||||||
|
roomCount: z.string().max(10).optional(),
|
||||||
|
grossM2: z.coerce.number().min(0).optional().nullable(),
|
||||||
|
netM2: z.coerce.number().min(0).optional().nullable(),
|
||||||
|
floor: z.coerce.number().optional().nullable(),
|
||||||
|
totalFloors: z.coerce.number().optional().nullable(),
|
||||||
|
buildingAge: z.coerce.number().min(0).optional().nullable(),
|
||||||
|
city: z.string().min(1, "Şehir zorunlu").max(100),
|
||||||
|
district: z.string().max(100).optional(),
|
||||||
|
neighborhood: z.string().max(100).optional(),
|
||||||
|
address: z.string().max(500).optional(),
|
||||||
|
mapLat: z.coerce.number().optional().nullable(),
|
||||||
|
mapLng: z.coerce.number().optional().nullable(),
|
||||||
|
featuresJson: z.string().max(2000).optional(),
|
||||||
|
imageIds: z.string().max(2000).optional(),
|
||||||
|
assigneeId: z.string().max(36).optional(),
|
||||||
|
});
|
||||||
|
|
||||||
|
export type PropertyFormValues = z.infer<typeof propertySchema>;
|
||||||
Reference in New Issue
Block a user