Эх сурвалжийг харах

Merge branch 'RHL-001-create-first-apis-to-connect-to-core-folder' of Code_Uwe/rhl-lieferscheine into main

Code_Uwe 2 долоо хоног өмнө
parent
commit
3b199a2806

+ 27 - 0
app/api/branches/[branch]/[year]/[month]/days/route.js

@@ -0,0 +1,27 @@
+// app/api/branches/[branch]/[year]/[month]/days/route.js
+import { NextResponse } from "next/server";
+import { listDays } from "@/lib/storage";
+
+export function GET(request, { params }) {
+	const { branch, year, month } = params;
+
+	if (!branch || !year || !month) {
+		return NextResponse.json(
+			{ error: "branch, year oder month fehlt" },
+			{ status: 400 }
+		);
+	}
+
+	return listDays(branch, month.length === 4 ? branch : year, month)
+		.then((days) => NextResponse.json({ branch, year, month, days }))
+		.catch((error) => {
+			console.error(
+				"[api/branches/[branch]/[year]/[month]/days] Fehler:",
+				error
+			);
+			return NextResponse.json(
+				{ error: "Fehler beim Lesen der Tage" },
+				{ status: 500 }
+			);
+		});
+}

+ 25 - 0
app/api/branches/[branch]/[year]/months/route.js

@@ -0,0 +1,25 @@
+// app/api/branches/[branch]/[year]/months/route.js
+import { NextResponse } from "next/server";
+import { listMonths } from "@/lib/storage";
+
+export async function GET(request, { params }) {
+	const { branch, year } = params;
+
+	if (!branch || !year) {
+		return NextResponse.json(
+			{ error: "branch oder year fehlt" },
+			{ status: 400 }
+		);
+	}
+
+	try {
+		const months = await listMonths(branch, year);
+		return NextResponse.json({ branch, year, months });
+	} catch (error) {
+		console.error("[api/branches/[branch]/[year]/months] Fehler:", error);
+		return NextResponse.json(
+			{ error: "Fehler beim Lesen der Monate" },
+			{ status: 500 }
+		);
+	}
+}

+ 25 - 0
app/api/branches/[branch]/years/route.js

@@ -0,0 +1,25 @@
+// app/api/branches/[branch]/years/route.js
+import { NextResponse } from "next/server";
+import { listYears } from "@/lib/storage";
+
+export async function GET(request, { params }) {
+	const { branch } = params;
+
+	if (!branch) {
+		return NextResponse.json(
+			{ error: "branch Parameter fehlt" },
+			{ status: 400 }
+		);
+	}
+
+	try {
+		const years = await listYears(branch);
+		return NextResponse.json({ branch, years });
+	} catch (error) {
+		console.error("[api/branches/[branch]/years] Fehler:", error);
+		return NextResponse.json(
+			{ error: "Fehler beim Lesen der Jahre" },
+			{ status: 500 }
+		);
+	}
+}

+ 16 - 0
app/api/branches/route.js

@@ -0,0 +1,16 @@
+// app/api/branches/route.js
+import { NextResponse } from "next/server";
+import { listBranches } from "@/lib/storage";
+
+export async function GET() {
+	try {
+		const branches = await listBranches();
+		return NextResponse.json({ branches });
+	} catch (error) {
+		console.error("[api/branches] Fehler:", error);
+		return NextResponse.json(
+			{ error: "Fehler beim Lesen der Niederlassungen" },
+			{ status: 500 }
+		);
+	}
+}

+ 30 - 0
app/api/files/route.js

@@ -0,0 +1,30 @@
+// app/api/files/route.js
+import { NextResponse } from "next/server";
+import { listFiles } from "@/lib/storage";
+
+export async function GET(request) {
+	const { searchParams } = new URL(request.url);
+
+	const branch = searchParams.get("branch");
+	const year = searchParams.get("year");
+	const month = searchParams.get("month");
+	const day = searchParams.get("day");
+
+	if (!branch || !year || !month || !day) {
+		return NextResponse.json(
+			{ error: "branch, year, month, day sind erforderlich" },
+			{ status: 400 }
+		);
+	}
+
+	try {
+		const files = await listFiles(branch, year, month, day);
+		return NextResponse.json({ branch, year, month, day, files });
+	} catch (error) {
+		console.error("[api/files] Fehler:", error);
+		return NextResponse.json(
+			{ error: "Fehler beim Lesen der Dateien" },
+			{ status: 500 }
+		);
+	}
+}

+ 89 - 0
lib/storage.js

@@ -0,0 +1,89 @@
+// lib/storage.js
+import fs from "fs/promises";
+import path from "path";
+
+const ROOT = process.env.NAS_ROOT_PATH;
+
+if (!ROOT) {
+	console.warn(
+		"[storage] NAS_ROOT_PATH ist nicht gesetzt – Dateizugriff wird fehlschlagen"
+	);
+}
+
+function fullPath(...segments) {
+	if (!ROOT) {
+		throw new Error("NAS_ROOT_PATH ist nicht gesetzt");
+	}
+
+	return path.join(ROOT, ...segments.map(String));
+}
+
+// Hilfsfunktion: sortiert numerische Strings wie "1","2","10" korrekt
+function sortNumericStrings(a, b) {
+	const na = parseInt(a, 10);
+	const nb = parseInt(b, 10);
+	if (!Number.isNaN(na) && !Number.isNaN(nb)) {
+		return na - nb;
+	}
+	return a.localeCompare(b, "de");
+}
+
+export async function listBranches() {
+	const entries = await fs.readdir(fullPath(), { withFileTypes: true });
+
+	return entries
+		.filter(
+			(e) =>
+				e.isDirectory() &&
+				e.name !== "@Recently-Snapshot" &&
+				/^NL\d+$/i.test(e.name)
+		)
+		.map((e) => e.name)
+		.sort((a, b) =>
+			sortNumericStrings(a.replace("NL", ""), b.replace("NL", ""))
+		);
+}
+
+export async function listYears(branch) {
+	const dir = fullPath(branch);
+	const entries = await fs.readdir(dir, { withFileTypes: true });
+
+	return entries
+		.filter((e) => e.isDirectory() && /^\d{4}$/.test(e.name))
+		.map((e) => e.name)
+		.sort(sortNumericStrings);
+}
+
+export async function listMonths(branch, year) {
+	const dir = fullPath(branch, year);
+	const entries = await fs.readdir(dir, { withFileTypes: true });
+
+	return entries
+		.filter((e) => e.isDirectory() && /^\d{1,2}$/.test(e.name))
+		.map((e) => e.name.padStart(2, "0"))
+		.sort(sortNumericStrings);
+}
+
+export async function listDays(branch, year, month) {
+	const dir = fullPath(branch, month.length === 1 ? year : year, month);
+	const entries = await fs.readdir(dir, { withFileTypes: true });
+
+	return entries
+		.filter((e) => e.isDirectory() && /^\d{1,2}$/.test(e.name))
+		.map((e) => e.name.padStart(2, "0"))
+		.sort(sortNumericStrings);
+}
+
+export async function listFiles(branch, year, month, day) {
+	const dir = fullPath(branch, year, month, day);
+	const entries = await fs.readdir(dir, { withFileTypes: true });
+
+	return entries
+		.filter((name) => name.toLowerCase().endsWith(".pdf"))
+		.sort((a, b) => a.localeCompare(b, "de"))
+		.map((name) => ({
+			name,
+			// relativer Pfad, den du später für Downloads nutzen kannst
+			relativePath: `${branch}/${year}/${month}/${day}/${name}`,
+		}));
+}