diff --git a/web/lib/auftraege/fotos.test.ts b/web/lib/auftraege/fotos.test.ts new file mode 100644 index 0000000..bf99487 --- /dev/null +++ b/web/lib/auftraege/fotos.test.ts @@ -0,0 +1,53 @@ +import { describe, expect, it } from "vitest"; +import { sortiereUndBeschrifteFotos } from "./fotos"; + +describe("sortiereUndBeschrifteFotos", () => { + it("sortiert Pflichtfotos in der festen Positions-Reihenfolge, unabhaengig von der Eingabe-Reihenfolge", () => { + const fotos = [ + { position: "heck", storage_path: "a/heck.jpg", uploaded_at: "2026-01-01T10:00:00Z" }, + { position: "front", storage_path: "a/front.jpg", uploaded_at: "2026-01-01T09:00:00Z" }, + ]; + const result = sortiereUndBeschrifteFotos(fotos); + expect(result.map((f) => f.position)).toEqual(["front", "heck"]); + }); + + it("beschriftet Pflichtfotos mit Leerzeichen statt Unterstrich", () => { + const fotos = [ + { position: "seitlich_rechts", storage_path: "a/seitlich_rechts.jpg", uploaded_at: "2026-01-01T09:00:00Z" }, + ]; + const result = sortiereUndBeschrifteFotos(fotos); + expect(result[0].label).toBe("seitlich rechts"); + }); + + it("laesst fehlende Pflicht-Positionen einfach weg", () => { + const fotos = [{ position: "front", storage_path: "a/front.jpg", uploaded_at: "2026-01-01T09:00:00Z" }]; + const result = sortiereUndBeschrifteFotos(fotos); + expect(result).toHaveLength(1); + expect(result[0].position).toBe("front"); + }); + + it("beschriftet ein einzelnes Schadensfoto ohne Nummer", () => { + const fotos = [{ position: "schaden", storage_path: "a/schaden-1.jpg", uploaded_at: "2026-01-01T09:00:00Z" }]; + const result = sortiereUndBeschrifteFotos(fotos); + expect(result[0].label).toBe("Schaden"); + }); + + it("nummeriert mehrere Schadensfotos nach Upload-Zeitpunkt, unabhaengig von der Eingabe-Reihenfolge", () => { + const fotos = [ + { position: "schaden", storage_path: "a/schaden-2.jpg", uploaded_at: "2026-01-01T10:00:00Z" }, + { position: "schaden", storage_path: "a/schaden-1.jpg", uploaded_at: "2026-01-01T09:00:00Z" }, + ]; + const result = sortiereUndBeschrifteFotos(fotos); + expect(result.map((f) => f.storage_path)).toEqual(["a/schaden-1.jpg", "a/schaden-2.jpg"]); + expect(result.map((f) => f.label)).toEqual(["Schaden 1", "Schaden 2"]); + }); + + it("stellt Pflichtfotos vor Schadensfotos, unabhaengig von der Eingabe-Reihenfolge", () => { + const fotos = [ + { position: "schaden", storage_path: "a/schaden.jpg", uploaded_at: "2026-01-01T08:00:00Z" }, + { position: "front", storage_path: "a/front.jpg", uploaded_at: "2026-01-01T09:00:00Z" }, + ]; + const result = sortiereUndBeschrifteFotos(fotos); + expect(result.map((f) => f.position)).toEqual(["front", "schaden"]); + }); +}); diff --git a/web/lib/auftraege/fotos.ts b/web/lib/auftraege/fotos.ts new file mode 100644 index 0000000..e40dc59 --- /dev/null +++ b/web/lib/auftraege/fotos.ts @@ -0,0 +1,72 @@ +import { createClient } from "@/lib/supabase/server"; +import { PFLICHT_FOTO_POSITIONEN } from "./foto-positionen"; + +export type AuftragFoto = { + position: string; + label: string; + url: string; + downloadUrl: string; +}; + +export type FotoZeile = { + position: string; + storage_path: string; + uploaded_at: string; +}; + +export function sortiereUndBeschrifteFotos(fotos: FotoZeile[]): (FotoZeile & { label: string })[] { + const pflichtFotos = PFLICHT_FOTO_POSITIONEN.map((position) => + fotos.find((foto) => foto.position === position) + ).filter((foto): foto is FotoZeile => foto !== undefined); + + const schadenFotos = fotos + .filter((foto) => foto.position === "schaden") + .sort((a, b) => a.uploaded_at.localeCompare(b.uploaded_at)); + + const beschriftetePflichtFotos = pflichtFotos.map((foto) => ({ + ...foto, + label: foto.position.replaceAll("_", " "), + })); + + const beschrifteteSchadenFotos = schadenFotos.map((foto, index) => ({ + ...foto, + label: schadenFotos.length > 1 ? `Schaden ${index + 1}` : "Schaden", + })); + + return [...beschriftetePflichtFotos, ...beschrifteteSchadenFotos]; +} + +export async function loadAuftragFotos(auftragId: string): Promise { + const supabase = await createClient(); + const { data } = await supabase + .from("auftrag_fotos") + .select("position, storage_path, uploaded_at") + .eq("auftrag_id", auftragId); + + if (!data || data.length === 0) { + return []; + } + + const sortiert = sortiereUndBeschrifteFotos(data); + const publicUrl = process.env.NEXT_PUBLIC_SUPABASE_URL!; + + const fotos = await Promise.all( + sortiert.map(async (foto): Promise => { + const { data: signiert } = await supabase.storage + .from("auftrag-fotos") + .createSignedUrl(foto.storage_path, 3600); + if (!signiert) { + return null; + } + + const geparst = new URL(signiert.signedUrl); + const url = `${publicUrl}${geparst.pathname}${geparst.search}`; + const dateiname = foto.storage_path.split("/").pop() ?? `${foto.position}.jpg`; + const downloadUrl = `${url}&download=${encodeURIComponent(dateiname)}`; + + return { position: foto.position, label: foto.label, url, downloadUrl }; + }) + ); + + return fotos.filter((foto): foto is AuftragFoto => foto !== null); +} diff --git a/web/vitest.config.ts b/web/vitest.config.ts index cea1141..dbf417a 100644 --- a/web/vitest.config.ts +++ b/web/vitest.config.ts @@ -1,6 +1,12 @@ import { defineConfig } from "vitest/config"; +import path from "path"; export default defineConfig({ + resolve: { + alias: { + "@": path.resolve(__dirname, "."), + }, + }, test: { environment: "node", env: {