forked from fahrPool/fahrPool
127 lines
4.6 KiB
TypeScript
127 lines
4.6 KiB
TypeScript
"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}`);
|
|
}
|