Persist circuit protection updates
This commit is contained in:
@@ -0,0 +1,108 @@
|
||||
import { protectionDeviceConfigurationSchema } from "../../shared/validation/protection-device.schemas.js";
|
||||
import type {
|
||||
BreakerTripCharacteristic,
|
||||
FuseUtilizationCategory,
|
||||
ProtectionDeviceType,
|
||||
RcdType,
|
||||
} from "../../shared/constants/protection-device.js";
|
||||
import type { SerializedProjectCommand } from "./project-command.model.js";
|
||||
|
||||
export const circuitProtectionUpdateCommandType =
|
||||
"circuit-protection.update" as const;
|
||||
export const circuitProtectionUpdateCommandSchemaVersion = 1 as const;
|
||||
|
||||
export interface CircuitProtectionSnapshot {
|
||||
circuitId: string;
|
||||
type: ProtectionDeviceType;
|
||||
ratedCurrentA: number;
|
||||
fuseUtilizationCategory: FuseUtilizationCategory | null;
|
||||
tripCharacteristic: BreakerTripCharacteristic | null;
|
||||
rcdType: RcdType | null;
|
||||
ratedResidualCurrentMa: number | null;
|
||||
}
|
||||
|
||||
interface CircuitProtectionUpdatePayload {
|
||||
circuitId: string;
|
||||
expected: CircuitProtectionSnapshot | null;
|
||||
target: CircuitProtectionSnapshot | null;
|
||||
}
|
||||
|
||||
export interface CircuitProtectionUpdateProjectCommand
|
||||
extends SerializedProjectCommand<CircuitProtectionUpdatePayload> {
|
||||
schemaVersion: typeof circuitProtectionUpdateCommandSchemaVersion;
|
||||
type: typeof circuitProtectionUpdateCommandType;
|
||||
}
|
||||
|
||||
export function createCircuitProtectionUpdateProjectCommand(
|
||||
circuitId: string,
|
||||
expected: CircuitProtectionSnapshot | null,
|
||||
target: CircuitProtectionSnapshot | null
|
||||
): CircuitProtectionUpdateProjectCommand {
|
||||
const command: CircuitProtectionUpdateProjectCommand = {
|
||||
schemaVersion: circuitProtectionUpdateCommandSchemaVersion,
|
||||
type: circuitProtectionUpdateCommandType,
|
||||
payload: { circuitId, expected, target },
|
||||
};
|
||||
assertCircuitProtectionUpdateProjectCommand(command);
|
||||
return command;
|
||||
}
|
||||
|
||||
export function assertCircuitProtectionUpdateProjectCommand(
|
||||
command: SerializedProjectCommand<unknown>
|
||||
): asserts command is CircuitProtectionUpdateProjectCommand {
|
||||
if (
|
||||
command.schemaVersion !== circuitProtectionUpdateCommandSchemaVersion ||
|
||||
command.type !== circuitProtectionUpdateCommandType ||
|
||||
!isPlainObject(command.payload) ||
|
||||
Object.keys(command.payload).length !== 3
|
||||
) {
|
||||
throw new Error("Unsupported circuit-protection update command.");
|
||||
}
|
||||
const { circuitId, expected, target } = command.payload;
|
||||
if (typeof circuitId !== "string" || !circuitId.trim()) {
|
||||
throw new Error("Circuit protection requires a circuit id.");
|
||||
}
|
||||
if (expected === null && target === null) {
|
||||
throw new Error("Circuit protection update must change state.");
|
||||
}
|
||||
if (expected !== null) {
|
||||
assertSnapshot(expected, circuitId);
|
||||
}
|
||||
if (target !== null) {
|
||||
assertSnapshot(target, circuitId);
|
||||
}
|
||||
if (JSON.stringify(expected) === JSON.stringify(target)) {
|
||||
throw new Error("Circuit protection update must change state.");
|
||||
}
|
||||
}
|
||||
|
||||
function assertSnapshot(value: unknown, circuitId: string) {
|
||||
if (
|
||||
!isPlainObject(value) ||
|
||||
Object.keys(value).length !== 7 ||
|
||||
value.circuitId !== circuitId
|
||||
) {
|
||||
throw new Error("Circuit-protection snapshot is invalid.");
|
||||
}
|
||||
const result = protectionDeviceConfigurationSchema.safeParse({
|
||||
type: value.type,
|
||||
ratedCurrentA: value.ratedCurrentA,
|
||||
...(value.fuseUtilizationCategory === null
|
||||
? {}
|
||||
: { fuseUtilizationCategory: value.fuseUtilizationCategory }),
|
||||
...(value.tripCharacteristic === null
|
||||
? {}
|
||||
: { tripCharacteristic: value.tripCharacteristic }),
|
||||
...(value.rcdType === null ? {} : { rcdType: value.rcdType }),
|
||||
...(value.ratedResidualCurrentMa === null
|
||||
? {}
|
||||
: { ratedResidualCurrentMa: value.ratedResidualCurrentMa }),
|
||||
});
|
||||
if (!result.success) {
|
||||
throw new Error("Circuit-protection configuration is invalid.");
|
||||
}
|
||||
}
|
||||
|
||||
function isPlainObject(value: unknown): value is Record<string, unknown> {
|
||||
return value !== null && typeof value === "object" && !Array.isArray(value);
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
import type {
|
||||
CircuitProtectionUpdateProjectCommand,
|
||||
} from "../models/circuit-protection-project-command.model.js";
|
||||
import type {
|
||||
AppendedProjectRevision,
|
||||
ProjectRevisionSource,
|
||||
} from "./project-revision.store.js";
|
||||
|
||||
export interface ExecuteCircuitProtectionCommandInput {
|
||||
projectId: string;
|
||||
expectedRevision: number;
|
||||
source: ProjectRevisionSource;
|
||||
description?: string;
|
||||
actorId?: string;
|
||||
historyTargetChangeSetId?: string;
|
||||
command: CircuitProtectionUpdateProjectCommand;
|
||||
}
|
||||
|
||||
export interface CircuitProtectionProjectCommandStore {
|
||||
execute(input: ExecuteCircuitProtectionCommandInput): {
|
||||
revision: AppendedProjectRevision;
|
||||
inverse: CircuitProtectionUpdateProjectCommand;
|
||||
};
|
||||
}
|
||||
@@ -19,6 +19,10 @@ import {
|
||||
assertCircuitUpdateProjectCommand,
|
||||
circuitUpdateCommandType,
|
||||
} from "../models/circuit-project-command.model.js";
|
||||
import {
|
||||
assertCircuitProtectionUpdateProjectCommand,
|
||||
circuitProtectionUpdateCommandType,
|
||||
} from "../models/circuit-protection-project-command.model.js";
|
||||
import {
|
||||
assertCircuitSectionReorderProjectCommand,
|
||||
circuitSectionReorderCommandType,
|
||||
@@ -95,6 +99,7 @@ import type { CircuitDeviceRowProjectCommandStore } from "../ports/circuit-devic
|
||||
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";
|
||||
import type { CircuitProjectCommandStore } from "../ports/circuit-project-command.store.js";
|
||||
import type { CircuitProtectionProjectCommandStore } from "../ports/circuit-protection-project-command.store.js";
|
||||
import type { CircuitSectionReorderProjectCommandStore } from "../ports/circuit-section-reorder-project-command.store.js";
|
||||
import type { CircuitSectionRenumberProjectCommandStore } from "../ports/circuit-section-renumber-project-command.store.js";
|
||||
import type { CircuitStructureProjectCommandStore } from "../ports/circuit-structure-project-command.store.js";
|
||||
@@ -177,6 +182,7 @@ export class ProjectCommandService implements ProjectCommandExecutor {
|
||||
private readonly circuitGroupRenumberStore: CircuitGroupRenumberProjectCommandStore,
|
||||
private readonly circuitGroupMoveStore: CircuitGroupMoveProjectCommandStore,
|
||||
private readonly circuitGroupSubtreeStore: CircuitGroupSubtreeProjectCommandStore,
|
||||
private readonly circuitProtectionStore: CircuitProtectionProjectCommandStore,
|
||||
private readonly historyStore: ProjectHistoryStore
|
||||
) {}
|
||||
|
||||
@@ -421,6 +427,13 @@ export class ProjectCommandService implements ProjectCommandExecutor {
|
||||
command: input.command,
|
||||
}).revision;
|
||||
}
|
||||
case circuitProtectionUpdateCommandType: {
|
||||
assertCircuitProtectionUpdateProjectCommand(input.command);
|
||||
return this.circuitProtectionStore.execute({
|
||||
...input,
|
||||
command: input.command,
|
||||
}).revision;
|
||||
}
|
||||
case projectFloorInsertCommandType: {
|
||||
assertProjectFloorInsertProjectCommand(input.command);
|
||||
return this.projectLocationStructureStore.execute({
|
||||
|
||||
Reference in New Issue
Block a user