forked from fahrPool/fahrPool
feat: Zuweisen, Umzuweisen und Zuweisung aufheben auf der Auftragsdetailseite
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
parent
98b548d865
commit
d6dedc578a
|
|
@ -6,6 +6,8 @@ import { validateFahrtBeginn, validateFahrtEnde, validateStartFin } from "@/lib/
|
||||||
import { STANDARD_FOTO_POSITIONEN } from "@/lib/auftraege/foto-positionen";
|
import { STANDARD_FOTO_POSITIONEN } from "@/lib/auftraege/foto-positionen";
|
||||||
import { loadAuftrag } from "@/lib/auftraege/queries";
|
import { loadAuftrag } from "@/lib/auftraege/queries";
|
||||||
import { loadProfile } from "@/lib/auth/profile";
|
import { loadProfile } from "@/lib/auth/profile";
|
||||||
|
import { kannAuftraegeVerwalten } from "@/lib/auth/roles";
|
||||||
|
import { zuweisungsUpdate } from "@/lib/auftraege/zuweisung";
|
||||||
import { parseSchadenUploads } from "@/lib/auftraege/schaden-upload";
|
import { parseSchadenUploads } from "@/lib/auftraege/schaden-upload";
|
||||||
import { istZielStatusErreicht, type StatusAktion } from "@/lib/auftraege/status-erreicht";
|
import { istZielStatusErreicht, type StatusAktion } from "@/lib/auftraege/status-erreicht";
|
||||||
|
|
||||||
|
|
@ -304,10 +306,7 @@ export async function fahrtBeenden(auftragId: string, formData: FormData): Promi
|
||||||
|
|
||||||
export async function stornieren(auftragId: string): Promise<void> {
|
export async function stornieren(auftragId: string): Promise<void> {
|
||||||
const profileResult = await loadProfile();
|
const profileResult = await loadProfile();
|
||||||
if (
|
if (profileResult.status !== "ok" || !kannAuftraegeVerwalten(profileResult.profile.role)) {
|
||||||
profileResult.status !== "ok" ||
|
|
||||||
(profileResult.profile.role !== "admin" && profileResult.profile.role !== "disponent")
|
|
||||||
) {
|
|
||||||
redirect(
|
redirect(
|
||||||
`/auftraege/${auftragId}?error=${encodeURIComponent("Nur Disponent/Admin duerfen einen Auftrag stornieren.")}`
|
`/auftraege/${auftragId}?error=${encodeURIComponent("Nur Disponent/Admin duerfen einen Auftrag stornieren.")}`
|
||||||
);
|
);
|
||||||
|
|
@ -326,3 +325,65 @@ export async function stornieren(auftragId: string): Promise<void> {
|
||||||
|
|
||||||
redirect(`/auftraege/${auftragId}`);
|
redirect(`/auftraege/${auftragId}`);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function fehlerRedirect(auftragId: string, meldung: string): never {
|
||||||
|
redirect(`/auftraege/${auftragId}?error=${encodeURIComponent(meldung)}`);
|
||||||
|
}
|
||||||
|
|
||||||
|
export async function zuweisen(auftragId: string, formData: FormData): Promise<void> {
|
||||||
|
const profileResult = await loadProfile();
|
||||||
|
if (profileResult.status !== "ok" || !kannAuftraegeVerwalten(profileResult.profile.role)) {
|
||||||
|
fehlerRedirect(auftragId, "Nur Disponent/Admin duerfen Auftraege zuweisen.");
|
||||||
|
}
|
||||||
|
|
||||||
|
const personIdRaw = formData.get("person_id");
|
||||||
|
const personId = typeof personIdRaw === "string" ? personIdRaw : "";
|
||||||
|
|
||||||
|
const supabase = await createClient();
|
||||||
|
const { data: aktuell } = await supabase
|
||||||
|
.from("auftraege")
|
||||||
|
.select("status, zugewiesen_an")
|
||||||
|
.eq("id", auftragId)
|
||||||
|
.maybeSingle();
|
||||||
|
if (!aktuell) {
|
||||||
|
fehlerRedirect(auftragId, "Auftrag nicht gefunden.");
|
||||||
|
}
|
||||||
|
|
||||||
|
const ergebnis = zuweisungsUpdate(aktuell, personId, new Date());
|
||||||
|
if (ergebnis.art === "fehler") fehlerRedirect(auftragId, ergebnis.meldung);
|
||||||
|
if (ergebnis.art === "keine_aenderung") redirect(`/auftraege/${auftragId}`);
|
||||||
|
|
||||||
|
// Optimistic Guard: nur wenn Status und Person noch dem gelesenen Stand
|
||||||
|
// entsprechen (paralleles Zuweisen/Uebernehmen gewinnt sonst genau einmal).
|
||||||
|
let query = supabase.from("auftraege").update(ergebnis.werte).eq("id", auftragId).eq("status", aktuell.status);
|
||||||
|
query = aktuell.zugewiesen_an === null ? query.is("zugewiesen_an", null) : query.eq("zugewiesen_an", aktuell.zugewiesen_an);
|
||||||
|
const { data, error } = await query.select("id");
|
||||||
|
|
||||||
|
if (error?.code === "42501") fehlerRedirect(auftragId, "Diese Person kann keine Aufträge übernehmen.");
|
||||||
|
if (error || !data || data.length === 0) {
|
||||||
|
fehlerRedirect(auftragId, "Der Auftrag wurde inzwischen geändert. Bitte erneut versuchen.");
|
||||||
|
}
|
||||||
|
|
||||||
|
redirect(`/auftraege/${auftragId}`);
|
||||||
|
}
|
||||||
|
|
||||||
|
export async function zuweisungAufheben(auftragId: string): Promise<void> {
|
||||||
|
const profileResult = await loadProfile();
|
||||||
|
if (profileResult.status !== "ok" || !kannAuftraegeVerwalten(profileResult.profile.role)) {
|
||||||
|
fehlerRedirect(auftragId, "Nur Disponent/Admin duerfen Zuweisungen aufheben.");
|
||||||
|
}
|
||||||
|
|
||||||
|
const supabase = await createClient();
|
||||||
|
const { data, error } = await supabase
|
||||||
|
.from("auftraege")
|
||||||
|
.update({ status: "offen", zugewiesen_an: null, start_zeit: null })
|
||||||
|
.eq("id", auftragId)
|
||||||
|
.eq("status", "zugewiesen")
|
||||||
|
.select("id");
|
||||||
|
|
||||||
|
if (error || !data || data.length === 0) {
|
||||||
|
fehlerRedirect(auftragId, "Zuweisung konnte nicht aufgehoben werden.");
|
||||||
|
}
|
||||||
|
|
||||||
|
redirect(`/auftraege/${auftragId}`);
|
||||||
|
}
|
||||||
|
|
|
||||||
|
|
@ -3,6 +3,8 @@ import Link from "next/link";
|
||||||
import { loadAuftrag, findeFolgeauftrag } from "@/lib/auftraege/queries";
|
import { loadAuftrag, findeFolgeauftrag } from "@/lib/auftraege/queries";
|
||||||
import { formatZeitplanung } from "@/lib/auftraege/zeitplanung";
|
import { formatZeitplanung } from "@/lib/auftraege/zeitplanung";
|
||||||
import { loadProfile } from "@/lib/auth/profile";
|
import { loadProfile } from "@/lib/auth/profile";
|
||||||
|
import { istEinsatzrolle, kannAuftraegeVerwalten, personBezeichnung } from "@/lib/auth/roles";
|
||||||
|
import { listEinsatzPersonen } from "@/lib/dashboard/queries";
|
||||||
import { fahrzeugBezeichnung } from "@/lib/orte-fahrzeuge/fahrzeug-bezeichnung";
|
import { fahrzeugBezeichnung } from "@/lib/orte-fahrzeuge/fahrzeug-bezeichnung";
|
||||||
import { finAbgleichText } from "@/lib/auftraege/fin-abgleich";
|
import { finAbgleichText } from "@/lib/auftraege/fin-abgleich";
|
||||||
import { STANDARD_FOTO_POSITIONEN } from "@/lib/auftraege/foto-positionen";
|
import { STANDARD_FOTO_POSITIONEN } from "@/lib/auftraege/foto-positionen";
|
||||||
|
|
@ -11,7 +13,15 @@ import { FinFeld } from "@/components/fahrzeuge/FinFeld";
|
||||||
import { FotoGalerie } from "@/components/auftraege/FotoGalerie";
|
import { FotoGalerie } from "@/components/auftraege/FotoGalerie";
|
||||||
import { SchadenFotoFelder } from "@/components/auftraege/SchadenFotoFelder";
|
import { SchadenFotoFelder } from "@/components/auftraege/SchadenFotoFelder";
|
||||||
import { UnterschriftCanvas } from "@/components/auftraege/UnterschriftCanvas";
|
import { UnterschriftCanvas } from "@/components/auftraege/UnterschriftCanvas";
|
||||||
import { uebernehmen, fahrtBeginnen, schadenFotoNachtragen, fahrtBeenden, stornieren } from "./actions";
|
import {
|
||||||
|
uebernehmen,
|
||||||
|
fahrtBeginnen,
|
||||||
|
schadenFotoNachtragen,
|
||||||
|
fahrtBeenden,
|
||||||
|
stornieren,
|
||||||
|
zuweisen,
|
||||||
|
zuweisungAufheben,
|
||||||
|
} from "./actions";
|
||||||
import { NetzSicheresFormular } from "@/components/pwa/NetzSicheresFormular";
|
import { NetzSicheresFormular } from "@/components/pwa/NetzSicheresFormular";
|
||||||
import { Card } from "@/components/ui/Card";
|
import { Card } from "@/components/ui/Card";
|
||||||
import { StatusBadge } from "@/components/ui/StatusBadge";
|
import { StatusBadge } from "@/components/ui/StatusBadge";
|
||||||
|
|
@ -35,24 +45,24 @@ export default async function AuftragDetailPage({
|
||||||
}
|
}
|
||||||
|
|
||||||
const istEigenerAuftrag = auftrag.zugewiesen_an === profileResult.profile.id;
|
const istEigenerAuftrag = auftrag.zugewiesen_an === profileResult.profile.id;
|
||||||
// Bewusst eine lokale Rollen-Pruefung statt `canManageUsers` (die ist fuer
|
const kannStornieren = kannAuftraegeVerwalten(profileResult.profile.role);
|
||||||
// Nutzerverwaltung gedacht und liefert nur "ist admin"). Laut
|
const kannZuweisen =
|
||||||
// Rechtematrix duerfen sowohl Admin als auch Disponent stornieren --
|
kannAuftraegeVerwalten(profileResult.profile.role) &&
|
||||||
// semantisch getrennte Berechtigung von der Nutzerverwaltung, auch wenn
|
(auftrag.status === "offen" || auftrag.status === "zugewiesen");
|
||||||
// beide aktuell denselben Rollenkreis (admin) ueberschneiden koennten.
|
|
||||||
const kannStornieren =
|
|
||||||
profileResult.profile.role === "admin" || profileResult.profile.role === "disponent";
|
|
||||||
const [folgeauftrag, fotos, unterschriftUrl] = await Promise.all([
|
const [folgeauftrag, fotos, unterschriftUrl] = await Promise.all([
|
||||||
auftrag.status === "abgeschlossen" ? findeFolgeauftrag(auftrag.id) : Promise.resolve(null),
|
auftrag.status === "abgeschlossen" ? findeFolgeauftrag(auftrag.id) : Promise.resolve(null),
|
||||||
loadAuftragFotos(auftrag.id),
|
loadAuftragFotos(auftrag.id),
|
||||||
loadUnterschriftUrl(auftrag.unterschrift_storage_path),
|
loadUnterschriftUrl(auftrag.unterschrift_storage_path),
|
||||||
]);
|
]);
|
||||||
|
const personen = kannZuweisen ? await listEinsatzPersonen() : [];
|
||||||
|
|
||||||
const uebernehmenAction = uebernehmen.bind(null, auftrag.id);
|
const uebernehmenAction = uebernehmen.bind(null, auftrag.id);
|
||||||
const fahrtBeginnenAction = fahrtBeginnen.bind(null, auftrag.id);
|
const fahrtBeginnenAction = fahrtBeginnen.bind(null, auftrag.id);
|
||||||
const schadenFotoNachtragenAction = schadenFotoNachtragen.bind(null, auftrag.id);
|
const schadenFotoNachtragenAction = schadenFotoNachtragen.bind(null, auftrag.id);
|
||||||
const fahrtBeendenAction = fahrtBeenden.bind(null, auftrag.id);
|
const fahrtBeendenAction = fahrtBeenden.bind(null, auftrag.id);
|
||||||
const stornierenAction = stornieren.bind(null, auftrag.id);
|
const stornierenAction = stornieren.bind(null, auftrag.id);
|
||||||
|
const zuweisenAction = zuweisen.bind(null, auftrag.id);
|
||||||
|
const zuweisungAufhebenAction = zuweisungAufheben.bind(null, auftrag.id);
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div className="mx-auto max-w-2xl p-4 md:p-8">
|
<div className="mx-auto max-w-2xl p-4 md:p-8">
|
||||||
|
|
@ -61,6 +71,14 @@ export default async function AuftragDetailPage({
|
||||||
<StatusBadge status={auftrag.status} />
|
<StatusBadge status={auftrag.status} />
|
||||||
<span className="text-sm font-medium text-asphalt-grey/70">{auftrag.auftragsnummer}</span>
|
<span className="text-sm font-medium text-asphalt-grey/70">{auftrag.auftragsnummer}</span>
|
||||||
</div>
|
</div>
|
||||||
|
{kannZuweisen && (
|
||||||
|
<Link
|
||||||
|
href={`/auftraege/${auftrag.id}/bearbeiten`}
|
||||||
|
className="mb-2 inline-flex min-h-11 items-center text-sm font-medium text-performance-blue hover:underline"
|
||||||
|
>
|
||||||
|
Bearbeiten
|
||||||
|
</Link>
|
||||||
|
)}
|
||||||
<h1 className="text-xl font-semibold text-asphalt-grey">
|
<h1 className="text-xl font-semibold text-asphalt-grey">
|
||||||
{auftrag.abholort.adresse} → {auftrag.zielort.adresse}
|
{auftrag.abholort.adresse} → {auftrag.zielort.adresse}
|
||||||
</h1>
|
</h1>
|
||||||
|
|
@ -157,6 +175,10 @@ export default async function AuftragDetailPage({
|
||||||
{formatZeitplanung(auftrag.zeitplanung_typ, auftrag.zeitplanung_zeitpunkt)}
|
{formatZeitplanung(auftrag.zeitplanung_typ, auftrag.zeitplanung_zeitpunkt)}
|
||||||
</dd>
|
</dd>
|
||||||
</div>
|
</div>
|
||||||
|
<div>
|
||||||
|
<dt className="text-sm text-asphalt-grey/70">Zugewiesen an</dt>
|
||||||
|
<dd className="text-asphalt-grey">{auftrag.zugewiesen_an_profil?.name ?? "–"}</dd>
|
||||||
|
</div>
|
||||||
{auftrag.erstellt_von && (
|
{auftrag.erstellt_von && (
|
||||||
<div>
|
<div>
|
||||||
<dt className="text-sm text-asphalt-grey/70">Ersteller</dt>
|
<dt className="text-sm text-asphalt-grey/70">Ersteller</dt>
|
||||||
|
|
@ -200,7 +222,7 @@ export default async function AuftragDetailPage({
|
||||||
</div>
|
</div>
|
||||||
)}
|
)}
|
||||||
|
|
||||||
{auftrag.status === "offen" && (
|
{auftrag.status === "offen" && istEinsatzrolle(profileResult.profile.role) && (
|
||||||
<Card className="mb-6">
|
<Card className="mb-6">
|
||||||
<NetzSicheresFormular action={uebernehmenAction}>
|
<NetzSicheresFormular action={uebernehmenAction}>
|
||||||
<Button type="submit">Auftrag übernehmen</Button>
|
<Button type="submit">Auftrag übernehmen</Button>
|
||||||
|
|
@ -208,6 +230,42 @@ export default async function AuftragDetailPage({
|
||||||
</Card>
|
</Card>
|
||||||
)}
|
)}
|
||||||
|
|
||||||
|
{kannZuweisen && (
|
||||||
|
<Card className="mb-6">
|
||||||
|
<h2 className="mb-4 text-lg font-semibold text-asphalt-grey">Zuweisung</h2>
|
||||||
|
<NetzSicheresFormular action={zuweisenAction}>
|
||||||
|
<FormField label="Person" htmlFor="person_id">
|
||||||
|
<select
|
||||||
|
id="person_id"
|
||||||
|
name="person_id"
|
||||||
|
required
|
||||||
|
defaultValue={auftrag.zugewiesen_an ?? ""}
|
||||||
|
className={FIELD_CLASSES}
|
||||||
|
>
|
||||||
|
<option value="" disabled>
|
||||||
|
Bitte wählen
|
||||||
|
</option>
|
||||||
|
{personen.map((person) => (
|
||||||
|
<option key={person.id} value={person.id}>
|
||||||
|
{personBezeichnung(person)}
|
||||||
|
</option>
|
||||||
|
))}
|
||||||
|
</select>
|
||||||
|
</FormField>
|
||||||
|
<Button type="submit">Zuweisen</Button>
|
||||||
|
</NetzSicheresFormular>
|
||||||
|
{auftrag.status === "zugewiesen" && (
|
||||||
|
<div className="mt-3">
|
||||||
|
<NetzSicheresFormular action={zuweisungAufhebenAction}>
|
||||||
|
<Button type="submit" variant="secondary">
|
||||||
|
Zuweisung aufheben
|
||||||
|
</Button>
|
||||||
|
</NetzSicheresFormular>
|
||||||
|
</div>
|
||||||
|
)}
|
||||||
|
</Card>
|
||||||
|
)}
|
||||||
|
|
||||||
{auftrag.status === "zugewiesen" && istEigenerAuftrag && (
|
{auftrag.status === "zugewiesen" && istEigenerAuftrag && (
|
||||||
<Card className="mb-6">
|
<Card className="mb-6">
|
||||||
<NetzSicheresFormular action={fahrtBeginnenAction}>
|
<NetzSicheresFormular action={fahrtBeginnenAction}>
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,40 @@
|
||||||
|
import { describe, expect, it } from "vitest";
|
||||||
|
import { zuweisungsUpdate } from "./zuweisung";
|
||||||
|
|
||||||
|
const jetzt = new Date("2026-11-02T09:30:00Z");
|
||||||
|
|
||||||
|
describe("zuweisungsUpdate", () => {
|
||||||
|
it("weist einen offenen Auftrag zu und setzt die Startzeit", () => {
|
||||||
|
expect(zuweisungsUpdate({ status: "offen", zugewiesen_an: null }, "P", jetzt)).toEqual({
|
||||||
|
art: "update",
|
||||||
|
werte: { status: "zugewiesen", zugewiesen_an: "P", start_zeit: "2026-11-02T09:30:00.000Z" },
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
it("weist einen zugewiesenen Auftrag an eine andere Person um", () => {
|
||||||
|
expect(zuweisungsUpdate({ status: "zugewiesen", zugewiesen_an: "A" }, "B", jetzt)).toEqual({
|
||||||
|
art: "update",
|
||||||
|
werte: { status: "zugewiesen", zugewiesen_an: "B", start_zeit: "2026-11-02T09:30:00.000Z" },
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
it("aendert nichts bei derselben Person", () => {
|
||||||
|
expect(zuweisungsUpdate({ status: "zugewiesen", zugewiesen_an: "A" }, "A", jetzt)).toEqual({ art: "keine_aenderung" });
|
||||||
|
});
|
||||||
|
|
||||||
|
it("lehnt Auftraege ausserhalb von offen/zugewiesen ab", () => {
|
||||||
|
for (const status of ["unterwegs", "abgeschlossen", "storniert"] as const) {
|
||||||
|
expect(zuweisungsUpdate({ status, zugewiesen_an: "A" }, "B", jetzt)).toEqual({
|
||||||
|
art: "fehler",
|
||||||
|
meldung: "Zuweisung ist nur bei offenen oder zugewiesenen Aufträgen möglich.",
|
||||||
|
});
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
it("verlangt eine Person", () => {
|
||||||
|
expect(zuweisungsUpdate({ status: "offen", zugewiesen_an: null }, "", jetzt)).toEqual({
|
||||||
|
art: "fehler",
|
||||||
|
meldung: "Bitte eine Person auswählen.",
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
@ -0,0 +1,19 @@
|
||||||
|
import type { AuftragStatus } from "./queries";
|
||||||
|
|
||||||
|
export type ZuweisungsErgebnis =
|
||||||
|
| { art: "keine_aenderung" }
|
||||||
|
| { art: "fehler"; meldung: string }
|
||||||
|
| { art: "update"; werte: { status: "zugewiesen"; zugewiesen_an: string; start_zeit: string } };
|
||||||
|
|
||||||
|
export function zuweisungsUpdate(
|
||||||
|
aktuell: { status: AuftragStatus; zugewiesen_an: string | null },
|
||||||
|
personId: string,
|
||||||
|
jetzt: Date
|
||||||
|
): ZuweisungsErgebnis {
|
||||||
|
if (!personId) return { art: "fehler", meldung: "Bitte eine Person auswählen." };
|
||||||
|
if (aktuell.status !== "offen" && aktuell.status !== "zugewiesen") {
|
||||||
|
return { art: "fehler", meldung: "Zuweisung ist nur bei offenen oder zugewiesenen Aufträgen möglich." };
|
||||||
|
}
|
||||||
|
if (aktuell.status === "zugewiesen" && aktuell.zugewiesen_an === personId) return { art: "keine_aenderung" };
|
||||||
|
return { art: "update", werte: { status: "zugewiesen", zugewiesen_an: personId, start_zeit: jetzt.toISOString() } };
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue