forked from jappel/leistungsbilanz-ts
Add print cover fields and office profile
- Project settings gain siteAddress, workStage, projectLead and projectEditor (project.update-settings schema version 3, migration 0009); versions 2 and 1 stay executable and keep the current cover values. Snapshot schema 6 carries the fields; older versions upgrade with null. - Global office profile (office name/address) next to the document logo: table document_branding_settings, PUT /api/document-branding/office, editable from the print view toolbar. - Cover page reads all fields; empty values show the "[ergänzen]" marker. The Planstände editor column shows the project editor. - Shared logo constants moved to a dependency-free module so the Next.js bundler can import them. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
parent
4779e4a1a7
commit
7692699ee2
35 changed files with 3479 additions and 113 deletions
77
tests/document-branding.repository.test.ts
Normal file
77
tests/document-branding.repository.test.ts
Normal file
|
|
@ -0,0 +1,77 @@
|
|||
import path from "node:path";
|
||||
import assert from "node:assert/strict";
|
||||
import { describe, it } from "node:test";
|
||||
import { migrate } from "drizzle-orm/better-sqlite3/migrator";
|
||||
import { createDatabaseContext } from "../src/db/database-context.js";
|
||||
import { DocumentBrandingRepository } from "../src/db/repositories/document-branding.repository.js";
|
||||
import { officeProfileFields } from "../src/shared/constants/project-cover.js";
|
||||
import { updateOfficeProfileSchema } from "../src/shared/validation/document-branding.schemas.js";
|
||||
|
||||
function createRepository() {
|
||||
const context = createDatabaseContext(":memory:");
|
||||
migrate(context.db, {
|
||||
migrationsFolder: path.resolve("src", "db", "migrations"),
|
||||
});
|
||||
return { context, repository: new DocumentBrandingRepository(context.db) };
|
||||
}
|
||||
|
||||
describe("document branding repository", () => {
|
||||
it("stores, replaces and removes the office profile", () => {
|
||||
const { context, repository } = createRepository();
|
||||
try {
|
||||
assert.deepEqual(repository.getSettings(officeProfileFields), {
|
||||
officeName: null,
|
||||
officeAddress: null,
|
||||
});
|
||||
repository.setSettings(
|
||||
updateOfficeProfileSchema.parse({
|
||||
officeName: " kbp köhler beraten + planen GmbH ",
|
||||
officeAddress: "Straße 1\n12345 Ort",
|
||||
})
|
||||
);
|
||||
assert.deepEqual(repository.getSettings(officeProfileFields), {
|
||||
officeName: "kbp köhler beraten + planen GmbH",
|
||||
officeAddress: "Straße 1\n12345 Ort",
|
||||
});
|
||||
repository.setSettings(
|
||||
updateOfficeProfileSchema.parse({ officeName: "Büro", officeAddress: "" })
|
||||
);
|
||||
assert.deepEqual(repository.getSettings(officeProfileFields), {
|
||||
officeName: "Büro",
|
||||
officeAddress: null,
|
||||
});
|
||||
assert.equal(
|
||||
updateOfficeProfileSchema.safeParse({ officeName: "x".repeat(201), officeAddress: null })
|
||||
.success,
|
||||
false
|
||||
);
|
||||
} finally {
|
||||
context.close();
|
||||
}
|
||||
});
|
||||
|
||||
it("replaces and deletes the logo", () => {
|
||||
const { context, repository } = createRepository();
|
||||
try {
|
||||
repository.upsert({
|
||||
key: "logo",
|
||||
mimeType: "image/png",
|
||||
fileName: "a.png",
|
||||
data: Buffer.from([1, 2, 3]),
|
||||
});
|
||||
repository.upsert({
|
||||
key: "logo",
|
||||
mimeType: "image/svg+xml",
|
||||
fileName: "b.svg",
|
||||
data: Buffer.from("<svg/>"),
|
||||
});
|
||||
const logo = repository.get("logo");
|
||||
assert.equal(logo?.mimeType, "image/svg+xml");
|
||||
assert.equal(logo?.data.toString(), "<svg/>");
|
||||
assert.equal(repository.delete("logo"), true);
|
||||
assert.equal(repository.get("logo"), undefined);
|
||||
} finally {
|
||||
context.close();
|
||||
}
|
||||
});
|
||||
});
|
||||
|
|
@ -1074,6 +1074,10 @@ describe("project command service", () => {
|
|||
"MSR",
|
||||
"SiBe",
|
||||
],
|
||||
siteAddress: null,
|
||||
workStage: null,
|
||||
projectLead: null,
|
||||
projectEditor: null,
|
||||
}),
|
||||
});
|
||||
assert.equal(updated.history.currentRevision, 1);
|
||||
|
|
|
|||
|
|
@ -52,6 +52,10 @@ function getSettings(context: DatabaseContext) {
|
|||
threePhaseVoltageV: projects.threePhaseVoltageV,
|
||||
enabledDistributionBoardSupplyTypes:
|
||||
projects.enabledDistributionBoardSupplyTypes,
|
||||
siteAddress: projects.siteAddress,
|
||||
workStage: projects.workStage,
|
||||
projectLead: projects.projectLead,
|
||||
projectEditor: projects.projectEditor,
|
||||
currentRevision: projects.currentRevision,
|
||||
})
|
||||
.from(projects)
|
||||
|
|
@ -81,6 +85,10 @@ function settings(
|
|||
"MSR",
|
||||
"SiBe",
|
||||
],
|
||||
siteAddress: null,
|
||||
workStage: null,
|
||||
projectLead: null,
|
||||
projectEditor: null,
|
||||
...overrides,
|
||||
};
|
||||
}
|
||||
|
|
@ -217,6 +225,10 @@ describe("project settings project-command repository", () => {
|
|||
"MSR",
|
||||
"SiBe",
|
||||
],
|
||||
siteAddress: null,
|
||||
workStage: null,
|
||||
projectLead: null,
|
||||
projectEditor: null,
|
||||
},
|
||||
},
|
||||
]);
|
||||
|
|
@ -289,6 +301,10 @@ describe("project settings project-command repository", () => {
|
|||
"MSR",
|
||||
"SiBe",
|
||||
],
|
||||
siteAddress: null,
|
||||
workStage: null,
|
||||
projectLead: null,
|
||||
projectEditor: null,
|
||||
});
|
||||
|
||||
const undoStep = history.getNextCommand("project-1", "undo");
|
||||
|
|
@ -317,6 +333,10 @@ describe("project settings project-command repository", () => {
|
|||
"MSR",
|
||||
"SiBe",
|
||||
],
|
||||
siteAddress: null,
|
||||
workStage: null,
|
||||
projectLead: null,
|
||||
projectEditor: null,
|
||||
currentRevision: 2,
|
||||
});
|
||||
assert.deepEqual(getDerivedVoltages(context), {
|
||||
|
|
@ -356,6 +376,10 @@ describe("project settings project-command repository", () => {
|
|||
"MSR",
|
||||
"SiBe",
|
||||
],
|
||||
siteAddress: null,
|
||||
workStage: null,
|
||||
projectLead: null,
|
||||
projectEditor: null,
|
||||
currentRevision: 3,
|
||||
});
|
||||
assert.deepEqual(getDerivedVoltages(context), {
|
||||
|
|
@ -433,6 +457,117 @@ describe("project settings project-command repository", () => {
|
|||
}
|
||||
});
|
||||
|
||||
it("persists the print cover fields with persisted undo", () => {
|
||||
const context = createTestDatabase();
|
||||
try {
|
||||
const repository = new ProjectSettingsProjectCommandRepository(
|
||||
context.db
|
||||
);
|
||||
const readCover = () =>
|
||||
context.db
|
||||
.select({
|
||||
siteAddress: projects.siteAddress,
|
||||
workStage: projects.workStage,
|
||||
projectLead: projects.projectLead,
|
||||
projectEditor: projects.projectEditor,
|
||||
})
|
||||
.from(projects)
|
||||
.where(eq(projects.id, "project-1"))
|
||||
.get();
|
||||
const forward = repository.executeUpdate({
|
||||
projectId: "project-1",
|
||||
expectedRevision: 0,
|
||||
source: "user",
|
||||
command: createProjectSettingsUpdateProjectCommand(
|
||||
settings({
|
||||
siteAddress: "Musterstraße 1\n12345 Musterstadt",
|
||||
workStage: "LPH 5",
|
||||
projectLead: "P. Leitung",
|
||||
projectEditor: "B. Arbeiter",
|
||||
})
|
||||
),
|
||||
});
|
||||
assert.deepEqual(readCover(), {
|
||||
siteAddress: "Musterstraße 1\n12345 Musterstadt",
|
||||
workStage: "LPH 5",
|
||||
projectLead: "P. Leitung",
|
||||
projectEditor: "B. Arbeiter",
|
||||
});
|
||||
assert.equal(forward.inverse.schemaVersion, 3);
|
||||
|
||||
const undoStep = new ProjectHistoryRepository(
|
||||
context.db
|
||||
).getNextCommand("project-1", "undo");
|
||||
assert.ok(undoStep);
|
||||
repository.executeUpdate({
|
||||
projectId: "project-1",
|
||||
expectedRevision: 1,
|
||||
source: "undo",
|
||||
historyTargetChangeSetId: undoStep.changeSetId,
|
||||
command: forward.inverse,
|
||||
});
|
||||
assert.deepEqual(readCover(), {
|
||||
siteAddress: null,
|
||||
workStage: null,
|
||||
projectLead: null,
|
||||
projectEditor: null,
|
||||
});
|
||||
|
||||
assert.throws(
|
||||
() =>
|
||||
createProjectSettingsUpdateProjectCommand(
|
||||
settings({ workStage: " LPH 5" })
|
||||
),
|
||||
/invalid values/
|
||||
);
|
||||
} finally {
|
||||
context.close();
|
||||
}
|
||||
});
|
||||
|
||||
it("keeps the cover fields when applying a version 2 command", () => {
|
||||
const context = createTestDatabase();
|
||||
try {
|
||||
context.db
|
||||
.update(projects)
|
||||
.set({ workStage: "LPH 3", projectLead: "P. Leitung" })
|
||||
.where(eq(projects.id, "project-1"))
|
||||
.run();
|
||||
const repository = new ProjectSettingsProjectCommandRepository(
|
||||
context.db
|
||||
);
|
||||
const {
|
||||
siteAddress: _siteAddress,
|
||||
workStage: _workStage,
|
||||
projectLead: _projectLead,
|
||||
projectEditor: _projectEditor,
|
||||
...versionTwoPayload
|
||||
} = settings({ name: "Stand aus Version 2" });
|
||||
const forward = repository.executeUpdate({
|
||||
projectId: "project-1",
|
||||
expectedRevision: 0,
|
||||
source: "user",
|
||||
command: {
|
||||
schemaVersion: 2,
|
||||
type: "project.update-settings",
|
||||
payload: versionTwoPayload,
|
||||
},
|
||||
});
|
||||
const project = context.db
|
||||
.select()
|
||||
.from(projects)
|
||||
.where(eq(projects.id, "project-1"))
|
||||
.get();
|
||||
assert.equal(project?.name, "Stand aus Version 2");
|
||||
assert.equal(project?.workStage, "LPH 3");
|
||||
assert.equal(project?.projectLead, "P. Leitung");
|
||||
assert.equal(forward.inverse.schemaVersion, 2);
|
||||
assert.equal(Object.keys(forward.inverse.payload).length, 9);
|
||||
} finally {
|
||||
context.close();
|
||||
}
|
||||
});
|
||||
|
||||
it("persists the project supply-type selection and rejects disabling an in-use type", () => {
|
||||
const context = createTestDatabase();
|
||||
try {
|
||||
|
|
|
|||
|
|
@ -8,6 +8,23 @@ import {
|
|||
} from "../src/domain/models/project-state-snapshot.model.js";
|
||||
import { createNamedProjectSnapshotSchema } from "../src/shared/validation/project-snapshot.schemas.js";
|
||||
|
||||
/** Removes the fields introduced by snapshot schema version 6. */
|
||||
function withoutVersionSixFields<
|
||||
T extends { project: object; distributionBoards: object[] },
|
||||
>(snapshot: T): T {
|
||||
const project = snapshot.project as Record<string, unknown>;
|
||||
for (const field of ["siteAddress", "workStage", "projectLead", "projectEditor"]) {
|
||||
delete project[field];
|
||||
}
|
||||
for (const board of snapshot.distributionBoards) {
|
||||
const record = board as Record<string, unknown>;
|
||||
for (const field of ["boardNumber", "location", "fedFrom", "remark"]) {
|
||||
delete record[field];
|
||||
}
|
||||
}
|
||||
return snapshot;
|
||||
}
|
||||
|
||||
function minimalSnapshot() {
|
||||
return {
|
||||
schemaVersion: projectStateSnapshotSchemaVersion,
|
||||
|
|
@ -29,6 +46,10 @@ function minimalSnapshot() {
|
|||
"MSR",
|
||||
"SiBe",
|
||||
],
|
||||
siteAddress: "Musterstraße 1\n12345 Musterstadt" as string | null,
|
||||
workStage: "LPH 5" as string | null,
|
||||
projectLead: "P. Leitung" as string | null,
|
||||
projectEditor: null as string | null,
|
||||
},
|
||||
distributionBoards: [],
|
||||
externalCsvConfiguration: null,
|
||||
|
|
@ -155,7 +176,7 @@ describe("project state snapshot model", () => {
|
|||
});
|
||||
|
||||
it("upgrades the supported baseline snapshot with a safe building default", () => {
|
||||
const baseline = structuredClone(minimalSnapshot());
|
||||
const baseline = withoutVersionSixFields(structuredClone(minimalSnapshot()));
|
||||
baseline.schemaVersion = 1 as typeof baseline.schemaVersion;
|
||||
delete (baseline.project as Record<string, unknown>).isPublicBuilding;
|
||||
delete (baseline as Record<string, unknown>).externalCsvConfiguration;
|
||||
|
|
@ -169,7 +190,7 @@ describe("project state snapshot model", () => {
|
|||
});
|
||||
|
||||
it("upgrades snapshot version 2 with an empty external CSV configuration", () => {
|
||||
const previous = structuredClone(minimalSnapshot());
|
||||
const previous = withoutVersionSixFields(structuredClone(minimalSnapshot()));
|
||||
previous.schemaVersion = 2 as typeof previous.schemaVersion;
|
||||
delete (previous as Record<string, unknown>).externalCsvConfiguration;
|
||||
delete (previous as Record<string, unknown>).externalModel;
|
||||
|
|
@ -187,7 +208,7 @@ describe("project state snapshot model", () => {
|
|||
});
|
||||
|
||||
it("upgrades snapshot version 3 with an empty external model", () => {
|
||||
const previous = structuredClone(minimalSnapshot());
|
||||
const previous = withoutVersionSixFields(structuredClone(minimalSnapshot()));
|
||||
previous.schemaVersion = 3 as typeof previous.schemaVersion;
|
||||
delete (previous as Record<string, unknown>).externalModel;
|
||||
|
||||
|
|
@ -203,20 +224,22 @@ describe("project state snapshot model", () => {
|
|||
});
|
||||
});
|
||||
|
||||
it("upgrades snapshot version 5 with empty distribution-board info", () => {
|
||||
const previous = protectedSnapshot();
|
||||
it("upgrades snapshot version 5 with empty board info and cover fields", () => {
|
||||
const previous = withoutVersionSixFields(protectedSnapshot());
|
||||
previous.schemaVersion = 5 as typeof previous.schemaVersion;
|
||||
for (const board of previous.distributionBoards) {
|
||||
const record = board as Record<string, unknown>;
|
||||
delete record.boardNumber;
|
||||
delete record.location;
|
||||
delete record.fedFrom;
|
||||
delete record.remark;
|
||||
}
|
||||
|
||||
const upgraded = parseProjectStateSnapshot(previous);
|
||||
|
||||
assert.equal(upgraded.schemaVersion, projectStateSnapshotSchemaVersion);
|
||||
assert.deepEqual(
|
||||
{
|
||||
siteAddress: upgraded.project.siteAddress,
|
||||
workStage: upgraded.project.workStage,
|
||||
projectLead: upgraded.project.projectLead,
|
||||
projectEditor: upgraded.project.projectEditor,
|
||||
},
|
||||
{ siteAddress: null, workStage: null, projectLead: null, projectEditor: null }
|
||||
);
|
||||
assert.deepEqual(
|
||||
{
|
||||
boardNumber: upgraded.distributionBoards[0]?.boardNumber,
|
||||
|
|
@ -228,27 +251,26 @@ describe("project state snapshot model", () => {
|
|||
);
|
||||
});
|
||||
|
||||
it("keeps distribution-board info values and rejects them in older versions", () => {
|
||||
it("keeps board info and cover values and rejects them in older versions", () => {
|
||||
const parsed = parseProjectStateSnapshot(protectedSnapshot());
|
||||
assert.equal(parsed.distributionBoards[0]?.boardNumber, "UV 1.1");
|
||||
assert.equal(parsed.distributionBoards[0]?.location, "Raum 003 Technik");
|
||||
assert.equal(parsed.distributionBoards[0]?.fedFrom, "HV 0.1");
|
||||
assert.equal(parsed.project.siteAddress, "Musterstraße 1\n12345 Musterstadt");
|
||||
assert.equal(parsed.project.workStage, "LPH 5");
|
||||
|
||||
const mislabeled = protectedSnapshot();
|
||||
mislabeled.schemaVersion = 5 as typeof mislabeled.schemaVersion;
|
||||
assert.throws(() => parseProjectStateSnapshot(mislabeled));
|
||||
|
||||
const missingCover = structuredClone(protectedSnapshot());
|
||||
delete (missingCover.project as Record<string, unknown>).workStage;
|
||||
assert.throws(() => parseProjectStateSnapshot(missingCover));
|
||||
});
|
||||
|
||||
it("upgrades snapshot version 4 with the existing quantity as manual share", () => {
|
||||
const previous = protectedSnapshot();
|
||||
const previous = withoutVersionSixFields(protectedSnapshot());
|
||||
previous.schemaVersion = 4 as typeof previous.schemaVersion;
|
||||
for (const board of previous.distributionBoards) {
|
||||
const record = board as Record<string, unknown>;
|
||||
delete record.boardNumber;
|
||||
delete record.location;
|
||||
delete record.fedFrom;
|
||||
delete record.remark;
|
||||
}
|
||||
previous.circuits[0]!.isReserve = false;
|
||||
previous.circuits[0]!.deviceRows = [{
|
||||
id: "row-1",
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue