Projektordner in der Nextcloud anlegen und je nach Status verschieben (P10)

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01AmyPfuNc4sCRfaF3oURXZz
This commit is contained in:
Claude
2026-10-19 19:07:03 +00:00
parent 6615acf936
commit a3bb310cea
9 changed files with 256 additions and 4 deletions

View File

@@ -1,5 +1,6 @@
// Prüft Server und Datenbank zusammen. Läuft nur mit TEST_DATABASE_URL (lokal und in der CI).
import { describe, it, expect, beforeAll, afterAll } from "vitest";
import { falscheNextcloud } from "./falsche-nextcloud.js";
import type { FastifyInstance } from "fastify";
import { erstellePool, type Pool } from "../src/server/db.js";
import { migriere } from "../src/server/migrate.js";
@@ -231,3 +232,38 @@ describe.skipIf(!url)("Übernahme aus V70 über die API", () => {
expect((await app.inject(`/kalender/${kalenderToken.slice(0, -1)}${kalenderToken.endsWith("x") ? "y" : "x"}/baustellen.ics`)).statusCode).toBe(404);
});
});
describe.skipIf(!url)("Projektordner in der Nextcloud über die API", () => {
let pool: Pool;
let app: FastifyInstance;
const f = falscheNextcloud();
beforeAll(async () => {
pool = erstellePool(url!);
await pool.query("drop schema public cascade; create schema public;");
await migriere(pool);
app = await baueApp({ pool, webOrdner: "/gibt-es-nicht", nextcloud: f.nc });
});
afterAll(async () => {
await app?.close();
await pool?.end();
});
it("neues Projekt bekommt einen Ordner, Statuswechsel verschiebt ihn", async () => {
const p = (await app.inject({ method: "POST", url: "/api/projekte", payload: { nummer: "A-26-130", bezeichnung: "Carport Most", status: "angebot" } })).json();
expect(p.nextcloud_link).toBe("01 Angebot/A-26-130 Carport Most");
const q = (await app.inject({ method: "PATCH", url: `/api/projekte/${p.id}`, payload: { nummer: "P-26-021", status: "auftrag" } })).json();
expect(q.nextcloud_link).toBe("02 laufende Projekte/P-26-021 Carport Most");
expect(f.relativ()).toContain("02 laufende Projekte/P-26-021 Carport Most/02 Umsetzung");
});
it("Fehler der Nextcloud verhindert das Speichern nicht", async () => {
f.ordner.clear(); // Wurzel fehlt: jede Anlage schlägt fehl
const r = await app.inject({ method: "POST", url: "/api/projekte", payload: { nummer: "A-26-131", bezeichnung: "Gaube", status: "angebot" } });
expect(r.statusCode).toBe(201);
expect(r.json().ordner_fehler).toMatch(/Nextcloud/);
f.ordner.add(f.wurzel);
const nochmal = (await app.inject({ method: "POST", url: `/api/projekte/${r.json().id}/ordner` })).json();
expect(nochmal.nextcloud_link).toBe("01 Angebot/A-26-131 Gaube");
});
});

36
test/falsche-nextcloud.ts Normal file
View File

