Merge Issue #3 logical distribution board feeds

This commit is contained in:
Julian Appel 2026-09-28 20:58:35 +02:00
commit 552362bad1
61 changed files with 5399 additions and 352 deletions

View file

@ -52,7 +52,8 @@ See `docs/current-architecture.md` for the complete module and request flow.
- Device rows inside a circuit do not have their own equipment identifiers.
- Protection and cable data belong to the circuit, not to individual devices.
- Device-level values include quantity, power per unit, simultaneity factor, cosPhi, room data, cost group and category.
- Circuit total power is the sum of all device rows in that circuit.
- Circuit total power is the sum of all device rows in that circuit plus the
read-only loads of logically linked downstream boards on that feeder circuit.
- Existing equipment identifiers must never be changed automatically.
- Renumbering is always an explicit user action.
@ -284,16 +285,19 @@ not change the project revision or undo/redo stacks. Restoring a server-stored
snapshot verifies its checksum and the current-state hash, replaces supported
project data atomically and records a new `restore` revision with a complete
inverse command. Restore can therefore be undone and redone after a restart.
Current snapshot schema version 8 additionally stores the optional net room
area `rooms[].areaM2` (see `docs/building-structure.md`); versions 1 to 7 are
upgraded with `areaM2 = null`. Version 7 stores the project buildings and
Current snapshot schema version 9 additionally stores
`feedingDistributionBoardId` and `feedingCircuitId` on distribution boards;
versions 1 to 8 are upgraded with both references set to `null`. Version 8
stores the optional net room area `rooms[].areaM2` (see
`docs/building-structure.md`); versions 1 to 7 are upgraded with
`areaM2 = null`. Version 7 stores the project buildings and
`floors[].buildingId`; versions 1 to 6 are upgraded with no buildings and
`buildingId = null`. Version 6 stores the
distribution-board info fields `boardNumber`, `location`, `fedFrom` and
`remark` and the project cover fields `siteAddress`, `workStage`,
`projectLead` and `projectEditor`; all older supported versions are upgraded
with `null` for these fields.
Stored named and automatic snapshots of every supported version (1 to 8)
Stored named and automatic snapshots of every supported version (1 to 9)
remain restorable; they are upgraded while parsing. Version 5 stores
the manual quantity share of every CircuitDeviceRow. Version 4 snapshots are upgraded with
`manualQuantity = quantity`; they already contain the complete confirmed
@ -309,7 +313,7 @@ project graph.
Project-wide Revit CSV configuration uses
`external-csv-configuration.update`. Its complete expected/target snapshot is
persisted through the shared project-command transaction, so create, update,
delete, Undo and Redo are revision-safe. Snapshot schema 5-8, restore and portable
delete, Undo and Redo are revision-safe. Snapshot schema 5-9, restore and portable
project duplication preserve the configuration; duplication remaps its internal
id and project link.
The project page exposes the Revit CSV modal. It edits the complete
@ -323,7 +327,7 @@ immutable import batches with original bytes plus the classified cell matrix,
normalized source-room mappings and external objects unique by
`(sourceId, ifcGuid)`. Source values, planning values and overrides are separate.
The general external-model repository is read-only; runtime writes belong only
to typed command adapters. Snapshot/restore and portable project transfer schema versions 5-8
to typed command adapters. Snapshot/restore and portable project transfer schema versions 5-9
capture the complete external state and remap every internal UUID and link while
preserving IFCGUIDs, source values, original bytes and the classified matrix.
Import batches additionally store the monotonic project CSV configuration
@ -463,22 +467,34 @@ snapshot contains the group, optional component protections, complete circuits,
circuit protections and all device-row link/override metadata. Delete
re-captures and compares the subtree before cascading; restore preserves every
UUID in foreign-key-safe order.
The circuit-tree read model exposes fixed `headerComponents`, group-owned
The project-wide circuit-tree read model exposes fixed `headerComponents`, group-owned
`components`, `footerComponents`, optional group category/number and optional
one-to-one protection DTOs for circuits and group components. These are
the only supported circuit-protection source; no flat compatibility fields remain.
It resolves logical board feeds once for the complete project and adds each
downstream board's demand after its own board simultaneity factor to the chosen
feeding circuit as a read-only virtual row. Recursive chains and several boards
on one circuit are supported. A board-only link affects hierarchy but adds no
load until a feeding circuit is selected.
Distribution-board floor assignment and a project-enabled supply type use
`distribution-board.update`; both values are snapshot/export fields and one
persistent undo step.
The same command (still schema version 1, additively extended) carries the
The same command uses schema version 2 for the logical
`feedingDistributionBoardId` and optional matching `feedingCircuitId` together
with the existing fields. Version-1 history commands remain executable and do
not change logical feed references. The command rejects self-links, cycles and
circuits from another board. The
board info-card fields `boardNumber`, `location`, `fedFrom` and `remark`
(migration `0007`). They are nullable, trimmed planner values; the PUT route
only includes submitted info fields in the command and its inverse. An empty
board with info values is no longer an unchanged setup, so history deletion of
it is rejected like a changed simultaneity factor. Populated-subtree commands
stored before these fields existed are normalized to `null` values
(`normalizeDistributionBoardSubtreeSnapshot`). `fedFrom` is deliberately free
text: the logical link to the feeding board/circuit is an open design decision.
(`normalizeDistributionBoardSubtreeSnapshot`). `fedFrom` remains free text for
external feeds and notes; project-board feeds use the two UUID references.
Deleting a referenced board or feeding circuit is blocked in every direct,
subtree and history-only removal path. Board copies retain their incoming feed,
and project duplication remaps both references.
These fields are intended to be prefillable from Revit parameters later, but
only through the Phase 14 rules (preview, explicit confirmation, no silent
overwrite).
@ -521,9 +537,10 @@ Required operations:
`<thead>` so it repeats on printed pages. PDF export uses the browser print
dialog; page size, margins and "Seite x von y" come from injected named
`@page` rules (`lb-portrait`, `lb-landscape`).
- The board hierarchy and "Σ inkl. unterlagert" in the overview are
PRELIMINARY (text match of `fedFrom` against board number/name, plain sum,
no upstream simultaneity factor). Replace this once the feed link is modeled.
- The board hierarchy follows logical board ids. The overview shows power
before board GZF and demand including linked downstream boards; it does not
add a second cumulative column. Board plans render downstream-board loads as
read-only rows. `fedFrom` groups only external root feeds.
- Colors/fonts follow the kbp corporate design (`--pd-*` variables in
`print-document.css`). Empty cover values are shown as
"[ergänzen]"; the Planstände editor column shows the project's current
@ -701,9 +718,9 @@ for the early-phase power balance by DIN 276 cost group (see
- full electrical sizing
- multi-user/PostgreSQL operation
- supported production deployment
- logical feed link between distribution boards (`fedFrom` stays free text
until the project owner decides whether it references a board, a feeding
circuit or both) and the resulting hierarchical power summation
- second/replacement board feeds, including changeover semantics for SV/SiBe
- Revit prefill proposals for logical board feeds through the Phase 14 preview
and explicit-confirmation workflow
Do not implement these while working on an unrelated phase.

View file

@ -67,10 +67,13 @@ Prüfsumme; Ersetzen übernimmt die Grobbilanzen atomar und mit Undo/Redo,
Duplizieren vergibt neue IDs. Transferformat 1 bleibt ohne Grobbilanzen
importierbar.
Das aktuelle Projekt-Snapshot-Schema 8 enthält außerdem die optionale
Netto-Raumfläche `rooms[].areaM2`. Snapshot-Versionen 1–7 werden dafür mit
Das aktuelle Projekt-Snapshot-Schema 9 enthält die logischen Einspeisungsverweise
der Verteilungen; Versionen 1–8 werden mit leeren Verweisen hochgestuft. Schema 8
enthält außerdem die optionale Netto-Raumfläche `rooms[].areaM2`.
Snapshot-Versionen 1–7 werden dafür mit
`areaM2 = null` hochgestuft. Restore, Export und Import erhalten vorhandene
Flächen; eine eigene Transferformat-Version ist nicht nötig, weil Format 2 den
Flächen und Einspeisungsverweise; beim Duplizieren werden Board- und Circuit-IDs
neu zugeordnet. Eine eigene Transferformat-Version ist nicht nötig, weil Format 2 den
versionierten Projektsnapshot einbettet.
The public dispatcher supports the Circuit and CircuitDeviceRow commands
@ -254,23 +257,30 @@ commands. Endpoints, payloads and calculation rules: `docs/estimate-balance.md`.
- stale revisions return `409 PROJECT_REVISION_CONFLICT`
- `PUT /projects/:projectId/distribution-boards/:distributionBoardId`
- body:
`{ "floorId": null, "supplyType": "AV", "simultaneityFactor": 1, "expectedRevision": 13 }`
`{ "floorId": null, "supplyType": "AV", "simultaneityFactor": 1, "feedingDistributionBoardId": "board_hv", "feedingCircuitId": "circuit_out", "expectedRevision": 13 }`
- optional info-card fields: `boardNumber` (max. 50), `location`,
`fedFrom` (max. 200 each) and `remark` (max. 2000); strings are trimmed
and an empty string is stored as `null`
- executes `distribution-board.update`; all submitted values, including the
- `feedingDistributionBoardId` and `feedingCircuitId` are optional nullable
fields. A feeding circuit requires a feeding board and must belong to it;
self-links and cycles are rejected. A board-only link creates hierarchy
without assigning its load to an upstream circuit.
- executes `distribution-board.update` schema version 2; all submitted values, including the
submitted info fields, are restored together by persistent Undo/Redo
- `fedFrom` is free text until the logical feed link is modeled
- `fedFrom` remains free text for external feeds such as HAK, metering,
transformer or generator and for supplementary notes
- `POST /projects/:projectId/distribution-boards/:distributionBoardId/copy`
- body: `{ "name": "UV-02 Kopie", "expectedRevision": 14 }`
- duplicates the complete distribution-board subtree with new owning UUIDs
while preserving project-device and room links
while preserving project-device, room and incoming feeding links
- response includes the new `distributionBoard`, its `circuitList`, the
revision and current history state
- `DELETE /projects/:projectId/distribution-boards/:distributionBoardId`
- body: `{ "expectedRevision": 15 }`
- removes the complete, exactly captured distribution-board subtree in one
transaction
- rejects deletion while another board references this board or one of its
circuits as a feeder; circuit and populated-group deletion use the same guard
- response includes `distributionBoardId`, the revision and current history
state
- Copy and deletion use `distribution-board.insert-subtree` and
@ -327,12 +337,17 @@ commands. Endpoints, payloads and calculation rules: `docs/estimate-balance.md`.
### Tree Endpoint
- `GET /projects/:projectId/circuit-trees`
- returns all circuit trees for one project from one project-wide read and is
used by the print view and feeding-board selection
- `GET /projects/:projectId/circuit-lists/:circuitListId/tree`
- Purpose: returns fixed header components, grouped sections with optional
protection components, complete circuit/device-row blocks and auxiliary
footer components. Circuit and component protection devices use their
separate one-to-one DTOs. Calculated row, circuit, section and board totals
remain included.
remain included. Circuit totals contain the demand of linked downstream
boards. `downstreamBoardLoads` exposes those contributions as read-only rows;
`feedingReference` resolves the current board/circuit labels by id.
Response sketch:

View file

@ -33,10 +33,13 @@
- Protection and cable values are planner-owned manual selections. The later
sizing/warning engine does not yet validate them against load, length or
voltage-drop rules.
- Distribution-board `fedFrom` is free text. The print overview derives its
supply structure and "Σ inkl. unterlagert" only from exact text matches with
board numbers/names and adds values without an upstream simultaneity factor
(see `docs/print-view.md`).
- One logical incoming feed per distribution board is supported. A second or
replacement feed with changeover semantics (for example SV/SiBe) is still a
follow-up. `fedFrom` remains the free-text field for external sources and
supplementary notes. Revit does not yet propose logical feed links.
- A logical link without a selected feeding circuit appears in the hierarchy,
but deliberately contributes no load to an upstream circuit until the planner
selects that circuit (see `docs/print-view.md`).
- The print view uses a kbp corporate design derived from logo and
presentation material. Snapshots do not record an editor, so the cover's
Planstände list shows the project's current editor for every entry. On screen, long

View file

@ -468,8 +468,22 @@ Derselbe Befehl versioniert die Felder der Verteiler-Infokarte
(`boardNumber`, `location`, `fedFrom`, `remark`, Migration `0007`). Die
Infokarte liegt im Bearbeiten-Dialog der Verteilung; die Werte sind lokale
Planungswerte und sollen später über den Revit-Abgleich nur nach Vorschau und
ausdrücklicher Bestätigung vorbelegt werden. `fedFrom` ist vorerst Freitext.
Snapshot-Schema 8 enthält zusätzlich die optionale Netto-Raumfläche
ausdrücklicher Bestätigung vorbelegt werden. `fedFrom` bleibt Freitext für
externe Einspeisungen und Hinweise.
`distribution-board.update` verwendet Schema-Version 2 für die optionalen
logischen Verweise `feedingDistributionBoardId` und `feedingCircuitId`.
Version-1-Befehle aus der Historie bleiben ausführbar und verändern diese
Verweise nicht. Der speisende Stromkreis muss zur speisenden Verteilung gehören;
Selbstverweise und Zyklen werden abgelehnt. Löschbefehle für referenzierte
Verteilungen und Stromkreise, einschließlich Unterbaum- und Undo-Pfaden, werden
vor der Datenbankänderung fachlich gesperrt.
Die projektweite Circuit-Tree-Projektion berechnet rekursiv die Lasten aller
Verteilungen. Die Leistung einer Unterverteilung nach ihrem eigenen
Verteiler-GZF wird als virtuelle, schreibgeschützte Zeile genau dem gewählten
Abgang zugerechnet. Eine Verknüpfung ohne Stromkreis bildet nur die Hierarchie.
Snapshot-Schema 9 enthält die beiden Einspeisungsverweise; Versionen 1–8 werden
mit `null` hochgestuft. Restore und Projektduplizierung erhalten beziehungsweise
remappen die Verweise. Snapshot-Schema 8 enthält zusätzlich die optionale Netto-Raumfläche
`rooms[].areaM2`; Versionen 1–7 werden dafür mit `null` hochgestuft.
Snapshot-Schema 7 enthält die Gebäude und die Gebäudezuordnung der Etagen;
ältere Versionen werden ohne Gebäude hochgestuft.
@ -580,7 +594,7 @@ Oberfläche: `/projects/:projectId/grobbilanz`. Details:
`toLowerCase()` gegen das vollständige Register der Stromkreisliste und wird
von jedem Anlage-/Umbenennungspfad für Stromkreise und Verteilerkomponenten
aufgerufen, auch dort, wo zuvor kein Vorab-Check existierte. Snapshot- und
Transfer-Integration verwenden aktuell Snapshot-Schema 8. Persistente
Transfer-Integration verwenden aktuell Snapshot-Schema 9. Persistente
Insert/Delete/Update-Commands für veränderliche Verteilerkomponenten,
Gruppen einschließlich befüllter Unterbäume sowie vollständige
Gruppensortierung sind integriert. Der Editor zeigt die geschützte Struktur

View file

@ -5,8 +5,9 @@ Die Aktionen für Büroangaben und Logo speichern dagegen globale Einstellungen.
Projekt- und Verteilerangaben werden in den jeweiligen Bearbeitungsdialogen
über Projektbefehle mit Undo/Redo gepflegt.
Beim Laden werden die Projektrevisionen aller Stromkreislisten und ein
abschließend erneut gelesener Projektstand verglichen. Ändert sich das Projekt
Beim Laden werden alle Stromkreislisten in einer projektweiten Abfrage gelesen;
deren Projektrevision und ein abschließend erneut gelesener Projektstand werden
verglichen. Ändert sich das Projekt
währenddessen, wird statt eines gemischten Datenstands eine Aufforderung zum
Neuladen angezeigt. Eine bereits geladene Seitenansicht aktualisiert sich nicht
automatisch bei späteren Änderungen in einem anderen Tab.
@ -31,12 +32,14 @@ automatisch bei späteren Änderungen in einem anderen Tab.
erscheinen als orangefarbenes „[ergänzen]“.
2. **Verteilerübersicht (quer)** – Versorgungsstruktur und Tabelle je
Verteiler mit Stromkreisanzahl, P<sub>inst</sub> (Anzahl × Leistung je
Stück), P<sub>Geräte</sub> (mit Geräte-GZF), Verteiler-GZF,
P<sub>gl</sub>, I<sub>B</sub> und „Σ inkl. unterlagert“.
Stück, nur lokale Geräte), Leistung vor Verteiler-GZF einschließlich
gespeister Unterverteilungen, Verteiler-GZF, P<sub>gl</sub> einschließlich
Unterverteilungen und I<sub>B</sub>. Die Hierarchie folgt den logischen IDs.
3. **Verteilerpläne (quer, je Verteiler)** – Titelleiste mit
Verteilernummer, Bezeichnung, Örtlichkeit, Netzart, „Gespeist von“ und
P<sub>gl</sub>; darunter Kopfkomponenten, Gruppen, Stromkreise
(mehrzeilige mit Sammelzeile und eingerückten Gerätezeilen),
(mehrzeilige mit Sammelzeile, eingerückten Gerätezeilen und
schreibgeschützten Unterverteilungszeilen),
Gruppensummen und Verteilersumme. Fehlende Kabelangaben sind als „offen“
markiert. Die Titelleiste steht im Tabellenkopf und wiederholt sich beim
Drucken auf Folgeseiten.
@ -46,19 +49,25 @@ Seitenformat, Ränder und „Seite x von y“ kommen beim Drucken aus benannten
einfügt. Die Bildschirmansicht zeigt Blätter in Papiergröße; die exakte
Seitenaufteilung langer Verteilerpläne entsteht erst beim Drucken.
## Vorläufige Punkte
## Berechnung und offene Punkte
- **Einspeisung / Hierarchie:** `fedFrom` ist Freitext. Die Übersicht ordnet
einen Verteiler nur dann unter, wenn der Text eindeutig der Nummer oder
Bezeichnung eines anderen Verteilers entspricht (ohne Beachtung von
Groß-/Kleinschreibung und äußeren Leerzeichen). Mehrdeutige Treffer bleiben
ohne Zuordnung; „Σ inkl. unterlagert“ ist
eine einfache Summe ohne übergeordneten GZF. Die fachliche Verknüpfung
(Verteiler und/oder speisender Stromkreis) und die Summierungslogik sind
offen.
- **I<sub>B</sub>** ist ein symmetrischer Drehstromwert
S / (√3 · U<sub>3~</sub>) mit S aus P / cos φ je Gerätezeile
(fehlendes cos φ = 1) und Verteiler-GZF.
- **Einspeisung / Hierarchie:** `feedingDistributionBoardId` bestimmt die
Hierarchie. Ist zusätzlich `feedingCircuitId` gesetzt, fließt die Leistung
der Unterverteilung nach deren eigenem Verteiler-GZF als virtuelle Last in
genau diesen Abgang ein. Ketten werden rekursiv berechnet; mehrere
Unterverteilungen an einem Abgang sind erlaubt. Ohne Abgang bleibt es bei der
Hierarchie ohne Leistungsaufschlag. `fedFrom` bezeichnet nur externe Quellen
oder ergänzende Hinweise.
- **Keine Doppelzählung:** Die Abgangs- und Verteilersummen enthalten die
Unterverteilungsleistung bereits. Deshalb gibt es keine zusätzliche Spalte
„Σ inkl. unterlagert“. Die Summenzeile verwendet für hierarchische Leistungen
nur Wurzelverteilungen; P<sub>inst</sub> summiert weiterhin die lokalen Geräte.
- **I<sub>B</sub>:** S entsteht aus Σ(P / cos φ) für Geräte und den wirksamen
cos φ der Unterverteilungen, jeweils mit dem fachlich zugehörigen GZF. Bei
einem 1-phasigen speisenden Abgang gilt I = S / U<sub>1~</sub>, sonst
I = S / (√3 · U<sub>3~</sub>). Für externe oder nur bis zur Verteilung
bekannte Einspeisungen bleibt die bisherige Darstellung als symmetrischer
Drehstromwert bestehen.
- **Corporate Design:** Farben (`--pd-*` in
`src/frontend/print/print-document.css`) und Schrift (Arial) sind aus Logo
und kbp-Präsentationsmaterial abgeleitet und werden durch die offizielle
@ -118,10 +127,11 @@ ihren Wert.
## Bestehende Datenbanken
Vor dem Anwenden der additiven Migrationen `0007` und `0008` eine verifizierte
Vor dem Anwenden der additiven Migrationen einschließlich `0012` eine verifizierte
Datenbanksicherung mit `npm run db:backup` erstellen. Die neuen Projekt- und
Verteilerfelder bleiben bei bestehenden Datensätzen zunächst leer (`null`).
Snapshot-Schema 6 enthält beide Feldgruppen; die unterstützten Versionen 1–5
werden beim Lesen hochgestuft. Restore und Projektkopie erhalten die neuen
Angaben. Globale Büroangaben und Logo gehören zur Datenbanksicherung, nicht
Snapshot-Schema 9 enthält zusätzlich die logischen Einspeisungsverweise; die
unterstützten Versionen 1–8 werden mit leeren Verweisen hochgestuft. Restore und
Projektkopie erhalten beziehungsweise remappen die neuen Angaben. Globale
Büroangaben und Logo gehören zur Datenbanksicherung, nicht
zum logischen Projektsnapshot oder Projekttransfer.

