feat(modules): connections, products, jobs (list/form/detail-placeholder)

Connections (clinic ↔ lab)
  - request via member number, approve/reject (counterparty), cancel pending,
    delete approved
  - permission rows opened to both teams; audit log for every mutation
  - /connections page: own code card, request form, pending inbound/outbound
    tables, approved connections table with delete confirm

Products (lab catalog)
  - createProstheticAction + update + archive/restore + delete (lab-only)
  - zod validation, dev-mode error surfacing
  - /products page: catalog table + add form + edit dialog. Hidden from
    clinic accounts via requireTenantKind.

Jobs (work orders)
  - createJobAction (clinic-only) — checks approved connection before write,
    permissions opened to both clinic and lab teams
  - listInboundJobs (lab perspective), listOutboundJobs (clinic perspective),
    listApprovedLabsForClinic for the new-job form
  - /jobs/inbound + /jobs/outbound tables with role-aware copy
  - /jobs/new full form (lab select, patient code, prosthetic type, member
    count, color, due date, price/currency, description)
  - /jobs/[jobId] placeholder detail page with stepper visualisation;
    status/step updates and file upload come next session

All new mutations follow the memory rules: schema-checked row payloads,
admin client behind requireTenant + requireRole/requireTenantKind, audit
log calls best-effort, no empty-string Radix Select values.
This commit is contained in:
kovakmedya
2026-05-21 19:59:23 +03:00
parent 7fb8288f79
commit 76e02754b8
26 changed files with 2765 additions and 42 deletions
@@ -0,0 +1,42 @@
"use client";
import { useState } from "react";
import { Check, Copy } from "lucide-react";
import { Button } from "@/components/ui/button";
import { Card, CardContent, CardDescription, CardHeader, CardTitle } from "@/components/ui/card";
export function ConnectionCodeCard({ memberNumber }: { memberNumber: string }) {
const [copied, setCopied] = useState(false);
const copy = async () => {
if (!memberNumber) return;
try {
await navigator.clipboard.writeText(memberNumber);
setCopied(true);
setTimeout(() => setCopied(false), 2000);
} catch {
/* ignore */
}
};
return (
<Card>
<CardHeader>
<CardTitle>Bağlantı kodunuz</CardTitle>
<CardDescription>
Karşı taraf bu kodu girerek size bağlantı talebi gönderir.
</CardDescription>
</CardHeader>
<CardContent>
<div className="bg-muted/40 flex items-center justify-between gap-3 rounded-md border px-4 py-3">
<span className="font-mono text-2xl tracking-[0.4em]">{memberNumber || "—"}</span>
<Button type="button" variant="outline" size="sm" onClick={copy} disabled={!memberNumber}>
{copied ? <Check className="size-4" /> : <Copy className="size-4" />}
{copied ? "Kopyalandı" : "Kopyala"}
</Button>
</div>
</CardContent>
</Card>
);
}
@@ -0,0 +1,61 @@
"use client";
import { useActionState, useEffect, useRef } from "react";
import { Link2, 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 { requestConnectionAction } from "@/lib/appwrite/connection-actions";
import { initialConnectionRequestState } from "@/lib/appwrite/connection-types";
export function ConnectionRequestForm({ counterpartLabel }: { counterpartLabel: string }) {
const [state, formAction, isPending] = useActionState(
requestConnectionAction,
initialConnectionRequestState,
);
const formRef = useRef<HTMLFormElement>(null);
useEffect(() => {
if (state.ok) {
toast.success("Bağlantı talebi gönderildi.");
formRef.current?.reset();
} else if (state.error) {
toast.error(state.error);
}
}, [state]);
return (
<form ref={formRef} action={formAction} className="grid gap-3 sm:grid-cols-[1fr_auto] sm:items-end">
<div className="grid gap-2">
<Label htmlFor="memberNumber">{counterpartLabel.charAt(0).toUpperCase() + counterpartLabel.slice(1)} bağlantı kodu</Label>
<Input
id="memberNumber"
name="memberNumber"
type="text"
placeholder="6 hane"
maxLength={12}
autoCapitalize="characters"
autoComplete="off"
required
style={{ textTransform: "uppercase", letterSpacing: "0.3em" }}
className="font-mono"
/>
</div>
<Button type="submit" disabled={isPending}>
{isPending ? (
<>
<Loader2 className="size-4 animate-spin" />
Gönderiliyor...
</>
) : (
<>
<Link2 className="size-4" />
Talep gönder
</>
)}
</Button>
</form>
);
}
@@ -0,0 +1,135 @@
"use client";
import { useActionState, useEffect, useState, useTransition } from "react";
import { Loader2, Trash2 } from "lucide-react";
import { toast } from "sonner";
import { Badge } from "@/components/ui/badge";
import { Button } from "@/components/ui/button";
import {
Dialog,
DialogClose,
DialogContent,
DialogDescription,
DialogFooter,
DialogHeader,
DialogTitle,
DialogTrigger,
} from "@/components/ui/dialog";
import {
Table,
TableBody,
TableCell,
TableHead,
TableHeader,
TableRow,
} from "@/components/ui/table";
import { deleteConnectionAction } from "@/lib/appwrite/connection-actions";
import { initialConnectionActionState } from "@/lib/appwrite/connection-types";
import type { ConnectionWithCounterpart } from "@/lib/appwrite/connection-queries";
const dateFormatter = new Intl.DateTimeFormat("tr-TR", {
day: "2-digit",
month: "2-digit",
year: "numeric",
});
export function ConnectionsTable({ rows }: { rows: ConnectionWithCounterpart[] }) {
if (rows.length === 0) {
return (
<p className="text-muted-foreground py-6 text-center text-sm">
Henüz onaylanmış bağlantınız yok. Yukarıdan talep gönderebilir veya kodunuzu paylaşabilirsiniz.
</p>
);
}
return (
<Table>
<TableHeader>
<TableRow>
<TableHead>Karşı taraf</TableHead>
<TableHead>Tür</TableHead>
<TableHead>Onay tarihi</TableHead>
<TableHead className="text-right">İşlem</TableHead>
</TableRow>
</TableHeader>
<TableBody>
{rows.map((r) => (
<ApprovedRow key={r.$id} row={r} />
))}
</TableBody>
</Table>
);
}
function ApprovedRow({ row }: { row: ConnectionWithCounterpart }) {
const [state, action, pending] = useActionState(
deleteConnectionAction,
initialConnectionActionState,
);
const [, startTransition] = useTransition();
const [open, setOpen] = useState(false);
useEffect(() => {
if (state.ok) {
toast.success("Bağlantı silindi.");
setOpen(false);
} else if (state.error) {
toast.error(state.error);
}
}, [state]);
const kindLabel =
row.counterpart?.kind === "lab"
? "Laboratuvar"
: row.counterpart?.kind === "clinic"
? "Klinik"
: "—";
return (
<TableRow>
<TableCell className="font-medium">{row.counterpart?.companyName ?? "—"}</TableCell>
<TableCell>
<Badge variant="secondary">{kindLabel}</Badge>
</TableCell>
<TableCell className="text-muted-foreground">
{row.approvedAt ? dateFormatter.format(new Date(row.approvedAt)) : "—"}
</TableCell>
<TableCell className="text-right">
<Dialog open={open} onOpenChange={setOpen}>
<DialogTrigger asChild>
<Button size="sm" variant="outline">
<Trash2 className="size-4" />
Sil
</Button>
</DialogTrigger>
<DialogContent>
<DialogHeader>
<DialogTitle>Bağlantı silinsin mi?</DialogTitle>
<DialogDescription>
{row.counterpart?.companyName ?? "Karşı taraf"} ile bağlantınız sonlandırılacak.
Mevcut işleriniz etkilenmez ancak yeni gönderemezsiniz.
</DialogDescription>
</DialogHeader>
<DialogFooter>
<DialogClose asChild>
<Button type="button" variant="outline">Vazgeç</Button>
</DialogClose>
<form
action={(fd) => {
startTransition(() => action(fd));
}}
>
<input type="hidden" name="connectionId" value={row.$id} />
<Button type="submit" disabled={pending} variant="destructive">
{pending ? <Loader2 className="size-4 animate-spin" /> : <Trash2 className="size-4" />}
Sil
</Button>
</form>
</DialogFooter>
</DialogContent>
</Dialog>
</TableCell>
</TableRow>
);
}
@@ -0,0 +1,128 @@
"use client";
import { useActionState, useEffect, useTransition } from "react";
import { Check, Loader2, X } from "lucide-react";
import { toast } from "sonner";
import { Badge } from "@/components/ui/badge";
import { Button } from "@/components/ui/button";
import {
Table,
TableBody,
TableCell,
TableHead,
TableHeader,
TableRow,
} from "@/components/ui/table";
import {
approveConnectionAction,
rejectConnectionAction,
} from "@/lib/appwrite/connection-actions";
import { initialConnectionActionState } from "@/lib/appwrite/connection-types";
import type { ConnectionWithCounterpart } from "@/lib/appwrite/connection-queries";
const dateFormatter = new Intl.DateTimeFormat("tr-TR", {
day: "2-digit",
month: "2-digit",
year: "numeric",
});
export function PendingInboundTable({ rows }: { rows: ConnectionWithCounterpart[] }) {
if (rows.length === 0) {
return (
<p className="text-muted-foreground py-6 text-center text-sm">
Bekleyen talep yok.
</p>
);
}
return (
<Table>
<TableHeader>
<TableRow>
<TableHead>Talep eden</TableHead>
<TableHead>Tür</TableHead>
<TableHead>Tarih</TableHead>
<TableHead className="text-right">İşlem</TableHead>
</TableRow>
</TableHeader>
<TableBody>
{rows.map((r) => (
<InboundRow key={r.$id} row={r} />
))}
</TableBody>
</Table>
);
}
function InboundRow({ row }: { row: ConnectionWithCounterpart }) {
const [approveState, approveAction, approvePending] = useActionState(
approveConnectionAction,
initialConnectionActionState,
);
const [rejectState, rejectAction, rejectPending] = useActionState(
rejectConnectionAction,
initialConnectionActionState,
);
const [, startTransition] = useTransition();
useEffect(() => {
if (approveState.ok) toast.success("Bağlantı onaylandı.");
else if (approveState.error) toast.error(approveState.error);
}, [approveState]);
useEffect(() => {
if (rejectState.ok) toast.success("Talep reddedildi.");
else if (rejectState.error) toast.error(rejectState.error);
}, [rejectState]);
const kindLabel =
row.counterpart?.kind === "lab"
? "Laboratuvar"
: row.counterpart?.kind === "clinic"
? "Klinik"
: "—";
return (
<TableRow>
<TableCell className="font-medium">{row.counterpart?.companyName ?? "—"}</TableCell>
<TableCell>
<Badge variant="secondary">{kindLabel}</Badge>
</TableCell>
<TableCell className="text-muted-foreground">
{dateFormatter.format(new Date(row.requestedAt))}
</TableCell>
<TableCell className="text-right">
<div className="flex justify-end gap-2">
<form
action={(fd) => {
startTransition(() => approveAction(fd));
}}
>
<input type="hidden" name="connectionId" value={row.$id} />
<Button type="submit" size="sm" disabled={approvePending || rejectPending}>
{approvePending ? <Loader2 className="size-4 animate-spin" /> : <Check className="size-4" />}
Onayla
</Button>
</form>
<form
action={(fd) => {
startTransition(() => rejectAction(fd));
}}
>
<input type="hidden" name="connectionId" value={row.$id} />
<Button
type="submit"
size="sm"
variant="outline"
disabled={approvePending || rejectPending}
>
{rejectPending ? <Loader2 className="size-4 animate-spin" /> : <X className="size-4" />}
Reddet
</Button>
</form>
</div>
</TableCell>
</TableRow>
);
}
@@ -0,0 +1,98 @@
"use client";
import { useActionState, useEffect, useTransition } from "react";
import { Loader2, X } from "lucide-react";
import { toast } from "sonner";
import { Badge } from "@/components/ui/badge";
import { Button } from "@/components/ui/button";
import {
Table,
TableBody,
TableCell,
TableHead,
TableHeader,
TableRow,
} from "@/components/ui/table";
import { cancelConnectionAction } from "@/lib/appwrite/connection-actions";
import { initialConnectionActionState } from "@/lib/appwrite/connection-types";
import type { ConnectionWithCounterpart } from "@/lib/appwrite/connection-queries";
const dateFormatter = new Intl.DateTimeFormat("tr-TR", {
day: "2-digit",
month: "2-digit",
year: "numeric",
});
export function PendingOutboundTable({ rows }: { rows: ConnectionWithCounterpart[] }) {
if (rows.length === 0) {
return (
<p className="text-muted-foreground py-6 text-center text-sm">
Gönderilmiş talebiniz yok.
</p>
);
}
return (
<Table>
<TableHeader>
<TableRow>
<TableHead>Karşı taraf</TableHead>
<TableHead>Tür</TableHead>
<TableHead>Gönderim</TableHead>
<TableHead className="text-right">İşlem</TableHead>
</TableRow>
</TableHeader>
<TableBody>
{rows.map((r) => (
<OutboundRow key={r.$id} row={r} />
))}
</TableBody>
</Table>
);
}
function OutboundRow({ row }: { row: ConnectionWithCounterpart }) {
const [state, action, pending] = useActionState(
cancelConnectionAction,
initialConnectionActionState,
);
const [, startTransition] = useTransition();
useEffect(() => {
if (state.ok) toast.success("Talep iptal edildi.");
else if (state.error) toast.error(state.error);
}, [state]);
const kindLabel =
row.counterpart?.kind === "lab"
? "Laboratuvar"
: row.counterpart?.kind === "clinic"
? "Klinik"
: "—";
return (
<TableRow>
<TableCell className="font-medium">{row.counterpart?.companyName ?? "—"}</TableCell>
<TableCell>
<Badge variant="secondary">{kindLabel}</Badge>
</TableCell>
<TableCell className="text-muted-foreground">
{dateFormatter.format(new Date(row.requestedAt))}
</TableCell>
<TableCell className="text-right">
<form
action={(fd) => {
startTransition(() => action(fd));
}}
>
<input type="hidden" name="connectionId" value={row.$id} />
<Button type="submit" size="sm" variant="outline" disabled={pending}>
{pending ? <Loader2 className="size-4 animate-spin" /> : <X className="size-4" />}
İptal et
</Button>
</form>
</TableCell>
</TableRow>
);
}
+72 -13
View File
@@ -1,7 +1,21 @@
import { redirect } from "next/navigation";
import { Card, CardContent, CardDescription, CardHeader, CardTitle } from "@/components/ui/card";
import {
listApprovedConnections,
listPendingInbound,
listPendingOutbound,
} from "@/lib/appwrite/connection-queries";
import { requireTenant } from "@/lib/appwrite/tenant-guard";
import { ConnectionCodeCard } from "./components/connection-code-card";
import { ConnectionRequestForm } from "./components/connection-request-form";
import { PendingInboundTable } from "./components/pending-inbound-table";
import { PendingOutboundTable } from "./components/pending-outbound-table";
import { ConnectionsTable } from "./components/connections-table";
export const metadata = {
title: "DLS — Bağlantı Kur",
};
export default async function ConnectionsPage() {
let ctx;
@@ -11,32 +25,77 @@ export default async function ConnectionsPage() {
redirect("/onboarding");
}
const memberNumber = ctx.settings?.memberNumber ?? "";
const isLab = ctx.kind === "lab";
const counterpartLabel = isLab ? "klinik" : "laboratuvar";
const [approved, pendingInbound, pendingOutbound] = await Promise.all([
listApprovedConnections(ctx.tenantId),
listPendingInbound(ctx.tenantId, ctx.user.id),
listPendingOutbound(ctx.tenantId, ctx.user.id),
]);
return (
<div className="flex-1 space-y-6 px-6">
<div className="flex flex-col gap-1">
<h1 className="text-2xl font-bold tracking-tight">Bağlantı Kur</h1>
<p className="text-muted-foreground text-sm">
Klinik ve laboratuvar arasında bağlantı taleplerini yönetin.
{isLab
? "Sizinle çalışan klinikleri yönetin. Bağlantı kodunuzu paylaşın veya bir kliniğe talep gönderin."
: "Çalıştığınız laboratuvarları yönetin. Bağlantı kodunuzu paylaşın veya bir laboratuvara talep gönderin."}
</p>
</div>
<div className="grid gap-6 lg:grid-cols-2">
<ConnectionCodeCard memberNumber={memberNumber} />
<Card>
<CardHeader>
<CardTitle>Bağlantı talep et</CardTitle>
<CardDescription>
Karşı tarafın 6 haneli kodunu girin, onaylarsa bağlantı kurulur.
</CardDescription>
</CardHeader>
<CardContent>
<ConnectionRequestForm counterpartLabel={counterpartLabel} />
</CardContent>
</Card>
</div>
<div className="grid gap-6 xl:grid-cols-2">
<Card>
<CardHeader>
<CardTitle>Gelen Talepler</CardTitle>
<CardDescription>
Size gönderilen, onayınızı bekleyen bağlantı talepleri.
</CardDescription>
</CardHeader>
<CardContent>
<PendingInboundTable rows={pendingInbound} />
</CardContent>
</Card>
<Card>
<CardHeader>
<CardTitle>Gönderilen Talepler</CardTitle>
<CardDescription>
Sizin gönderdiğiniz, karşı tarafın yanıtını bekleyen talepler.
</CardDescription>
</CardHeader>
<CardContent>
<PendingOutboundTable rows={pendingOutbound} />
</CardContent>
</Card>
</div>
<Card>
<CardHeader>
<CardTitle>Bağlantı kodunuz</CardTitle>
<CardDescription>Karşı taraf bu kodu girerek size bağlantı talebi gönderir.</CardDescription>
<CardTitle>Bağlantılarım</CardTitle>
<CardDescription>Onaylanmış aktif bağlantılar.</CardDescription>
</CardHeader>
<CardContent>
<div className="bg-muted/40 rounded-md border px-4 py-3 font-mono text-lg tracking-widest">
{ctx.settings?.memberNumber ?? "—"}
</div>
<ConnectionsTable rows={approved} />
</CardContent>
</Card>
<Card>
<CardHeader>
<CardTitle>Yapım aşamasında</CardTitle>
<CardDescription>Bağlantı talepleri ve bağlı taraflar listesi sonraki sürümde eklenecek.</CardDescription>
</CardHeader>
<CardContent />
</Card>
</div>
);
}