Add configurable distribution supply types

This commit is contained in:
2026-07-29 09:31:20 +02:00
parent 194bc9c0b1
commit b1a11397b3
43 changed files with 5697 additions and 126 deletions
@@ -13,14 +13,20 @@ import { circuitLists } from "../src/db/schema/circuit-lists.js";
import { circuitSections } from "../src/db/schema/circuit-sections.js";
import { circuits } from "../src/db/schema/circuits.js";
import { distributionBoards } from "../src/db/schema/distribution-boards.js";
import { floors } from "../src/db/schema/floors.js";
import { projectRevisions } from "../src/db/schema/project-revisions.js";
import { projects } from "../src/db/schema/projects.js";
import { ProjectRevisionConflictError } from "../src/domain/errors/project-revision-conflict.error.js";
import {
createDistributionBoardInsertProjectCommand,
createDistributionBoardStructureSnapshot,
type DistributionBoardStructureProjectCommand,
} from "../src/domain/models/distribution-board-structure-project-command.model.js";
import { createDistributionBoard } from "../src/frontend/utils/api.js";
import { createDistributionBoardUpdateProjectCommand } from "../src/domain/models/distribution-board-project-command.model.js";
import {
createDistributionBoard,
updateDistributionBoard,
} from "../src/frontend/utils/api.js";
function createTestDatabase(): DatabaseContext {
const context = createDatabaseContext(":memory:");
@@ -50,9 +56,11 @@ describe("distribution-board structure project command", () => {
it("builds the fixed setup and sends the expected frontend revision", async () => {
const structure = createDistributionBoardStructureSnapshot(
"project-1",
" UV-02 "
" UV-02 ",
{ floorId: null, supplyType: "SV" }
);
assert.equal(structure.distributionBoard.name, "UV-02");
assert.equal(structure.distributionBoard.supplyType, "SV");
assert.equal(
structure.circuitList.name,
"UV-02 Stromkreisliste"
@@ -91,14 +99,27 @@ describe("distribution-board structure project command", () => {
});
};
try {
await createDistributionBoard("project-1", "UV-02", 7);
await createDistributionBoard(
"project-1",
{
name: "UV-02",
floorId: null,
supplyType: "SV",
},
7
);
} finally {
globalThis.fetch = originalFetch;
}
assert.deepEqual(requests, [
{
url: "/api/projects/project-1/distribution-boards",
body: { name: "UV-02", expectedRevision: 7 },
body: {
name: "UV-02",
floorId: null,
supplyType: "SV",
expectedRevision: 7,
},
},
]);
});
@@ -178,6 +199,239 @@ describe("distribution-board structure project command", () => {
}
});
it("allows only supply types enabled in the project", () => {
const context = createTestDatabase();
try {
context.db
.update(projects)
.set({
enabledDistributionBoardSupplyTypes: ["MSR", "SiBe"],
})
.where(eq(projects.id, "project-1"))
.run();
const repository =
new DistributionBoardStructureProjectCommandRepository(
context.db
);
assert.throws(
() =>
repository.execute({
projectId: "project-1",
expectedRevision: 0,
source: "user",
command: createDistributionBoardInsertProjectCommand(
createDistributionBoardStructureSnapshot(
"project-1",
"UV AV",
{ floorId: null, supplyType: "AV" }
)
),
}),
/nicht aktiviert/
);
const structure = createDistributionBoardStructureSnapshot(
"project-1",
"UV SiBe",
{ floorId: null, supplyType: "SiBe" }
);
repository.execute({
projectId: "project-1",
expectedRevision: 0,
source: "user",
command: createDistributionBoardInsertProjectCommand(structure),
});
assert.equal(
context.db.select().from(distributionBoards).get()?.supplyType,
"SiBe"
);
} finally {
context.close();
}
});
it("applies stored version-one setup commands with unassigned new fields", () => {
const context = createTestDatabase();
try {
const repository =
new DistributionBoardStructureProjectCommandRepository(
context.db
);
const current = createDistributionBoardStructureSnapshot(
"project-1",
"UV Alt",
{ floorId: null, supplyType: null }
);
const {
floorId: _floorId,
supplyType: _supplyType,
...legacyBoard
} = current.distributionBoard;
const legacyCommand = {
schemaVersion: 1,
type: "distribution-board.insert",
payload: {
structure: {
...current,
distributionBoard: legacyBoard,
},
},
} as unknown as DistributionBoardStructureProjectCommand;
repository.execute({
projectId: "project-1",
expectedRevision: 0,
source: "user",
command: legacyCommand,
});
const inserted = context.db
.select()
.from(distributionBoards)
.get();
assert.equal(inserted?.floorId, null);
assert.equal(inserted?.supplyType, null);
} finally {
context.close();
}
});
it("updates floor and supply type with persisted undo and validates project ownership", () => {
const context = createTestDatabase();
try {
context.db
.insert(floors)
.values([
{
id: "floor-1",
projectId: "project-1",
name: "EG",
sortOrder: 10,
},
{
id: "floor-foreign",
projectId: "project-2",
name: "Fremd",
sortOrder: 10,
},
])
.run();
const repository =
new DistributionBoardStructureProjectCommandRepository(
context.db
);
const structure = createDistributionBoardStructureSnapshot(
"project-1",
"UV-02",
{ floorId: null, supplyType: "AV" }
);
repository.execute({
projectId: "project-1",
expectedRevision: 0,
source: "user",
command:
createDistributionBoardInsertProjectCommand(structure),
});
const forward = repository.executeUpdate({
projectId: "project-1",
expectedRevision: 1,
source: "user",
command: createDistributionBoardUpdateProjectCommand(
structure.distributionBoard.id,
{ floorId: "floor-1", supplyType: "SV" }
),
});
assert.deepEqual(
context.db
.select({
floorId: distributionBoards.floorId,
supplyType: distributionBoards.supplyType,
})
.from(distributionBoards)
.get(),
{ floorId: "floor-1", supplyType: "SV" }
);
const undoStep = new ProjectHistoryRepository(
context.db
).getNextCommand("project-1", "undo");
assert.ok(undoStep);
repository.executeUpdate({
projectId: "project-1",
expectedRevision: 2,
source: "undo",
historyTargetChangeSetId: undoStep.changeSetId,
command: forward.inverse,
});
assert.deepEqual(
context.db
.select({
floorId: distributionBoards.floorId,
supplyType: distributionBoards.supplyType,
})
.from(distributionBoards)
.get(),
{ floorId: null, supplyType: "AV" }
);
assert.throws(
() =>
repository.executeUpdate({
projectId: "project-1",
expectedRevision: 3,
source: "user",
command: createDistributionBoardUpdateProjectCommand(
structure.distributionBoard.id,
{ floorId: "floor-foreign" }
),
}),
/does not belong/
);
assert.equal(
context.db
.select()
.from(projectRevisions)
.all().length,
3
);
} finally {
context.close();
}
});
it("sends distribution-board updates to the revision-safe API", async () => {
const requests: Array<{ url: string; method: string; body: unknown }> = [];
const originalFetch = globalThis.fetch;
globalThis.fetch = async (input, init) => {
requests.push({
url: String(input),
method: init?.method ?? "GET",
body: JSON.parse(String(init?.body)),
});
return new Response(JSON.stringify({}), {
status: 200,
headers: { "Content-Type": "application/json" },
});
};
try {
await updateDistributionBoard(
"project-1",
"board-1",
{ floorId: "floor-1", supplyType: "USV" },
9
);
} finally {
globalThis.fetch = originalFetch;
}
assert.deepEqual(requests, [
{
url: "/api/projects/project-1/distribution-boards/board-1",
method: "PUT",
body: {
floorId: "floor-1",
supplyType: "USV",
expectedRevision: 9,
},
},
]);
});
it("rejects foreign, stale and changed structures without partial writes", () => {
const context = createTestDatabase();
try {
+8
View File
@@ -958,6 +958,14 @@ describe("project command service", () => {
description: null,
singlePhaseVoltageV: 240,
threePhaseVoltageV: 415,
enabledDistributionBoardSupplyTypes: [
"AV",
"SV",
"EV",
"USV",
"MSR",
"SiBe",
],
}),
});
assert.equal(updated.history.currentRevision, 1);
@@ -10,6 +10,7 @@ import {
import { ProjectHistoryRepository } from "../src/db/repositories/project-history.repository.js";
import { ProjectLocationStructureProjectCommandRepository } from "../src/db/repositories/project-location-structure-project-command.repository.js";
import { consumers } from "../src/db/schema/consumers.js";
import { distributionBoards } from "../src/db/schema/distribution-boards.js";
import { floors } from "../src/db/schema/floors.js";
import { projectRevisions } from "../src/db/schema/project-revisions.js";
import { projects } from "../src/db/schema/projects.js";
@@ -183,6 +184,52 @@ describe("project-location structure project command", () => {
}
});
it("does not undo a floor assigned to a distribution board", () => {
const context = createTestDatabase();
try {
const repository =
new ProjectLocationStructureProjectCommandRepository(context.db);
const floor = createProjectFloorSnapshot("project-1", "EG", 0);
const inserted = repository.execute({
projectId: "project-1",
expectedRevision: 0,
source: "user",
command: createProjectFloorInsertProjectCommand(floor),
});
context.db
.insert(distributionBoards)
.values({
id: "board-1",
projectId: "project-1",
name: "UV-01",
floorId: floor.id,
supplyType: "AV",
})
.run();
const undoStep = new ProjectHistoryRepository(
context.db
).getNextCommand("project-1", "undo");
assert.ok(undoStep);
assert.throws(
() =>
repository.execute({
projectId: "project-1",
expectedRevision: 1,
source: "undo",
historyTargetChangeSetId: undoStep.changeSetId,
command: inserted.inverse,
}),
/assigned to a distribution board/
);
assert.equal(
context.db.select().from(projectRevisions).all().length,
1
);
} finally {
context.close();
}
});
it("creates a stable room and supports persisted undo and redo", () => {
const context = createTestDatabase();
try {
@@ -11,6 +11,7 @@ import { ProjectHistoryRepository } from "../src/db/repositories/project-history
import { ProjectSettingsProjectCommandRepository } from "../src/db/repositories/project-settings-project-command.repository.js";
import { projectRevisions } from "../src/db/schema/project-revisions.js";
import { projects } from "../src/db/schema/projects.js";
import { distributionBoards } from "../src/db/schema/distribution-boards.js";
import { ProjectRevisionConflictError } from "../src/domain/errors/project-revision-conflict.error.js";
import { createProjectSettingsUpdateProjectCommand } from "../src/domain/models/project-settings-project-command.model.js";
import { updateProjectSettings } from "../src/frontend/utils/api.js";
@@ -43,6 +44,8 @@ function getSettings(context: DatabaseContext) {
description: projects.description,
singlePhaseVoltageV: projects.singlePhaseVoltageV,
threePhaseVoltageV: projects.threePhaseVoltageV,
enabledDistributionBoardSupplyTypes:
projects.enabledDistributionBoardSupplyTypes,
currentRevision: projects.currentRevision,
})
.from(projects)
@@ -63,6 +66,14 @@ function settings(
description: null,
singlePhaseVoltageV: 230,
threePhaseVoltageV: 400,
enabledDistributionBoardSupplyTypes: [
"AV",
"SV",
"EV",
"USV",
"MSR",
"SiBe",
],
...overrides,
};
}
@@ -110,6 +121,14 @@ describe("project settings project-command repository", () => {
description: null,
singlePhaseVoltageV: 240,
threePhaseVoltageV: 415,
enabledDistributionBoardSupplyTypes: [
"AV",
"SV",
"EV",
"USV",
"MSR",
"SiBe",
],
},
},
]);
@@ -128,6 +147,14 @@ describe("project settings project-command repository", () => {
buildingOwner: "Beispiel Bau GmbH",
singlePhaseVoltageV: 240,
threePhaseVoltageV: 415,
enabledDistributionBoardSupplyTypes: [
"AV",
"SV",
"EV",
"USV",
"MSR",
"SiBe",
],
});
const forward = repository.executeUpdate({
projectId: "project-1",
@@ -136,13 +163,12 @@ describe("project settings project-command repository", () => {
command,
});
assert.deepEqual(getSettings(context), {
name: "Neuer Projektname",
internalProjectNumber: "INT-1",
externalProjectNumber: null,
buildingOwner: "Beispiel Bau GmbH",
description: null,
singlePhaseVoltageV: 240,
threePhaseVoltageV: 415,
...settings({
name: "Neuer Projektname",
buildingOwner: "Beispiel Bau GmbH",
singlePhaseVoltageV: 240,
threePhaseVoltageV: 415,
}),
currentRevision: 1,
});
assert.deepEqual(forward.inverse.payload, {
@@ -153,6 +179,14 @@ describe("project settings project-command repository", () => {
description: null,
singlePhaseVoltageV: 230,
threePhaseVoltageV: 400,
enabledDistributionBoardSupplyTypes: [
"AV",
"SV",
"EV",
"USV",
"MSR",
"SiBe",
],
});
const undoStep = history.getNextCommand("project-1", "undo");
@@ -172,6 +206,14 @@ describe("project settings project-command repository", () => {
description: null,
singlePhaseVoltageV: 230,
threePhaseVoltageV: 400,
enabledDistributionBoardSupplyTypes: [
"AV",
"SV",
"EV",
"USV",
"MSR",
"SiBe",
],
currentRevision: 2,
});
@@ -192,6 +234,14 @@ describe("project settings project-command repository", () => {
description: null,
singlePhaseVoltageV: 240,
threePhaseVoltageV: 415,
enabledDistributionBoardSupplyTypes: [
"AV",
"SV",
"EV",
"USV",
"MSR",
"SiBe",
],
currentRevision: 3,
});
} finally {
@@ -230,6 +280,51 @@ describe("project settings project-command repository", () => {
}
});
it("persists the project supply-type selection and rejects disabling an in-use type", () => {
const context = createTestDatabase();
try {
const repository = new ProjectSettingsProjectCommandRepository(
context.db
);
repository.executeUpdate({
projectId: "project-1",
expectedRevision: 0,
source: "user",
command: createProjectSettingsUpdateProjectCommand({
...settings(),
enabledDistributionBoardSupplyTypes: ["AV", "MSR", "SiBe"],
}),
});
assert.deepEqual(
getSettings(context)?.enabledDistributionBoardSupplyTypes,
["AV", "MSR", "SiBe"]
);
context.db.insert(distributionBoards).values({
id: "board-1",
projectId: "project-1",
name: "UV SiBe",
supplyType: "SiBe",
}).run();
assert.throws(
() =>
repository.executeUpdate({
projectId: "project-1",
expectedRevision: 1,
source: "user",
command: createProjectSettingsUpdateProjectCommand({
...settings(),
enabledDistributionBoardSupplyTypes: ["AV", "MSR"],
}),
}),
/SiBe/
);
assert.equal(getSettings(context)?.currentRevision, 1);
} finally {
context.close();
}
});
it("rejects no-op, invalid project and stale revision without partial writes", () => {
const context = createTestDatabase();
try {
@@ -277,13 +372,7 @@ describe("project settings project-command repository", () => {
ProjectRevisionConflictError
);
assert.deepEqual(getSettings(context), {
name: "Testprojekt",
internalProjectNumber: "INT-1",
externalProjectNumber: null,
buildingOwner: null,
description: null,
singlePhaseVoltageV: 230,
threePhaseVoltageV: 400,
...settings(),
currentRevision: 0,
});
assert.equal(
@@ -323,13 +412,7 @@ describe("project settings project-command repository", () => {
/forced project settings history failure/
);
assert.deepEqual(getSettings(context), {
name: "Testprojekt",
internalProjectNumber: "INT-1",
externalProjectNumber: null,
buildingOwner: null,
description: null,
singlePhaseVoltageV: 230,
threePhaseVoltageV: 400,
...settings(),
currentRevision: 0,
});
assert.equal(
+63
View File
@@ -16,6 +16,7 @@ import { circuitDeviceRows } from "../src/db/schema/circuit-device-rows.js";
import { circuitLists } from "../src/db/schema/circuit-lists.js";
import { circuitSections } from "../src/db/schema/circuit-sections.js";
import { circuits } from "../src/db/schema/circuits.js";
import { distributionBoards } from "../src/db/schema/distribution-boards.js";
import { floors } from "../src/db/schema/floors.js";
import { projectDevices } from "../src/db/schema/project-devices.js";
import { projectSnapshots } from "../src/db/schema/project-snapshots.js";
@@ -64,6 +65,11 @@ function createTestDatabase(): DatabaseContext {
sortOrder: 10,
})
.run();
context.db
.update(distributionBoards)
.set({ floorId: "floor-1", supplyType: "SV" })
.where(eq(distributionBoards.id, board.id))
.run();
context.db
.insert(rooms)
.values({
@@ -168,6 +174,8 @@ describe("project snapshot repository", () => {
);
assert.equal(state.project.id, "project-1");
assert.equal(state.distributionBoards.length, 1);
assert.equal(state.distributionBoards[0].floorId, "floor-1");
assert.equal(state.distributionBoards[0].supplyType, "SV");
assert.equal(state.circuitLists.length, 1);
assert.equal(
state.circuitSections.length,
@@ -279,6 +287,13 @@ describe("project snapshot repository", () => {
delete currentPayload.project.externalProjectNumber;
delete currentPayload.project.buildingOwner;
delete currentPayload.project.description;
delete currentPayload.project.enabledDistributionBoardSupplyTypes;
for (const board of currentPayload.distributionBoards as Array<
Record<string, unknown>
>) {
delete board.floorId;
delete board.supplyType;
}
const legacyPayloadJson = JSON.stringify(currentPayload);
context.db
.update(projectSnapshots)
@@ -352,6 +367,14 @@ describe("portable project transfer", () => {
copied.state.circuits[0].deviceRows[0].roomId,
copied.state.rooms[0].id
);
assert.equal(
copied.state.distributionBoards[0].floorId,
copied.state.floors[0].id
);
assert.equal(
copied.state.distributionBoards[0].supplyType,
"SV"
);
assert.equal(
copied.state.circuits[0].deviceRows[0].legacyConsumerId,
null
@@ -390,6 +413,46 @@ describe("portable project transfer", () => {
}
});
it("imports a checksum-valid version-two project transfer", () => {
const context = createTestDatabase();
try {
const repository = new ProjectTransferRepository(context.db);
const current = repository.exportProject("project-1");
assert.ok(current);
const legacyState = structuredClone(
current.projectState
) as unknown as Record<string, unknown> & {
distributionBoards: Array<Record<string, unknown>>;
};
legacyState.schemaVersion = 2;
delete (
legacyState.project as Record<string, unknown>
).enabledDistributionBoardSupplyTypes;
for (const board of legacyState.distributionBoards) {
delete board.floorId;
delete board.supplyType;
}
const legacyPayloadJson = JSON.stringify(legacyState);
const duplicate = repository.importDuplicate({
...current,
projectState: legacyState,
payloadSha256: crypto
.createHash("sha256")
.update(legacyPayloadJson)
.digest("hex"),
});
const copied = readProjectStateSnapshot(
context.db,
duplicate.projectId
);
assert.ok(copied);
assert.equal(copied.state.distributionBoards[0].floorId, null);
assert.equal(copied.state.distributionBoards[0].supplyType, null);
} finally {
context.close();
}
});
it("rolls back a duplicate when a late project-state insert fails", () => {
const context = createTestDatabase();
try {
+84
View File
@@ -20,6 +20,14 @@ function minimalSnapshot() {
description: null,
singlePhaseVoltageV: 230,
threePhaseVoltageV: 400,
enabledDistributionBoardSupplyTypes: [
"AV",
"SV",
"EV",
"USV",
"MSR",
"SiBe",
],
},
distributionBoards: [],
circuitLists: [],
@@ -61,9 +69,65 @@ describe("project state snapshot model", () => {
externalProjectNumber: null,
buildingOwner: null,
description: null,
enabledDistributionBoardSupplyTypes: [
"AV",
"SV",
"EV",
"USV",
"MSR",
"SiBe",
],
});
});
it("upgrades version-two distribution boards without inferred assignments", () => {
const previous = {
...minimalSnapshot(),
schemaVersion: 2 as const,
project: {
...minimalSnapshot().project,
enabledDistributionBoardSupplyTypes: undefined,
},
distributionBoards: [
{
id: "board-1",
projectId: "project-1",
name: "UV 1",
},
],
};
delete previous.project.enabledDistributionBoardSupplyTypes;
const snapshot = parseProjectStateSnapshot(previous);
assert.deepEqual(snapshot.distributionBoards, [
{
...previous.distributionBoards[0],
floorId: null,
supplyType: null,
},
]);
});
it("upgrades version-three snapshots with all project supply types", () => {
const previous = {
...minimalSnapshot(),
schemaVersion: 3 as const,
project: {
...minimalSnapshot().project,
enabledDistributionBoardSupplyTypes: undefined,
},
};
delete previous.project.enabledDistributionBoardSupplyTypes;
const snapshot = parseProjectStateSnapshot(previous);
assert.deepEqual(snapshot.project.enabledDistributionBoardSupplyTypes, [
"AV",
"SV",
"EV",
"USV",
"MSR",
"SiBe",
]);
});
it("rejects duplicate ids and cross-project ownership", () => {
assert.throws(
() =>
@@ -74,11 +138,15 @@ describe("project state snapshot model", () => {
id: "board-1",
projectId: "project-1",
name: "UV 1",
floorId: null,
supplyType: null,
},
{
id: "board-1",
projectId: "project-1",
name: "UV 2",
floorId: null,
supplyType: null,
},
],
}),
@@ -102,6 +170,22 @@ describe("project state snapshot model", () => {
});
it("rejects invalid references and inconsistent reserve circuits", () => {
assert.throws(
() =>
parseProjectStateSnapshot({
...minimalSnapshot(),
distributionBoards: [
{
id: "board-1",
projectId: "project-1",
name: "UV 1",
floorId: "missing",
supplyType: "AV",
},
],
}),
/distribution board floor reference is invalid/
);
assert.throws(
() =>
parseProjectStateSnapshot({
+35
View File
@@ -4,6 +4,7 @@ import { createElement } from "react";
import { renderToStaticMarkup } from "react-dom/server";
import { ProjectVersionHistory } from "../src/frontend/components/project-version-history.js";
import { ProjectDeviceModal } from "../src/frontend/components/project-device-modal.js";
import { ProjectSettingsModal } from "../src/frontend/components/project-settings-modal.js";
import type { ProjectRevisionSummaryDto } from "../src/frontend/types.js";
import {
createNamedProjectSnapshot,
@@ -99,6 +100,40 @@ describe("project version history presentation", () => {
});
});
describe("project settings presentation", () => {
it("shows all supply types and marks types already in use", () => {
const markup = renderToStaticMarkup(
createElement(ProjectSettingsModal, {
isSaving: false,
onClose: () => undefined,
onExport: async () => undefined,
onImport: async () => undefined,
onSave: async () => undefined,
project: {
id: "project-1",
name: "Projekt",
internalProjectNumber: null,
externalProjectNumber: null,
buildingOwner: null,
description: null,
singlePhaseVoltageV: 230,
threePhaseVoltageV: 400,
enabledDistributionBoardSupplyTypes: [
"AV",
"MSR",
"SiBe",
],
currentRevision: 0,
},
usedDistributionBoardSupplyTypes: ["SiBe"],
})
);
assert.match(markup, /MSR/);
assert.match(markup, /SiBe/);
assert.match(markup, /in Verwendung/);
});
});
describe("project device modal presentation", () => {
it("renders explicit German labels for every editable device field", () => {
const markup = renderToStaticMarkup(