forked from fahrPool/fahrPool
feat: add auftrag detail page with uebernehmen and fahrt-beginnen actions
fahrtBeginnen uses plain insert (not upsert) for auftrag_fotos writes: the partial unique index from Task 3 (where position <> 'schaden') can't be targeted by PostgREST's onConflict option (Postgres 42P10), and fahrer have no UPDATE grant on auftrag_fotos anyway, so an upsert's DO UPDATE branch could never succeed for them. Verified live against the local Supabase stack that the original upsert-based approach silently wrote zero photo rows, blocking every real fahrtBeginnen attempt at the DB trigger.
This commit is contained in:
parent
d292d122c9
commit
89564e527d
|
|
@ -0,0 +1,126 @@
|
||||||
|
"use server";
|
||||||
|
|
||||||
|
import { redirect } from "next/navigation";
|
||||||
|
import { createClient } from "@/lib/supabase/server";
|
||||||
|
import { validateFahrtBeginn } from "@/lib/auftraege/validate-fahrt";
|
||||||
|
import { PFLICHT_FOTO_POSITIONEN } from "@/lib/auftraege/foto-positionen";
|
||||||
|
|
||||||
|
export async function uebernehmen(auftragId: string): Promise<void> {
|
||||||
|
const supabase = await createClient();
|
||||||
|
const {
|
||||||
|
data: { user },
|
||||||
|
} = await supabase.auth.getUser();
|
||||||
|
|
||||||
|
if (!user) {
|
||||||
|
redirect("/login");
|
||||||
|
}
|
||||||
|
|
||||||
|
const { data, error } = await supabase
|
||||||
|
.from("auftraege")
|
||||||
|
.update({ status: "zugewiesen", zugewiesen_an: user.id, start_zeit: new Date().toISOString() })
|
||||||
|
.eq("id", auftragId)
|
||||||
|
.eq("status", "offen")
|
||||||
|
.select("id");
|
||||||
|
|
||||||
|
if (error || !data || data.length === 0) {
|
||||||
|
redirect(
|
||||||
|
`/auftraege/${auftragId}?error=${encodeURIComponent(
|
||||||
|
"Auftrag wurde bereits übernommen oder ist nicht mehr offen."
|
||||||
|
)}`
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
redirect(`/auftraege/${auftragId}`);
|
||||||
|
}
|
||||||
|
|
||||||
|
export async function fahrtBeginnen(auftragId: string, formData: FormData): Promise<void> {
|
||||||
|
const supabase = await createClient();
|
||||||
|
const {
|
||||||
|
data: { user },
|
||||||
|
} = await supabase.auth.getUser();
|
||||||
|
|
||||||
|
if (!user) {
|
||||||
|
redirect("/login");
|
||||||
|
}
|
||||||
|
|
||||||
|
const uploads: { position: string; file: File }[] = [];
|
||||||
|
for (const position of PFLICHT_FOTO_POSITIONEN) {
|
||||||
|
const file = formData.get(`foto_${position}`);
|
||||||
|
if (file instanceof File && file.size > 0) {
|
||||||
|
uploads.push({ position, file });
|
||||||
|
}
|
||||||
|
}
|
||||||
|
const schadenFiles = formData.getAll("foto_schaden").filter((f): f is File => f instanceof File && f.size > 0);
|
||||||
|
|
||||||
|
const validation = validateFahrtBeginn({
|
||||||
|
startKm: formData.get("start_km"),
|
||||||
|
vorhandenePositionen: uploads.map((u) => u.position),
|
||||||
|
});
|
||||||
|
|
||||||
|
if (!validation.valid) {
|
||||||
|
redirect(`/auftraege/${auftragId}?error=${encodeURIComponent(validation.error)}`);
|
||||||
|
}
|
||||||
|
|
||||||
|
for (const { position, file } of uploads) {
|
||||||
|
const path = `${auftragId}/${position}.jpg`;
|
||||||
|
const { error: uploadError } = await supabase.storage.from("auftrag-fotos").upload(path, file, {
|
||||||
|
upsert: true,
|
||||||
|
});
|
||||||
|
if (uploadError) {
|
||||||
|
redirect(
|
||||||
|
`/auftraege/${auftragId}?error=${encodeURIComponent(`Foto-Upload fehlgeschlagen: ${uploadError.message}`)}`
|
||||||
|
);
|
||||||
|
}
|
||||||
|
// Plain insert, not upsert: der partielle Unique-Index aus Task 3
|
||||||
|
// (`where position <> 'schaden'`) kann von PostgRESTs `onConflict`-Option
|
||||||
|
// nicht als Ziel adressiert werden -- Postgres meldet dafuer
|
||||||
|
// "42P10: there is no unique or exclusion constraint matching the ON
|
||||||
|
// CONFLICT specification", da ON-CONFLICT-Inferenz per Spaltenliste einen
|
||||||
|
// passenden VOLLSTAENDIGEN (nicht partiellen) Unique-Index verlangt. Ein
|
||||||
|
// "upsert" haette hier ausserdem auch bei passendem Index nie funktioniert:
|
||||||
|
// Task 3 vergibt fuer Fahrer nur eine INSERT-Policy auf auftrag_fotos,
|
||||||
|
// keine UPDATE-Policy (nur Admin darf aktualisieren) -- der
|
||||||
|
// UPDATE-Zweig eines ON CONFLICT DO UPDATE waere also fuer den
|
||||||
|
// hochladenden Fahrer ohnehin von RLS blockiert. Der Fehler wird
|
||||||
|
// (anders als im urspruenglich skizzierten Code) explizit geprueft,
|
||||||
|
// damit ein RLS- oder Unique-Verstoss nicht wieder stillschweigend
|
||||||
|
// verschluckt wird, bevor der Statuswechsel unten am DB-Trigger scheitert.
|
||||||
|
const { error: fotoError } = await supabase
|
||||||
|
.from("auftrag_fotos")
|
||||||
|
.insert({ auftrag_id: auftragId, position, storage_path: path, uploaded_by: user.id });
|
||||||
|
if (fotoError) {
|
||||||
|
redirect(
|
||||||
|
`/auftraege/${auftragId}?error=${encodeURIComponent(`Foto-Speichern fehlgeschlagen: ${fotoError.message}`)}`
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
for (let i = 0; i < schadenFiles.length; i++) {
|
||||||
|
const path = `${auftragId}/schaden-${Date.now()}-${i}.jpg`;
|
||||||
|
const { error: uploadError } = await supabase.storage.from("auftrag-fotos").upload(path, schadenFiles[i]);
|
||||||
|
if (uploadError) {
|
||||||
|
redirect(
|
||||||
|
`/auftraege/${auftragId}?error=${encodeURIComponent(`Foto-Upload fehlgeschlagen: ${uploadError.message}`)}`
|
||||||
|
);
|
||||||
|
}
|
||||||
|
const { error: fotoError } = await supabase
|
||||||
|
.from("auftrag_fotos")
|
||||||
|
.insert({ auftrag_id: auftragId, position: "schaden", storage_path: path, uploaded_by: user.id });
|
||||||
|
if (fotoError) {
|
||||||
|
redirect(
|
||||||
|
`/auftraege/${auftragId}?error=${encodeURIComponent(`Foto-Speichern fehlgeschlagen: ${fotoError.message}`)}`
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const { error } = await supabase
|
||||||
|
.from("auftraege")
|
||||||
|
.update({ status: "unterwegs", start_km: validation.startKm })
|
||||||
|
.eq("id", auftragId);
|
||||||
|
|
||||||
|
if (error) {
|
||||||
|
redirect(`/auftraege/${auftragId}?error=${encodeURIComponent(error.message)}`);
|
||||||
|
}
|
||||||
|
|
||||||
|
redirect(`/auftraege/${auftragId}`);
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,75 @@
|
||||||
|
import { notFound } from "next/navigation";
|
||||||
|
import { loadAuftrag } from "@/lib/auftraege/queries";
|
||||||
|
import { loadProfile } from "@/lib/auth/profile";
|
||||||
|
import { PFLICHT_FOTO_POSITIONEN } from "@/lib/auftraege/foto-positionen";
|
||||||
|
import { uebernehmen, fahrtBeginnen } from "./actions";
|
||||||
|
|
||||||
|
export default async function AuftragDetailPage({
|
||||||
|
params,
|
||||||
|
searchParams,
|
||||||
|
}: {
|
||||||
|
params: Promise<{ id: string }>;
|
||||||
|
searchParams: Promise<{ error?: string }>;
|
||||||
|
}) {
|
||||||
|
const { id } = await params;
|
||||||
|
const { error } = await searchParams;
|
||||||
|
const [auftrag, profileResult] = await Promise.all([loadAuftrag(id), loadProfile()]);
|
||||||
|
|
||||||
|
if (!auftrag || profileResult.status !== "ok") {
|
||||||
|
notFound();
|
||||||
|
}
|
||||||
|
|
||||||
|
const istEigenerAuftrag = auftrag.zugewiesen_an === profileResult.profile.id;
|
||||||
|
const uebernehmenAction = uebernehmen.bind(null, auftrag.id);
|
||||||
|
const fahrtBeginnenAction = fahrtBeginnen.bind(null, auftrag.id);
|
||||||
|
|
||||||
|
return (
|
||||||
|
<main>
|
||||||
|
<h1>
|
||||||
|
Auftrag: {auftrag.abholort.adresse} → {auftrag.zielort.adresse}
|
||||||
|
</h1>
|
||||||
|
{error && <p role="alert">{error}</p>}
|
||||||
|
<dl>
|
||||||
|
<dt>Status</dt>
|
||||||
|
<dd>{auftrag.status}</dd>
|
||||||
|
<dt>Fahrzeug</dt>
|
||||||
|
<dd>{auftrag.fahrzeug.kennzeichen}</dd>
|
||||||
|
<dt>Abholort</dt>
|
||||||
|
<dd>
|
||||||
|
{auftrag.abholort.adresse} ({auftrag.abholort.ansprechpartner})
|
||||||
|
</dd>
|
||||||
|
<dt>Zielort</dt>
|
||||||
|
<dd>
|
||||||
|
{auftrag.zielort.adresse} ({auftrag.zielort.ansprechpartner})
|
||||||
|
</dd>
|
||||||
|
</dl>
|
||||||
|
|
||||||
|
{auftrag.status === "offen" && (
|
||||||
|
<form action={uebernehmenAction}>
|
||||||
|
<button type="submit">Auftrag übernehmen</button>
|
||||||
|
</form>
|
||||||
|
)}
|
||||||
|
|
||||||
|
{auftrag.status === "zugewiesen" && istEigenerAuftrag && (
|
||||||
|
<form action={fahrtBeginnenAction} encType="multipart/form-data">
|
||||||
|
<h2>Fahrt beginnen</h2>
|
||||||
|
<div>
|
||||||
|
<label htmlFor="start_km">KM-Stand</label>
|
||||||
|
<input id="start_km" name="start_km" type="number" min="0" required />
|
||||||
|
</div>
|
||||||
|
{PFLICHT_FOTO_POSITIONEN.map((position) => (
|
||||||
|
<div key={position}>
|
||||||
|
<label htmlFor={`foto_${position}`}>{position.replaceAll("_", " ")}</label>
|
||||||
|
<input id={`foto_${position}`} name={`foto_${position}`} type="file" accept="image/*" required />
|
||||||
|
</div>
|
||||||
|
))}
|
||||||
|
<div>
|
||||||
|
<label htmlFor="foto_schaden">Schadensfotos (optional, mehrere möglich)</label>
|
||||||
|
<input id="foto_schaden" name="foto_schaden" type="file" accept="image/*" multiple />
|
||||||
|
</div>
|
||||||
|
<button type="submit">Fahrt beginnen</button>
|
||||||
|
</form>
|
||||||
|
)}
|
||||||
|
</main>
|
||||||
|
);
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue