|
|
|
|
@@ -10,7 +10,7 @@ import { mitNeuplanung, pruefeVerknuepfung, terminplan } from "./terminplan.js";
|
|
|
|
|
import { legeZahlungsplanAn, restNachziehen, zahlungsplanFest, zahlungsplaeneAutomatisch } from "./zahlungsplan.js";
|
|
|
|
|
import { uebersicht } from "./uebersicht.js";
|
|
|
|
|
import { aufgabenKalender, baustellenKalender } from "./kalender.js";
|
|
|
|
|
import { type Nextcloud, ordnerAbgleichen } from "./nextcloud.js";
|
|
|
|
|
import { Nextcloud, STATUSORDNER, ordnerAbgleichen, webdavBasis } from "./nextcloud.js";
|
|
|
|
|
import { NUR_SICHERUNG, TABELLEN } from "./tabellen.js";
|
|
|
|
|
import { EinstellungenSchema, type Einstellungen } from "./einstellungen.js";
|
|
|
|
|
import { liquiditaet } from "../domain/liquiditaet.js";
|
|
|
|
|
@@ -36,8 +36,12 @@ import { formatDatum, plusTage } from "../domain/datum.js";
|
|
|
|
|
|
|
|
|
|
export interface Optionen {
|
|
|
|
|
pool: Pool;
|
|
|
|
|
/** P10: Projektordner in der Nextcloud; ohne Zugang wird nichts angelegt */
|
|
|
|
|
/** P10: Projektordner in der Nextcloud; ohne Zugang wird nichts angelegt. Fest vorgegeben (.env, Tests) geht vor der Maske unter Daten */
|
|
|
|
|
nextcloud?: Nextcloud;
|
|
|
|
|
/** Zugang über die Maske unter Daten; in der Browser-Testversion aus, weil sie die Nextcloud nicht erreicht */
|
|
|
|
|
nextcloudEinrichtbar?: boolean;
|
|
|
|
|
/** nur für Tests: WebDAV-Anfragen abfangen */
|
|
|
|
|
nextcloudAbruf?: typeof fetch;
|
|
|
|
|
webOrdner?: string;
|
|
|
|
|
logger?: boolean;
|
|
|
|
|
/** passwort: Server; testversion: Browser-Testversion und Tests, ohne Passwörter */
|
|
|
|
|
@@ -85,10 +89,22 @@ export async function baueApp(o: Optionen): Promise<FastifyInstance> {
|
|
|
|
|
|
|
|
|
|
// 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.
|
|
|
|
|
const einrichtbar = o.nextcloudEinrichtbar !== false;
|
|
|
|
|
/** Der Zugang gilt sofort nach dem Speichern unter Daten, ohne Neustart */
|
|
|
|
|
async function nextcloud(): Promise<Nextcloud | undefined> {
|
|
|
|
|
if (o.nextcloud) return o.nextcloud;
|
|
|
|
|
if (!einrichtbar) return undefined;
|
|
|
|
|
const z = (await ladeEinstellungen()).nextcloud;
|
|
|
|
|
if (!z.adresse || !z.benutzer) return undefined;
|
|
|
|
|
const passwort = (await pool.query<{ passwort: string }>("select passwort from nextcloud_zugang")).rows[0]?.passwort;
|
|
|
|
|
return passwort ? new Nextcloud({ url: webdavBasis(z), benutzer: z.benutzer, passwort }, o.nextcloudAbruf) : undefined;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async function mitOrdner(p: Record<string, any>, wer: string): Promise<Record<string, unknown>> {
|
|
|
|
|
if (!o.nextcloud) return p;
|
|
|
|
|
const nc = await nextcloud();
|
|
|
|
|
if (!nc) return p;
|
|
|
|
|
try {
|
|
|
|
|
const pfad = await ordnerAbgleichen(o.nextcloud, p as { nummer: string; bezeichnung: string; status: string }, p.nextcloud_link ?? null);
|
|
|
|
|
const pfad = await ordnerAbgleichen(nc, 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) {
|
|
|
|
|
@@ -106,10 +122,72 @@ export async function baueApp(o: Optionen): Promise<FastifyInstance> {
|
|
|
|
|
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." });
|
|
|
|
|
if (!(await nextcloud())) return reply.status(409).send({ fehler: "Für die Projektordner ist kein Nextcloud-Zugang eingerichtet." });
|
|
|
|
|
return mitOrdner(p, benutzer(req));
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
// Maske unter Daten (Henning, 23.10.2026): Adresse, Benutzer, Ordner und App-Passwort eintragen und prüfen.
|
|
|
|
|
// Das Passwort geht nie zurück an die Oberfläche, nur ob eines gespeichert ist.
|
|
|
|
|
app.get("/api/nextcloud", async () => {
|
|
|
|
|
const z = (await ladeEinstellungen()).nextcloud;
|
|
|
|
|
const pw = (await pool.query("select geaendert_am from nextcloud_zugang")).rows[0];
|
|
|
|
|
return { ...z, passwortGesetzt: !!pw, passwortGeaendert: pw?.geaendert_am ?? null, fest: !!o.nextcloud, einrichtbar,
|
|
|
|
|
webdav: z.adresse && z.benutzer ? webdavBasis(z) : null };
|
|
|
|
|
});
|
|
|
|
|
const ZugangEingabe = z.object({
|
|
|
|
|
adresse: z.string().trim().max(300).refine((a) => !a || /^https:\/\/[^\s/]+/i.test(a), "Die Adresse muss mit https:// beginnen."),
|
|
|
|
|
benutzer: z.string().trim().max(100),
|
|
|
|
|
ordner: z.string().trim().max(300).refine((a) => !/^[a-z]:\\/i.test(a), "Bitte den Ordner in der Nextcloud angeben, nicht den Pfad auf dem PC (z. B. „01 Projekte“)."),
|
|
|
|
|
// leer = bisheriges Passwort behalten
|
|
|
|
|
passwort: z.string().max(200).optional(),
|
|
|
|
|
passwortLoeschen: z.boolean().optional(),
|
|
|
|
|
});
|
|
|
|
|
app.put("/api/nextcloud", async (req, reply) => {
|
|
|
|
|
const d = ZugangEingabe.parse(req.body);
|
|
|
|
|
if ((d.passwort || d.passwortLoeschen) && !einrichtbar) {
|
|
|
|
|
return reply.status(409).send({ fehler: "Das App-Passwort trägst du erst auf dem Server ein. Adresse, Benutzer und Ordner sind gespeichert und ziehen mit der Sicherung um." });
|
|
|
|
|
}
|
|
|
|
|
const e = await ladeEinstellungen();
|
|
|
|
|
const neu = EinstellungenSchema.parse({ ...e, nextcloud: { adresse: d.adresse.replace(/\/+$/, ""), benutzer: d.benutzer, ordner: d.ordner.replace(/\\/g, "/").replace(/^\/+|\/+$/g, "") } });
|
|
|
|
|
await transaktion(pool, benutzer(req), async (c) => {
|
|
|
|
|
await c.query("update einstellungen set daten = $1", [neu]);
|
|
|
|
|
if (d.passwortLoeschen) await c.query("delete from nextcloud_zugang");
|
|
|
|
|
else if (d.passwort) {
|
|
|
|
|
await c.query(`insert into nextcloud_zugang (id, passwort, geaendert_von) values (true, $1, $2)
|
|
|
|
|
on conflict (id) do update set passwort = excluded.passwort, geaendert_am = now(), geaendert_von = excluded.geaendert_von`, [d.passwort, benutzer(req)]);
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
return reply.send({ ok: true });
|
|
|
|
|
});
|
|
|
|
|
// Verbindung prüfen: erreicht das Programm den Ordner, und welche Statusordner gibt es schon?
|
|
|
|
|
app.post("/api/nextcloud/pruefen", async (_req, reply) => {
|
|
|
|
|
const nc = await nextcloud();
|
|
|
|
|
if (!nc) return reply.status(409).send({ fehler: einrichtbar || o.nextcloud ? "Bitte Adresse, Benutzer und App-Passwort eintragen." : "Die Verbindung zur Nextcloud geht erst auf dem Server." });
|
|
|
|
|
try {
|
|
|
|
|
if (!(await nc.gibtEs(""))) return { ok: false, fehler: "Den Ordner gibt es unter dieser Adresse nicht. Bitte Adresse, Benutzer und Ordner prüfen." };
|
|
|
|
|
const da = await nc.liste("");
|
|
|
|
|
const statusordner = [...new Set(Object.values(STATUSORDNER))];
|
|
|
|
|
return { ok: true, vorhanden: statusordner.filter((s) => da.includes(s)), fehlen: statusordner.filter((s) => !da.includes(s)), anzahl: da.length };
|
|
|
|
|
} catch (err) {
|
|
|
|
|
const text = (err as Error).message;
|
|
|
|
|
if (/ 401 /.test(text)) return { ok: false, fehler: "Die Nextcloud lehnt die Anmeldung ab: Benutzer oder App-Passwort stimmt nicht." };
|
|
|
|
|
return { ok: false, fehler: `Keine Verbindung: ${text}` };
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
// Alle Projekte einmal abgleichen: vorhandene Ordner mit gleicher Nummer übernehmen, fehlende anlegen, falsche verschieben
|
|
|
|
|
app.post("/api/nextcloud/abgleichen", async (req, reply) => {
|
|
|
|
|
if (!(await nextcloud())) return reply.status(409).send({ fehler: "Für die Projektordner ist kein Nextcloud-Zugang eingerichtet." });
|
|
|
|
|
const projekte = (await pool.query("select * from projekt order by nummer")).rows;
|
|
|
|
|
let ok = 0;
|
|
|
|
|
const fehler: string[] = [];
|
|
|
|
|
for (const p of projekte) {
|
|
|
|
|
const r = await mitOrdner(p, benutzer(req));
|
|
|
|
|
if (r.ordner_fehler) fehler.push(`${p.nummer}: ${String(r.ordner_fehler).replace(/^Projektordner in der Nextcloud: /, "")}`);
|
|
|
|
|
else ok++;
|
|
|
|
|
}
|
|
|
|
|
return { ok, fehler };
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
// Ablage auf der Projektseite (Henning, 20.10.2026): Inhalt des Nextcloud-Projektordners anzeigen und Dateien öffnen
|
|
|
|
|
const AblageAnfrage = z.object({ pfad: z.string().max(500).default("") })
|
|
|
|
|
.refine((q) => !q.pfad.split("/").some((t) => t === ".." || t === "."), { message: "Pfad ungültig" });
|
|
|
|
|
@@ -124,11 +202,12 @@ export async function baueApp(o: Optionen): Promise<FastifyInstance> {
|
|
|
|
|
const { id } = IdParam.parse(req.params);
|
|
|
|
|
const { pfad } = AblageAnfrage.parse(req.query);
|
|
|
|
|
if (!(await ablageErlaubt(req, id))) return reply.status(403).send({ fehler: "Dafür fehlt dir die Berechtigung." });
|
|
|
|
|
if (!o.nextcloud) return reply.status(409).send({ fehler: "Die Ablage zeigt den Projektordner in der Nextcloud. Das geht, sobald das Programm auf dem Server läuft." });
|
|
|
|
|
const nc = await nextcloud();
|
|
|
|
|
if (!nc) return reply.status(409).send({ fehler: einrichtbar ? "Die Ablage zeigt den Projektordner in der Nextcloud. Dafür unter Daten den Nextcloud-Zugang eintragen." : "Die Ablage zeigt den Projektordner in der Nextcloud. Das geht, sobald das Programm auf dem Server läuft." });
|
|
|
|
|
const p = (await pool.query("select nextcloud_link from projekt where id = $1", [id])).rows[0];
|
|
|
|
|
if (!p) return reply.status(404).send({ fehler: "Nicht gefunden" });
|
|
|
|
|
if (!p.nextcloud_link) return reply.status(409).send({ fehler: "Für dieses Projekt gibt es noch keinen Ordner in der Nextcloud." });
|
|
|
|
|
const eintraege = await o.nextcloud.inhalt((await ablagePfad(id, pfad))!);
|
|
|
|
|
const eintraege = await nc.inhalt((await ablagePfad(id, pfad))!);
|
|
|
|
|
if (!eintraege) return reply.status(404).send({ fehler: "Ordner nicht gefunden" });
|
|
|
|
|
return { ordner: p.nextcloud_link, pfad, eintraege };
|
|
|
|
|
});
|
|
|
|
|
@@ -136,10 +215,11 @@ export async function baueApp(o: Optionen): Promise<FastifyInstance> {
|
|
|
|
|
const { id } = IdParam.parse(req.params);
|
|
|
|
|
const { pfad } = AblageAnfrage.parse(req.query);
|
|
|
|
|
if (!(await ablageErlaubt(req, id))) return reply.status(403).send({ fehler: "Dafür fehlt dir die Berechtigung." });
|
|
|
|
|
if (!o.nextcloud || !pfad) return reply.status(404).send({ fehler: "Nicht gefunden" });
|
|
|
|
|
const nc = await nextcloud();
|
|
|
|
|
if (!nc || !pfad) return reply.status(404).send({ fehler: "Nicht gefunden" });
|
|
|
|
|
const voll = await ablagePfad(id, pfad);
|
|
|
|
|
if (!voll) return reply.status(404).send({ fehler: "Nicht gefunden" });
|
|
|
|
|
const r = await o.nextcloud.datei(voll);
|
|
|
|
|
const r = await nc.datei(voll);
|
|
|
|
|
const name = pfad.split("/").at(-1)!;
|
|
|
|
|
reply.header("content-type", r.headers.get("content-type") ?? "application/octet-stream");
|
|
|
|
|
reply.header("content-disposition", `inline; filename*=UTF-8''${encodeURIComponent(name)}`);
|
|
|
|
|
|