forked from fahrPool/fahrPool
feat: add loadAuftragFotos query with sort/label logic and signed-URL origin rewrite
- Add fotos.ts: loadAuftragFotos query function and sortiereUndBeschrifteFotos utility - Add fotos.test.ts: 6 unit tests for sorting/labeling logic - Update vitest.config.ts: add path alias resolution for @/ imports
This commit is contained in:
parent
a2254ae539
commit
0a0b0b3112
|
|
@ -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"]);
|
||||
});
|
||||
});
|
||||
|
|
@ -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<AuftragFoto[]> {
|
||||
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<AuftragFoto | null> => {
|
||||
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);
|
||||
}
|
||||
|
|
@ -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: {
|
||||
|
|
|
|||
Loading…
Reference in New Issue