@@ -0,0 +1,36 @@
import { Nextcloud } from "../src/server/nextcloud.js";
/** Kleine WebDAV-Nachbildung im Speicher: nur Ordner, wie Nextcloud sie beantwortet */
export function falscheNextcloud(basis = "https://cloud.test/remote.php/dav/files/henning/Projekte") {
const ordner = new Set<string>();
const pfad = (u: string) => decodeURIComponent(new URL(u).pathname).replace(/\/+$/, "");
const wurzel = pfad(basis);
ordner.add(wurzel);
const abruf = (async (u: string | URL, init?: RequestInit) => {
const p = pfad(String(u));
const kopf = (init?.headers ?? {}) as Record<string, string>;
const eltern = p.slice(0, p.lastIndexOf("/"));
switch (init?.method) {
case "PROPFIND": {
if (!ordner.has(p)) return new Response(null, { status: 404 });
const kinder = kopf.depth === "1" ? [...ordner].filter((o) => o.startsWith(p + "/") && !o.slice(p.length + 1).includes("/")) : [];
const hrefs = [p, ...kinder].map((o) => `<d:response><d:href>${o.split("/").map(encodeURIComponent).join("/")}/</d:href></d:response>`).join("");
return new Response(`<?xml version="1.0"?><d:multistatus xmlns:d="DAV:">${hrefs}</d:multistatus>`, { status: 207 });
}
case "MKCOL":
if (ordner.has(p)) return new Response(null, { status: 405 });
if (!ordner.has(eltern)) return new Response(null, { status: 409 });
ordner.add(p);
return new Response(null, { status: 201 });
case "MOVE": {
const ziel = pfad(kopf.destination!);
if (!ordner.has(p) || ordner.has(ziel) || !ordner.has(ziel.slice(0, ziel.lastIndexOf("/")))) return new Response(null, { status: 409 });
for (const o of [...ordner]) if (o === p || o.startsWith(p + "/")) { ordner.delete(o); ordner.add(ziel + o.slice(p.length)); }
return new Response(null, { status: 201 });
}
}
return new Response(null, { status: 400 });
}) as typeof fetch;
const relativ = () => [...ordner].filter((o) => o !== wurzel).map((o) => o.slice(wurzel.length + 1)).sort();
return { nc: new Nextcloud({ url: basis, benutzer: "henning", passwort: "geheim" }, abruf), ordner, wurzel, relativ };
}

35
test/nextcloud.test.ts Normal file
View File

@@ -0,0 +1,35 @@
import { describe, expect, it } from "vitest";
import { ordnerAbgleichen, ordnerName } from "../src/server/nextcloud.js";
import { falscheNextcloud } from "./falsche-nextcloud.js";
describe("Projektordner in der Nextcloud (P10)", () => {
it("Ordnername ohne verbotene Zeichen", () => {
expect(ordnerName("P-26-020", "Terrassendach / Carport")).toBe("P-26-020 Terrassendach - Carport");
});
it("legt neu an mit Unterordnern und verschiebt nach Status und Umbenennung", async () => {
const f = falscheNextcloud();
const p = { nummer: "A-26-120", bezeichnung: "Terrassendachsanierung", status: "angebot" };
let pfad = await ordnerAbgleichen(f.nc, p, null);
expect(pfad).toBe("01 Angebot/A-26-120 Terrassendachsanierung");
expect(f.relativ()).toContain("01 Angebot/A-26-120 Terrassendachsanierung/01 Auftrag/03 Stundenlohnarb.");
// Angebot angenommen: neue Nummer und Status Auftrag
pfad = await ordnerAbgleichen(f.nc, { ...p, nummer: "P-26-020", status: "auftrag" }, pfad);
expect(pfad).toBe("02 laufende Projekte/P-26-020 Terrassendachsanierung");
expect(f.relativ()).toContain("02 laufende Projekte/P-26-020 Terrassendachsanierung/03 Bilder");
expect(f.relativ().some((o) => o.startsWith("01 Angebot/"))).toBe(false);
pfad = await ordnerAbgleichen(f.nc, { ...p, nummer: "P-26-020", status: "gewaehrleistung" }, pfad);
expect(pfad).toBe("03 Gewährl.-Projekte/P-26-020 Terrassendachsanierung");
});
it("übernimmt einen vorhandenen Ordner mit gleicher Nummer statt einen zweiten anzulegen", async () => {
const f = falscheNextcloud();
f.ordner.add(`${f.wurzel}/02 laufende Projekte`);
f.ordner.add(`${f.wurzel}/02 laufende Projekte/P-26-014 Bohlentausch Stadt`);
f.ordner.add(`${f.wurzel}/02 laufende Projekte/P-26-014 Bohlentausch Stadt/Fotos`);
const pfad = await ordnerAbgleichen(f.nc, { nummer: "P-26-014", bezeichnung: "Bohlentausch", status: "abgeschlossen" }, null);
expect(pfad).toBe("04 Archiv/P-26-014 Bohlentausch");
expect(f.relativ()).toContain("04 Archiv/P-26-014 Bohlentausch/Fotos");
expect(f.relativ().filter((o) => o.includes("P-26-014"))).toHaveLength(2);
});
});