Persist project device synchronization
This commit is contained in:
@@ -1,6 +1,11 @@
|
||||
import { CircuitDeviceRowRepository } from "../../db/repositories/circuit-device-row.repository.js";
|
||||
import { ProjectDeviceRepository } from "../../db/repositories/project-device.repository.js";
|
||||
import type { ProjectDeviceRowSyncStore } from "../ports/project-device-row-sync.store.js";
|
||||
import {
|
||||
createProjectDeviceRowSyncProjectCommand,
|
||||
projectDeviceSyncRowSnapshotFields,
|
||||
type ProjectDeviceRowSyncProjectCommand,
|
||||
type ProjectDeviceSyncRowSnapshot,
|
||||
} from "../models/project-device-row-sync-project-command.model.js";
|
||||
import {
|
||||
projectDeviceSyncFields,
|
||||
type ProjectDeviceSyncField,
|
||||
@@ -20,20 +25,9 @@ type LinkedRow = Awaited<ReturnType<CircuitDeviceRowRepository["listLinkedByProj
|
||||
|
||||
type SyncDependencies = {
|
||||
projectDeviceRepository: Pick<ProjectDeviceRepository, "findById">;
|
||||
deviceRowRepository: Pick<
|
||||
CircuitDeviceRowRepository,
|
||||
| "listLinkedByProjectDevice"
|
||||
| "listLinkStatesByIds"
|
||||
>;
|
||||
deviceRowSyncStore: ProjectDeviceRowSyncStore;
|
||||
deviceRowRepository: Pick<CircuitDeviceRowRepository, "listLinkedByProjectDevice">;
|
||||
};
|
||||
|
||||
export interface ProjectDeviceSyncRestoreRow {
|
||||
rowId: string;
|
||||
values: Partial<Record<ProjectDeviceSyncField, string | number | null>>;
|
||||
overriddenFields: ProjectDeviceSyncField[];
|
||||
}
|
||||
|
||||
function sourceValue(projectDevice: ProjectDevice, field: ProjectDeviceSyncField) {
|
||||
return projectDevice[field];
|
||||
}
|
||||
@@ -56,19 +50,10 @@ function buildDifferences(projectDevice: ProjectDevice, row: LinkedRow) {
|
||||
export class ProjectDeviceSyncService {
|
||||
private readonly projectDeviceRepository: SyncDependencies["projectDeviceRepository"];
|
||||
private readonly deviceRowRepository: SyncDependencies["deviceRowRepository"];
|
||||
private readonly deviceRowSyncStore?: SyncDependencies["deviceRowSyncStore"];
|
||||
|
||||
constructor(deps?: Partial<SyncDependencies>) {
|
||||
this.projectDeviceRepository = deps?.projectDeviceRepository ?? new ProjectDeviceRepository();
|
||||
this.deviceRowRepository = deps?.deviceRowRepository ?? new CircuitDeviceRowRepository();
|
||||
this.deviceRowSyncStore = deps?.deviceRowSyncStore;
|
||||
}
|
||||
|
||||
private getDeviceRowSyncStore() {
|
||||
if (!this.deviceRowSyncStore) {
|
||||
throw new Error("Project-device row synchronization is not configured.");
|
||||
}
|
||||
return this.deviceRowSyncStore;
|
||||
}
|
||||
|
||||
async getPreview(projectId: string, projectDeviceId: string) {
|
||||
@@ -100,12 +85,12 @@ export class ProjectDeviceSyncService {
|
||||
};
|
||||
}
|
||||
|
||||
async synchronize(
|
||||
async createSynchronizeCommand(
|
||||
projectId: string,
|
||||
projectDeviceId: string,
|
||||
rowIds: string[],
|
||||
fields: ProjectDeviceSyncField[]
|
||||
) {
|
||||
): Promise<ProjectDeviceRowSyncProjectCommand> {
|
||||
const projectDevice = await this.projectDeviceRepository.findById(projectId, projectDeviceId);
|
||||
if (!projectDevice) {
|
||||
throw new Error("Project device not found.");
|
||||
@@ -113,81 +98,60 @@ export class ProjectDeviceSyncService {
|
||||
const linkedRows = await this.deviceRowRepository.listLinkedByProjectDevice(projectId, projectDeviceId);
|
||||
const selectedRows = this.resolveSelectedRows(linkedRows, rowIds);
|
||||
const selectedFields = [...new Set(fields)];
|
||||
const undoRows: ProjectDeviceSyncRestoreRow[] = selectedRows.map((row) => ({
|
||||
rowId: row.id,
|
||||
values: Object.fromEntries(selectedFields.map((field) => [field, row[field] ?? null])),
|
||||
overriddenFields: parseOverriddenFields(row.overriddenFields),
|
||||
}));
|
||||
|
||||
const changes = [];
|
||||
for (const row of selectedRows) {
|
||||
const next = this.copyRow(row);
|
||||
const assignments = selectedRows.flatMap((row) => {
|
||||
const expected = toSyncSnapshot(row);
|
||||
const target = { ...expected };
|
||||
for (const field of selectedFields) {
|
||||
Object.assign(next, { [field]: sourceValue(projectDevice, field) ?? undefined });
|
||||
Object.assign(target, {
|
||||
[field]: sourceValue(projectDevice, field) ?? null,
|
||||
});
|
||||
}
|
||||
const remainingOverrides = parseOverriddenFields(row.overriddenFields).filter(
|
||||
(field) => !selectedFields.includes(field)
|
||||
);
|
||||
next.overriddenFields = serializeOverriddenFields(remainingOverrides);
|
||||
changes.push({ rowId: row.id, input: next });
|
||||
}
|
||||
this.getDeviceRowSyncStore().updateLinkedRows(projectDeviceId, changes);
|
||||
|
||||
return {
|
||||
preview: await this.getPreview(projectId, projectDeviceId),
|
||||
undo: { rows: undoRows },
|
||||
};
|
||||
}
|
||||
|
||||
async disconnect(projectId: string, projectDeviceId: string, rowIds: string[]) {
|
||||
const linkedRows = await this.deviceRowRepository.listLinkedByProjectDevice(projectId, projectDeviceId);
|
||||
const selectedRows = this.resolveSelectedRows(linkedRows, rowIds);
|
||||
const disconnectedRowIds = selectedRows.map((row) => row.id);
|
||||
|
||||
this.getDeviceRowSyncStore().disconnectLinkedRows(
|
||||
projectDeviceId,
|
||||
disconnectedRowIds
|
||||
);
|
||||
|
||||
return { disconnectedRowIds, undo: { rowIds: disconnectedRowIds } };
|
||||
}
|
||||
|
||||
async restore(
|
||||
projectId: string,
|
||||
projectDeviceId: string,
|
||||
restoreRows: ProjectDeviceSyncRestoreRow[]
|
||||
) {
|
||||
const linkedRows = await this.deviceRowRepository.listLinkedByProjectDevice(projectId, projectDeviceId);
|
||||
const selectedRows = this.resolveSelectedRows(linkedRows, restoreRows.map((row) => row.rowId));
|
||||
const restoreById = new Map(restoreRows.map((row) => [row.rowId, row]));
|
||||
const changes = selectedRows.map((row) => {
|
||||
const restore = restoreById.get(row.id)!;
|
||||
const next = this.copyRow(row);
|
||||
for (const [field, value] of Object.entries(restore.values) as Array<
|
||||
[ProjectDeviceSyncField, string | number | null]
|
||||
>) {
|
||||
Object.assign(next, { [field]: value ?? undefined });
|
||||
}
|
||||
next.overriddenFields = serializeOverriddenFields(restore.overriddenFields);
|
||||
return { rowId: row.id, input: next };
|
||||
target.overriddenFields =
|
||||
serializeOverriddenFields(remainingOverrides) ?? null;
|
||||
return snapshotsEqual(expected, target)
|
||||
? []
|
||||
: [{ rowId: row.id, expected, target }];
|
||||
});
|
||||
|
||||
this.getDeviceRowSyncStore().updateLinkedRows(projectDeviceId, changes);
|
||||
return this.getPreview(projectId, projectDeviceId);
|
||||
return createProjectDeviceRowSyncProjectCommand(
|
||||
projectDeviceId,
|
||||
"synchronize",
|
||||
assignments
|
||||
);
|
||||
}
|
||||
|
||||
async reconnect(projectId: string, projectDeviceId: string, rowIds: string[]) {
|
||||
const projectDevice = await this.projectDeviceRepository.findById(projectId, projectDeviceId);
|
||||
async createDisconnectCommand(
|
||||
projectId: string,
|
||||
projectDeviceId: string,
|
||||
rowIds: string[]
|
||||
): Promise<ProjectDeviceRowSyncProjectCommand> {
|
||||
const projectDevice = await this.projectDeviceRepository.findById(
|
||||
projectId,
|
||||
projectDeviceId
|
||||
);
|
||||
if (!projectDevice) {
|
||||
throw new Error("Project device not found.");
|
||||
}
|
||||
const uniqueRowIds = [...new Set(rowIds)];
|
||||
const linkStates = await this.deviceRowRepository.listLinkStatesByIds(projectId, uniqueRowIds);
|
||||
if (linkStates.length !== uniqueRowIds.length || linkStates.some((row) => row.linkedProjectDeviceId !== null)) {
|
||||
throw new Error("One or more rows cannot be reconnected.");
|
||||
}
|
||||
this.getDeviceRowSyncStore().reconnectRows(projectDeviceId, uniqueRowIds);
|
||||
return this.getPreview(projectId, projectDeviceId);
|
||||
const linkedRows = await this.deviceRowRepository.listLinkedByProjectDevice(projectId, projectDeviceId);
|
||||
const selectedRows = this.resolveSelectedRows(linkedRows, rowIds);
|
||||
return createProjectDeviceRowSyncProjectCommand(
|
||||
projectDeviceId,
|
||||
"disconnect",
|
||||
selectedRows.map((row) => {
|
||||
const expected = toSyncSnapshot(row);
|
||||
return {
|
||||
rowId: row.id,
|
||||
expected,
|
||||
target: {
|
||||
...expected,
|
||||
linkedProjectDeviceId: null,
|
||||
},
|
||||
};
|
||||
})
|
||||
);
|
||||
}
|
||||
|
||||
private resolveSelectedRows(linkedRows: LinkedRow[], rowIds: string[]) {
|
||||
@@ -200,25 +164,31 @@ export class ProjectDeviceSyncService {
|
||||
return selectedRows;
|
||||
}
|
||||
|
||||
private copyRow(row: LinkedRow) {
|
||||
return {
|
||||
linkedProjectDeviceId: row.linkedProjectDeviceId ?? undefined,
|
||||
name: row.name,
|
||||
displayName: row.displayName,
|
||||
phaseType: row.phaseType ?? undefined,
|
||||
connectionKind: row.connectionKind ?? undefined,
|
||||
costGroup: row.costGroup ?? undefined,
|
||||
category: row.category ?? undefined,
|
||||
level: row.level ?? undefined,
|
||||
roomId: row.roomId ?? undefined,
|
||||
roomNumberSnapshot: row.roomNumberSnapshot ?? undefined,
|
||||
roomNameSnapshot: row.roomNameSnapshot ?? undefined,
|
||||
quantity: row.quantity,
|
||||
powerPerUnit: row.powerPerUnit,
|
||||
simultaneityFactor: row.simultaneityFactor,
|
||||
cosPhi: row.cosPhi ?? undefined,
|
||||
remark: row.remark ?? undefined,
|
||||
overriddenFields: row.overriddenFields ?? undefined,
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
function toSyncSnapshot(row: LinkedRow): ProjectDeviceSyncRowSnapshot {
|
||||
return {
|
||||
linkedProjectDeviceId: row.linkedProjectDeviceId,
|
||||
name: row.name,
|
||||
displayName: row.displayName,
|
||||
phaseType: row.phaseType,
|
||||
connectionKind: row.connectionKind,
|
||||
costGroup: row.costGroup,
|
||||
category: row.category,
|
||||
quantity: row.quantity,
|
||||
powerPerUnit: row.powerPerUnit,
|
||||
simultaneityFactor: row.simultaneityFactor,
|
||||
cosPhi: row.cosPhi,
|
||||
remark: row.remark,
|
||||
overriddenFields: row.overriddenFields,
|
||||
};
|
||||
}
|
||||
|
||||
function snapshotsEqual(
|
||||
left: ProjectDeviceSyncRowSnapshot,
|
||||
right: ProjectDeviceSyncRowSnapshot
|
||||
) {
|
||||
return projectDeviceSyncRowSnapshotFields.every(
|
||||
(field) => left[field] === right[field]
|
||||
);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user