Testversion im Browser und Sicherung einspielen

Die Testversion bündelt Oberfläche und Server-Logik mit PGlite (Postgres als
WebAssembly). POST /api/import/stand spielt einen kompletten Stand mit allen IDs
in ein leeres Programm ein; damit ziehen die Daten später auf den Server um.

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-20 04:18:42 +00:00
parent e406c5a842
commit 9bd88bb7ec
17 changed files with 510 additions and 8 deletions

View File

@@ -16,6 +16,7 @@ import { liquiditaet } from "../domain/liquiditaet.js";
import { liquiditaetsDaten } from "../domain/projektplan.js";
import { uebernimmV70 } from "../domain/v70.js";
import { speichereUebernahme } from "./v70import.js";
import { pruefeStand, speichereStand } from "./stand.js";
import { VERSION } from "./version.js";
export interface Optionen {
@@ -248,6 +249,18 @@ export async function baueApp(o: Optionen): Promise<FastifyInstance> {
return reply.status(201).send({ anzahl, hinweise: u.hinweise });
});
// D1: Sicherung aus GET /api/export in ein leeres Programm einspielen (Umzug aus der Testversion)
app.post("/api/import/stand", async (req, reply) => {
let s;
try {
s = pruefeStand(req.body);
} catch (err) {
return reply.status(400).send({ fehler: (err as Error).message });
}
const anzahl = await transaktion(pool, benutzer(req), (c) => speichereStand(c, s));
return reply.status(201).send({ anzahl });
});
// D1: kompletter Stand als JSON-Datei
app.get("/api/export", async (_req, reply) => {
const daten: Record<string, unknown[]> = {};

42
src/server/stand.ts Normal file
View File

@@ -0,0 +1,42 @@
// D1: einen kompletten Stand (GET /api/export) in ein leeres Programm einspielen, alle IDs bleiben.
// Damit ziehen die Daten aus der Testversion im Browser auf den Server um.
import type { Client } from "./db.js";
import { EinstellungenSchema } from "./einstellungen.js";
import { TABELLEN } from "./tabellen.js";
import { NichtLeer } from "./v70import.js";
export interface Stand {
programm: "betrieb";
einstellungen?: unknown;
daten: Record<string, Record<string, unknown>[]>;
}
export function pruefeStand(x: unknown): Stand {
const s = x as Partial<Stand>;
if (!s || s.programm !== "betrieb" || typeof s.daten !== "object" || s.daten === null) {
throw new Error("Das ist keine Sicherung aus diesem Programm.");
}
return s as Stand;
}
export async function speichereStand(c: Client, s: Stand): Promise<Record<string, number>> {
const belegt = await c.query<{ n: number }>(
`select ${TABELLEN.map((t) => `(select count(*) from ${t.name})`).join(" + ")} as n`,
);
if (Number(belegt.rows[0]!.n) > 0) throw new NichtLeer("Eine Sicherung lässt sich nur in ein leeres Programm einspielen.");
const anzahl: Record<string, number> = {};
// TABELLEN steht in der Reihenfolge der Fremdschlüssel
for (const t of TABELLEN) {
const zeilen = s.daten[t.pfad] ?? [];
for (const z of zeilen) {
await c.query(`insert into ${t.name} select * from json_populate_record(null::${t.name}, $1::json)`, [JSON.stringify(z)]);
}
anzahl[t.pfad] = zeilen.length;
}
if (s.einstellungen !== undefined) {
await c.query("update einstellungen set daten = $1", [EinstellungenSchema.parse(s.einstellungen)]);
}
await c.query("select setval('vorgang_nummer_seq', greatest((select max(nummer) from vorgang), 1000))");
return anzahl;
}