forked from fahrPool/fahrPool
feat: add fahrt-beenden, stornieren and folgeauftrag suggestion
Adds fahrtBeenden (unterwegs -> abgeschlossen with end_km validation) and stornieren (any non-terminal status -> storniert) Server Actions, plus findeFolgeauftrag to surface a linked follow-up order on the detail page once an order is abgeschlossen. kannStornieren uses a local admin/disponent role check rather than canManageUsers, since the latter is scoped to user management and would incorrectly hide the storno action from disponents.
This commit is contained in:
parent
ad15af965b
commit
4459a6cfcb
|
|
@ -2,7 +2,7 @@
|
|||
|
||||
import { redirect } from "next/navigation";
|
||||
import { createClient } from "@/lib/supabase/server";
|
||||
import { validateFahrtBeginn } from "@/lib/auftraege/validate-fahrt";
|
||||
import { validateFahrtBeginn, validateFahrtEnde } from "@/lib/auftraege/validate-fahrt";
|
||||
import { PFLICHT_FOTO_POSITIONEN } from "@/lib/auftraege/foto-positionen";
|
||||
|
||||
export async function uebernehmen(auftragId: string): Promise<void> {
|
||||
|
|
@ -124,3 +124,43 @@ export async function fahrtBeginnen(auftragId: string, formData: FormData): Prom
|
|||
|
||||
redirect(`/auftraege/${auftragId}`);
|
||||
}
|
||||
|
||||
export async function fahrtBeenden(auftragId: string, formData: FormData): Promise<void> {
|
||||
const supabase = await createClient();
|
||||
|
||||
const { data: auftrag } = await supabase.from("auftraege").select("start_km").eq("id", auftragId).single();
|
||||
if (!auftrag || auftrag.start_km === null) {
|
||||
redirect(`/auftraege/${auftragId}?error=${encodeURIComponent("Auftrag hat keinen Start-KM-Stand.")}`);
|
||||
}
|
||||
|
||||
const validation = validateFahrtEnde({ endKm: formData.get("end_km"), startKm: auftrag!.start_km! });
|
||||
if (!validation.valid) {
|
||||
redirect(`/auftraege/${auftragId}?error=${encodeURIComponent(validation.error)}`);
|
||||
}
|
||||
|
||||
const { error } = await supabase
|
||||
.from("auftraege")
|
||||
.update({
|
||||
status: "abgeschlossen",
|
||||
end_km: validation.endKm,
|
||||
uebergabe_bestaetigt_at: new Date().toISOString(),
|
||||
})
|
||||
.eq("id", auftragId);
|
||||
|
||||
if (error) {
|
||||
redirect(`/auftraege/${auftragId}?error=${encodeURIComponent(error.message)}`);
|
||||
}
|
||||
|
||||
redirect(`/auftraege/${auftragId}`);
|
||||
}
|
||||
|
||||
export async function stornieren(auftragId: string): Promise<void> {
|
||||
const supabase = await createClient();
|
||||
const { error } = await supabase.from("auftraege").update({ status: "storniert" }).eq("id", auftragId);
|
||||
|
||||
if (error) {
|
||||
redirect(`/auftraege/${auftragId}?error=${encodeURIComponent(error.message)}`);
|
||||
}
|
||||
|
||||
redirect(`/auftraege/${auftragId}`);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,8 +1,9 @@
|
|||
import { notFound } from "next/navigation";
|
||||
import { loadAuftrag } from "@/lib/auftraege/queries";
|
||||
import Link from "next/link";
|
||||
import { loadAuftrag, findeFolgeauftrag } from "@/lib/auftraege/queries";
|
||||
import { loadProfile } from "@/lib/auth/profile";
|
||||
import { PFLICHT_FOTO_POSITIONEN } from "@/lib/auftraege/foto-positionen";
|
||||
import { uebernehmen, fahrtBeginnen } from "./actions";
|
||||
import { uebernehmen, fahrtBeginnen, fahrtBeenden, stornieren } from "./actions";
|
||||
|
||||
export default async function AuftragDetailPage({
|
||||
params,
|
||||
|
|
@ -20,8 +21,19 @@ export default async function AuftragDetailPage({
|
|||
}
|
||||
|
||||
const istEigenerAuftrag = auftrag.zugewiesen_an === profileResult.profile.id;
|
||||
// Bewusst eine lokale Rollen-Pruefung statt `canManageUsers` (die ist fuer
|
||||
// Nutzerverwaltung gedacht und liefert nur "ist admin"). Laut
|
||||
// Rechtematrix duerfen sowohl Admin als auch Disponent stornieren --
|
||||
// semantisch getrennte Berechtigung von der Nutzerverwaltung, auch wenn
|
||||
// beide aktuell denselben Rollenkreis (admin) ueberschneiden koennten.
|
||||
const kannStornieren =
|
||||
profileResult.profile.role === "admin" || profileResult.profile.role === "disponent";
|
||||
const folgeauftrag = auftrag.status === "abgeschlossen" ? await findeFolgeauftrag(auftrag.id) : null;
|
||||
|
||||
const uebernehmenAction = uebernehmen.bind(null, auftrag.id);
|
||||
const fahrtBeginnenAction = fahrtBeginnen.bind(null, auftrag.id);
|
||||
const fahrtBeendenAction = fahrtBeenden.bind(null, auftrag.id);
|
||||
const stornierenAction = stornieren.bind(null, auftrag.id);
|
||||
|
||||
return (
|
||||
<main>
|
||||
|
|
@ -70,6 +82,29 @@ export default async function AuftragDetailPage({
|
|||
<button type="submit">Fahrt beginnen</button>
|
||||
</form>
|
||||
)}
|
||||
|
||||
{auftrag.status === "unterwegs" && istEigenerAuftrag && (
|
||||
<form action={fahrtBeendenAction}>
|
||||
<h2>Fahrt beenden</h2>
|
||||
<div>
|
||||
<label htmlFor="end_km">End-KM-Stand</label>
|
||||
<input id="end_km" name="end_km" type="number" min="0" required />
|
||||
</div>
|
||||
<button type="submit">Übergabe bestätigen</button>
|
||||
</form>
|
||||
)}
|
||||
|
||||
{auftrag.status === "abgeschlossen" && folgeauftrag && (
|
||||
<p>
|
||||
Anschlussauftrag verfügbar: <Link href={`/auftraege/${folgeauftrag.id}`}>Jetzt übernehmen</Link>
|
||||
</p>
|
||||
)}
|
||||
|
||||
{kannStornieren && auftrag.status !== "abgeschlossen" && auftrag.status !== "storniert" && (
|
||||
<form action={stornierenAction}>
|
||||
<button type="submit">Auftrag stornieren</button>
|
||||
</form>
|
||||
)}
|
||||
</main>
|
||||
);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -83,3 +83,13 @@ export async function loadAuftrag(id: string): Promise<Auftrag | null> {
|
|||
const { data } = await supabase.from("auftraege").select(AUFTRAG_SELECT).eq("id", id).single();
|
||||
return (data as unknown as Auftrag) ?? null;
|
||||
}
|
||||
|
||||
export async function findeFolgeauftrag(vorgaengerId: string): Promise<{ id: string } | null> {
|
||||
const supabase = await createClient();
|
||||
const { data } = await supabase
|
||||
.from("auftraege")
|
||||
.select("id")
|
||||
.eq("vorgaenger_auftrag_id", vorgaengerId)
|
||||
.maybeSingle();
|
||||
return data;
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue