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:
@@ -9,6 +9,7 @@ import { type Client, type Pool, transaktion } from "./db.js";
|
||||
import { mitNeuplanung, pruefeVerknuepfung, terminplan } from "./terminplan.js";
|
||||
import { uebersicht } from "./uebersicht.js";
|
||||
import { aufgabenKalender, baustellenKalender } from "./kalender.js";
|
||||
import { type Nextcloud, ordnerAbgleichen } from "./nextcloud.js";
|
||||
import { TABELLEN } from "./tabellen.js";
|
||||
import { EinstellungenSchema, type Einstellungen } from "./einstellungen.js";
|
||||
import { liquiditaet } from "../domain/liquiditaet.js";
|
||||
@@ -19,6 +20,8 @@ import { VERSION } from "./version.js";
|
||||
|
||||
export interface Optionen {
|
||||
pool: Pool;
|
||||
/** P10: Projektordner in der Nextcloud; ohne Zugang wird nichts angelegt */
|
||||
nextcloud?: Nextcloud;
|
||||
webOrdner?: string;
|
||||
logger?: boolean;
|
||||
}
|
||||
@@ -56,6 +59,28 @@ export async function baueApp(o: Optionen): Promise<FastifyInstance> {
|
||||
return { ok: true, version: VERSION };
|
||||
});
|
||||
|
||||
// P10: Ordner nach dem Speichern abgleichen. Ein Fehler in der Nextcloud macht die Änderung nicht
|
||||
// rückgängig; der Hinweis kommt mit zurück, und der Abgleich lässt sich wiederholen.
|
||||
async function mitOrdner(p: Record<string, any>, wer: string): Promise<Record<string, unknown>> {
|
||||
if (!o.nextcloud) return p;
|
||||
try {
|
||||
const pfad = await ordnerAbgleichen(o.nextcloud, p as { nummer: string; bezeichnung: string; status: string }, p.nextcloud_link ?? null);
|
||||
if (pfad === p.nextcloud_link) return p;
|
||||
return await transaktion(pool, wer, async (c) => (await c.query("update projekt set nextcloud_link = $2 where id = $1 returning *", [p.id, pfad])).rows[0]);
|
||||
} catch (err) {
|
||||
app.log.warn(err);
|
||||
return { ...p, ordner_fehler: `Projektordner in der Nextcloud: ${(err as Error).message}` };
|
||||
}
|
||||
}
|
||||
|
||||
app.post("/api/projekte/:id/ordner", async (req, reply) => {
|
||||
const { id } = IdParam.parse(req.params);
|
||||
const p = (await pool.query("select * from projekt where id = $1", [id])).rows[0];
|
||||
if (!p) return reply.status(404).send({ fehler: "Nicht gefunden" });
|
||||
if (!o.nextcloud) return reply.status(409).send({ fehler: "Für die Projektordner ist kein Nextcloud-Zugang eingerichtet." });
|
||||
return mitOrdner(p, benutzer(req));
|
||||
});
|
||||
|
||||
// Änderungen an Vorgängen und Verknüpfungen planen die Nachfolger in derselben Transaktion neu
|
||||
const PLAN = new Set(["vorgang", "vorgang_vorgaenger"]);
|
||||
async function geplant<T>(c: Client, tabelle: string, fn: () => Promise<T>, bewegt: string[] = []): Promise<T> {
|
||||
@@ -89,6 +114,7 @@ export async function baueApp(o: Optionen): Promise<FastifyInstance> {
|
||||
spalten.map((s) => daten[s]),
|
||||
)).rows[0]);
|
||||
});
|
||||
if (t.name === "projekt") return reply.status(201).send(await mitOrdner(row, benutzer(req)));
|
||||
return reply.status(201).send(row);
|
||||
});
|
||||
|
||||
@@ -108,6 +134,7 @@ export async function baueApp(o: Optionen): Promise<FastifyInstance> {
|
||||
[id, ...spalten.map((s) => daten[s])],
|
||||
)).rows[0], t.name === "vorgang" && daten.start !== undefined ? [id] : []);
|
||||
});
|
||||
if (row && t.name === "projekt" && (daten.nummer || daten.bezeichnung || daten.status)) return mitOrdner(row, benutzer(req));
|
||||
return row ?? reply.status(404).send({ fehler: "Nicht gefunden" });
|
||||
});
|
||||
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
import { erstellePool } from "./db.js";
|
||||
import { migriere } from "./migrate.js";
|
||||
import { baueApp } from "./app.js";
|
||||
import { Nextcloud } from "./nextcloud.js";
|
||||
|
||||
const url = process.env.DATABASE_URL;
|
||||
if (!url) {
|
||||
@@ -10,7 +11,10 @@ if (!url) {
|
||||
|
||||
const pool = erstellePool(url);
|
||||
await migriere(pool, (t) => console.log(t));
|
||||
const app = await baueApp({ pool, logger: true });
|
||||
// P10: Ordner der Projekte; gleicher Zugang wie für die Sicherung, eigene Adresse
|
||||
const { NEXTCLOUD_PROJEKTE_URL: ncUrl, NEXTCLOUD_BENUTZER: ncBenutzer, NEXTCLOUD_PASSWORT: ncPasswort } = process.env;
|
||||
const nextcloud = ncUrl && ncBenutzer && ncPasswort ? new Nextcloud({ url: ncUrl, benutzer: ncBenutzer, passwort: ncPasswort }) : undefined;
|
||||
const app = await baueApp({ pool, logger: true, nextcloud });
|
||||
const port = Number(process.env.PORT ?? 3000);
|
||||
await app.listen({ host: "0.0.0.0", port });
|
||||
|
||||
|
||||
103
src/server/nextcloud.ts
Normal file
103
src/server/nextcloud.ts
Normal file
@@ -0,0 +1,103 @@
|
||||
// P10: Projektordner in Hennings Nextcloud anlegen und je nach Status verschieben (WebDAV, App-Passwort).
|
||||
// Statusordner und Unterordner wie in seiner Ablage (Henning, 18./19.10.2026).
|
||||
|
||||
export interface NextcloudZugang {
|
||||
/** WebDAV-Adresse des Ordners, in dem „01 Angebot“ … „04 Archiv“ liegen */
|
||||
url: string;
|
||||
benutzer: string;
|
||||
passwort: string;
|
||||
}
|
||||
|
||||
export const STATUSORDNER: Record<string, string> = {
|
||||
anfrage: "01 Angebot",
|
||||
angebot: "01 Angebot",
|
||||
auftrag: "02 laufende Projekte",
|
||||
ausfuehrung: "02 laufende Projekte",
|
||||
abgerechnet: "02 laufende Projekte",
|
||||
gewaehrleistung: "03 Gewährl.-Projekte",
|
||||
abgeschlossen: "04 Archiv",
|
||||
};
|
||||
|
||||
export const UNTERORDNER = ["01 Auftrag", "01 Auftrag/03 Stundenlohnarb.", "02 Umsetzung", "03 Bilder"];
|
||||
|
||||
/** „P-26-020 Terrassendachsanierung“; Schrägstriche und Steuerzeichen sind in Ordnernamen nicht erlaubt. */
|
||||
export function ordnerName(nummer: string, bezeichnung: string): string {
|
||||
return `${nummer} ${bezeichnung}`.replace(/[\\/:*?"<>|\u0000-\u001f]/g, "-").replace(/\s+/g, " ").trim();
|
||||
}
|
||||
|
||||
export function zielPfad(p: { nummer: string; bezeichnung: string; status: string }): string {
|
||||
return `${STATUSORDNER[p.status] ?? STATUSORDNER.anfrage}/${ordnerName(p.nummer, p.bezeichnung)}`;
|
||||
}
|
||||
|
||||
const kodiert = (pfad: string) => pfad.split("/").map(encodeURIComponent).join("/");
|
||||
|
||||
export class Nextcloud {
|
||||
constructor(private z: NextcloudZugang, private abruf: typeof fetch = fetch) {}
|
||||
|
||||
private adresse(pfad: string): string {
|
||||
return `${this.z.url.replace(/\/+$/, "")}/${kodiert(pfad)}`;
|
||||
}
|
||||
|
||||
private async anfrage(methode: string, pfad: string, kopf: Record<string, string> = {}, body?: string): Promise<Response> {
|
||||
return this.abruf(this.adresse(pfad), {
|
||||
method: methode,
|
||||
headers: { authorization: `Basic ${btoa(`${this.z.benutzer}:${this.z.passwort}`)}`, ...kopf },
|
||||
body,
|
||||
});
|
||||
}
|
||||
|
||||
async gibtEs(pfad: string): Promise<boolean> {
|
||||
const r = await this.anfrage("PROPFIND", pfad, { depth: "0" });
|
||||
if (r.status === 404) return false;
|
||||
if (r.ok || r.status === 207) return true;
|
||||
throw new Error(`Nextcloud antwortet mit ${r.status} für ${pfad}`);
|
||||
}
|
||||
|
||||
/** Namen der Unterordner (eine Ebene) */
|
||||
async liste(pfad: string): Promise<string[]> {
|
||||
const r = await this.anfrage("PROPFIND", pfad, { depth: "1" });
|
||||
if (r.status === 404) return [];
|
||||
if (r.status !== 207) throw new Error(`Nextcloud antwortet mit ${r.status} für ${pfad}`);
|
||||
const xml = await r.text();
|
||||
const basis = new URL(this.adresse(pfad)).pathname.replace(/\/+$/, "");
|
||||
return [...xml.matchAll(/<d:href>([^<]+)<\/d:href>/gi)]
|
||||
.map((m) => decodeURIComponent(m[1]!).replace(/\/+$/, ""))
|
||||
.filter((h) => h !== decodeURIComponent(basis) && h.startsWith(decodeURIComponent(basis)))
|
||||
.map((h) => h.slice(decodeURIComponent(basis).length + 1));
|
||||
}
|
||||
|
||||
async ordner(pfad: string): Promise<void> {
|
||||
const r = await this.anfrage("MKCOL", pfad);
|
||||
if (!(r.status === 201 || r.status === 405)) throw new Error(`Ordner ${pfad} ließ sich nicht anlegen (${r.status})`);
|
||||
}
|
||||
|
||||
async verschiebe(von: string, nach: string): Promise<void> {
|
||||
const r = await this.anfrage("MOVE", von, { destination: this.adresse(nach), overwrite: "F" });
|
||||
if (!(r.status === 201 || r.status === 204)) throw new Error(`${von} ließ sich nicht nach ${nach} verschieben (${r.status})`);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Bringt den Ordner eines Projekts an die richtige Stelle. `bisher` ist der zuletzt bekannte Pfad
|
||||
* (projekt.nextcloud_link). Ohne bekannten Pfad wird ein vorhandener Ordner mit derselben Nummer in
|
||||
* den Statusordnern gesucht und übernommen, sonst ein neuer mit Unterordnern angelegt.
|
||||
* Gibt den neuen Pfad zurück.
|
||||
*/
|
||||
export async function ordnerAbgleichen(nc: Nextcloud, p: { nummer: string; bezeichnung: string; status: string }, bisher: string | null): Promise<string> {
|
||||
const ziel = zielPfad(p);
|
||||
if (bisher === ziel && (await nc.gibtEs(ziel))) return ziel;
|
||||
let quelle = bisher && (await nc.gibtEs(bisher)) ? bisher : null;
|
||||
if (!quelle) {
|
||||
for (const s of new Set(Object.values(STATUSORDNER))) {
|
||||
const treffer = (await nc.liste(s)).find((n) => n === p.nummer || n.startsWith(`${p.nummer} `));
|
||||
if (treffer) { quelle = `${s}/${treffer}`; break; }
|
||||
}
|
||||
}
|
||||
await nc.ordner(ziel.split("/")[0]!);
|
||||
if (quelle && quelle !== ziel) await nc.verschiebe(quelle, ziel);
|
||||
if (!quelle) {
|
||||
await nc.ordner(ziel);
|
||||
for (const u of UNTERORDNER) await nc.ordner(`${ziel}/${u}`);
|
||||
}
|
||||
return ziel;
|
||||
}
|
||||
@@ -118,12 +118,17 @@ function bestaetigen(frage: string): Promise<boolean> {
|
||||
async function bearbeiten(pfad: string, titel: string, felder: Feld[], zeile: Zeile | null, vorgaben: Zeile = {}): Promise<boolean> {
|
||||
const e = await dialog(titel, felder, zeile ?? vorgaben, !!zeile);
|
||||
if (!e) return false;
|
||||
let antwort: Zeile | undefined;
|
||||
if (e.aktion === "loeschen") await api(`${pfad}/${zeile!.id}`, "DELETE");
|
||||
else if (zeile) await api(`${pfad}/${zeile.id}`, "PATCH", e.daten);
|
||||
else await api(pfad, "POST", { ...vorgaben, ...e.daten });
|
||||
else if (zeile) antwort = await api(`${pfad}/${zeile.id}`, "PATCH", e.daten);
|
||||
else antwort = await api(pfad, "POST", { ...vorgaben, ...e.daten });
|
||||
// gespeichert, aber z. B. der Projektordner in der Nextcloud hat nicht geklappt
|
||||
if (antwort?.ordner_fehler) hinweisNachLaden = antwort.ordner_fehler;
|
||||
return true;
|
||||
}
|
||||
|
||||
let hinweisNachLaden = "";
|
||||
|
||||
function tabelle(spalten: { titel: string; wert: (z: Zeile) => string; klasse?: string }[], zeilen: Zeile[], klick?: (z: Zeile) => void): HTMLTableElement {
|
||||
const t = document.createElement("table");
|
||||
t.innerHTML = `<thead><tr>${spalten.map((s) => `<th class="${s.klasse ?? ""}">${esc(s.titel)}</th>`).join("")}</tr></thead>
|
||||
@@ -137,7 +142,8 @@ function tabelle(spalten: { titel: string; wert: (z: Zeile) => string; klasse?:
|
||||
|
||||
// Kopf wie bei Plancraft: Titel links, Hauptknopf rechts; darunter eine Leiste für Filter
|
||||
function seite(titel: string, knopf?: { text: string; aktion: () => void }): HTMLElement {
|
||||
inhalt.innerHTML = `<div class="kopf"><h1>${esc(titel)}</h1></div>`;
|
||||
inhalt.innerHTML = `<div class="kopf"><h1>${esc(titel)}</h1></div>${hinweisNachLaden ? `<div class="warnung">${esc(hinweisNachLaden)}</div>` : ""}`;
|
||||
hinweisNachLaden = "";
|
||||
if (knopf) {
|
||||
const b = document.createElement("button");
|
||||
b.className = "haupt";
|
||||
|
||||
Reference in New Issue
Block a user