View file

@ -149,9 +149,10 @@ The pre-release cleanup established the following compatibility boundary:
- [x] Rough power balance for LPH 2/3 by cost group, including the comparison
with the detailed balance and the explicit copy of one row as a
ProjectDevice.
- [x] Logical distribution-board feed link with an optional feeding circuit,
recursive load rollup and deletion guards (#3).
Open follow-ups (Gitea issues): logical feed link between distribution boards
(#3), bill of quantities export (#5), area-based loads in W/m² for
Open follow-ups (Gitea issues): bill of quantities export (#5), area-based loads in W/m² for
ProjectDevices and device rows (#20).
## Recommended Sequence

View file

@ -1541,6 +1541,16 @@ a.kpi:hover {
color: var(--text-faint);
}
.tree-grid .downstream-board-load-row td {
background: var(--panel-bg-subtle);
color: var(--text-muted);
font-style: italic;
}
.tree-grid .downstream-board-load-row td:nth-child(2) {
padding-left: 1.5rem;
}
.tree-grid .empty-circuit-row td {
background: var(--panel-bg-subtle);
}

View file

@ -56,6 +56,7 @@ import {
disconnectProjectDeviceRows,
exportProjectTransfer,
getProject,
getProjectCircuitTrees,
getProjectDeviceSyncPreview,
listBuildings,
listCircuitLists,
@ -73,6 +74,7 @@ import {
import type {
BuildingDto,
CircuitListDto,
CircuitTreeResponseDto,
CreateRoomInput,
CreateProjectDeviceInput,
DistributionBoardDto,
@ -151,6 +153,7 @@ export default function ProjectDetailPage() {
const [project, setProject] = useState<ProjectDto | null>(null);
const [boards, setBoards] = useState<DistributionBoardDto[]>([]);
const [circuitLists, setCircuitLists] = useState<CircuitListDto[]>([]);
const [circuitTrees, setCircuitTrees] = useState<CircuitTreeResponseDto[]>([]);
const [buildings, setBuildings] = useState<BuildingDto[]>([]);
const [floors, setFloors] = useState<FloorDto[]>([]);
const [rooms, setRooms] = useState<RoomDto[]>([]);
@ -170,6 +173,8 @@ export default function ProjectDetailPage() {
setEditingBoardSimultaneityFactor,
] = useState("1");
const [editingBoardCopyName, setEditingBoardCopyName] = useState("");
const [editingFeedingBoardId, setEditingFeedingBoardId] = useState("");
const [editingFeedingCircuitId, setEditingFeedingCircuitId] = useState("");
const [editingBoardInfo, setEditingBoardInfo] =
useState<DistributionBoardInfoDraft>(
toDistributionBoardInfoDraft({
@ -209,6 +214,7 @@ export default function ProjectDetailPage() {
getProject(projectId),
listDistributionBoards(projectId),
listCircuitLists(projectId),
getProjectCircuitTrees(projectId),
listBuildings(projectId),
listFloors(projectId),
listRooms(projectId),
@ -219,6 +225,7 @@ export default function ProjectDetailPage() {
currentProject,
distributionBoards,
loadedCircuitLists,
loadedCircuitTrees,
loadedBuildings,
loadedFloors,
loadedRooms,
@ -228,6 +235,7 @@ export default function ProjectDetailPage() {
setProject(currentProject);
setBoards(distributionBoards);
setCircuitLists(loadedCircuitLists);
setCircuitTrees(loadedCircuitTrees.trees);
setBuildings(loadedBuildings);
setFloors(loadedFloors);
setRooms(loadedRooms);
@ -310,6 +318,48 @@ export default function ProjectDetailPage() {
() => new Map(circuitLists.map((circuitList) => [circuitList.distributionBoardId, circuitList])),
[circuitLists]
);
const circuitTreeByBoardId = useMemo(
() => new Map(circuitTrees.map((tree) => [tree.distributionBoardId, tree])),
[circuitTrees]
);
const feedingBoardOptions = useMemo(() => {
if (!editingBoard) return [];
const boardById = new Map(boards.map((board) => [board.id, board]));
return boards.filter((candidate) => {
if (candidate.id === editingBoard.id) return false;
const visited = new Set<string>();
let current: DistributionBoardDto | undefined = candidate;
while (current && current.feedingDistributionBoardId && !visited.has(current.id)) {
if (current.feedingDistributionBoardId === editingBoard.id) return false;
visited.add(current.id);
current = boardById.get(current.feedingDistributionBoardId);
}
return true;
});
}, [boards, editingBoard]);
const selectedFeedingTree = editingFeedingBoardId
? circuitTreeByBoardId.get(editingFeedingBoardId)
: undefined;
const selectedFeedingCircuits = useMemo(
() =>
selectedFeedingTree?.sections.flatMap((section) =>
section.circuits.map((circuit) => ({ circuit, section }))
) ?? [],
[selectedFeedingTree]
);
const selectedFeedingCircuit = selectedFeedingCircuits.find(
({ circuit }) => circuit.id === editingFeedingCircuitId
);
const selectedFeedingBoard = boards.find(
(board) => board.id === editingFeedingBoardId
);
const selectedFeedingCircuitUseCount = editingFeedingCircuitId
? boards.filter(
(board) =>
board.id !== editingBoard?.id &&
board.feedingCircuitId === editingFeedingCircuitId
).length
: 0;
const enabledBoardSupplyTypes =
project?.enabledDistributionBoardSupplyTypes ?? [];
const usedBoardSupplyTypes = useMemo(
@ -329,6 +379,21 @@ export default function ProjectDetailPage() {
[boards]
);
function formatBoardFeedingReference(board: DistributionBoardDto) {
if (!board.feedingDistributionBoardId) return board.fedFrom ?? "–";
const feedingBoard = boards.find(
(candidate) => candidate.id === board.feedingDistributionBoardId
);
if (!feedingBoard) return "Unbekannte Verteilung";
const boardLabel = feedingBoard.boardNumber ?? feedingBoard.name;
if (!board.feedingCircuitId) return boardLabel;
const circuit = circuitTreeByBoardId
.get(feedingBoard.id)
?.sections.flatMap((section) => section.circuits)
.find((candidate) => candidate.id === board.feedingCircuitId);
return `${boardLabel}${circuit?.equipmentIdentifier ?? ""}`;
}
function applyProjectRevision(currentRevision: number) {
setProject((current) =>
current ? { ...current, currentRevision } : current
@ -358,6 +423,7 @@ export default function ProjectDetailPage() {
]);
setCircuitLists(await listCircuitLists(projectId));
applyProjectRevision(result.history.currentRevision);
setCircuitTrees((await getProjectCircuitTrees(projectId)).trees);
setBoardName("");
setBoardFloorId("");
setBoardSupplyType(
@ -488,6 +554,8 @@ export default function ProjectDetailPage() {
);
setEditingBoardCopyName(`${board.name} Kopie`);
setEditingBoardInfo(toDistributionBoardInfoDraft(board));
setEditingFeedingBoardId(board.feedingDistributionBoardId ?? "");
setEditingFeedingCircuitId(board.feedingCircuitId ?? "");
}
function openBoardCreator() {
@ -523,6 +591,8 @@ export default function ProjectDetailPage() {
floorId: editingBoardFloorId || null,
supplyType: editingBoardSupplyType,
simultaneityFactor,
feedingDistributionBoardId: editingFeedingBoardId || null,
feedingCircuitId: editingFeedingCircuitId || null,
...fromDistributionBoardInfoDraft(editingBoardInfo),
},
project.currentRevision
@ -535,6 +605,7 @@ export default function ProjectDetailPage() {
)
);
applyProjectRevision(result.history.currentRevision);
setCircuitTrees((await getProjectCircuitTrees(projectId)).trees);
setEditingBoard(null);
} catch (err) {
setError(
@ -590,6 +661,7 @@ export default function ProjectDetailPage() {
result.circuitList,
]);
applyProjectRevision(result.history.currentRevision);
setCircuitTrees((await getProjectCircuitTrees(projectId)).trees);
setEditingBoard(null);
} catch (err) {
setError(
@ -633,6 +705,7 @@ export default function ProjectDetailPage() {
)
);
applyProjectRevision(result.history.currentRevision);
setCircuitTrees((await getProjectCircuitTrees(projectId)).trees);
setEditingBoard(null);
} catch (err) {
setError(
@ -989,6 +1062,7 @@ export default function ProjectDetailPage() {
<th>Örtlichkeit</th>
<th>Etage</th>
<th>Netzart</th>
<th>Gespeist von</th>
<th>GZF</th>
<th className="text-end">Aktionen</th>
</tr>
@ -1015,6 +1089,7 @@ export default function ProjectDetailPage() {
]
: "Nicht festgelegt"}
</td>
<td>{formatBoardFeedingReference(board)}</td>
<td>
{board.simultaneityFactor.toLocaleString(
"de-DE",
@ -1392,7 +1467,7 @@ export default function ProjectDetailPage() {
{editingBoard ? (
<FormModal
description={`${editingBoard.name}: Etagenzuordnung, Netzart und Gleichzeitigkeitsfaktor ändern.`}
description={`${editingBoard.name}: Etagenzuordnung, Netzart, Einspeisung und Gleichzeitigkeitsfaktor ändern.`}
isSaving={isSaving}
onClose={() => setEditingBoard(null)}
onSubmit={handleUpdateBoard}
@ -1479,6 +1554,91 @@ export default function ProjectDetailPage() {
</div>
</div>
</div>
<section className="card mt-4" aria-labelledby="feeding-reference-heading">
<div className="card-header" id="feeding-reference-heading">
Logische Einspeisung
</div>
<div className="card-body row g-3">
<div className="col-12 col-md-6">
<label className="form-label" htmlFor="feeding-distribution-board">
Speisende Verteilung
</label>
<select
className="form-select"
id="feeding-distribution-board"
onChange={(event) => {
setEditingFeedingBoardId(event.target.value);
setEditingFeedingCircuitId("");
}}
value={editingFeedingBoardId}
>
<option value="">Keine Projektverteilung</option>
{feedingBoardOptions.map((board) => (
<option key={board.id} value={board.id}>
{board.boardNumber ?? board.name}
</option>
))}
</select>
</div>
<div className="col-12 col-md-6">
<label className="form-label" htmlFor="feeding-circuit">
Speisender Abgang
</label>
<select
className="form-select"
disabled={!editingFeedingBoardId}
id="feeding-circuit"
onChange={(event) => setEditingFeedingCircuitId(event.target.value)}
value={editingFeedingCircuitId}
>
<option value="">Nur Verteilung bekannt</option>
{selectedFeedingCircuits.map(({ circuit }) => (
<option key={circuit.id} value={circuit.id}>
{circuit.equipmentIdentifier}
{circuit.displayName ? ` · ${circuit.displayName}` : ""}
</option>
))}
</select>
</div>
{editingFeedingBoardId && !editingFeedingCircuitId ? (
<div className="col-12">
<div className="alert alert-warning mb-0 py-2">
Ohne speisenden Abgang wird die Hierarchie angezeigt, die
Leistung dieser Verteilung aber keinem Stromkreis zugerechnet.
</div>
</div>
) : null}
{selectedFeedingBoard?.supplyType &&
editingBoardSupplyType !== selectedFeedingBoard.supplyType ? (
<div className="col-12">
<div className="alert alert-warning mb-0 py-2">
Die Netzarten weichen ab: {selectedFeedingBoard.supplyType}
{" → "}{editingBoardSupplyType}. Bitte Umschaltung und
Versorgungskonzept prüfen.
</div>
</div>
) : null}
{selectedFeedingCircuit &&
selectedFeedingCircuit.section.category !== "three_phase" ? (
<div className="col-12">
<div className="alert alert-warning mb-0 py-2">
Der gewählte Abgang ist 1-phasig. Bitte prüfen, ob diese
Einspeisung für die Unterverteilung geeignet ist.
</div>
</div>
) : null}
{selectedFeedingCircuitUseCount > 0 ? (
<div className="col-12">
<div className="alert alert-info mb-0 py-2">
Dieser Abgang speist bereits {selectedFeedingCircuitUseCount}
{selectedFeedingCircuitUseCount === 1
? " weitere Verteilung."
: " weitere Verteilungen."}
</div>
</div>
) : null}
</div>
</section>
<div className="mt-4">
<DistributionBoardInfoCard
draft={editingBoardInfo}

View file

@ -7,10 +7,9 @@ import "../../../../frontend/print/print-document.css";
import {
deleteDocumentLogo,
documentLogoUrl,
getCircuitTree,
getProjectCircuitTrees,
getDocumentBranding,
getProject,
listCircuitLists,
listDistributionBoards,
listFloors,
listProjectSnapshots,
@ -31,7 +30,7 @@ import {
import { PrintDocument } from "../../../../frontend/print/print-document";
import {
assertPrintRevisionConsistency,
buildPreliminaryBoardHierarchy,
buildBoardHierarchy,
summarizePrintBoard,
type PrintBoardSummary,
} from "../../../../frontend/print/print-document-model";
@ -89,17 +88,20 @@ export default function ProjectPrintPage() {
}
let cancelled = false;
async function load() {
const [project, boards, circuitLists, floors, snapshots, branding] =
const [project, boards, projectTrees, floors, snapshots, branding] =
await Promise.all([
getProject(projectId),
listDistributionBoards(projectId),
listCircuitLists(projectId),
getProjectCircuitTrees(projectId),
listFloors(projectId),
listProjectSnapshots(projectId),
getDocumentBranding(),
]);
const floorNameById = new Map(floors.map((floor) => [floor.id, floor.name]));
const summaries = await Promise.all(
const treeByBoardId = new Map(
projectTrees.trees.map((tree) => [tree.distributionBoardId, tree])
);
const summaries =
[...boards]
.sort(
(left, right) =>
@ -107,20 +109,17 @@ export default function ProjectPrintPage() {
boardSortCollator.compare(left.name, right.name)
)
.flatMap((board) => {
const circuitList = circuitLists.find(
(list) => list.distributionBoardId === board.id
);
return circuitList ? [{ board, circuitList }] : [];
const tree = treeByBoardId.get(board.id);
return tree ? [{ board, tree }] : [];
})
.map(async ({ board, circuitList }) =>
.map(({ board, tree }) =>
summarizePrintBoard({
board,
circuitListId: circuitList.id,
tree: await getCircuitTree(projectId, circuitList.id),
circuitListId: tree.circuitListId,
tree,
floorName: board.floorId ? floorNameById.get(board.floorId) ?? null : null,
})
)
);
);
const finalProject = await getProject(projectId);
assertPrintRevisionConsistency(project.currentRevision, summaries, finalProject.currentRevision);
if (cancelled) {
@ -148,7 +147,7 @@ export default function ProjectPrintPage() {
}, [projectId]);
const entries = useMemo(
() => (data ? buildPreliminaryBoardHierarchy(data.summaries) : []),
() => (data ? buildBoardHierarchy(data.summaries) : []),
[data]
);
const stand = useMemo(

View file

@ -0,0 +1,4 @@
ALTER TABLE `distribution_boards` ADD `feeding_distribution_board_id` text REFERENCES distribution_boards(id);--> statement-breakpoint
ALTER TABLE `distribution_boards` ADD `feeding_circuit_id` text REFERENCES circuits(id);--> statement-breakpoint
CREATE INDEX `distribution_boards_feeding_board_idx` ON `distribution_boards` (`feeding_distribution_board_id`);--> statement-breakpoint
CREATE INDEX `distribution_boards_feeding_circuit_idx` ON `distribution_boards` (`feeding_circuit_id`);

File diff suppressed because it is too large Load diff

View file

@ -85,6 +85,13 @@
"when": 1790531351492,
"tag": "0011_glossy_norman_osborn",
"breakpoints": true
},
{
"idx": 12,
"version": "6",
"when": 1790540142918,
"tag": "0012_good_gateway",
"breakpoints": true
}
]
}

View file

@ -23,6 +23,7 @@ import { circuits } from "../schema/circuits.js";
import { executeProjectCommandTransaction } from "./project-command-transaction.persistence.js";
import { updateDerivedCircuitVoltage } from "./project-voltage.persistence.js";
import { resolveCircuitVoltage } from "./project-voltage.persistence.js";
import { assertCircuitsAreNotReferencedAsFeeders } from "./distribution-board-feeding-deletion-guard.persistence.js";
interface PersistedMoveRow {
id: string;
@ -171,6 +172,9 @@ export class CircuitDeviceRowMoveProjectCommandRepository
projectId,
destinationCircuitIds
);
assertCircuitsAreNotReferencedAsFeeders(database, projectId, [
targetCircuit.id,
]);
const deleted = database
.delete(circuits)
.where(

View file

@ -26,6 +26,7 @@ import { distributionBoardComponentProtectionDevices } from "../schema/distribut
import { distributionBoardComponents } from "../schema/distribution-board-components.js";
import { toCircuitDeviceRowInsertValues } from "./circuit-device-row-structure.persistence.js";
import { executeProjectCommandTransaction } from "./project-command-transaction.persistence.js";
import { assertCircuitsAreNotReferencedAsFeeders } from "./distribution-board-feeding-deletion-guard.persistence.js";
export class CircuitGroupSubtreeProjectCommandRepository
implements CircuitGroupSubtreeProjectCommandStore
@ -65,6 +66,11 @@ export class CircuitGroupSubtreeProjectCommandRepository
"Circuit-group subtree changed before deletion."
);
}
assertCircuitsAreNotReferencedAsFeeders(
database,
projectId,
current.circuits.map((entry) => entry.circuit.id)
);
const deleted = database
.delete(circuitSections)
.where(eq(circuitSections.id, current.group.id))

View file

@ -1,5 +1,5 @@
import crypto from "node:crypto";
import { and, asc, eq } from "drizzle-orm";
import { and, asc, eq, inArray } from "drizzle-orm";
import { defaultCircuitSectionDefinitions } from "../../domain/models/distribution-board-structure-project-command.model.js";
import type { AppDatabase } from "../database-context.js";
import { circuitSections } from "../schema/circuit-sections.js";
@ -30,6 +30,15 @@ export class CircuitSectionRepository {
.orderBy(asc(circuitSections.sortOrder));
}
async listByCircuitLists(circuitListIds: string[]) {
if (circuitListIds.length === 0) return [];
return this.database
.select()
.from(circuitSections)
.where(inArray(circuitSections.circuitListId, circuitListIds))
.orderBy(asc(circuitSections.sortOrder), asc(circuitSections.id));
}
async createDefaults(circuitListId: string) {
const db = this.database;
for (const entry of createDefaultCircuitSectionValues(circuitListId)) {

View file

@ -26,6 +26,7 @@ import {
toCircuitDeviceRowSnapshot,
} from "./circuit-device-row-structure.persistence.js";
import { executeProjectCommandTransaction } from "./project-command-transaction.persistence.js";
import { assertCircuitsAreNotReferencedAsFeeders } from "./distribution-board-feeding-deletion-guard.persistence.js";
import { resolveCircuitVoltage } from "./project-voltage.persistence.js";
import { assertEquipmentIdentifierAvailable } from "./equipment-identifier-uniqueness.persistence.js";
@ -236,6 +237,8 @@ export class CircuitStructureProjectCommandRepository
protectionDevice: protectionDevice ?? null,
});
assertCircuitsAreNotReferencedAsFeeders(database, projectId, [circuit.id]);
const result = database
.delete(circuits)
.where(

View file

@ -1,4 +1,4 @@
import { asc, eq } from "drizzle-orm";
import { asc, eq, inArray } from "drizzle-orm";
import type { AppDatabase } from "../database-context.js";
import { circuits } from "../schema/circuits.js";
@ -14,6 +14,15 @@ export class CircuitRepository {
.orderBy(asc(circuits.sortOrder), asc(circuits.equipmentIdentifier));
}
async listByCircuitLists(circuitListIds: string[]) {
if (circuitListIds.length === 0) return [];
return this.database
.select()
.from(circuits)
.where(inArray(circuits.circuitListId, circuitListIds))
.orderBy(asc(circuits.sortOrder), asc(circuits.equipmentIdentifier));
}
async listBySection(sectionId: string) {
const db = this.database;
return db

View file

@ -19,6 +19,18 @@ export class DistributionBoardComponentRepository {
);
}
async listByCircuitLists(circuitListIds: string[]) {
if (circuitListIds.length === 0) return [];
return this.database
.select()
.from(distributionBoardComponents)
.where(inArray(distributionBoardComponents.circuitListId, circuitListIds))
.orderBy(
asc(distributionBoardComponents.sortOrder),
asc(distributionBoardComponents.id)
);
}
async listProtectionByComponentIds(componentIds: string[]) {
if (componentIds.length === 0) {
return [];

View file

@ -0,0 +1,65 @@
import { and, asc, eq, inArray } from "drizzle-orm";
import type { AppDatabase } from "../database-context.js";
import { distributionBoards } from "../schema/distribution-boards.js";
export function assertBoardsAreNotReferencedAsFeeders(
database: AppDatabase,
projectId: string,
distributionBoardIds: string[]
) {
if (distributionBoardIds.length === 0) return;
const dependent = database
.select({
name: distributionBoards.name,
boardNumber: distributionBoards.boardNumber,
})
.from(distributionBoards)
.where(
and(
eq(distributionBoards.projectId, projectId),
inArray(
distributionBoards.feedingDistributionBoardId,
[...new Set(distributionBoardIds)]
)
)
)
.orderBy(asc(distributionBoards.boardNumber), asc(distributionBoards.name))
.limit(1)
.get();
if (dependent) throwFeedingReferenceError(dependent);
}
export function assertCircuitsAreNotReferencedAsFeeders(
database: AppDatabase,
projectId: string,
circuitIds: string[]
) {
if (circuitIds.length === 0) return;
const dependent = database
.select({
name: distributionBoards.name,
boardNumber: distributionBoards.boardNumber,
})
.from(distributionBoards)
.where(
and(
eq(distributionBoards.projectId, projectId),
inArray(
distributionBoards.feedingCircuitId,
[...new Set(circuitIds)]
)
)
)
.orderBy(asc(distributionBoards.boardNumber), asc(distributionBoards.name))
.limit(1)
.get();
if (dependent) throwFeedingReferenceError(dependent);
}
function throwFeedingReferenceError(dependent: {
name: string;
boardNumber: string | null;
}): never {
const label = dependent.boardNumber ?? dependent.name;
throw new Error(`Speist ${label} – erst Einspeisung ändern.`);
}

View file

@ -30,6 +30,7 @@ import { distributionBoards } from "../schema/distribution-boards.js";
import { floors } from "../schema/floors.js";
import { projects } from "../schema/projects.js";
import { executeProjectCommandTransaction } from "./project-command-transaction.persistence.js";
import { assertBoardsAreNotReferencedAsFeeders } from "./distribution-board-feeding-deletion-guard.persistence.js";
export class DistributionBoardStructureProjectCommandRepository
implements DistributionBoardStructureProjectCommandStore
@ -248,6 +249,8 @@ export class DistributionBoardStructureProjectCommandRepository
);
}
assertBoardsAreNotReferencedAsFeeders(database, projectId, [board.id]);
const deleted = database
.delete(distributionBoards)
.where(
@ -325,6 +328,16 @@ export class DistributionBoardStructureProjectCommandRepository
patch.supplyType
);
}
this.assertFeedingReference(database, input.projectId, current.id, {
feedingDistributionBoardId:
patch.feedingDistributionBoardId === undefined
? current.feedingDistributionBoardId
: patch.feedingDistributionBoardId,
feedingCircuitId:
patch.feedingCircuitId === undefined
? current.feedingCircuitId
: patch.feedingCircuitId,
});
const inversePatch = Object.fromEntries(
input.command.payload.changes.map((change) => [
change.field,
@ -333,7 +346,8 @@ export class DistributionBoardStructureProjectCommandRepository
) as DistributionBoardUpdatePatch;
const inverse = createDistributionBoardUpdateProjectCommand(
current.id,
inversePatch
inversePatch,
input.command.schemaVersion
);
const updated = database
.update(distributionBoards)
@ -352,6 +366,77 @@ export class DistributionBoardStructureProjectCommandRepository
}
return inverse;
}
private assertFeedingReference(
database: AppDatabase,
projectId: string,
distributionBoardId: string,
reference: {
feedingDistributionBoardId: string | null;
feedingCircuitId: string | null;
}
) {
const { feedingDistributionBoardId, feedingCircuitId } = reference;
if (feedingDistributionBoardId === null) {
if (feedingCircuitId !== null) {
throw new Error(
"Ein speisender Stromkreis erfordert eine speisende Verteilung."
);
}
return;
}
if (feedingDistributionBoardId === distributionBoardId) {
throw new Error("Eine Verteilung kann sich nicht selbst speisen.");
}
const projectBoards = database
.select({
id: distributionBoards.id,
feedingDistributionBoardId:
distributionBoards.feedingDistributionBoardId,
})
.from(distributionBoards)
.where(eq(distributionBoards.projectId, projectId))
.all();
const boardById = new Map(projectBoards.map((board) => [board.id, board]));
if (!boardById.has(feedingDistributionBoardId)) {
throw new Error(
"Die speisende Verteilung gehört nicht zum Projekt."
);
}
const visited = new Set<string>([distributionBoardId]);
let currentId: string | null = feedingDistributionBoardId;
while (currentId !== null) {
if (visited.has(currentId)) {
throw new Error("Die Einspeisungsverknüpfung würde einen Zyklus erzeugen.");
}
visited.add(currentId);
currentId = boardById.get(currentId)?.feedingDistributionBoardId ?? null;
}
if (feedingCircuitId === null) {
return;
}
const feedingCircuit = database
.select({
projectId: circuitLists.projectId,
distributionBoardId: circuitLists.distributionBoardId,
})
.from(circuits)
.innerJoin(
circuitLists,
eq(circuitLists.id, circuits.circuitListId)
)
.where(eq(circuits.id, feedingCircuitId))
.get();
if (
!feedingCircuit ||
feedingCircuit.projectId !== projectId ||
feedingCircuit.distributionBoardId !== feedingDistributionBoardId
) {
throw new Error(
"Der speisende Stromkreis gehört nicht zur gewählten Verteilung."
);
}
}
}
function assertSupplyTypeEnabled(
@ -392,11 +477,20 @@ function structureMatches(
location,
fedFrom,
remark,
feedingDistributionBoardId,
feedingCircuitId,
...actualDistributionBoard
} = actual.board;
if (
simultaneityFactor !== 1 ||
[boardNumber, location, fedFrom, remark].some((value) => value !== null) ||
[
boardNumber,
location,
fedFrom,
remark,
feedingDistributionBoardId,
feedingCircuitId,
].some((value) => value !== null) ||
!sameRecord(
expected.distributionBoard,
actualDistributionBoard

View file

@ -32,6 +32,7 @@ import { projects } from "../schema/projects.js";
import { rooms } from "../schema/rooms.js";
import { toCircuitDeviceRowInsertValues } from "./circuit-device-row-structure.persistence.js";
import { executeProjectCommandTransaction } from "./project-command-transaction.persistence.js";
import { assertBoardsAreNotReferencedAsFeeders } from "./distribution-board-feeding-deletion-guard.persistence.js";
export class DistributionBoardSubtreeProjectCommandRepository
implements DistributionBoardSubtreeProjectCommandStore
@ -82,6 +83,9 @@ export class DistributionBoardSubtreeProjectCommandRepository
"Distribution-board subtree changed before deletion."
);
}
assertBoardsAreNotReferencedAsFeeders(database, projectId, [
current.distributionBoard.id,
]);
const deleted = database
.delete(distributionBoards)
.where(
@ -300,6 +304,69 @@ export class DistributionBoardSubtreeProjectCommandRepository
);
}
}
const feedingDistributionBoardId =
snapshot.distributionBoard.feedingDistributionBoardId;
const feedingCircuitId = snapshot.distributionBoard.feedingCircuitId;
if (feedingDistributionBoardId === null) {
if (feedingCircuitId !== null) {
throw new Error(
"Ein speisender Stromkreis erfordert eine speisende Verteilung."
);
}
} else {
if (feedingDistributionBoardId === snapshot.distributionBoard.id) {
throw new Error("Eine Verteilung kann sich nicht selbst speisen.");
}
const projectBoards = database
.select({
id: distributionBoards.id,
feedingDistributionBoardId:
distributionBoards.feedingDistributionBoardId,
})
.from(distributionBoards)
.where(eq(distributionBoards.projectId, projectId))
.all();
const boardById = new Map(projectBoards.map((board) => [board.id, board]));
if (!boardById.has(feedingDistributionBoardId)) {
throw new Error(
"Die speisende Verteilung gehört nicht zum Projekt."
);
}
const visited = new Set<string>([snapshot.distributionBoard.id]);
let currentId: string | null = feedingDistributionBoardId;
while (currentId !== null) {
if (visited.has(currentId)) {
throw new Error(
"Die Einspeisungsverknüpfung würde einen Zyklus erzeugen."
);
}
visited.add(currentId);
currentId = boardById.get(currentId)?.feedingDistributionBoardId ?? null;
}
if (feedingCircuitId !== null) {
const feedingCircuit = database
.select({
projectId: circuitLists.projectId,
distributionBoardId: circuitLists.distributionBoardId,
})
.from(circuits)
.innerJoin(
circuitLists,
eq(circuitLists.id, circuits.circuitListId)
)
.where(eq(circuits.id, feedingCircuitId))
.get();
if (
!feedingCircuit ||
feedingCircuit.projectId !== projectId ||
feedingCircuit.distributionBoardId !== feedingDistributionBoardId
) {
throw new Error(
"Der speisende Stromkreis gehört nicht zur gewählten Verteilung."
);
}
}
}
const linkedProjectDeviceIds = uniqueStrings(
snapshot.circuits.flatMap((circuit) =>
circuit.deviceRows.flatMap((row) =>

View file

@ -33,6 +33,7 @@ import {
snapshotsEqual,
} from "./external-object-assignment.persistence.js";
import { assertEquipmentIdentifierAvailable } from "./equipment-identifier-uniqueness.persistence.js";
import { assertCircuitsAreNotReferencedAsFeeders } from "./distribution-board-feeding-deletion-guard.persistence.js";
export class ExternalObjectNewCircuitProjectCommandRepository
implements ExternalObjectNewCircuitProjectCommandStore
@ -162,6 +163,9 @@ export class ExternalObjectNewCircuitProjectCommandRepository
objects,
"External object changed during new-circuit assignment."
);
assertCircuitsAreNotReferencedAsFeeders(database, projectId, [
expectedCircuit.id,
]);
const deleted = database.delete(circuits).where(and(
eq(circuits.id, expectedCircuit.id),
eq(circuits.circuitListId, expectedCircuit.circuitListId)

View file

@ -270,6 +270,14 @@ function replaceProjectState(
database: AppDatabase,
state: ProjectStateSnapshot
) {
database
.update(distributionBoards)
.set({
feedingDistributionBoardId: null,
feedingCircuitId: null,
})
.where(eq(distributionBoards.projectId, state.project.id))
.run();
database
.delete(externalModelSources)
.where(eq(externalModelSources.projectId, state.project.id))
@ -330,7 +338,15 @@ function replaceProjectState(
}
insertMany(database, rooms, state.rooms);
insertMany(database, projectDevices, state.projectDevices);
insertMany(database, distributionBoards, state.distributionBoards);
insertMany(
database,
distributionBoards,
state.distributionBoards.map((board) => ({
...board,
feedingDistributionBoardId: null,
feedingCircuitId: null,
}))
);
insertMany(database, circuitLists, state.circuitLists);
insertMany(database, circuitSections, state.circuitSections);
insertMany(
@ -341,6 +357,7 @@ function replaceProjectState(
isReserve: circuit.isReserve ? 1 : 0,
}))
);
restoreDistributionBoardFeedingReferences(database, state);
insertMany(
database,
distributionBoardComponents,
@ -378,6 +395,31 @@ function replaceProjectState(
insertMany(database, externalModelObjects, state.externalModel.objects);
}
function restoreDistributionBoardFeedingReferences(
database: AppDatabase,
state: ProjectStateSnapshot
) {
for (const board of state.distributionBoards) {
if (
board.feedingDistributionBoardId === null &&
board.feedingCircuitId === null
) {
continue;
}
const updated = database
.update(distributionBoards)
.set({
feedingDistributionBoardId: board.feedingDistributionBoardId,
feedingCircuitId: board.feedingCircuitId,
})
.where(eq(distributionBoards.id, board.id))
.run();
if (updated.changes !== 1) {
throw new Error("Distribution-board feeding reference could not be restored.");
}
}
}
function insertMany<TTable extends Parameters<AppDatabase["insert"]>[0]>(
database: AppDatabase,
table: TTable,

View file

@ -173,7 +173,15 @@ function insertProjectState(
insertMany(database, floors, state.floors);
insertMany(database, rooms, state.rooms);
insertMany(database, projectDevices, state.projectDevices);
insertMany(database, distributionBoards, state.distributionBoards);
insertMany(
database,
distributionBoards,
state.distributionBoards.map((board) => ({
...board,
feedingDistributionBoardId: null,
feedingCircuitId: null,
}))
);
insertMany(database, circuitLists, state.circuitLists);
insertMany(database, circuitSections, state.circuitSections);
insertMany(
@ -184,6 +192,25 @@ function insertProjectState(
isReserve: circuit.isReserve ? 1 : 0,
}))
);
for (const board of state.distributionBoards) {
if (
board.feedingDistributionBoardId === null &&
board.feedingCircuitId === null
) {
continue;
}
const updated = database
.update(distributionBoards)
.set({
feedingDistributionBoardId: board.feedingDistributionBoardId,
feedingCircuitId: board.feedingCircuitId,
})
.where(eq(distributionBoards.id, board.id))
.run();
if (updated.changes !== 1) {
throw new Error("Distribution-board feeding reference could not be imported.");
}
}
insertMany(
database,
distributionBoardComponents,

View file

@ -1,4 +1,4 @@
import { sqliteTable, text, unique } from "drizzle-orm/sqlite-core";
import { sqliteTable, text, unique, type AnySQLiteColumn } from "drizzle-orm/sqlite-core";
import { distributionBoards } from "./distribution-boards.js";
import { projects } from "./projects.js";
@ -11,7 +11,7 @@ export const circuitLists = sqliteTable(
.references(() => projects.id, { onDelete: "cascade" }),
distributionBoardId: text("distribution_board_id")
.notNull()
.references(() => distributionBoards.id, { onDelete: "cascade" }),
.references((): AnySQLiteColumn => distributionBoards.id, { onDelete: "cascade" }),
name: text("name").notNull(),
},
(table) => [unique("circuit_lists_distribution_board_id_unique").on(table.distributionBoardId)]

View file

@ -1,4 +1,4 @@
import { index, integer, real, sqliteTable, text, unique } from "drizzle-orm/sqlite-core";
import { index, integer, real, sqliteTable, text, unique, type AnySQLiteColumn } from "drizzle-orm/sqlite-core";
import { circuitLists } from "./circuit-lists.js";
import { circuitSections } from "./circuit-sections.js";
@ -8,7 +8,7 @@ export const circuits = sqliteTable(
id: text("id").primaryKey(),
circuitListId: text("circuit_list_id")
.notNull()
.references(() => circuitLists.id, { onDelete: "cascade" }),
.references((): AnySQLiteColumn => circuitLists.id, { onDelete: "cascade" }),
sectionId: text("section_id")
.notNull()
.references(() => circuitSections.id, { onDelete: "cascade" }),

View file

@ -1,24 +1,44 @@
import { real, sqliteTable, text } from "drizzle-orm/sqlite-core";
import { index, real, sqliteTable, text, type AnySQLiteColumn } from "drizzle-orm/sqlite-core";
import { circuits } from "./circuits.js";
import { floors } from "./floors.js";
import { projects } from "./projects.js";
import type { DistributionBoardSupplyType } from "../../shared/constants/distribution-board.js";
export const distributionBoards = sqliteTable("distribution_boards", {
id: text("id").primaryKey(),
projectId: text("project_id")
.notNull()
.references(() => projects.id, { onDelete: "cascade" }),
name: text("name").notNull(),
floorId: text("floor_id").references(() => floors.id, {
onDelete: "set null",
}),
supplyType: text("supply_type").$type<DistributionBoardSupplyType>(),
simultaneityFactor: real("simultaneity_factor")
.notNull()
.default(1),
boardNumber: text("board_number"),
location: text("location"),
fedFrom: text("fed_from"),
remark: text("remark"),
});
export const distributionBoards = sqliteTable(
"distribution_boards",
{
id: text("id").primaryKey(),
projectId: text("project_id")
.notNull()
.references(() => projects.id, { onDelete: "cascade" }),
name: text("name").notNull(),
floorId: text("floor_id").references(() => floors.id, {
onDelete: "set null",
}),
supplyType: text("supply_type").$type<DistributionBoardSupplyType>(),
simultaneityFactor: real("simultaneity_factor")
.notNull()
.default(1),
boardNumber: text("board_number"),
location: text("location"),
fedFrom: text("fed_from"),
remark: text("remark"),
feedingDistributionBoardId: text("feeding_distribution_board_id").references(
(): AnySQLiteColumn => distributionBoards.id,
{ onDelete: "restrict" }
),
feedingCircuitId: text("feeding_circuit_id").references(
(): AnySQLiteColumn => circuits.id,
{ onDelete: "restrict" }
),
},
(table) => [
index("distribution_boards_feeding_board_idx").on(
table.feedingDistributionBoardId
),
index("distribution_boards_feeding_circuit_idx").on(
table.feedingCircuitId
),
]
);

View file

@ -0,0 +1,217 @@
export interface DistributionBoardLoadBoardInput {
id: string;
name: string;
boardNumber: string | null;
simultaneityFactor: number;
feedingDistributionBoardId: string | null;
feedingCircuitId: string | null;
}
export interface DistributionBoardLoadCircuitInput {
id: string;
distributionBoardId: string;
rows: Array<{
activePowerKw: number;
cosPhi: number | null;
}>;
}
export interface DownstreamDistributionBoardLoad {
distributionBoardId: string;
name: string;
boardNumber: string | null;
activePowerKw: number;
apparentPowerKva: number;
cosPhi: number;
}
export interface CalculatedCircuitLoad {
circuitId: string;
deviceActivePowerKw: number;
deviceApparentPowerKva: number;
activePowerKw: number;
apparentPowerKva: number;
cosPhi: number;
downstreamBoards: DownstreamDistributionBoardLoad[];
}
export interface CalculatedDistributionBoardLoad {
distributionBoardId: string;
activePowerBeforeSimultaneityKw: number;
apparentPowerBeforeSimultaneityKva: number;
demandActivePowerKw: number;
demandApparentPowerKva: number;
cosPhi: number;
}
export interface DistributionBoardLoadCalculation {
boards: Map<string, CalculatedDistributionBoardLoad>;
circuits: Map<string, CalculatedCircuitLoad>;
}
/**
* Calculates one project graph. A child board enters exactly one feeder
* circuit with its demand after the child's own board simultaneity factor.
* Board-only relations form a hierarchy but intentionally add no load.
*/
export function calculateDistributionBoardLoads(input: {
boards: DistributionBoardLoadBoardInput[];
circuits: DistributionBoardLoadCircuitInput[];
}): DistributionBoardLoadCalculation {
const boardById = new Map(input.boards.map((board) => [board.id, board]));
const circuitById = new Map(input.circuits.map((circuit) => [circuit.id, circuit]));
const circuitsByBoardId = groupBy(
input.circuits,
(circuit) => circuit.distributionBoardId
);
const childrenByCircuitId = new Map<string, DistributionBoardLoadBoardInput[]>();
for (const board of input.boards) {
assertBoardInput(board, boardById, circuitById);
if (board.feedingCircuitId === null) continue;
const children = childrenByCircuitId.get(board.feedingCircuitId) ?? [];
children.push(board);
childrenByCircuitId.set(board.feedingCircuitId, children);
}
const boards = new Map<string, CalculatedDistributionBoardLoad>();
const circuits = new Map<string, CalculatedCircuitLoad>();
const activePath = new Set<string>();
const calculateBoard = (boardId: string): CalculatedDistributionBoardLoad => {
const cached = boards.get(boardId);
if (cached) return cached;
if (activePath.has(boardId)) {
throw new Error("Distribution-board feeding graph contains a cycle.");
}
const board = boardById.get(boardId);
if (!board) throw new Error("Distribution board is missing from load graph.");
activePath.add(boardId);
let activePowerBeforeSimultaneityKw = 0;
let apparentPowerBeforeSimultaneityKva = 0;
for (const circuit of circuitsByBoardId.get(boardId) ?? []) {
let deviceActivePowerKw = 0;
let deviceApparentPowerKva = 0;
for (const row of circuit.rows) {
assertNonNegativeFinite(row.activePowerKw, "row active power");
const cosPhi = row.cosPhi ?? 1;
if (!Number.isFinite(cosPhi) || cosPhi <= 0 || cosPhi > 1) {
throw new Error("Row cos phi must be greater than zero and at most one.");
}
deviceActivePowerKw += row.activePowerKw;
deviceApparentPowerKva += row.activePowerKw / cosPhi;
}
const downstreamBoards = (childrenByCircuitId.get(circuit.id) ?? [])
.map((child) => {
const childLoad = calculateBoard(child.id);
return {
distributionBoardId: child.id,
name: child.name,
boardNumber: child.boardNumber,
activePowerKw: childLoad.demandActivePowerKw,
apparentPowerKva: childLoad.demandApparentPowerKva,
cosPhi: effectiveCosPhi(
childLoad.demandActivePowerKw,
childLoad.demandApparentPowerKva
),
};
});
const activePowerKw =
deviceActivePowerKw +
downstreamBoards.reduce((sum, child) => sum + child.activePowerKw, 0);
const apparentPowerKva =
deviceApparentPowerKva +
downstreamBoards.reduce((sum, child) => sum + child.apparentPowerKva, 0);
circuits.set(circuit.id, {
circuitId: circuit.id,
deviceActivePowerKw,
deviceApparentPowerKva,
activePowerKw,
apparentPowerKva,
cosPhi: effectiveCosPhi(activePowerKw, apparentPowerKva),
downstreamBoards,
});
activePowerBeforeSimultaneityKw += activePowerKw;
apparentPowerBeforeSimultaneityKva += apparentPowerKva;
}
activePath.delete(boardId);
const result = {
distributionBoardId: boardId,
activePowerBeforeSimultaneityKw,
apparentPowerBeforeSimultaneityKva,
demandActivePowerKw:
activePowerBeforeSimultaneityKw * board.simultaneityFactor,
demandApparentPowerKva:
apparentPowerBeforeSimultaneityKva * board.simultaneityFactor,
cosPhi: effectiveCosPhi(
activePowerBeforeSimultaneityKw,
apparentPowerBeforeSimultaneityKva
),
};
boards.set(boardId, result);
return result;
};
for (const board of input.boards) calculateBoard(board.id);
return { boards, circuits };
}
function assertBoardInput(
board: DistributionBoardLoadBoardInput,
boardById: Map<string, DistributionBoardLoadBoardInput>,
circuitById: Map<string, DistributionBoardLoadCircuitInput>
) {
if (
!Number.isFinite(board.simultaneityFactor) ||
board.simultaneityFactor < 0 ||
board.simultaneityFactor > 1
) {
throw new Error("Board simultaneity factor must be between zero and one.");
}
if (board.feedingDistributionBoardId === board.id) {
throw new Error("Distribution board cannot feed itself.");
}
if (
board.feedingDistributionBoardId !== null &&
!boardById.has(board.feedingDistributionBoardId)
) {
throw new Error("Feeding distribution board is missing from load graph.");
}
if (
board.feedingCircuitId !== null &&
board.feedingDistributionBoardId === null
) {
throw new Error("Feeding circuit requires a feeding distribution board.");
}
if (
board.feedingCircuitId !== null &&
circuitById.get(board.feedingCircuitId)?.distributionBoardId !==
board.feedingDistributionBoardId
) {
throw new Error("Feeding circuit belongs to another distribution board.");
}
}
function effectiveCosPhi(activePowerKw: number, apparentPowerKva: number) {
return activePowerKw > 0 && apparentPowerKva > 0
? activePowerKw / apparentPowerKva
: 1;
}
function assertNonNegativeFinite(value: number, label: string) {
if (!Number.isFinite(value) || value < 0) {
throw new Error(`${label} must be a non-negative finite number.`);
}
}
function groupBy<T>(values: T[], keyOf: (value: T) => string) {
const groups = new Map<string, T[]>();
for (const value of values) {
const key = keyOf(value);
const group = groups.get(key) ?? [];
group.push(value);
groups.set(key, group);
}
return groups;
}

View file

@ -40,7 +40,10 @@ export interface CircuitTreeCircuit {
isReserve: boolean;
remark?: string;
circuitTotalPower: number;
circuitApparentPower: number;
circuitCosPhi: number;
deviceRows: CircuitTreeDeviceRow[];
downstreamBoardLoads: CircuitTreeDownstreamBoardLoad[];
protectionDevice?: CircuitTreeProtectionDevice;
}
@ -59,6 +62,11 @@ export interface CircuitTreeSectionBlock {
export interface CircuitTreeResponse {
circuitListId: string;
distributionBoardId: string;
distributionBoardName: string;
distributionBoardNumber?: string;
distributionBoardSupplyType?: DistributionBoardSupplyType;
feedingReference?: CircuitTreeFeedingReference;
currentRevision: number;
singlePhaseVoltageV: number;
threePhaseVoltageV: number;
@ -70,7 +78,36 @@ export interface CircuitTreeResponse {
footerComponents: CircuitTreeComponent[];
}
export interface CircuitTreeDownstreamBoardLoad {
distributionBoardId: string;
name: string;
boardNumber?: string;
supplyType?: DistributionBoardSupplyType;
activePowerKw: number;
apparentPowerKva: number;
cosPhi: number;
}
export interface CircuitTreeFeedingReference {
distributionBoardId: string;
circuitListId: string;
distributionBoardName: string;
distributionBoardNumber?: string;
circuitId?: string;
circuitEquipmentIdentifier?: string;
circuitDisplayName?: string;
circuitCategory?: CircuitGroupCategory;
}
export interface ProjectCircuitTreesResponse {
currentRevision: number;
trees: CircuitTreeResponse[];
}
import type { CircuitGroupCategory } from "../../shared/constants/circuit-group.js";
import type {
DistributionBoardSupplyType,
} from "../../shared/constants/distribution-board.js";
import type {
DistributionBoardComponentPlacement,
DistributionBoardComponentRole,

View file

@ -9,16 +9,18 @@ import type { SerializedProjectCommand } from "./project-command.model.js";
export const distributionBoardUpdateCommandType =
"distribution-board.update" as const;
// The info-card fields were added additively to schema version 1: every
// previously stored command remains valid, and the inverse is still derived
// from the persisted row for exactly the changed fields.
export const distributionBoardUpdateCommandSchemaVersion = 1 as const;
// Version 2 adds logical feeding references. Version-1 commands remain
// executable and can only change the fields they originally knew.
export const distributionBoardUpdateCommandSchemaVersion = 2 as const;
const legacyDistributionBoardUpdateCommandSchemaVersion = 1 as const;
export interface DistributionBoardUpdateValues
extends Record<DistributionBoardInfoField, string | null> {
floorId: string | null;
supplyType: DistributionBoardSupplyType | null;
simultaneityFactor: number;
feedingDistributionBoardId: string | null;
feedingCircuitId: string | null;
}
export type DistributionBoardUpdateField =
@ -41,16 +43,20 @@ export interface DistributionBoardUpdateCommandPayload {
export interface DistributionBoardUpdateProjectCommand
extends SerializedProjectCommand<DistributionBoardUpdateCommandPayload> {
schemaVersion: typeof distributionBoardUpdateCommandSchemaVersion;
schemaVersion:
| typeof legacyDistributionBoardUpdateCommandSchemaVersion
| typeof distributionBoardUpdateCommandSchemaVersion;
type: typeof distributionBoardUpdateCommandType;
}
export function createDistributionBoardUpdateProjectCommand(
distributionBoardId: string,
patch: DistributionBoardUpdatePatch
patch: DistributionBoardUpdatePatch,
schemaVersion: DistributionBoardUpdateProjectCommand["schemaVersion"] =
distributionBoardUpdateCommandSchemaVersion
): DistributionBoardUpdateProjectCommand {
const command: DistributionBoardUpdateProjectCommand = {
schemaVersion: distributionBoardUpdateCommandSchemaVersion,
schemaVersion,
type: distributionBoardUpdateCommandType,
payload: {
distributionBoardId,
@ -68,7 +74,8 @@ export function assertDistributionBoardUpdateProjectCommand(
command: SerializedProjectCommand<unknown>
): asserts command is DistributionBoardUpdateProjectCommand {
if (
command.schemaVersion !== distributionBoardUpdateCommandSchemaVersion ||
(command.schemaVersion !== legacyDistributionBoardUpdateCommandSchemaVersion &&
command.schemaVersion !== distributionBoardUpdateCommandSchemaVersion) ||
command.type !== distributionBoardUpdateCommandType ||
!isPlainObject(command.payload)
) {
@ -93,7 +100,12 @@ export function assertDistributionBoardUpdateProjectCommand(
(change.field !== "floorId" &&
change.field !== "supplyType" &&
change.field !== "simultaneityFactor" &&
change.field !== "feedingDistributionBoardId" &&
change.field !== "feedingCircuitId" &&
!isInfoField(change.field)) ||
(command.schemaVersion === legacyDistributionBoardUpdateCommandSchemaVersion &&
(change.field === "feedingDistributionBoardId" ||
change.field === "feedingCircuitId")) ||
seen.has(change.field)
) {
throw new Error(
@ -105,7 +117,9 @@ export function assertDistributionBoardUpdateProjectCommand(
} else if (change.field === "floorId") {
if (
change.value !== null &&
(typeof change.value !== "string" || !change.value.trim())
(typeof change.value !== "string" ||
!change.value.trim() ||
change.value !== change.value.trim())
) {
throw new Error("floorId must be a non-empty string or null.");
}
@ -118,6 +132,16 @@ export function assertDistributionBoardUpdateProjectCommand(
) {
throw new Error("supplyType is invalid.");
}
} else if (
change.field === "feedingDistributionBoardId" ||
change.field === "feedingCircuitId"
) {
if (
change.value !== null &&
(typeof change.value !== "string" || !change.value.trim())
) {
throw new Error(`${change.field} must be a non-empty string or null.`);
}
} else if (
typeof change.value !== "number" ||
!Number.isFinite(change.value) ||

View file

@ -74,6 +74,8 @@ export interface DistributionBoardSubtreeSnapshot {
floorId: string | null;
supplyType: DistributionBoardSupplyType | null;
simultaneityFactor: number;
feedingDistributionBoardId: string | null;
feedingCircuitId: string | null;
} & Record<DistributionBoardInfoField, string | null>;
circuitList: {
id: string;
@ -184,6 +186,9 @@ export function normalizeDistributionBoardSubtreeSnapshot(
for (const field of distributionBoardInfoFields) {
board[field] = board[field] ?? null;
}
board.feedingDistributionBoardId =
board.feedingDistributionBoardId ?? null;
board.feedingCircuitId = board.feedingCircuitId ?? null;
return { ...snapshot, distributionBoard: board };
}
@ -227,6 +232,18 @@ export function assertDistributionBoardSubtreeSnapshot(
assertNullableString(board[field], `distributionBoard.${field}`);
}
}
if (board.feedingDistributionBoardId !== undefined) {
assertNullableString(
board.feedingDistributionBoardId,
"distributionBoard.feedingDistributionBoardId"
);
}
if (board.feedingCircuitId !== undefined) {
assertNullableString(
board.feedingCircuitId,
"distributionBoard.feedingCircuitId"
);
}
for (const field of [
"id",
"projectId",

View file

@ -35,7 +35,8 @@ import {
type ExternalModelStateSnapshot,
} from "../../external-model/domain/external-model-contracts.js";
export const projectStateSnapshotSchemaVersion = 8 as const;
export const projectStateSnapshotSchemaVersion = 9 as const;
const roomAreaProjectStateSnapshotSchemaVersion = 8 as const;
const buildingProjectStateSnapshotSchemaVersion = 7 as const;
const coverProjectStateSnapshotSchemaVersion = 6 as const;
const boardInfoProjectStateSnapshotSchemaVersion = 5 as const;
@ -112,7 +113,7 @@ const distributionBoardInfoValueSchema = (field: DistributionBoardInfoField) =>
.max(distributionBoardInfoFieldMaxLengths[field])
.nullable();
const distributionBoardSchema = legacyDistributionBoardSchema
const distributionBoardWithoutFeedingSchema = legacyDistributionBoardSchema
.extend({
boardNumber: distributionBoardInfoValueSchema("boardNumber"),
location: distributionBoardInfoValueSchema("location"),
@ -121,6 +122,13 @@ const distributionBoardSchema = legacyDistributionBoardSchema
})
.strict();
const distributionBoardSchema = distributionBoardWithoutFeedingSchema
.extend({
feedingDistributionBoardId: idSchema.nullable(),
feedingCircuitId: idSchema.nullable(),
})
.strict();
function withEmptyDistributionBoardInfo(
boards: Array<z.infer<typeof legacyDistributionBoardSchema>>
) {
@ -133,6 +141,16 @@ function withEmptyDistributionBoardInfo(
}));
}
function withEmptyDistributionBoardFeeding(
boards: Array<z.infer<typeof distributionBoardWithoutFeedingSchema>>
) {
return boards.map((board) => ({
...board,
feedingDistributionBoardId: null,
feedingCircuitId: null,
}));
}
const circuitListSchema = z
.object({
id: idSchema,
@ -553,11 +571,22 @@ export const projectStateSnapshotSchema = z
})
.strict();
const roomAreaProjectStateSnapshotSchema = z
.object({
schemaVersion: z.literal(roomAreaProjectStateSnapshotSchemaVersion),
project: projectSchema,
distributionBoards: z.array(distributionBoardWithoutFeedingSchema),
externalCsvConfiguration: externalCsvConfigurationSnapshotSchema.nullable(),
externalModel: externalModelStateSchema,
...projectStateSnapshotContents,
})
.strict();
const buildingProjectStateSnapshotSchema = z
.object({
schemaVersion: z.literal(buildingProjectStateSnapshotSchemaVersion),
project: projectSchema,
distributionBoards: z.array(distributionBoardSchema),
distributionBoards: z.array(distributionBoardWithoutFeedingSchema),
externalCsvConfiguration: externalCsvConfigurationSnapshotSchema.nullable(),
externalModel: externalModelStateSchema,
...buildingProjectStateSnapshotContents,
@ -568,7 +597,7 @@ const coverProjectStateSnapshotSchema = z
.object({
schemaVersion: z.literal(coverProjectStateSnapshotSchemaVersion),
project: projectSchema,
distributionBoards: z.array(distributionBoardSchema),
distributionBoards: z.array(distributionBoardWithoutFeedingSchema),
externalCsvConfiguration: externalCsvConfigurationSnapshotSchema.nullable(),
externalModel: externalModelStateSchema,
...previousProjectStateSnapshotContents,
@ -655,6 +684,27 @@ export function parseProjectStateSnapshot(
}
function upgradePreviousProjectStateSnapshot(value: unknown): unknown {
const roomArea = upgradeToRoomAreaProjectStateSnapshot(value);
if (
roomArea !== null &&
typeof roomArea === "object" &&
!Array.isArray(roomArea) &&
(roomArea as { schemaVersion?: unknown }).schemaVersion ===
roomAreaProjectStateSnapshotSchemaVersion
) {
const previous = roomAreaProjectStateSnapshotSchema.parse(roomArea);
return {
...previous,
schemaVersion: projectStateSnapshotSchemaVersion,
distributionBoards: withEmptyDistributionBoardFeeding(
previous.distributionBoards
),
};
}
return roomArea;
}
function upgradeToRoomAreaProjectStateSnapshot(value: unknown): unknown {
const cover = upgradeToCoverProjectStateSnapshot(value);
if (
cover !== null &&
@ -666,7 +716,7 @@ function upgradePreviousProjectStateSnapshot(value: unknown): unknown {
const previous = buildingProjectStateSnapshotSchema.parse(cover);
return {
...previous,
schemaVersion: projectStateSnapshotSchemaVersion,
schemaVersion: roomAreaProjectStateSnapshotSchemaVersion,
rooms: previous.rooms.map((room) => ({ ...room, areaM2: null })),
};
}
@ -683,7 +733,7 @@ function upgradePreviousProjectStateSnapshot(value: unknown): unknown {
// building. Build version 7 first so the room-area upgrade above remains a
// single exact transition.
const previous = coverProjectStateSnapshotSchema.parse(cover);
return upgradePreviousProjectStateSnapshot({
return upgradeToRoomAreaProjectStateSnapshot({
...previous,
schemaVersion: buildingProjectStateSnapshotSchemaVersion,
buildings: [],
@ -873,6 +923,15 @@ function assertProjectStateSnapshotRelations(
const sectionById = new Map(
snapshot.circuitSections.map((entry) => [entry.id, entry])
);
const boardById = new Map(
snapshot.distributionBoards.map((entry) => [entry.id, entry])
);
const circuitListById = new Map(
snapshot.circuitLists.map((entry) => [entry.id, entry])
);
const circuitById = new Map(
snapshot.circuits.map((entry) => [entry.id, entry])
);
const componentById = new Map(
snapshot.distributionBoardComponents.map((entry) => [
entry.id,
@ -901,6 +960,47 @@ function assertProjectStateSnapshotRelations(
"Snapshot distribution board uses a disabled supply type."
);
}
if (board.feedingDistributionBoardId === board.id) {
throw new Error("Snapshot distribution board cannot feed itself.");
}
if (board.feedingDistributionBoardId !== null) {
assertReference(
boardIds,
board.feedingDistributionBoardId,
"feeding distribution board"
);
}
if (board.feedingCircuitId !== null) {
if (board.feedingDistributionBoardId === null) {
throw new Error(
"Snapshot feeding circuit requires a feeding distribution board."
);
}
assertReference(circuitIds, board.feedingCircuitId, "feeding circuit");
const feedingCircuit = circuitById.get(board.feedingCircuitId);
const feedingList = feedingCircuit
? circuitListById.get(feedingCircuit.circuitListId)
: undefined;
if (
!feedingList ||
feedingList.distributionBoardId !== board.feedingDistributionBoardId
) {
throw new Error(
"Snapshot feeding circuit belongs to another distribution board."
);
}
}
}
for (const board of snapshot.distributionBoards) {
const visited = new Set<string>([board.id]);
let currentId = board.feedingDistributionBoardId;
while (currentId !== null) {
if (visited.has(currentId)) {
throw new Error("Snapshot distribution-board feeding graph contains a cycle.");
}
visited.add(currentId);
currentId = boardById.get(currentId)?.feedingDistributionBoardId ?? null;
}
}
for (const list of snapshot.circuitLists) {
assertProjectOwnership(list.projectId, projectId, "circuit list");

View file

@ -223,6 +223,14 @@ export function remapProjectState(
...board,
id: requiredId(boardIds, board.id),
projectId: targetProjectId,
feedingDistributionBoardId:
board.feedingDistributionBoardId === null
? null
: requiredId(boardIds, board.feedingDistributionBoardId),
feedingCircuitId:
board.feedingCircuitId === null
? null
: requiredId(circuitIds, board.feedingCircuitId),
floorId:
board.floorId === null
? null

View file

@ -22,6 +22,9 @@ export function deriveCircuitPhase(
* Rows without cos φ count as 1; a circuit without load yields 1.
*/
export function deriveCircuitCosPhi(circuit: CircuitTreeCircuitDto): number {
if (Number.isFinite(circuit.circuitCosPhi) && circuit.circuitCosPhi > 0) {
return Math.round(circuit.circuitCosPhi * 100) / 100;
}
let activePowerKw = 0;
let apparentPowerKva = 0;
for (const row of circuit.deviceRows) {

View file

@ -1,5 +1,6 @@
"use client";
import Link from "next/link";
import { DragEvent, KeyboardEvent, useEffect, useMemo, useRef, useState } from "react";
import {
ArrowDown,
@ -3293,6 +3294,23 @@ export function CircuitTreeEditor(props: { projectId: string; circuitListId: str
threePhaseVoltageV={data.threePhaseVoltageV}
/>
) : null}
{data.feedingReference ? (
<div className="alert alert-info py-2" role="status">
Gespeist von{" "}
<Link
href={`/projects/${projectId}/circuit-lists/${
data.feedingReference.circuitListId
}/tree-edit`}
>
{data.feedingReference.distributionBoardNumber ??
data.feedingReference.distributionBoardName}
{data.feedingReference.circuitEquipmentIdentifier ?? ""}
</Link>
{data.feedingReference.circuitId
? null
: " (speisender Stromkreis noch nicht festgelegt)"}
</div>
) : null}
<div className="editor-toolbar">
<IconButton
disabled={!historyState || historyState.undoDepth === 0 || historyBusy || isSaving}
@ -4062,6 +4080,8 @@ export function CircuitTreeEditor(props: { projectId: string; circuitListId: str
? "summary-row"
: row.rowType === "deviceRow"
? "device-row"
: row.rowType === "downstreamBoardLoad"
? "downstream-board-load-row"
: row.rowType === "reserveCircuit"
? "empty-circuit-row"
: row.rowType === "placeholder"
@ -4667,7 +4687,10 @@ export function CircuitTreeEditor(props: { projectId: string; circuitListId: str
: ""
}`}
>
{row.circuit && row.rowType !== "deviceRow" ? (
{row.circuit &&
(row.rowType === "circuitCompact" ||
row.rowType === "circuitSummary" ||
row.rowType === "reserveCircuit") ? (
<>
<IconButton
disabled={isSaving}

View file

@ -13,7 +13,7 @@ export type DistributionBoardInfoDraft = Record<DistributionBoardInfoField, stri
const placeholders: Record<DistributionBoardInfoField, string> = {
boardNumber: "z. B. UV 1.1",
location: "z. B. Raum 003 Technik",
fedFrom: "z. B. HV 0.1 oder HAK / Zähleranlage",
fedFrom: "z. B. HAK, Zähleranlage, Trafo oder NEA",
remark: "",
};
@ -98,8 +98,8 @@ export function DistributionBoardInfoCard({
)}
{field === "fedFrom" ? (
<div className="form-text">
Vorerst Freitext. Die logische Verknüpfung mit dem
speisenden Verteiler folgt.
Für externe Einspeisungen oder ergänzende Hinweise. Eine
speisende Projektverteilung wird oben logisch verknüpft.
</div>
) : null}
</div>

View file

@ -19,14 +19,14 @@ export interface PrintBoardSummary {
circuitCount: number;
/** Σ quantity × power per unit, without any simultaneity factor. */
installedPowerKw: number;
/** Σ row totals, i.e. with the device-row simultaneity factors. */
devicePowerKw: number;
/** Device-row demand plus downstream-board demand before this board's GZF. */
powerBeforeBoardSimultaneityKw: number;
simultaneityFactor: number;
/** Device power with the board simultaneity factor applied. */
/** Total active power with this board's simultaneity factor applied. */
demandPowerKw: number;
/**
* Symmetrical three-phase design current from the apparent power
* S = Σ(row total / cos φ) × board GZF. A missing cos φ counts as 1.
* Design current from the apparent power including downstream boards.
* The selected feeder circuit determines whether one or three phases apply.
*/
designCurrentA: number;
}
@ -46,8 +46,13 @@ export function summarizePrintBoard(input: {
circuitCount += 1;
for (const row of circuit.deviceRows) {
installedPowerKw += row.quantity * row.powerPerUnit;
apparentPowerKva += row.rowTotalPower / (row.cosPhi || 1);
}
apparentPowerKva +=
circuit.circuitApparentPower ??
circuit.deviceRows.reduce(
(sum, row) => sum + row.rowTotalPower / (row.cosPhi || 1),
0
);
}
}
const simultaneityFactor = tree.distributionBoardSimultaneityFactor;
@ -55,12 +60,13 @@ export function summarizePrintBoard(input: {
...input,
circuitCount,
installedPowerKw,
devicePowerKw: tree.distributionBoardTotalPower,
powerBeforeBoardSimultaneityKw: tree.distributionBoardTotalPower,
simultaneityFactor,
demandPowerKw: tree.distributionBoardTotalPowerWithSimultaneityFactor,
designCurrentA:
(apparentPowerKva * simultaneityFactor * 1000) /
(Math.sqrt(3) * tree.threePhaseVoltageV),
designCurrentA: calculateDesignCurrent(
apparentPowerKva * simultaneityFactor,
tree
),
};
}
@ -68,8 +74,6 @@ export interface PrintBoardHierarchyEntry {
summary: PrintBoardSummary;
depth: number;
parentBoardId: string | null;
/** Own demand plus the plain sum of all resolved downstream boards. */
cumulatedDemandPowerKw: number;
}
export function assertPrintRevisionConsistency(
@ -82,31 +86,21 @@ export function assertPrintRevisionConsistency(
}
}
/**
* PRELIMINARY: `fedFrom` is free text until the logical feed link is
* decided. A board is shown below another board only when its `fedFrom`
* text equals that board's number or name (case-insensitive). The
* cumulated value is a plain sum without an upstream simultaneity factor.
* Unresolved texts and cycles keep a board at the top level.
*/
export function buildPreliminaryBoardHierarchy(
export function buildBoardHierarchy(
summaries: PrintBoardSummary[]
): PrintBoardHierarchyEntry[] {
const normalize = (value: string | null) =>
value?.trim().toLocaleLowerCase("de") || null;
const summaryById = new Map(
summaries.map((summary) => [summary.board.id, summary])
);
const parentById = new Map<string, string | null>();
for (const summary of summaries) {
const fedFrom = normalize(summary.board.fedFrom);
const candidates = fedFrom
? summaries.filter(
(candidate) =>
candidate !== summary &&
(normalize(candidate.board.boardNumber) === fedFrom ||
normalize(candidate.board.name) === fedFrom)
)
: [];
const parent = candidates.length === 1 ? candidates[0] : undefined;
parentById.set(summary.board.id, parent?.board.id ?? null);
const parentId = summary.board.feedingDistributionBoardId;
parentById.set(
summary.board.id,
parentId && parentId !== summary.board.id && summaryById.has(parentId)
? parentId
: null
);
}
for (const summary of summaries) {
const visited = new Set<string>([summary.board.id]);
@ -125,20 +119,12 @@ export function buildPreliminaryBoardHierarchy(
summaries.filter(
(summary) => parentById.get(summary.board.id) === boardId
);
const cumulated = (summary: PrintBoardSummary): number =>
summary.demandPowerKw +
childrenOf(summary.board.id).reduce(
(sum, child) => sum + cumulated(child),
0
);
const entries: PrintBoardHierarchyEntry[] = [];
const walk = (summary: PrintBoardSummary, depth: number) => {
entries.push({
summary,
depth,
parentBoardId: parentById.get(summary.board.id) ?? null,
cumulatedDemandPowerKw: cumulated(summary),
});
childrenOf(summary.board.id).forEach((child) => walk(child, depth + 1));
};
@ -146,6 +132,25 @@ export function buildPreliminaryBoardHierarchy(
return entries;
}
export function formatFeedingReference(summary: PrintBoardSummary): string {
const reference = summary.tree.feedingReference;
if (!reference) return summary.board.fedFrom ?? "–";
return `${
reference.distributionBoardNumber ?? reference.distributionBoardName
}${reference.circuitEquipmentIdentifier ?? ""}`;
}
function calculateDesignCurrent(
apparentPowerKva: number,
tree: CircuitTreeResponseDto
) {
const category = tree.feedingReference?.circuitCategory;
return category === "lighting" || category === "single_phase"
? (apparentPowerKva * 1000) / tree.singlePhaseVoltageV
: (apparentPowerKva * 1000) /
(Math.sqrt(3) * tree.threePhaseVoltageV);
}
const kilowattFormat = new Intl.NumberFormat("de-DE", {
minimumFractionDigits: 2,
maximumFractionDigits: 2,

View file

@ -13,6 +13,7 @@ import {
formatCircuitCable,
formatCurrent,
formatFactor,
formatFeedingReference,
formatKilowatt,
formatProtectionDevice,
formatUnitPower,
@ -269,8 +270,8 @@ function OverviewSheet({
stand: string;
}) {
const roots = entries.filter((entry) => entry.parentBoardId === null);
// Top-level boards whose "fed from" text names no other board are grouped
// below that text as an external feed (e.g. "HAK / Zähleranlage").
// Root boards may still carry an external feed description such as
// "HAK / Zähleranlage"; logical board links determine the actual tree.
const feedGroups = new Map<string, PrintBoardHierarchyEntry[]>();
for (const root of roots) {
const feed = root.summary.board.fedFrom ?? "";
@ -320,11 +321,10 @@ function OverviewSheet({
<th>Nr.</th><th>Bezeichnung</th><th>Örtlichkeit</th><th>Etage</th><th>Netz</th><th>Gespeist von</th>
<th className="pd-r">SK</th>
<th className="pd-r">P<sub>inst</sub><br />[kW]</th>
<th className="pd-r">P<sub>Geräte</sub><br />[kW]</th>
<th className="pd-r">P vor Vert.-GZF<br />[kW]</th>
<th className="pd-r">GZF<br />Vert.</th>
<th className="pd-r">P<sub>gl</sub><br />[kW]</th>
<th className="pd-r">P<sub>gl</sub> inkl. UV<br />[kW]</th>
<th className="pd-r">I<sub>B</sub><br />[A]</th>
<th className="pd-r">Σ inkl.<br />unterlagert [kW]</th>
</tr>
</thead>
<tbody>
@ -340,41 +340,45 @@ function OverviewSheet({
<td>{board.location ?? "–"}</td>
<td>{entry.summary.floorName ?? "–"}</td>
<td>{board.supplyType ?? "–"}</td>
<td>{board.fedFrom ?? "–"}</td>
<td>{formatFeedingReference(entry.summary)}</td>
<td className="pd-r">{entry.summary.circuitCount}</td>
<td className="pd-r">{formatKilowatt(entry.summary.installedPowerKw)}</td>
<td className="pd-r">{formatKilowatt(entry.summary.devicePowerKw)}</td>
<td className="pd-r">{formatKilowatt(entry.summary.powerBeforeBoardSimultaneityKw)}</td>
<td className="pd-r">{formatFactor(entry.summary.simultaneityFactor)}</td>
<td className="pd-r"><strong>{formatKilowatt(entry.summary.demandPowerKw)}</strong></td>
<td className="pd-r">{formatCurrent(entry.summary.designCurrentA)}</td>
<td className="pd-r">{formatKilowatt(entry.cumulatedDemandPowerKw)}</td>
</tr>
);
})}
</tbody>
<tfoot>
<tr>
<td colSpan={7}>Summe aller Verteiler (ohne übergeordneten GZF)</td>
<td colSpan={7}>Summe der Einspeisungen</td>
<td className="pd-r">{formatKilowatt(sum((entry) => entry.summary.installedPowerKw))}</td>
<td className="pd-r">{formatKilowatt(sum((entry) => entry.summary.devicePowerKw))}</td>
<td />
<td className="pd-r">{formatKilowatt(sum((entry) => entry.summary.demandPowerKw))}</td>
<td className="pd-r">{formatKilowatt(sum((entry) =>
entry.parentBoardId === null
? entry.summary.powerBeforeBoardSimultaneityKw
: 0
))}</td>
<td />
<td className="pd-r">{formatKilowatt(sum((entry) =>
entry.parentBoardId === null ? entry.summary.demandPowerKw : 0
))}</td>
<td />
</tr>
</tfoot>
</table>
<p className="pd-note">
<strong>Vorläufig:</strong> „Gespeist von“ ist noch ein Freitextfeld. Struktur und
„Σ inkl. unterlagert“ werden nur über eindeutig passende Verteilernummern bzw.
-bezeichnungen zugeordnet und einfach addiert (kein übergeordneter GZF).
Mehrdeutige Angaben bleiben ohne Zuordnung. Die fachliche
Verknüpfung und Summierung folgt.
Die Struktur folgt den logischen Verknüpfungen. Eine Unterverteilung
wird im gewählten Abgang mit ihrer Leistung nach eigenem
Verteiler-GZF berücksichtigt. Ohne gewählten Abgang erscheint sie
nur in der Hierarchie.
</p>
<p className="pd-legend">
P<sub>inst</sub> = Anzahl × Leistung je Stück · P<sub>Geräte</sub> = mit Geräte-GZF ·
P<sub>gl</sub> = mit Verteiler-GZF · I<sub>B</sub> = S / (√3 · {project.threePhaseVoltageV} V),
S aus cos φ je Gerätezeile
P<sub>inst</sub> = lokale Geräte ohne GZF · P vor Vert.-GZF =
Geräte-GZF einschließlich gespeister Unterverteilungen · P<sub>gl</sub> =
zusätzlich mit Verteiler-GZF · I<sub>B</sub> aus S und der Phasenart
des speisenden Abgangs
</p>
</div>
</div>
@ -404,6 +408,7 @@ function ComponentRow({ component }: { component: CircuitTreeComponentDto }) {
function CircuitRows({ circuit }: { circuit: CircuitTreeCircuitDto }) {
const rows = circuit.deviceRows;
const downstreamBoards = circuit.downstreamBoardLoads ?? [];
const circuitCells = (
<>
<td>{formatProtectionDevice(circuit.protectionDevice) ?? <Open />}</td>
@ -414,7 +419,7 @@ function CircuitRows({ circuit }: { circuit: CircuitTreeCircuitDto }) {
<td>{circuit.remark ?? ""}</td>
</>
);
if (rows.length === 0) {
if (rows.length === 0 && downstreamBoards.length === 0) {
return (
<tr className="pd-circuit pd-reserve">
<td className="pd-bmk">{circuit.equipmentIdentifier}</td>
@ -426,7 +431,7 @@ function CircuitRows({ circuit }: { circuit: CircuitTreeCircuitDto }) {
</tr>
);
}
if (rows.length === 1) {
if (rows.length === 1 && downstreamBoards.length === 0) {
const row = rows[0];
const title = circuit.displayName ?? row.displayName;
return (
@ -468,6 +473,16 @@ function CircuitRows({ circuit }: { circuit: CircuitTreeCircuitDto }) {
<td colSpan={4} />
</tr>
))}
{downstreamBoards.map((load) => (
<tr className="pd-device" key={`downstream:${load.distributionBoardId}`}>
<td />
<td>Unterverteilung {load.boardNumber ?? load.name}</td>
<td>–</td>
<td /><td /><td />
<td className="pd-r">{formatKilowatt(load.activePowerKw)}</td>
<td colSpan={4}>Automatisch aus der verknüpften Verteilung</td>
</tr>
))}
</>
);
}
@ -501,7 +516,7 @@ function BoardPlanSheet({
rows={[
["Örtlichkeit", [board.location, summary.floorName].filter(Boolean).join(" · ") || "–"],
["Netzart", board.supplyType ?? "–"],
["Gespeist von", board.fedFrom ?? "–"],
["Gespeist von", formatFeedingReference(summary)],
[
<>P<sub>gl</sub></>,
<>
@ -542,7 +557,7 @@ function BoardPlanSheet({
<td colSpan={5}>
Summe Verteiler (P<sub>inst</sub> {formatKilowatt(summary.installedPowerKw)} kW)
</td>
<td className="pd-r">{formatKilowatt(summary.devicePowerKw)}</td>
<td className="pd-r">{formatKilowatt(summary.powerBeforeBoardSimultaneityKw)}</td>
<td colSpan={4} />
</tr>
<tr>

View file

@ -195,6 +195,8 @@ export interface DistributionBoardDto extends DistributionBoardInfoDto {
floorId: string | null;
supplyType: DistributionBoardSupplyType | null;
simultaneityFactor: number;
feedingDistributionBoardId: string | null;
feedingCircuitId: string | null;
}
export interface DocumentLogoMetadataDto {
@ -442,7 +444,10 @@ export interface CircuitTreeCircuitDto {
isReserve: boolean;
remark?: string;
circuitTotalPower: number;
circuitApparentPower: number;
circuitCosPhi: number;
deviceRows: CircuitTreeDeviceRowDto[];
downstreamBoardLoads: CircuitTreeDownstreamBoardLoadDto[];
protectionDevice?: CircuitTreeProtectionDeviceDto;
}
@ -461,6 +466,11 @@ export interface CircuitTreeSectionDto {
export interface CircuitTreeResponseDto {
circuitListId: string;
distributionBoardId: string;
distributionBoardName: string;
distributionBoardNumber?: string;
distributionBoardSupplyType?: DistributionBoardSupplyType;
feedingReference?: CircuitTreeFeedingReferenceDto;
currentRevision: number;
singlePhaseVoltageV: number;
threePhaseVoltageV: number;
@ -472,6 +482,32 @@ export interface CircuitTreeResponseDto {
footerComponents: CircuitTreeComponentDto[];
}
export interface CircuitTreeDownstreamBoardLoadDto {
distributionBoardId: string;
name: string;
boardNumber?: string;
supplyType?: DistributionBoardSupplyType;
activePowerKw: number;
apparentPowerKva: number;
cosPhi: number;
}
export interface CircuitTreeFeedingReferenceDto {
distributionBoardId: string;
circuitListId: string;
distributionBoardName: string;
distributionBoardNumber?: string;
circuitId?: string;
circuitEquipmentIdentifier?: string;
circuitDisplayName?: string;
circuitCategory?: CircuitGroupCategory;
}
export interface ProjectCircuitTreesResponseDto {
currentRevision: number;
trees: CircuitTreeResponseDto[];
}
export interface ExternalCircuitListObjectDto {
id: string;
ifcGuid: string;

View file

@ -33,6 +33,7 @@ import type {
ProjectDto,
RoomDto,
CircuitTreeResponseDto,
ProjectCircuitTreesResponseDto,
ExternalCsvConfigurationSnapshotDto,
ExternalCsvPreviewDto,
ExternalInitialImportPlanDto,
@ -372,7 +373,13 @@ export function updateDistributionBoard(
floorId: string | null;
supplyType: NonNullable<DistributionBoardDto["supplyType"]>;
simultaneityFactor: number;
} & Partial<DistributionBoardInfoDto>,
} & Partial<DistributionBoardInfoDto> &
Partial<
Pick<
DistributionBoardDto,
"feedingDistributionBoardId" | "feedingCircuitId"
>
>,
expectedRevision: number
) {
return request<DistributionBoardCommandResultDto>(
@ -523,6 +530,12 @@ export function getCircuitTree(projectId: string, circuitListId: string) {
return request<CircuitTreeResponseDto>(`/api/projects/${projectId}/circuit-lists/${circuitListId}/tree`);
}
export function getProjectCircuitTrees(projectId: string) {
return request<ProjectCircuitTreesResponseDto>(
`/api/projects/${projectId}/circuit-trees`
);
}
export function insertDistributionBoardComponentCommand(
projectId: string,
expectedRevision: number,

View file

@ -44,6 +44,7 @@ export type RowType =
| "circuitCompact"
| "circuitSummary"
| "deviceRow"
| "downstreamBoardLoad"
| "reserveCircuit"
| "placeholder"
| "footerComponent";
@ -614,6 +615,7 @@ export function getCellKind(rowType: RowType, key: CellKey): CellKind {
return "readonly";
}
if (rowType === "deviceRow") return deviceFieldKeys.has(key) ? "deviceField" : "readonly";
if (rowType === "downstreamBoardLoad") return "readonly";
if (rowType === "circuitSummary") return circuitFieldKeys.has(key) ? "circuitField" : "readonly";
if (rowType === "reserveCircuit" || rowType === "circuitCompact") {
if (deviceFieldKeys.has(key)) return "deviceField";

View file

@ -2,6 +2,7 @@ import type {
CircuitTreeCircuitDto,
CircuitTreeComponentDto,
CircuitTreeDeviceRowDto,
CircuitTreeDownstreamBoardLoadDto,
CircuitTreeSectionDto,
} from "../types.js";
import { buildCircuitStructureProjection } from "./circuit-structure-projection";
@ -45,6 +46,7 @@ export interface VisibleGridRow {
circuit?: CircuitTreeCircuitDto;
device?: CircuitTreeDeviceRowDto;
component?: CircuitTreeComponentDto;
downstreamBoardLoad?: CircuitTreeDownstreamBoardLoadDto;
cells: VisibleGridCell[];
}
@ -126,13 +128,16 @@ function makeVisibleGridRow(
rowType: RowType,
sectionId: string,
circuit?: CircuitTreeCircuitDto,
device?: CircuitTreeDeviceRowDto
device?: CircuitTreeDeviceRowDto,
downstreamBoardLoad?: CircuitTreeDownstreamBoardLoadDto
): VisibleGridRow {
const rowKey =
rowType === "placeholder"
? `placeholder:${sectionId}`
: rowType === "deviceRow" && device
? `device:${device.id}`
: rowType === "downstreamBoardLoad" && downstreamBoardLoad
? `downstream-board:${circuit?.id}:${downstreamBoardLoad.distributionBoardId}`
: `${rowType}:${circuit?.id ?? sectionId}`;
const cells = allColumns.map((column): VisibleGridCell => {
@ -140,7 +145,17 @@ function makeVisibleGridRow(
const editable = kind === "circuitField" || kind === "deviceField";
let value: GridValue;
if (rowType === "placeholder") {
if (rowType === "downstreamBoardLoad" && downstreamBoardLoad) {
value = column.key === "displayName"
? `Unterverteilung ${downstreamBoardLoad.boardNumber ?? downstreamBoardLoad.name}`
: column.key === "rowTotalPower"
? downstreamBoardLoad.activePowerKw
: column.key === "cosPhi"
? downstreamBoardLoad.cosPhi
: column.key === "remark"
? "Automatisch aus der verknüpften Verteilung"
: undefined;
} else if (rowType === "placeholder") {
value = column.key === "equipmentIdentifier" ? "-frei-" : undefined;
} else if (kind === "deviceField" && device) {
value = getDeviceValue(device, column.key);
@ -166,7 +181,15 @@ function makeVisibleGridRow(
return { cellKey: column.key, editable, kind, value };
});
return { rowKey, rowType, sectionId, circuit, device, cells };
return {
rowKey,
rowType,
sectionId,
circuit,
device,
downstreamBoardLoad,
cells,
};
}
export function buildVisibleGridRowsWithStructure(
@ -221,15 +244,27 @@ export function buildVisibleGridRowsWithStructure(
}
if (row.rowType === "circuitBlock") {
const circuit = row.circuit;
if (circuit.deviceRows.length === 0) {
const downstreamLoads = circuit.downstreamBoardLoads ?? [];
if (circuit.deviceRows.length === 0 && downstreamLoads.length === 0) {
rows.push(makeVisibleGridRow("reserveCircuit", section.id, circuit));
} else if (circuit.deviceRows.length === 1) {
} else if (circuit.deviceRows.length === 1 && downstreamLoads.length === 0) {
rows.push(makeVisibleGridRow("circuitCompact", section.id, circuit, circuit.deviceRows[0]));
} else {
rows.push(makeVisibleGridRow("circuitSummary", section.id, circuit));
for (const device of circuit.deviceRows) {
rows.push(makeVisibleGridRow("deviceRow", section.id, circuit, device));
}
for (const load of downstreamLoads) {
rows.push(
makeVisibleGridRow(
"downstreamBoardLoad",
section.id,
circuit,
undefined,
load
)
);
}
}
continue;
}

View file

@ -1,13 +1,11 @@
import type { Request, Response } from "express";
import {
applyDistributionBoardSimultaneityFactor,
calculateCircuitTotalPower,
calculateDistributionBoardTotalPower,
calculateRowTotalPower,
calculateSectionTotalPower,
} from "../../domain/calculations/circuit-power-calculation.js";
import type { CircuitTreeResponse } from "../../domain/models/circuit-tree.model.js";
import type { CircuitTreeProtectionDevice } from "../../domain/models/circuit-tree.model.js";
import { calculateDistributionBoardLoads } from "../../domain/calculations/distribution-board-load-calculation.js";
import { calculateRowTotalPower } from "../../domain/calculations/circuit-power-calculation.js";
import type {
CircuitTreeProtectionDevice,
CircuitTreeResponse,
ProjectCircuitTreesResponse,
} from "../../domain/models/circuit-tree.model.js";
import {
circuitDeviceRowRepository,
circuitListRepository,
@ -20,9 +18,7 @@ import {
} from "../composition/application-repositories.js";
export function isMissingCircuitTreeSchemaError(error: unknown): boolean {
if (!(error instanceof Error)) {
return false;
}
if (!(error instanceof Error)) return false;
return (
error.message.includes("no such table: circuit_sections") ||
error.message.includes("no such table: circuits") ||
@ -37,70 +33,147 @@ export async function getCircuitTree(req: Request, res: Response) {
if (typeof projectId !== "string" || typeof circuitListId !== "string") {
return res.status(400).json({ error: "Invalid parameters" });
}
const list = await circuitListRepository.findById(projectId, circuitListId);
if (!list) {
return res.status(404).json({ error: "Circuit list not found" });
}
const project = await projectRepository.findById(projectId);
if (!project) {
return res.status(404).json({ error: "Project not found" });
}
const distributionBoard =
await distributionBoardRepository.findById(
projectId,
list.distributionBoardId
);
if (!distributionBoard) {
return res.status(404).json({
error: "Distribution board not found",
});
}
if (!list) return res.status(404).json({ error: "Circuit list not found" });
try {
const sections = await circuitSectionRepository.listByCircuitList(circuitListId);
const circuits = await circuitRepository.listByCircuitList(circuitListId);
const rows = await circuitDeviceRowRepository.listByCircuitList(circuits.map((entry) => entry.id));
const components =
await distributionBoardComponentRepository.listByCircuitList(
circuitListId
);
const [circuitProtections, componentProtections] =
await Promise.all([
circuitProtectionDeviceRepository.listByCircuitIds(
circuits.map((entry) => entry.id)
),
distributionBoardComponentRepository.listProtectionByComponentIds(
components.map((entry) => entry.id)
),
]);
const sectionById = new Map(sections.map((section) => [section.id, section]));
const rowsByCircuitId = new Map<string, typeof rows>();
const circuitProtectionById = new Map(
circuitProtections.map((entry) => [entry.circuitId, entry])
);
const componentProtectionById = new Map(
componentProtections.map((entry) => [entry.componentId, entry])
);
for (const row of rows) {
if (!rowsByCircuitId.has(row.circuitId)) {
rowsByCircuitId.set(row.circuitId, []);
}
rowsByCircuitId.get(row.circuitId)!.push(row);
const result = await buildProjectCircuitTrees(projectId);
if (!result) return res.status(404).json({ error: "Project not found" });
const tree = result.trees.find((entry) => entry.circuitListId === circuitListId);
if (!tree) {
return res.status(404).json({ error: "Distribution board not found" });
}
return res.json(tree);
} catch (error) {
if (isMissingCircuitTreeSchemaError(error)) {
const project = await projectRepository.findById(projectId);
const board = await distributionBoardRepository.findById(
projectId,
list.distributionBoardId
);
if (!project || !board) {
return res.status(404).json({ error: "Project structure not found" });
}
return res.json(emptyCircuitTree(project, board, circuitListId));
}
throw error;
}
}
export async function getProjectCircuitTrees(req: Request, res: Response) {
const { projectId } = req.params;
if (typeof projectId !== "string") {
return res.status(400).json({ error: "Invalid parameters" });
}
try {
const result = await buildProjectCircuitTrees(projectId);
if (!result) return res.status(404).json({ error: "Project not found" });
return res.json(result);
} catch (error) {
if (isMissingCircuitTreeSchemaError(error)) {
const project = await projectRepository.findById(projectId);
if (!project) return res.status(404).json({ error: "Project not found" });
return res.json({ currentRevision: project.currentRevision, trees: [] });
}
throw error;
}
}
export async function buildProjectCircuitTrees(
projectId: string
): Promise<ProjectCircuitTreesResponse | null> {
const project = await projectRepository.findById(projectId);
if (!project) return null;
const [lists, boards] = await Promise.all([
circuitListRepository.listByProject(projectId),
distributionBoardRepository.listByProject(projectId),
]);
const listIds = lists.map((entry) => entry.id);
const [sections, circuits, components] = await Promise.all([
circuitSectionRepository.listByCircuitLists(listIds),
circuitRepository.listByCircuitLists(listIds),
distributionBoardComponentRepository.listByCircuitLists(listIds),
]);
const [rows, circuitProtections, componentProtections] = await Promise.all([
circuitDeviceRowRepository.listByCircuitList(circuits.map((entry) => entry.id)),
circuitProtectionDeviceRepository.listByCircuitIds(circuits.map((entry) => entry.id)),
distributionBoardComponentRepository.listProtectionByComponentIds(
components.map((entry) => entry.id)
),
]);
const boardById = new Map(boards.map((board) => [board.id, board]));
const listById = new Map(lists.map((list) => [list.id, list]));
const listByBoardId = new Map(
lists.map((list) => [list.distributionBoardId, list])
);
const sectionById = new Map(sections.map((section) => [section.id, section]));
const circuitById = new Map(circuits.map((circuit) => [circuit.id, circuit]));
const rowsByCircuitId = groupBy(rows, (row) => row.circuitId);
const sectionsByListId = groupBy(sections, (section) => section.circuitListId);
const circuitsByListId = groupBy(circuits, (circuit) => circuit.circuitListId);
const componentsByListId = groupBy(components, (component) => component.circuitListId);
const circuitProtectionById = new Map(
circuitProtections.map((entry) => [entry.circuitId, entry])
);
const componentProtectionById = new Map(
componentProtections.map((entry) => [entry.componentId, entry])
);
const loads = calculateDistributionBoardLoads({
boards: boards.map((board) => ({
id: board.id,
name: board.name,
boardNumber: board.boardNumber,
simultaneityFactor: board.simultaneityFactor,
feedingDistributionBoardId: board.feedingDistributionBoardId,
feedingCircuitId: board.feedingCircuitId,
})),
circuits: circuits.map((circuit) => {
const list = listById.get(circuit.circuitListId);
if (!list) throw new Error("Circuit list is missing from project tree.");
return {
id: circuit.id,
distributionBoardId: list.distributionBoardId,
rows: (rowsByCircuitId.get(circuit.id) ?? []).map((row) => ({
activePowerKw: calculateRowTotalPower(
row.quantity,
row.powerPerUnit,
row.simultaneityFactor
),
cosPhi: row.cosPhi,
})),
};
}),
});
const trees = lists.map((list): CircuitTreeResponse => {
const board = boardById.get(list.distributionBoardId);
if (!board) throw new Error("Distribution board is missing from project tree.");
const boardLoad = loads.boards.get(board.id);
if (!boardLoad) throw new Error("Distribution-board load is missing.");
const tree: CircuitTreeResponse = {
circuitListId,
circuitListId: list.id,
distributionBoardId: board.id,
distributionBoardName: board.name,
distributionBoardNumber: board.boardNumber ?? undefined,
distributionBoardSupplyType: board.supplyType ?? undefined,
feedingReference: resolveFeedingReference(
board,
boardById,
listByBoardId,
circuitById,
sectionById
),
currentRevision: project.currentRevision,
singlePhaseVoltageV: project.singlePhaseVoltageV,
threePhaseVoltageV: project.threePhaseVoltageV,
distributionBoardSimultaneityFactor:
distributionBoard.simultaneityFactor,
distributionBoardTotalPower: 0,
distributionBoardTotalPowerWithSimultaneityFactor: 0,
distributionBoardSimultaneityFactor: board.simultaneityFactor,
distributionBoardTotalPower: boardLoad.activePowerBeforeSimultaneityKw,
distributionBoardTotalPowerWithSimultaneityFactor:
boardLoad.demandActivePowerKw,
headerComponents: [],
sections: sections.map((section) => ({
sections: (sectionsByListId.get(list.id) ?? []).map((section) => ({
id: section.id,
key: section.key,
displayName: section.displayName,
@ -115,9 +188,8 @@ export async function getCircuitTree(req: Request, res: Response) {
footerComponents: [],
};
const sectionBlocks = new Map(tree.sections.map((section) => [section.id, section]));
for (const component of components) {
const protection =
componentProtectionById.get(component.id);
for (const component of componentsByListId.get(list.id) ?? []) {
const protection = componentProtectionById.get(component.id);
const dto = {
id: component.id,
circuitListId: component.circuitListId,
@ -127,24 +199,18 @@ export async function getCircuitTree(req: Request, res: Response) {
role: component.role,
placement: component.placement,
sortOrder: component.sortOrder,
protectionDevice: protection
? toProtectionDeviceDto(protection)
: undefined,
protectionDevice: protection ? toProtectionDeviceDto(protection) : undefined,
};
if (component.placement === "header") {
tree.headerComponents.push(dto);
} else if (component.placement === "footer") {
tree.footerComponents.push(dto);
} else if (component.sectionId !== null) {
if (component.placement === "header") tree.headerComponents.push(dto);
else if (component.placement === "footer") tree.footerComponents.push(dto);
else if (component.sectionId !== null) {
sectionBlocks.get(component.sectionId)?.components.push(dto);
}
}
for (const circuit of circuits) {
const section = sectionById.get(circuit.sectionId);
if (!section || section.circuitListId !== circuit.circuitListId) {
continue;
}
for (const circuit of circuitsByListId.get(list.id) ?? []) {
const block = sectionBlocks.get(circuit.sectionId);
if (!block) continue;
const deviceRows = (rowsByCircuitId.get(circuit.id) ?? []).map((row) => ({
id: row.id,
linkedProjectDeviceId: row.linkedProjectDeviceId ?? undefined,
@ -166,12 +232,16 @@ export async function getCircuitTree(req: Request, res: Response) {
cosPhi: row.cosPhi ?? undefined,
remark: row.remark ?? undefined,
overriddenFields: row.overriddenFields ?? undefined,
rowTotalPower: calculateRowTotalPower(row.quantity, row.powerPerUnit, row.simultaneityFactor),
rowTotalPower: calculateRowTotalPower(
row.quantity,
row.powerPerUnit,
row.simultaneityFactor
),
}));
const circuitTotalPower = calculateCircuitTotalPower(deviceRows);
const circuitLoad = loads.circuits.get(circuit.id);
if (!circuitLoad) throw new Error("Circuit load is missing.");
const protection = circuitProtectionById.get(circuit.id);
sectionBlocks.get(section.id)?.circuits.push({
block.circuits.push({
id: circuit.id,
circuitListId: circuit.circuitListId,
sectionId: circuit.sectionId,
@ -186,50 +256,97 @@ export async function getCircuitTree(req: Request, res: Response) {
voltage: circuit.voltage ?? undefined,
controlRequirement: circuit.controlRequirement ?? undefined,
status: circuit.status ?? undefined,
isReserve: Boolean(circuit.isReserve),
isReserve:
Boolean(circuit.isReserve) &&
circuitLoad.downstreamBoards.length === 0,
remark: circuit.remark ?? undefined,
circuitTotalPower,
circuitTotalPower: circuitLoad.activePowerKw,
circuitApparentPower: circuitLoad.apparentPowerKva,
circuitCosPhi: circuitLoad.cosPhi,
deviceRows,
protectionDevice: protection
? toProtectionDeviceDto(protection)
: undefined,
downstreamBoardLoads: circuitLoad.downstreamBoards
.map((load) => ({
distributionBoardId: load.distributionBoardId,
name: load.name,
boardNumber: load.boardNumber ?? undefined,
supplyType: boardById.get(load.distributionBoardId)?.supplyType ?? undefined,
activePowerKw: load.activePowerKw,
apparentPowerKva: load.apparentPowerKva,
cosPhi: load.cosPhi,
}))
.sort((left, right) =>
(left.boardNumber ?? left.name).localeCompare(
right.boardNumber ?? right.name,
"de"
)
),
protectionDevice: protection ? toProtectionDeviceDto(protection) : undefined,
});
}
for (const section of tree.sections) {
section.sectionTotalPower = calculateSectionTotalPower(
section.circuits
section.sectionTotalPower = section.circuits.reduce(
(sum, circuit) => sum + circuit.circuitTotalPower,
0
);
}
tree.distributionBoardTotalPower =
calculateDistributionBoardTotalPower(tree.sections);
tree.distributionBoardTotalPowerWithSimultaneityFactor =
applyDistributionBoardSimultaneityFactor(
tree.distributionBoardTotalPower,
tree.distributionBoardSimultaneityFactor
);
return tree;
});
return res.json(tree);
} catch (error) {
if (isMissingCircuitTreeSchemaError(error)) {
return res.json({
circuitListId,
currentRevision: project.currentRevision,
singlePhaseVoltageV: project.singlePhaseVoltageV,
threePhaseVoltageV: project.threePhaseVoltageV,
distributionBoardSimultaneityFactor:
distributionBoard.simultaneityFactor,
distributionBoardTotalPower: 0,
distributionBoardTotalPowerWithSimultaneityFactor: 0,
headerComponents: [],
sections: [],
footerComponents: [],
warning:
"Circuit-first tables are not available yet. Run database migrations (including 0008_circuit_first_model).",
});
}
throw error;
}
return { currentRevision: project.currentRevision, trees };
}
function resolveFeedingReference(
board: Awaited<ReturnType<typeof distributionBoardRepository.listByProject>>[number],
boardById: Map<string, Awaited<ReturnType<typeof distributionBoardRepository.listByProject>>[number]>,
listByBoardId: Map<string, Awaited<ReturnType<typeof circuitListRepository.listByProject>>[number]>,
circuitById: Map<string, Awaited<ReturnType<typeof circuitRepository.listByCircuitLists>>[number]>,
sectionById: Map<string, Awaited<ReturnType<typeof circuitSectionRepository.listByCircuitLists>>[number]>
) {
if (board.feedingDistributionBoardId === null) return undefined;
const feedingBoard = boardById.get(board.feedingDistributionBoardId);
if (!feedingBoard) throw new Error("Feeding distribution board is missing.");
const feedingList = listByBoardId.get(feedingBoard.id);
if (!feedingList) throw new Error("Feeding circuit list is missing.");
const feedingCircuit = board.feedingCircuitId === null
? undefined
: circuitById.get(board.feedingCircuitId);
return {
distributionBoardId: feedingBoard.id,
circuitListId: feedingList.id,
distributionBoardName: feedingBoard.name,
distributionBoardNumber: feedingBoard.boardNumber ?? undefined,
circuitId: feedingCircuit?.id,
circuitEquipmentIdentifier: feedingCircuit?.equipmentIdentifier,
circuitDisplayName: feedingCircuit?.displayName ?? undefined,
circuitCategory: feedingCircuit
? sectionById.get(feedingCircuit.sectionId)?.category ?? undefined
: undefined,
};
}
function emptyCircuitTree(
project: NonNullable<Awaited<ReturnType<typeof projectRepository.findById>>>,
board: NonNullable<Awaited<ReturnType<typeof distributionBoardRepository.findById>>>,
circuitListId: string
): CircuitTreeResponse & { warning: string } {
return {
circuitListId,
distributionBoardId: board.id,
distributionBoardName: board.name,
distributionBoardNumber: board.boardNumber ?? undefined,
distributionBoardSupplyType: board.supplyType ?? undefined,
currentRevision: project.currentRevision,
singlePhaseVoltageV: project.singlePhaseVoltageV,
threePhaseVoltageV: project.threePhaseVoltageV,
distributionBoardSimultaneityFactor: board.simultaneityFactor,
distributionBoardTotalPower: 0,
distributionBoardTotalPowerWithSimultaneityFactor: 0,
headerComponents: [],
sections: [],
footerComponents: [],
warning:
"Circuit-first tables are not available yet. Run database migrations (including 0008_circuit_first_model).",
};
}
function toProtectionDeviceDto(value: {
@ -243,11 +360,20 @@ function toProtectionDeviceDto(value: {
return {
type: value.type,
ratedCurrentA: value.ratedCurrentA,
fuseUtilizationCategory:
value.fuseUtilizationCategory ?? undefined,
fuseUtilizationCategory: value.fuseUtilizationCategory ?? undefined,
tripCharacteristic: value.tripCharacteristic ?? undefined,
rcdType: value.rcdType ?? undefined,
ratedResidualCurrentMa:
value.ratedResidualCurrentMa ?? undefined,
ratedResidualCurrentMa: value.ratedResidualCurrentMa ?? undefined,
};
}
function groupBy<T>(values: T[], keyOf: (value: T) => string) {
const groups = new Map<string, T[]>();
for (const value of values) {
const key = keyOf(value);
const group = groups.get(key) ?? [];
group.push(value);
groups.set(key, group);
}
return groups;
}

View file

@ -69,6 +69,8 @@ export async function createDistributionBoard(req: Request, res: Response) {
location: null,
fedFrom: null,
remark: null,
feedingDistributionBoardId: null,
feedingCircuitId: null,
},
});
} catch (error) {
@ -98,6 +100,17 @@ export async function updateDistributionBoard(
.filter((field) => parsed.data[field] !== undefined)
.map((field) => [field, parsed.data[field]])
);
const feedingPatch = {
...(parsed.data.feedingDistributionBoardId === undefined
? {}
: {
feedingDistributionBoardId:
parsed.data.feedingDistributionBoardId,
}),
...(parsed.data.feedingCircuitId === undefined
? {}
: { feedingCircuitId: parsed.data.feedingCircuitId }),
};
try {
const result = projectCommandService.executeUser({
projectId,
@ -110,6 +123,7 @@ export async function updateDistributionBoard(
supplyType: parsed.data.supplyType,
simultaneityFactor: parsed.data.simultaneityFactor,
...infoPatch,
...feedingPatch,
}
),
});

View file

@ -29,7 +29,10 @@ import {
listEstimateBalances,
updateEstimateBalance,
} from "../controllers/estimate-balance.controller.js";
import { getCircuitTree } from "../controllers/circuit-tree.controller.js";
import {
getCircuitTree,
getProjectCircuitTrees,
} from "../controllers/circuit-tree.controller.js";
import { getNextCircuitIdentifier } from "../controllers/circuit.controller.js";
import {
getProjectHistory,
@ -109,6 +112,7 @@ projectRouter.delete(
deleteDistributionBoard
);
projectRouter.get("/:projectId/circuit-lists", listCircuitListsByProject);
projectRouter.get("/:projectId/circuit-trees", getProjectCircuitTrees);
projectRouter.get("/:projectId/circuit-lists/:circuitListId/tree", getCircuitTree);
projectRouter.get(
"/:projectId/circuit-sections/:sectionId/next-identifier",

View file

@ -86,6 +86,8 @@ export const updateDistributionBoardSchema = z
location: distributionBoardInfoValueSchema("location"),
fedFrom: distributionBoardInfoValueSchema("fedFrom"),
remark: distributionBoardInfoValueSchema("remark"),
feedingDistributionBoardId: z.string().trim().min(1).nullable().optional(),
feedingCircuitId: z.string().trim().min(1).nullable().optional(),
})
.strict();

View file

@ -267,6 +267,15 @@ describe("cable sizing defaults from the circuit", () => {
sortOrder: 1,
isReserve: rows.length === 0,
circuitTotalPower: rows.reduce((sum, row) => sum + row.power, 0),
circuitApparentPower: rows.reduce(
(sum, row) => sum + row.power / (row.cosPhi ?? 1),
0
),
circuitCosPhi: rows.length
? rows.reduce((sum, row) => sum + row.power, 0) /
rows.reduce((sum, row) => sum + row.power / (row.cosPhi ?? 1), 0)
: 1,
downstreamBoardLoads: [],
deviceRows: rows.map((row, index) => ({
id: `row-${index}`,
sortOrder: index,
@ -288,6 +297,19 @@ describe("cable sizing defaults from the circuit", () => {
assert.equal(deriveCircuitCosPhi(circuit([])), 1);
});
it("uses the projected cos φ including downstream boards", () => {
const feeder = circuit([{ power: 1, cosPhi: 1 }]);
feeder.circuitCosPhi = 0.67;
feeder.downstreamBoardLoads = [{
distributionBoardId: "uv-2",
name: "UV 2",
activePowerKw: 3,
apparentPowerKva: 5,
cosPhi: 0.6,
}];
assert.equal(deriveCircuitCosPhi(feeder), 0.67);
});
it("derives the phase from the group category or three-phase rows", () => {
assert.equal(deriveCircuitPhase(circuit([{ power: 1, phaseType: "single_phase" }]), "single_phase"), 1);
assert.equal(deriveCircuitPhase(circuit([{ power: 1, phaseType: "three_phase" }]), "single_phase"), 3);

View file

@ -14,6 +14,7 @@ import { circuitDeviceRows } from "../src/db/schema/circuit-device-rows.js";
import { circuitProtectionDevices } from "../src/db/schema/circuit-protection-devices.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 { projectRevisions } from "../src/db/schema/project-revisions.js";
import { projects } from "../src/db/schema/projects.js";
import {
@ -611,6 +612,62 @@ describe("circuit device-row move project-command repository", () => {
}
});
it("blocks undo from removing a generated circuit used as a feeder", () => {
const fixture = createTestDatabase();
try {
const store = new CircuitDeviceRowMoveProjectCommandRepository(
fixture.context.db
);
const targetCircuit = createMoveTargetSnapshot(fixture);
const command = createCircuitDeviceRowMoveWithNewCircuitProjectCommand(
"create",
targetCircuit,
[{
rowId: "row-1",
expectedCircuitId: "circuit-1",
expectedSortOrder: 10,
targetCircuitId: targetCircuit.id,
targetSortOrder: 10,
}]
);
const moved = store.execute({
projectId: "project-1",
expectedRevision: 0,
source: "user",
command,
});
const downstream = new DistributionBoardFixtureRepository(
fixture.context.db
).createWithCircuitListAndDefaultSections("project-1", "UV-02");
fixture.context.db
.update(distributionBoards)
.set({
feedingDistributionBoardId: fixture.circuitListId,
feedingCircuitId: targetCircuit.id,
})
.where(eq(distributionBoards.id, downstream.id))
.run();
assert.throws(
() => store.execute({
projectId: "project-1",
expectedRevision: 1,
source: "undo",
historyTargetChangeSetId: moved.revision.changeSetId,
command: moved.inverse,
}),
/Speist UV-02 – erst Einspeisung ändern\./
);
assert.equal(getRow(fixture.context, "row-1")?.circuitId, targetCircuit.id);
assert.ok(
fixture.context.db.select().from(circuits)
.where(eq(circuits.id, targetCircuit.id)).get()
);
} finally {
fixture.context.close();
}
});
it("rejects deletion when the created target changed outside history", () => {
const fixture = createTestDatabase();
try {

View file

@ -47,7 +47,10 @@ function makeCircuit(
sortOrder,
isReserve: deviceRows.length === 0,
circuitTotalPower: deviceRows.reduce((total, row) => total + row.rowTotalPower, 0),
circuitApparentPower: deviceRows.reduce((total, row) => total + row.rowTotalPower, 0),
circuitCosPhi: 1,
deviceRows,
downstreamBoardLoads: [],
};
}
@ -176,6 +179,44 @@ describe("circuit grid projection", () => {
);
});
it("renders linked downstream boards as separate read-only rows", () => {
const feeder = {
...reserveCircuit,
isReserve: false,
circuitTotalPower: 4,
circuitApparentPower: 5,
circuitCosPhi: 0.8,
downstreamBoardLoads: [{
distributionBoardId: "board-child",
name: "Unterverteilung",
boardNumber: "UV-02",
activePowerKw: 4,
apparentPowerKva: 5,
cosPhi: 0.8,
}],
};
const rows = buildVisibleGridRowsWithStructure(
[{ ...sections[0], circuits: [feeder] }],
{ headerComponents: [], footerComponents: [] }
);
assert.deepEqual(rows.map((row) => row.rowType), [
"section",
"circuitSummary",
"downstreamBoardLoad",
"placeholder",
]);
const loadRow = rows[2];
assert.equal(
loadRow.cells.find((cell) => cell.cellKey === "displayName")?.value,
"Unterverteilung UV-02"
);
assert.equal(
loadRow.cells.find((cell) => cell.cellKey === "rowTotalPower")?.value,
4
);
assert.equal(loadRow.cells.every((cell) => !cell.editable), true);
});
it("shows circuit protection once on each visible circuit row", () => {
const protectedSections = sections.map((section) => ({
...section,

View file

@ -16,6 +16,7 @@ import { circuitSections } from "../src/db/schema/circuit-sections.js";
import { circuits } from "../src/db/schema/circuits.js";
import { distributionBoardComponentProtectionDevices } from "../src/db/schema/distribution-board-component-protection-devices.js";
import { distributionBoardComponents } from "../src/db/schema/distribution-board-components.js";
import { distributionBoards } from "../src/db/schema/distribution-boards.js";
import { projectRevisions } from "../src/db/schema/project-revisions.js";
import { projects } from "../src/db/schema/projects.js";
import { createCircuitGroupDeleteSubtreeProjectCommand } from "../src/domain/models/circuit-group-subtree-project-command.model.js";
@ -228,6 +229,45 @@ describe("circuit-group subtree project command", () => {
}
});
it("blocks deleting a group containing a feeding circuit", () => {
const context = createTestDatabase();
try {
const list = context.db.select().from(circuitLists).get()!;
const downstream = new DistributionBoardFixtureRepository(
context.db
).createWithCircuitListAndDefaultSections("project-1", "Unterverteiler");
context.db
.update(distributionBoards)
.set({
feedingDistributionBoardId: list.distributionBoardId,
feedingCircuitId: "subtree-circuit",
})
.where(eq(distributionBoards.id, downstream.id))
.run();
const repository =
new CircuitGroupSubtreeProjectCommandRepository(context.db);
const groupId = context.db
.select()
.from(circuits)
.where(eq(circuits.id, "subtree-circuit"))
.get()!.sectionId;
const snapshot = repository.capture("project-1", groupId);
assert.throws(
() => repository.execute({
projectId: "project-1",
expectedRevision: 0,
source: "user",
command: createCircuitGroupDeleteSubtreeProjectCommand(snapshot),
}),
/Speist Unterverteiler – erst Einspeisung ändern\./
);
assert.deepEqual(repository.capture("project-1", groupId), snapshot);
} finally {
context.close();
}
});
it("rolls back a cascaded deletion when history persistence fails", () => {
const context = createTestDatabase();
try {

View file

@ -14,6 +14,7 @@ import { circuitDeviceRows } from "../src/db/schema/circuit-device-rows.js";
import { circuitProtectionDevices } from "../src/db/schema/circuit-protection-devices.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 { projectDevices } from "../src/db/schema/project-devices.js";
import { projectRevisions } from "../src/db/schema/project-revisions.js";
import { projects } from "../src/db/schema/projects.js";
@ -442,6 +443,48 @@ describe("circuit structure project-command repository", () => {
}
});
it("blocks deleting a circuit that feeds a distribution board", () => {
const fixture = createTestDatabase();
try {
const downstream = new DistributionBoardFixtureRepository(
fixture.context.db
).createWithCircuitListAndDefaultSections("project-1", "UV Unterverteiler");
fixture.context.db
.update(distributionBoards)
.set({
boardNumber: "UV-02",
feedingDistributionBoardId: fixture.circuitListId,
feedingCircuitId: "circuit-existing",
})
.where(eq(distributionBoards.id, downstream.id))
.run();
const store = new CircuitStructureProjectCommandRepository(
fixture.context.db
);
assert.throws(
() =>
store.execute({
projectId: "project-1",
expectedRevision: 0,
source: "user",
command: createCircuitDeleteProjectCommand(
"circuit-existing",
fixture.circuitListId
),
}),
/Speist UV-02 – erst Einspeisung ändern\./
);
assert.ok(getCircuit(fixture.context, "circuit-existing"));
assert.equal(
fixture.context.db.select().from(projectRevisions).all().length,
0
);
} finally {
fixture.context.close();
}
});
it("rolls back insert and delete for late history failures", () => {
const fixture = createTestDatabase();
try {

View file

@ -38,13 +38,15 @@ describe("circuit tree controller", () => {
it("returns header, grouped and footer components with both protection layers", async () => {
const originals = {
findList: circuitListRepository.findById,
listLists: circuitListRepository.listByProject,
findProject: projectRepository.findById,
findBoard: distributionBoardRepository.findById,
listSections: circuitSectionRepository.listByCircuitList,
listCircuits: circuitRepository.listByCircuitList,
listBoards: distributionBoardRepository.listByProject,
listSections: circuitSectionRepository.listByCircuitLists,
listCircuits: circuitRepository.listByCircuitLists,
listRows: circuitDeviceRowRepository.listByCircuitList,
listComponents:
distributionBoardComponentRepository.listByCircuitList,
distributionBoardComponentRepository.listByCircuitLists,
listComponentProtection:
distributionBoardComponentRepository.listProtectionByComponentIds,
listCircuitProtection:
@ -57,6 +59,13 @@ describe("circuit tree controller", () => {
distributionBoardId: "board-1",
name: "UV-01",
}) as never;
circuitListRepository.listByProject = async () =>
[{
id: "list-1",
projectId: "project-1",
distributionBoardId: "board-1",
name: "UV-01",
}] as never;
projectRepository.findById = async () =>
({
id: "project-1",
@ -66,8 +75,19 @@ describe("circuit tree controller", () => {
threePhaseVoltageV: 400,
}) as never;
distributionBoardRepository.findById = async () =>
({ id: "board-1", simultaneityFactor: 0.8 }) as never;
circuitSectionRepository.listByCircuitList = async () =>
({ id: "board-1", name: "UV-01", simultaneityFactor: 0.8 }) as never;
distributionBoardRepository.listByProject = async () =>
[{
id: "board-1",
projectId: "project-1",
name: "UV-01",
boardNumber: "UV-01",
supplyType: "AV",
simultaneityFactor: 0.8,
feedingDistributionBoardId: null,
feedingCircuitId: null,
}] as never;
circuitSectionRepository.listByCircuitLists = async () =>
[
{
id: "group-1",
@ -80,7 +100,7 @@ describe("circuit tree controller", () => {
groupNumber: 1,
},
] as never;
circuitRepository.listByCircuitList = async () =>
circuitRepository.listByCircuitLists = async () =>
[
{
id: "circuit-1",
@ -90,10 +110,19 @@ describe("circuit tree controller", () => {
displayName: "Licht",
sortOrder: 10,
isReserve: 0,
cableType: null,
cableCrossSection: null,
cableLength: null,
rcdAssignment: null,
terminalDesignation: null,
voltage: 230,
controlRequirement: null,
status: null,
remark: null,
},
] as never;
circuitDeviceRowRepository.listByCircuitList = async () => [];
distributionBoardComponentRepository.listByCircuitList =
distributionBoardComponentRepository.listByCircuitLists =
async () =>
[
{
@ -170,13 +199,15 @@ describe("circuit tree controller", () => {
);
} finally {
circuitListRepository.findById = originals.findList;
circuitListRepository.listByProject = originals.listLists;
projectRepository.findById = originals.findProject;
distributionBoardRepository.findById = originals.findBoard;
circuitSectionRepository.listByCircuitList =
distributionBoardRepository.listByProject = originals.listBoards;
circuitSectionRepository.listByCircuitLists =
originals.listSections;
circuitRepository.listByCircuitList = originals.listCircuits;
circuitRepository.listByCircuitLists = originals.listCircuits;
circuitDeviceRowRepository.listByCircuitList = originals.listRows;
distributionBoardComponentRepository.listByCircuitList =
distributionBoardComponentRepository.listByCircuitLists =
originals.listComponents;
distributionBoardComponentRepository.listProtectionByComponentIds =
originals.listComponentProtection;

View file

@ -0,0 +1,89 @@
import assert from "node:assert/strict";
import { describe, it } from "node:test";
import { calculateDistributionBoardLoads } from "../src/domain/calculations/distribution-board-load-calculation.js";
const board = (
id: string,
simultaneityFactor: number,
feedingDistributionBoardId: string | null = null,
feedingCircuitId: string | null = null
) => ({
id,
name: id,
boardNumber: id.toUpperCase(),
simultaneityFactor,
feedingDistributionBoardId,
feedingCircuitId,
});
describe("distribution-board load calculation", () => {
it("rolls recursive demand into the selected feeder circuit exactly once", () => {
const result = calculateDistributionBoardLoads({
boards: [
board("nshv", 0.8),
board("uv1", 0.5, "nshv", "nshv-out"),
board("uv2", 0.25, "uv1", "uv1-out"),
],
circuits: [
{ id: "nshv-out", distributionBoardId: "nshv", rows: [{ activePowerKw: 2, cosPhi: 1 }] },
{ id: "uv1-out", distributionBoardId: "uv1", rows: [{ activePowerKw: 4, cosPhi: 0.8 }] },
{ id: "uv2-load", distributionBoardId: "uv2", rows: [{ activePowerKw: 8, cosPhi: 0.8 }] },
],
});
assert.deepEqual(result.boards.get("uv2"), {
distributionBoardId: "uv2",
activePowerBeforeSimultaneityKw: 8,
apparentPowerBeforeSimultaneityKva: 10,
demandActivePowerKw: 2,
demandApparentPowerKva: 2.5,
cosPhi: 0.8,
});
assert.equal(result.circuits.get("uv1-out")?.activePowerKw, 6);
assert.equal(result.boards.get("uv1")?.demandActivePowerKw, 3);
assert.equal(result.circuits.get("nshv-out")?.activePowerKw, 5);
assert.equal(result.boards.get("nshv")?.demandActivePowerKw, 4);
assert.equal(result.circuits.get("nshv-out")?.downstreamBoards.length, 1);
});
it("combines apparent power for multiple child boards on one circuit", () => {
const result = calculateDistributionBoardLoads({
boards: [board("main", 1), board("a", 1, "main", "out"), board("b", 1, "main", "out")],
circuits: [
{ id: "out", distributionBoardId: "main", rows: [] },
{ id: "a-load", distributionBoardId: "a", rows: [{ activePowerKw: 2, cosPhi: 0.5 }] },
{ id: "b-load", distributionBoardId: "b", rows: [{ activePowerKw: 2, cosPhi: 1 }] },
],
});
const feeder = result.circuits.get("out")!;
assert.equal(feeder.activePowerKw, 4);
assert.equal(feeder.apparentPowerKva, 6);
assert.equal(feeder.cosPhi, 2 / 3);
assert.deepEqual(feeder.downstreamBoards.map((entry) => entry.distributionBoardId), ["a", "b"]);
});
it("keeps a board-only hierarchy relation out of load rollup", () => {
const result = calculateDistributionBoardLoads({
boards: [board("main", 1), board("child", 1, "main", null)],
circuits: [
{ id: "main-load", distributionBoardId: "main", rows: [{ activePowerKw: 1, cosPhi: 1 }] },
{ id: "child-load", distributionBoardId: "child", rows: [{ activePowerKw: 5, cosPhi: 1 }] },
],
});
assert.equal(result.boards.get("main")?.demandActivePowerKw, 1);
assert.equal(result.boards.get("child")?.demandActivePowerKw, 5);
});
it("rejects cycles defensively", () => {
assert.throws(
() => calculateDistributionBoardLoads({
boards: [board("a", 1, "b", "b-out"), board("b", 1, "a", "a-out")],
circuits: [
{ id: "a-out", distributionBoardId: "a", rows: [] },
{ id: "b-out", distributionBoardId: "b", rows: [] },
],
}),
/cycle/
);
});
});

View file

@ -219,6 +219,54 @@ describe("distribution-board structure project command", () => {
}
});
it("blocks undo from removing an empty board used as a feeder", () => {
const context = createTestDatabase();
try {
const repository =
new DistributionBoardStructureProjectCommandRepository(context.db);
const upstream = createDistributionBoardStructureSnapshot(
"project-1",
"NSHV"
);
const inserted = repository.execute({
projectId: "project-1",
expectedRevision: 0,
source: "user",
command: createDistributionBoardInsertProjectCommand(upstream),
});
const downstream = createDistributionBoardStructureSnapshot(
"project-1",
"UV-01"
);
context.db.insert(distributionBoards).values({
...downstream.distributionBoard,
boardNumber: "UV-01",
feedingDistributionBoardId: upstream.distributionBoard.id,
}).run();
context.db.insert(circuitLists).values(downstream.circuitList).run();
context.db.insert(circuitSections).values(downstream.sections).run();
context.db.insert(distributionBoardComponents)
.values(downstream.components).run();
assert.throws(
() => repository.execute({
projectId: "project-1",
expectedRevision: 1,
source: "undo",
historyTargetChangeSetId: inserted.revision.changeSetId,
command: inserted.inverse,
}),
/Speist UV-01 – erst Einspeisung ändern\./
);
assert.ok(
context.db.select().from(distributionBoards)
.where(eq(distributionBoards.id, upstream.distributionBoard.id)).get()
);
} finally {
context.close();
}
});
it("allows only supply types enabled in the project", () => {
const context = createTestDatabase();
try {
@ -491,6 +539,159 @@ describe("distribution-board structure project command", () => {
}
});
it("links a feeding board and circuit while preserving version-1 updates", () => {
const context = createTestDatabase();
try {
const repository =
new DistributionBoardStructureProjectCommandRepository(context.db);
const upstream = createDistributionBoardStructureSnapshot(
"project-1",
"NSHV",
{ supplyType: "AV" }
);
const downstream = createDistributionBoardStructureSnapshot(
"project-1",
"UV EG",
{ supplyType: "AV" }
);
repository.execute({
projectId: "project-1",
expectedRevision: 0,
source: "user",
command: createDistributionBoardInsertProjectCommand(upstream),
});
repository.execute({
projectId: "project-1",
expectedRevision: 1,
source: "user",
command: createDistributionBoardInsertProjectCommand(downstream),
});
context.db.insert(circuits).values([
{
id: "feed-circuit",
circuitListId: upstream.circuitList.id,
sectionId: upstream.sections[2]!.id,
equipmentIdentifier: "-3F1.1",
sortOrder: 10,
},
{
id: "downstream-circuit",
circuitListId: downstream.circuitList.id,
sectionId: downstream.sections[2]!.id,
equipmentIdentifier: "-3F1.1",
sortOrder: 10,
},
]).run();
const linked = repository.executeUpdate({
projectId: "project-1",
expectedRevision: 2,
source: "user",
command: createDistributionBoardUpdateProjectCommand(
downstream.distributionBoard.id,
{
feedingDistributionBoardId: upstream.distributionBoard.id,
feedingCircuitId: "feed-circuit",
}
),
});
assert.deepEqual(
context.db
.select({
feedingDistributionBoardId:
distributionBoards.feedingDistributionBoardId,
feedingCircuitId: distributionBoards.feedingCircuitId,
})
.from(distributionBoards)
.where(eq(distributionBoards.id, downstream.distributionBoard.id))
.get(),
{
feedingDistributionBoardId: upstream.distributionBoard.id,
feedingCircuitId: "feed-circuit",
}
);
repository.executeUpdate({
projectId: "project-1",
expectedRevision: 3,
source: "undo",
historyTargetChangeSetId: new ProjectHistoryRepository(
context.db
).getNextCommand("project-1", "undo")?.changeSetId,
command: linked.inverse,
});
repository.executeUpdate({
projectId: "project-1",
expectedRevision: 4,
source: "user",
command: createDistributionBoardUpdateProjectCommand(
downstream.distributionBoard.id,
{
feedingDistributionBoardId: upstream.distributionBoard.id,
feedingCircuitId: null,
}
),
});
assert.throws(
() => repository.executeUpdate({
projectId: "project-1",
expectedRevision: 5,
source: "user",
command: createDistributionBoardUpdateProjectCommand(
upstream.distributionBoard.id,
{
feedingDistributionBoardId: downstream.distributionBoard.id,
feedingCircuitId: null,
}
),
}),
/Zyklus/
);
assert.throws(
() => repository.executeUpdate({
projectId: "project-1",
expectedRevision: 5,
source: "user",
command: createDistributionBoardUpdateProjectCommand(
downstream.distributionBoard.id,
{
feedingDistributionBoardId: upstream.distributionBoard.id,
feedingCircuitId: "downstream-circuit",
}
),
}),
/gehört nicht zur gewählten Verteilung/
);
repository.executeUpdate({
projectId: "project-1",
expectedRevision: 5,
source: "user",
command: {
schemaVersion: 1,
type: "distribution-board.update",
payload: {
distributionBoardId: downstream.distributionBoard.id,
changes: [{ field: "boardNumber", value: "UV-01" }],
},
},
});
const afterLegacyUpdate = context.db
.select()
.from(distributionBoards)
.where(eq(distributionBoards.id, downstream.distributionBoard.id))
.get();
assert.equal(afterLegacyUpdate?.boardNumber, "UV-01");
assert.equal(
afterLegacyUpdate?.feedingDistributionBoardId,
upstream.distributionBoard.id
);
} finally {
context.close();
}
});
it("sends distribution-board writes to the revision-safe API", async () => {
const requests: Array<{ url: string; method: string; body: unknown }> = [];
const originalFetch = globalThis.fetch;

View file

@ -291,6 +291,43 @@ describe("distribution-board subtree project command", () => {
}
});
it("blocks deleting a board subtree that feeds another board", () => {
const context = createTestDatabase();
try {
const upstream = createPopulatedBoard(context);
const downstream = new DistributionBoardFixtureRepository(
context.db
).createWithCircuitListAndDefaultSections("project-1", "Unterverteiler");
context.db
.update(distributionBoards)
.set({
boardNumber: "UV-03",
feedingDistributionBoardId: upstream.id,
})
.where(eq(distributionBoards.id, downstream.id))
.run();
const repository =
new DistributionBoardSubtreeProjectCommandRepository(context.db);
const snapshot = repository.capture("project-1", upstream.id);
assert.throws(
() =>
repository.execute({
projectId: "project-1",
expectedRevision: 0,
source: "user",
command:
createDistributionBoardDeleteSubtreeProjectCommand(snapshot),
}),
/Speist UV-03 – erst Einspeisung ändern\./
);
assert.deepEqual(repository.capture("project-1", upstream.id), snapshot);
assert.equal(context.db.select().from(projectRevisions).all().length, 0);
} finally {
context.close();
}
});
it("rolls back a cascaded deletion when history persistence fails", () => {
const context = createTestDatabase();
try {

View file

@ -19,6 +19,8 @@ function createSnapshot(): DistributionBoardSubtreeSnapshot {
floorId: "floor-1",
supplyType: "AV",
simultaneityFactor: 0.8,
feedingDistributionBoardId: "board-upstream",
feedingCircuitId: "circuit-upstream",
},
circuitList: {
id: "list-1",
@ -157,6 +159,11 @@ describe("distribution-board subtree snapshot", () => {
assert.equal(clone.distributionBoard.id, "new-1");
assert.equal(clone.circuitList.id, "new-1");
assert.equal(clone.distributionBoard.name, "UV 1 Kopie");
assert.equal(
clone.distributionBoard.feedingDistributionBoardId,
"board-upstream"
);
assert.equal(clone.distributionBoard.feedingCircuitId, "circuit-upstream");
assert.equal(clone.circuitList.name, "UV 1 Kopie Stromkreisliste");
assert.equal(clone.sections[0].id, "new-2");
assert.equal(clone.circuits[0].id, "new-3");

View file

@ -533,6 +533,61 @@ describe("external object new-circuit project command", () => {
}
});
it("blocks history removal when the generated circuit became a feeder", () => {
const context = createFixture(null);
try {
const object = objectSnapshot(null);
const circuit = newExternalCircuit(context.db);
const command = createExternalObjectNewCircuitProjectCommand(
externalObjectAssignToNewCircuitCommandType,
{
circuit,
objects: [{
expected: object,
target: { ...object, circuitDeviceRowId: "row-new" },
}],
confirmedConflictObjectIds: [],
}
);
const repository = new ExternalObjectNewCircuitProjectCommandRepository(context.db);
const inserted = repository.execute({
projectId: "project-1",
expectedRevision: 0,
source: "user",
command,
});
context.db.insert(distributionBoards).values({
id: "board-2",
projectId: "project-1",
name: "Unterverteiler",
boardNumber: "UV-02",
feedingDistributionBoardId: "board-1",
feedingCircuitId: circuit.id,
}).run();
assert.throws(
() => repository.execute({
projectId: "project-1",
expectedRevision: 1,
source: "undo",
historyTargetChangeSetId: inserted.revision.changeSetId,
command: inserted.inverse,
}),
/Speist UV-02 – erst Einspeisung ändern\./
);
assert.ok(
context.db.select().from(circuits)
.where(eq(circuits.id, circuit.id)).get()
);
assert.equal(
context.db.select().from(externalModelObjects).get()!.circuitDeviceRowId,
"row-new"
);
} finally {
context.close();
}
});
it("rolls back the complete circuit subtree when history persistence fails", () => {
const context = createFixture(null);
try {

View file

@ -2,7 +2,8 @@ import assert from "node:assert/strict";
import { describe, it } from "node:test";
import {
assertPrintRevisionConsistency,
buildPreliminaryBoardHierarchy,
buildBoardHierarchy,
formatFeedingReference,
formatCircuitCable,
formatProtectionDevice,
summarizePrintBoard,
@ -27,6 +28,8 @@ function board(
location: null,
fedFrom: null,
remark: null,
feedingDistributionBoardId: null,
feedingCircuitId: null,
...info,
};
}
@ -52,6 +55,8 @@ function tree(
const total = deviceRows.reduce((sum, row) => sum + row.rowTotalPower, 0);
return {
circuitListId: "list",
distributionBoardId: "board",
distributionBoardName: "Board",
currentRevision: 1,
singlePhaseVoltageV: 230,
threePhaseVoltageV: 400,
@ -79,7 +84,13 @@ function tree(
sortOrder: 1,
isReserve: false,
circuitTotalPower: total,
circuitApparentPower: deviceRows.reduce(
(sum, row) => sum + row.rowTotalPower / (row.cosPhi || 1),
0
),
circuitCosPhi: 1,
deviceRows,
downstreamBoardLoads: [],
},
],
},
@ -117,16 +128,15 @@ describe("print document model", () => {
assert.equal(formatCircuitCable(circuit), null);
});
it("does not guess a parent when feed text matches more than one board", () => {
it("does not derive hierarchy from the external free text", () => {
const summaries = [
summary("a", { boardNumber: "HV" }, 2),
summary("b", { name: "HV" }, 3),
summary("uv", { fedFrom: " hv " }, 1),
];
for (const ordered of [summaries, [...summaries].reverse()]) {
const entries = buildPreliminaryBoardHierarchy(ordered);
const entries = buildBoardHierarchy(ordered);
assert.ok(entries.every((entry) => entry.parentBoardId === null));
assert.equal(entries.reduce((sum, entry) => sum + entry.cumulatedDemandPowerKw, 0), 6);
}
});
@ -145,17 +155,39 @@ describe("print document model", () => {
});
assert.equal(result.circuitCount, 1);
assert.equal(result.installedPowerKw, 14);
assert.equal(result.devicePowerKw, 10);
assert.equal(result.powerBeforeBoardSimultaneityKw, 10);
assert.equal(result.demandPowerKw, 5);
// S = (4 / 0.8 + 6 / 1) × 0.5 = 5.5 kVA → I = 5500 / (√3 · 400)
assert.ok(Math.abs(result.designCurrentA - 5500 / (Math.sqrt(3) * 400)) < 1e-9);
});
it("nests boards by matching free-text feed and sums downstream demand", () => {
const entries = buildPreliminaryBoardHierarchy([
it("uses the selected one-phase feeder circuit for the design current", () => {
const inputTree = tree([
{ quantity: 1, powerPerUnit: 2.3, simultaneityFactor: 1, cosPhi: 1 },
]);
inputTree.feedingReference = {
distributionBoardId: "hv",
circuitListId: "hv-list",
distributionBoardName: "Hauptverteilung",
distributionBoardNumber: "NSHV",
circuitId: "out",
circuitEquipmentIdentifier: "-2F1.1",
circuitCategory: "single_phase",
};
const result = summarizePrintBoard({
board: board("uv"),
circuitListId: "uv",
floorName: null,
tree: inputTree,
});
assert.ok(Math.abs(result.designCurrentA - 10) < 1e-9);
});
it("nests boards by their logical feeding references", () => {
const entries = buildBoardHierarchy([
summary("hv", { boardNumber: "HV 0.1", fedFrom: "HAK" }, 10),
summary("uv-a", { boardNumber: "UV 1.1", fedFrom: " hv 0.1 " }, 4),
summary("uv-b", { boardNumber: "UV 1.2", fedFrom: "UV 1.1" }, 2),
summary("uv-a", { boardNumber: "UV 1.1", feedingDistributionBoardId: "hv" }, 4),
summary("uv-b", { boardNumber: "UV 1.2", feedingDistributionBoardId: "uv-a" }, 2),
summary("uv-c", { name: "UV-C", fedFrom: "unbekannt" }, 1),
]);
assert.deepEqual(
@ -163,28 +195,37 @@ describe("print document model", () => {
entry.summary.board.id,
entry.depth,
entry.parentBoardId,
entry.cumulatedDemandPowerKw,
]),
[
["hv", 0, null, 16],
["uv-a", 1, "hv", 6],
["uv-b", 2, "uv-a", 2],
["uv-c", 0, null, 1],
["hv", 0, null],
["uv-a", 1, "hv"],
["uv-b", 2, "uv-a"],
["uv-c", 0, null],
]
);
});
it("keeps boards with cyclic feed texts at the top level", () => {
const entries = buildPreliminaryBoardHierarchy([
summary("a", { boardNumber: "A", fedFrom: "B" }, 1),
summary("b", { boardNumber: "B", fedFrom: "A" }, 1),
it("keeps malformed cyclic logical links visible at the top level", () => {
const entries = buildBoardHierarchy([
summary("a", { boardNumber: "A", feedingDistributionBoardId: "b" }, 1),
summary("b", { boardNumber: "B", feedingDistributionBoardId: "a" }, 1),
]);
assert.equal(entries.length, 2);
assert.ok(entries.some((entry) => entry.parentBoardId === null));
assert.equal(
entries.reduce((sum, entry) => sum + (entry.depth === 0 ? entry.cumulatedDemandPowerKw : 0), 0),
2
);
});
it("formats the resolved feeding board and circuit instead of free text", () => {
const value = summary("uv", { fedFrom: "Alttext" }, 1);
value.tree.feedingReference = {
distributionBoardId: "hv",
circuitListId: "hv-list",
distributionBoardName: "Hauptverteilung",
distributionBoardNumber: "NSHV",
circuitId: "out",
circuitEquipmentIdentifier: "-3F1.1",
circuitCategory: "three_phase",
};
assert.equal(formatFeedingReference(value), "NSHV-3F1.1");
});
it("formats protection devices for the plan table", () => {

View file

@ -50,6 +50,8 @@ it("upgrades a populated pre-print database through the current schema without c
for (const [key, value] of Object.entries(oldRoom)) assert.deepEqual(room[key], value);
for (const field of ["site_address", "work_stage", "project_lead", "project_editor"]) assert.equal(project[field], null);
for (const field of ["board_number", "location", "fed_from", "remark"]) assert.equal(board[field], null);
assert.equal(board.feeding_distribution_board_id, null);
assert.equal(board.feeding_circuit_id, null);
assert.equal(floor.building_id, null);
assert.equal(room.area_m2, null);
assert.equal(context.sqlite.prepare("SELECT count(*) AS count FROM buildings").get()!.count, 0);

View file

@ -476,7 +476,14 @@ describe("project snapshot repository", () => {
delete payload.project[field];
}
for (const board of payload.distributionBoards) {
for (const field of ["boardNumber", "location", "fedFrom", "remark"]) {
for (const field of [
"boardNumber",
"location",
"fedFrom",
"remark",
"feedingDistributionBoardId",
"feedingCircuitId",
]) {
delete board[field];
}
}

View file

@ -6,12 +6,25 @@ import {
projectStateSnapshotSchemaVersion,
serializeProjectStateSnapshot,
} from "../src/domain/models/project-state-snapshot.model.js";
import { remapProjectState } from "../src/domain/models/project-transfer.model.js";
import { createNamedProjectSnapshotSchema } from "../src/shared/validation/project-snapshot.schemas.js";
/** Removes the fields introduced by snapshot schema version 9. */
function withoutVersionNineFields<
T extends { distributionBoards: object[] },
>(snapshot: T): T {
for (const board of snapshot.distributionBoards) {
delete (board as Record<string, unknown>).feedingDistributionBoardId;
delete (board as Record<string, unknown>).feedingCircuitId;
}
return snapshot;
}
/** Removes the fields introduced by snapshot schema version 7. */
function withoutVersionSevenFields<
T extends { floors: object[] },
T extends { floors: object[]; distributionBoards: object[] },
>(snapshot: T): T {
withoutVersionNineFields(snapshot);
delete (snapshot as Record<string, unknown>).buildings;
for (const floor of snapshot.floors) {
delete (floor as Record<string, unknown>).buildingId;
@ -117,6 +130,8 @@ function protectedSnapshot() {
location: "Raum 003 Technik" as string | null,
fedFrom: "HV 0.1" as string | null,
remark: null as string | null,
feedingDistributionBoardId: null as string | null,
feedingCircuitId: null as string | null,
},
],
circuitLists: [
@ -206,6 +221,96 @@ describe("project state snapshot model", () => {
);
});
it("upgrades snapshot version 8 with empty feeding references", () => {
const previous = withoutVersionNineFields(protectedSnapshot());
previous.schemaVersion = 8 as typeof previous.schemaVersion;
const upgraded = parseProjectStateSnapshot(previous);
assert.equal(upgraded.schemaVersion, projectStateSnapshotSchemaVersion);
assert.equal(
upgraded.distributionBoards[0]?.feedingDistributionBoardId,
null
);
assert.equal(upgraded.distributionBoards[0]?.feedingCircuitId, null);
});
it("validates logical feeding references and cycles", () => {
const linked = protectedSnapshot();
linked.distributionBoards.push({
id: "board-2",
projectId: "project-1",
name: "UV 2",
floorId: null,
supplyType: null,
simultaneityFactor: 1,
boardNumber: "UV 2.1",
location: null,
fedFrom: null,
remark: null,
feedingDistributionBoardId: "board-1",
feedingCircuitId: "circuit-1",
});
linked.circuitLists.push({
id: "list-2",
projectId: "project-1",
distributionBoardId: "board-2",
name: "UV 2 Stromkreisliste",
});
linked.circuitSections.push({
id: "section-2",
circuitListId: "list-2",
key: "three_phase",
displayName: "Dreiphasig 1",
prefix: "-3F1.",
sortOrder: 10,
category: "three_phase",
groupNumber: 1,
});
const parsed = parseProjectStateSnapshot(linked);
assert.equal(parsed.distributionBoards[1]?.feedingCircuitId, "circuit-1");
let nextId = 0;
const remapped = remapProjectState(
parsed,
"project-copy",
() => `copy-${++nextId}`
);
const remappedUpstream = remapped.distributionBoards.find(
(board) => board.name === "UV 1"
);
const remappedDownstream = remapped.distributionBoards.find(
(board) => board.name === "UV 2"
);
assert.ok(remappedUpstream);
assert.ok(remappedDownstream);
assert.equal(
remappedDownstream.feedingDistributionBoardId,
remappedUpstream.id
);
assert.notEqual(remappedDownstream.feedingCircuitId, "circuit-1");
assert.ok(
remapped.circuits.some(
(circuit) => circuit.id === remappedDownstream.feedingCircuitId
)
);
const missingBoard = structuredClone(linked);
missingBoard.distributionBoards[1]!.feedingDistributionBoardId = null;
assert.throws(
() => parseProjectStateSnapshot(missingBoard),
/requires a feeding distribution board/
);
const cycle = structuredClone(linked);
cycle.distributionBoards[0]!.feedingDistributionBoardId = "board-2";
cycle.distributionBoards[0]!.feedingCircuitId = null;
cycle.distributionBoards[1]!.feedingCircuitId = null;
assert.throws(
() => parseProjectStateSnapshot(cycle),
/contains a cycle/
);
});
it("upgrades the supported baseline snapshot with a safe building default", () => {
const baseline = withoutVersionSixFields(structuredClone(minimalSnapshot()));
baseline.schemaVersion = 1 as typeof baseline.schemaVersion;