Add project device sync history

This commit is contained in:
2026-07-24 08:47:25 +02:00
parent 4b4603b71b
commit 2668fc2f16
14 changed files with 1180 additions and 21 deletions
@@ -0,0 +1,245 @@
import {
assertCircuitDeviceRowUpdateProjectCommand,
circuitDeviceRowUpdateCommandSchemaVersion,
circuitDeviceRowUpdateCommandType,
type CircuitDeviceRowUpdateField,
type CircuitDeviceRowUpdateValues,
} from "./circuit-device-row-project-command.model.js";
import type { SerializedProjectCommand } from "./project-command.model.js";
export const projectDeviceRowSyncCommandType =
"project-device.sync-rows" as const;
export const projectDeviceRowSyncCommandSchemaVersion = 1 as const;
export const projectDeviceSyncRowSnapshotFields = [
"linkedProjectDeviceId",
"name",
"displayName",
"phaseType",
"connectionKind",
"costGroup",
"category",
"quantity",
"powerPerUnit",
"simultaneityFactor",
"cosPhi",
"remark",
"overriddenFields",
] as const satisfies readonly CircuitDeviceRowUpdateField[];
export type ProjectDeviceSyncRowSnapshot = Pick<
CircuitDeviceRowUpdateValues,
(typeof projectDeviceSyncRowSnapshotFields)[number]
>;
export type ProjectDeviceRowSyncOperation =
| "synchronize"
| "disconnect"
| "reconnect";
export interface ProjectDeviceRowSyncAssignment {
rowId: string;
expected: ProjectDeviceSyncRowSnapshot;
target: ProjectDeviceSyncRowSnapshot;
}
export interface ProjectDeviceRowSyncCommandPayload {
projectDeviceId: string;
operation: ProjectDeviceRowSyncOperation;
rows: ProjectDeviceRowSyncAssignment[];
}
export interface ProjectDeviceRowSyncProjectCommand
extends SerializedProjectCommand<ProjectDeviceRowSyncCommandPayload> {
schemaVersion: typeof projectDeviceRowSyncCommandSchemaVersion;
type: typeof projectDeviceRowSyncCommandType;
}
export function createProjectDeviceRowSyncProjectCommand(
projectDeviceId: string,
operation: ProjectDeviceRowSyncOperation,
rows: ProjectDeviceRowSyncAssignment[]
): ProjectDeviceRowSyncProjectCommand {
const command: ProjectDeviceRowSyncProjectCommand = {
schemaVersion: projectDeviceRowSyncCommandSchemaVersion,
type: projectDeviceRowSyncCommandType,
payload: { projectDeviceId, operation, rows },
};
assertProjectDeviceRowSyncProjectCommand(command);
return command;
}
export function assertProjectDeviceRowSyncProjectCommand(
command: SerializedProjectCommand<unknown>
): asserts command is ProjectDeviceRowSyncProjectCommand {
if (
command.schemaVersion !== projectDeviceRowSyncCommandSchemaVersion ||
command.type !== projectDeviceRowSyncCommandType
) {
throw new Error("Unsupported project-device row sync command.");
}
if (!isPlainObject(command.payload)) {
throw new Error(
"Project-device row sync payload must be an object."
);
}
const { projectDeviceId, operation, rows } = command.payload;
assertNonEmptyString(projectDeviceId, "projectDeviceId");
if (
operation !== "synchronize" &&
operation !== "disconnect" &&
operation !== "reconnect"
) {
throw new Error(
"Project-device row sync operation is invalid."
);
}
if (!Array.isArray(rows) || rows.length === 0) {
throw new Error(
"Project-device row sync command requires at least one row."
);
}
const rowIds = new Set<string>();
for (const row of rows) {
if (!isPlainObject(row)) {
throw new Error(
"Project-device row sync command contains an invalid row."
);
}
assertNonEmptyString(row.rowId, "rowId");
if (rowIds.has(row.rowId)) {
throw new Error(
"Project-device row sync command contains duplicate row ids."
);
}
assertProjectDeviceSyncRowSnapshot(row.expected);
assertProjectDeviceSyncRowSnapshot(row.target);
const expected =
row.expected as ProjectDeviceSyncRowSnapshot;
const target = row.target as ProjectDeviceSyncRowSnapshot;
if (snapshotsEqual(expected, target)) {
throw new Error(
"Project-device row sync command contains a no-op row."
);
}
assertOperationTransition(
operation,
projectDeviceId,
expected,
target
);
rowIds.add(row.rowId);
}
}
export function invertProjectDeviceRowSyncOperation(
operation: ProjectDeviceRowSyncOperation
): ProjectDeviceRowSyncOperation {
if (operation === "disconnect") {
return "reconnect";
}
if (operation === "reconnect") {
return "disconnect";
}
return "synchronize";
}
function assertProjectDeviceSyncRowSnapshot(snapshot: unknown) {
if (!isPlainObject(snapshot)) {
throw new Error(
"Project-device sync row snapshot must be an object."
);
}
const snapshotKeys = Object.keys(snapshot);
if (
snapshotKeys.length !== projectDeviceSyncRowSnapshotFields.length ||
projectDeviceSyncRowSnapshotFields.some(
(field) =>
!Object.prototype.hasOwnProperty.call(snapshot, field)
)
) {
throw new Error(
"Project-device sync row snapshot must contain every sync field."
);
}
assertCircuitDeviceRowUpdateProjectCommand({
schemaVersion: circuitDeviceRowUpdateCommandSchemaVersion,
type: circuitDeviceRowUpdateCommandType,
payload: {
rowId: "__sync_snapshot_validation__",
changes: projectDeviceSyncRowSnapshotFields.map((field) => ({
field,
value: snapshot[field],
})),
},
});
}
function assertOperationTransition(
operation: ProjectDeviceRowSyncOperation,
projectDeviceId: string,
expected: ProjectDeviceSyncRowSnapshot,
target: ProjectDeviceSyncRowSnapshot
) {
if (operation === "synchronize") {
if (
expected.linkedProjectDeviceId !== projectDeviceId ||
target.linkedProjectDeviceId !== projectDeviceId
) {
throw new Error(
"Synchronized rows must remain linked to the selected project device."
);
}
return;
}
const expectedLink =
operation === "disconnect" ? projectDeviceId : null;
const targetLink =
operation === "disconnect" ? null : projectDeviceId;
if (
expected.linkedProjectDeviceId !== expectedLink ||
target.linkedProjectDeviceId !== targetLink
) {
throw new Error(
"Project-device link transition does not match the operation."
);
}
if (!snapshotsEqualWithoutLink(expected, target)) {
throw new Error(
"Disconnect and reconnect must not change local row values."
);
}
}
function snapshotsEqual(
left: ProjectDeviceSyncRowSnapshot,
right: ProjectDeviceSyncRowSnapshot
) {
return projectDeviceSyncRowSnapshotFields.every(
(field) => left[field] === right[field]
);
}
function snapshotsEqualWithoutLink(
left: ProjectDeviceSyncRowSnapshot,
right: ProjectDeviceSyncRowSnapshot
) {
return projectDeviceSyncRowSnapshotFields
.filter((field) => field !== "linkedProjectDeviceId")
.every((field) => left[field] === right[field]);
}
function assertNonEmptyString(
value: unknown,
field: string
): asserts value is string {
if (typeof value !== "string" || !value.trim()) {
throw new Error(`${field} must be a non-empty string.`);
}
}
function isPlainObject(value: unknown): value is Record<string, unknown> {
return value !== null && typeof value === "object" && !Array.isArray(value);
}
@@ -0,0 +1,26 @@
import type { ProjectDeviceRowSyncProjectCommand } from "../models/project-device-row-sync-project-command.model.js";
import type {
AppendedProjectRevision,
ProjectRevisionSource,
} from "./project-revision.store.js";
export interface ExecuteProjectDeviceRowSyncCommandInput {
projectId: string;
expectedRevision: number;
source: ProjectRevisionSource;
description?: string;
actorId?: string;
historyTargetChangeSetId?: string;
command: ProjectDeviceRowSyncProjectCommand;
}
export interface ExecutedProjectDeviceRowSyncCommand {
revision: AppendedProjectRevision;
inverse: ProjectDeviceRowSyncProjectCommand;
}
export interface ProjectDeviceRowSyncProjectCommandStore {
execute(
input: ExecuteProjectDeviceRowSyncCommandInput
): ExecutedProjectDeviceRowSyncCommand;
}
@@ -37,6 +37,10 @@ import {
assertSerializedProjectCommand,
type SerializedProjectCommand,
} from "../models/project-command.model.js";
import {
assertProjectDeviceRowSyncProjectCommand,
projectDeviceRowSyncCommandType,
} from "../models/project-device-row-sync-project-command.model.js";
import type { CircuitDeviceRowProjectCommandStore } from "../ports/circuit-device-row-project-command.store.js";
import type { CircuitDeviceRowMoveProjectCommandStore } from "../ports/circuit-device-row-move-project-command.store.js";
import type { CircuitDeviceRowStructureProjectCommandStore } from "../ports/circuit-device-row-structure-project-command.store.js";
@@ -54,6 +58,7 @@ import type {
ProjectHistoryDirection,
ProjectHistoryStore,
} from "../ports/project-history.store.js";
import type { ProjectDeviceRowSyncProjectCommandStore } from "../ports/project-device-row-sync-project-command.store.js";
import type { ProjectRevisionSource } from "../ports/project-revision.store.js";
interface DispatchProjectCommandInput {
@@ -75,6 +80,7 @@ export class ProjectCommandService implements ProjectCommandExecutor {
private readonly circuitStructureStore: CircuitStructureProjectCommandStore,
private readonly circuitSectionReorderStore: CircuitSectionReorderProjectCommandStore,
private readonly circuitSectionRenumberStore: CircuitSectionRenumberProjectCommandStore,
private readonly projectDeviceRowSyncStore: ProjectDeviceRowSyncProjectCommandStore,
private readonly historyStore: ProjectHistoryStore
) {}
@@ -212,6 +218,13 @@ export class ProjectCommandService implements ProjectCommandExecutor {
command: input.command,
}).revision;
}
case projectDeviceRowSyncCommandType: {
assertProjectDeviceRowSyncProjectCommand(input.command);
return this.projectDeviceRowSyncStore.execute({
...input,
command: input.command,
}).revision;
}
default:
throw new Error(
`Unsupported project command type: ${input.command.type}.`