Persist project settings updates
This commit is contained in:
@@ -0,0 +1,61 @@
|
||||
import type { SerializedProjectCommand } from "./project-command.model.js";
|
||||
|
||||
export const projectSettingsUpdateCommandType =
|
||||
"project.update-settings" as const;
|
||||
export const projectSettingsUpdateCommandSchemaVersion = 1 as const;
|
||||
|
||||
export interface ProjectSettingsValues {
|
||||
singlePhaseVoltageV: number;
|
||||
threePhaseVoltageV: number;
|
||||
}
|
||||
|
||||
export interface ProjectSettingsUpdateProjectCommand
|
||||
extends SerializedProjectCommand<ProjectSettingsValues> {
|
||||
schemaVersion: typeof projectSettingsUpdateCommandSchemaVersion;
|
||||
type: typeof projectSettingsUpdateCommandType;
|
||||
}
|
||||
|
||||
export function createProjectSettingsUpdateProjectCommand(
|
||||
values: ProjectSettingsValues
|
||||
): ProjectSettingsUpdateProjectCommand {
|
||||
const command: ProjectSettingsUpdateProjectCommand = {
|
||||
schemaVersion: projectSettingsUpdateCommandSchemaVersion,
|
||||
type: projectSettingsUpdateCommandType,
|
||||
payload: { ...values },
|
||||
};
|
||||
assertProjectSettingsUpdateProjectCommand(command);
|
||||
return command;
|
||||
}
|
||||
|
||||
export function assertProjectSettingsUpdateProjectCommand(
|
||||
command: SerializedProjectCommand<unknown>
|
||||
): asserts command is ProjectSettingsUpdateProjectCommand {
|
||||
if (
|
||||
command.schemaVersion !== projectSettingsUpdateCommandSchemaVersion ||
|
||||
command.type !== projectSettingsUpdateCommandType
|
||||
) {
|
||||
throw new Error("Unsupported project settings update command.");
|
||||
}
|
||||
if (
|
||||
!isPlainObject(command.payload) ||
|
||||
!isPositiveFiniteNumber(command.payload.singlePhaseVoltageV) ||
|
||||
!isPositiveFiniteNumber(command.payload.threePhaseVoltageV) ||
|
||||
Object.keys(command.payload).length !== 2
|
||||
) {
|
||||
throw new Error(
|
||||
"Project settings update command contains invalid voltages."
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
function isPositiveFiniteNumber(value: unknown): value is number {
|
||||
return (
|
||||
typeof value === "number" &&
|
||||
Number.isFinite(value) &&
|
||||
value > 0
|
||||
);
|
||||
}
|
||||
|
||||
function isPlainObject(value: unknown): value is Record<string, unknown> {
|
||||
return value !== null && typeof value === "object" && !Array.isArray(value);
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
import type { ProjectSettingsUpdateProjectCommand } from "../models/project-settings-project-command.model.js";
|
||||
import type {
|
||||
AppendedProjectRevision,
|
||||
ProjectRevisionSource,
|
||||
} from "./project-revision.store.js";
|
||||
|
||||
export interface ExecuteProjectSettingsUpdateCommandInput {
|
||||
projectId: string;
|
||||
expectedRevision: number;
|
||||
source: ProjectRevisionSource;
|
||||
description?: string;
|
||||
actorId?: string;
|
||||
historyTargetChangeSetId?: string;
|
||||
command: ProjectSettingsUpdateProjectCommand;
|
||||
}
|
||||
|
||||
export interface ExecutedProjectSettingsUpdateCommand {
|
||||
revision: AppendedProjectRevision;
|
||||
inverse: ProjectSettingsUpdateProjectCommand;
|
||||
}
|
||||
|
||||
export interface ProjectSettingsProjectCommandStore {
|
||||
executeUpdate(
|
||||
input: ExecuteProjectSettingsUpdateCommandInput
|
||||
): ExecutedProjectSettingsUpdateCommand;
|
||||
}
|
||||
@@ -45,6 +45,10 @@ import {
|
||||
assertProjectStateRestoreCommand,
|
||||
projectStateRestoreCommandType,
|
||||
} from "../models/project-state-restore-command.model.js";
|
||||
import {
|
||||
assertProjectSettingsUpdateProjectCommand,
|
||||
projectSettingsUpdateCommandType,
|
||||
} from "../models/project-settings-project-command.model.js";
|
||||
import {
|
||||
assertProjectDeviceRowSyncProjectCommand,
|
||||
projectDeviceRowSyncCommandType,
|
||||
@@ -80,6 +84,7 @@ import type { ProjectDeviceProjectCommandStore } from "../ports/project-device-p
|
||||
import type { ProjectDeviceRowSyncProjectCommandStore } from "../ports/project-device-row-sync-project-command.store.js";
|
||||
import type { ProjectDeviceStructureProjectCommandStore } from "../ports/project-device-structure-project-command.store.js";
|
||||
import type { ProjectRevisionSource } from "../ports/project-revision.store.js";
|
||||
import type { ProjectSettingsProjectCommandStore } from "../ports/project-settings-project-command.store.js";
|
||||
import type { ProjectStateRestoreCommandStore } from "../ports/project-state-restore-command.store.js";
|
||||
|
||||
interface DispatchProjectCommandInput {
|
||||
@@ -104,6 +109,7 @@ export class ProjectCommandService implements ProjectCommandExecutor {
|
||||
private readonly projectDeviceStructureStore: ProjectDeviceStructureProjectCommandStore,
|
||||
private readonly projectDeviceStore: ProjectDeviceProjectCommandStore,
|
||||
private readonly projectDeviceRowSyncStore: ProjectDeviceRowSyncProjectCommandStore,
|
||||
private readonly projectSettingsStore: ProjectSettingsProjectCommandStore,
|
||||
private readonly projectStateRestoreStore: ProjectStateRestoreCommandStore,
|
||||
private readonly historyStore: ProjectHistoryStore
|
||||
) {}
|
||||
@@ -294,6 +300,13 @@ export class ProjectCommandService implements ProjectCommandExecutor {
|
||||
command: input.command,
|
||||
}).revision;
|
||||
}
|
||||
case projectSettingsUpdateCommandType: {
|
||||
assertProjectSettingsUpdateProjectCommand(input.command);
|
||||
return this.projectSettingsStore.executeUpdate({
|
||||
...input,
|
||||
command: input.command,
|
||||
}).revision;
|
||||
}
|
||||
case projectStateRestoreCommandType: {
|
||||
assertProjectStateRestoreCommand(input.command);
|
||||
return this.projectStateRestoreStore.execute({
|
||||
|
||||
Reference in New Issue
Block a user