feat(jobs): bulk-accept all pending inbox items
A lab opening its inbox first thing in the morning shouldn't have to
click 'İşleme Al' on every overnight submission. Added a single bulk
action that flips every currently-pending job into in_progress in one
shot.
- bulkAcceptPendingJobsAction (lab only, owner/admin/member):
lists every pending job for this lab (limit 200), then for each
row in parallel writes status=in_progress + currentStep=alt_yapi_prova
+ location=at_lab. History rows and clinic notifications fire as
fire-and-forget so a single failure doesn't block the rest. Returns
{ accepted } — count actually moved.
- BulkAcceptButton (client island, /jobs/inbound only) shows when
the current filtered list has at least one pending row, with a
confirm dialog. Disabled / spinner while in flight.
- 'Tümünü okundu işaretle' bulk action on /notifications was already
in place, so nothing else needed there.
Notifications mark-all was already wired earlier; this commit covers
the inbox half.
This commit is contained in:
@@ -0,0 +1,68 @@
|
|||||||
|
"use client";
|
||||||
|
|
||||||
|
import { useState, useTransition } from "react";
|
||||||
|
import { useRouter } from "next/navigation";
|
||||||
|
import { CheckCheck, Loader2 } from "lucide-react";
|
||||||
|
import { toast } from "sonner";
|
||||||
|
|
||||||
|
import { Button } from "@/components/ui/button";
|
||||||
|
import {
|
||||||
|
Dialog,
|
||||||
|
DialogClose,
|
||||||
|
DialogContent,
|
||||||
|
DialogDescription,
|
||||||
|
DialogFooter,
|
||||||
|
DialogHeader,
|
||||||
|
DialogTitle,
|
||||||
|
} from "@/components/ui/dialog";
|
||||||
|
import { bulkAcceptPendingJobsAction } from "@/lib/appwrite/job-actions";
|
||||||
|
|
||||||
|
export function BulkAcceptButton({ count }: { count: number }) {
|
||||||
|
const router = useRouter();
|
||||||
|
const [open, setOpen] = useState(false);
|
||||||
|
const [pending, startTransition] = useTransition();
|
||||||
|
|
||||||
|
if (count === 0) return null;
|
||||||
|
|
||||||
|
function onConfirm() {
|
||||||
|
startTransition(async () => {
|
||||||
|
const res = await bulkAcceptPendingJobsAction();
|
||||||
|
if (res.ok) {
|
||||||
|
toast.success(`${res.accepted ?? 0} iş işleme alındı.`);
|
||||||
|
setOpen(false);
|
||||||
|
router.refresh();
|
||||||
|
} else {
|
||||||
|
toast.error(res.error ?? "İşlem başarısız.");
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
return (
|
||||||
|
<Dialog open={open} onOpenChange={setOpen}>
|
||||||
|
<Button size="sm" onClick={() => setOpen(true)}>
|
||||||
|
<CheckCheck className="size-4" />
|
||||||
|
Bekleyen {count} işi al
|
||||||
|
</Button>
|
||||||
|
<DialogContent>
|
||||||
|
<DialogHeader>
|
||||||
|
<DialogTitle>{count} iş işleme alınsın mı?</DialogTitle>
|
||||||
|
<DialogDescription>
|
||||||
|
Tüm bekleyen işler aynı anda işleme alınır; her birinde alt yapı
|
||||||
|
üretimine başlanmış sayılır. Klinikler ayrı ayrı bilgilendirilir.
|
||||||
|
</DialogDescription>
|
||||||
|
</DialogHeader>
|
||||||
|
<DialogFooter>
|
||||||
|
<DialogClose asChild>
|
||||||
|
<Button type="button" variant="outline">
|
||||||
|
Vazgeç
|
||||||
|
</Button>
|
||||||
|
</DialogClose>
|
||||||
|
<Button onClick={onConfirm} disabled={pending}>
|
||||||
|
{pending ? <Loader2 className="size-4 animate-spin" /> : <CheckCheck className="size-4" />}
|
||||||
|
Hepsini al
|
||||||
|
</Button>
|
||||||
|
</DialogFooter>
|
||||||
|
</DialogContent>
|
||||||
|
</Dialog>
|
||||||
|
);
|
||||||
|
}
|
||||||
@@ -5,6 +5,7 @@ import { listInboundJobs } from "@/lib/appwrite/job-queries";
|
|||||||
import { requireTenant } from "@/lib/appwrite/tenant-guard";
|
import { requireTenant } from "@/lib/appwrite/tenant-guard";
|
||||||
import { JobsFilterBar } from "../_components/jobs-filter-bar";
|
import { JobsFilterBar } from "../_components/jobs-filter-bar";
|
||||||
import { JobsTable } from "../_components/jobs-table";
|
import { JobsTable } from "../_components/jobs-table";
|
||||||
|
import { BulkAcceptButton } from "./components/bulk-accept-button";
|
||||||
|
|
||||||
export const metadata = {
|
export const metadata = {
|
||||||
title: "DLS — Gelen İşler",
|
title: "DLS — Gelen İşler",
|
||||||
@@ -29,15 +30,19 @@ export default async function InboundJobsPage({
|
|||||||
q: sp.q,
|
q: sp.q,
|
||||||
};
|
};
|
||||||
const rows = ctx.kind === "lab" ? await listInboundJobs(ctx.tenantId, filters) : [];
|
const rows = ctx.kind === "lab" ? await listInboundJobs(ctx.tenantId, filters) : [];
|
||||||
|
const pendingCount = rows.filter((j) => j.status === "pending").length;
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div className="flex-1 space-y-6 px-6">
|
<div className="flex-1 space-y-6 px-6">
|
||||||
|
<div className="flex flex-wrap items-start justify-between gap-3">
|
||||||
<div className="flex flex-col gap-1">
|
<div className="flex flex-col gap-1">
|
||||||
<h1 className="text-2xl font-bold tracking-tight">Gelen İşler</h1>
|
<h1 className="text-2xl font-bold tracking-tight">Gelen İşler</h1>
|
||||||
<p className="text-muted-foreground text-sm">
|
<p className="text-muted-foreground text-sm">
|
||||||
Bağlı kliniklerden size yönlendirilmiş protez işleri.
|
Bağlı kliniklerden size yönlendirilmiş protez işleri.
|
||||||
</p>
|
</p>
|
||||||
</div>
|
</div>
|
||||||
|
{ctx.kind === "lab" && <BulkAcceptButton count={pendingCount} />}
|
||||||
|
</div>
|
||||||
|
|
||||||
<Card>
|
<Card>
|
||||||
<CardHeader>
|
<CardHeader>
|
||||||
|
|||||||
@@ -333,6 +333,68 @@ export async function acceptJobAction(
|
|||||||
return { ok: true };
|
return { ok: true };
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Lab takes all currently-pending jobs in one go. Same effect as calling
|
||||||
|
* acceptJobAction for each row individually — status flips to in_progress,
|
||||||
|
* step jumps to alt_yapi_prova, location lands at_lab. Partial failures
|
||||||
|
* are tolerated; we return how many actually moved.
|
||||||
|
*/
|
||||||
|
export async function bulkAcceptPendingJobsAction(): Promise<
|
||||||
|
JobActionState & { accepted?: number }
|
||||||
|
> {
|
||||||
|
let ctx;
|
||||||
|
try {
|
||||||
|
ctx = await requireTenant();
|
||||||
|
requireRole(ctx, ["owner", "admin", "member"]);
|
||||||
|
requireTenantKind(ctx, ["lab"]);
|
||||||
|
} catch {
|
||||||
|
return { ok: false, error: "Bu işlemi yalnızca laboratuvar yapabilir." };
|
||||||
|
}
|
||||||
|
|
||||||
|
const { tablesDB } = createAdminClient();
|
||||||
|
const result = await tablesDB.listRows({
|
||||||
|
databaseId: DATABASE_ID,
|
||||||
|
tableId: TABLES.jobs,
|
||||||
|
queries: [
|
||||||
|
Query.equal("labTenantId", ctx.tenantId),
|
||||||
|
Query.equal("status", "pending"),
|
||||||
|
Query.limit(200),
|
||||||
|
],
|
||||||
|
});
|
||||||
|
const rows = result.rows as unknown as Job[];
|
||||||
|
if (rows.length === 0) return { ok: true, accepted: 0 };
|
||||||
|
|
||||||
|
const outcomes = await Promise.allSettled(
|
||||||
|
rows.map(async (job) => {
|
||||||
|
await tablesDB.updateRow(DATABASE_ID, TABLES.jobs, job.$id, {
|
||||||
|
status: "in_progress",
|
||||||
|
currentStep: "alt_yapi_prova",
|
||||||
|
location: "at_lab",
|
||||||
|
});
|
||||||
|
void appendJobHistory({ job, step: "olcu", completedBy: ctx.user.id });
|
||||||
|
void createNotification({
|
||||||
|
tenantId: job.clinicTenantId,
|
||||||
|
jobId: job.$id,
|
||||||
|
message: `${ctx.settings?.companyName ?? "Lab"} hasta ${job.patientCode} işini işleme aldı, alt yapı üretiminde.`,
|
||||||
|
});
|
||||||
|
}),
|
||||||
|
);
|
||||||
|
const accepted = outcomes.filter((o) => o.status === "fulfilled").length;
|
||||||
|
|
||||||
|
void logAudit({
|
||||||
|
tenantId: ctx.tenantId,
|
||||||
|
userId: ctx.user.id,
|
||||||
|
action: "update",
|
||||||
|
entityType: "job",
|
||||||
|
entityId: "bulk",
|
||||||
|
changes: { bulk: "accept_pending", count: accepted },
|
||||||
|
});
|
||||||
|
|
||||||
|
revalidatePath("/jobs/inbound");
|
||||||
|
revalidatePath("/dashboard");
|
||||||
|
return { ok: true, accepted };
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Lab hands the work back to the clinic for the next physical step
|
* Lab hands the work back to the clinic for the next physical step
|
||||||
* (prova or final delivery). The current step stays the same — only the
|
* (prova or final delivery). The current step stays the same — only the
|
||||||
|
|||||||
Reference in New Issue
Block a user