Add component update commands

This commit is contained in:
2026-07-30 19:42:09 +02:00
parent 604604f056
commit 69b020339e
11 changed files with 547 additions and 21 deletions
@@ -13,6 +13,8 @@ export const distributionBoardComponentInsertCommandType =
"distribution-board-component.insert" as const;
export const distributionBoardComponentDeleteCommandType =
"distribution-board-component.delete" as const;
export const distributionBoardComponentUpdateCommandType =
"distribution-board-component.update" as const;
export const distributionBoardComponentStructureCommandSchemaVersion =
1 as const;
@@ -47,6 +49,11 @@ interface DistributionBoardComponentStructurePayload {
snapshot: DistributionBoardComponentSnapshot;
}
interface DistributionBoardComponentUpdatePayload {
expected: DistributionBoardComponentSnapshot;
target: DistributionBoardComponentSnapshot;
}
export interface DistributionBoardComponentInsertProjectCommand
extends SerializedProjectCommand<DistributionBoardComponentStructurePayload> {
schemaVersion: typeof distributionBoardComponentStructureCommandSchemaVersion;
@@ -59,9 +66,16 @@ export interface DistributionBoardComponentDeleteProjectCommand
type: typeof distributionBoardComponentDeleteCommandType;
}
export interface DistributionBoardComponentUpdateProjectCommand
extends SerializedProjectCommand<DistributionBoardComponentUpdatePayload> {
schemaVersion: typeof distributionBoardComponentStructureCommandSchemaVersion;
type: typeof distributionBoardComponentUpdateCommandType;
}
export type DistributionBoardComponentStructureProjectCommand =
| DistributionBoardComponentInsertProjectCommand
| DistributionBoardComponentDeleteProjectCommand;
| DistributionBoardComponentDeleteProjectCommand
| DistributionBoardComponentUpdateProjectCommand;
export function createDistributionBoardComponentInsertProjectCommand(
snapshot: DistributionBoardComponentSnapshot
@@ -89,6 +103,20 @@ export function createDistributionBoardComponentDeleteProjectCommand(
return command;
}
export function createDistributionBoardComponentUpdateProjectCommand(
expected: DistributionBoardComponentSnapshot,
target: DistributionBoardComponentSnapshot
): DistributionBoardComponentUpdateProjectCommand {
const command: DistributionBoardComponentUpdateProjectCommand = {
schemaVersion:
distributionBoardComponentStructureCommandSchemaVersion,
type: distributionBoardComponentUpdateCommandType,
payload: { expected, target },
};
assertDistributionBoardComponentUpdateProjectCommand(command);
return command;
}
export function assertDistributionBoardComponentInsertProjectCommand(
command: SerializedProjectCommand<unknown>
): asserts command is DistributionBoardComponentInsertProjectCommand {
@@ -107,6 +135,28 @@ export function assertDistributionBoardComponentDeleteProjectCommand(
);
}
export function assertDistributionBoardComponentUpdateProjectCommand(
command: SerializedProjectCommand<unknown>
): asserts command is DistributionBoardComponentUpdateProjectCommand {
if (
command.schemaVersion !==
distributionBoardComponentStructureCommandSchemaVersion ||
command.type !== distributionBoardComponentUpdateCommandType ||
!isPlainObject(command.payload) ||
Object.keys(command.payload).length !== 2
) {
throw new Error(
"Unsupported distribution-board component update command."
);
}
assertDistributionBoardComponentSnapshot(command.payload.expected);
assertDistributionBoardComponentSnapshot(command.payload.target);
assertSameComponentIdentity(
command.payload.expected,
command.payload.target
);
}
function assertStructureCommand(
command: SerializedProjectCommand<unknown>,
type:
@@ -127,6 +177,25 @@ function assertStructureCommand(
assertDistributionBoardComponentSnapshot(command.payload.snapshot);
}
function assertSameComponentIdentity(
expected: DistributionBoardComponentSnapshot,
target: DistributionBoardComponentSnapshot
) {
for (const field of [
"id",
"circuitListId",
"sectionId",
"role",
"placement",
] as const) {
if (expected.component[field] !== target.component[field]) {
throw new Error(
"Distribution-board component updates cannot change ownership or role."
);
}
}
}
export function assertDistributionBoardComponentSnapshot(
value: unknown
): asserts value is DistributionBoardComponentSnapshot {