Isolate project device sync transactions

This commit is contained in:
2026-07-23 18:57:18 +02:00
parent cf356bbedc
commit 91baa6c76c
10 changed files with 414 additions and 126 deletions
@@ -0,0 +1,28 @@
export interface ProjectDeviceLinkedRowValues {
linkedProjectDeviceId?: string;
name: string;
displayName: string;
phaseType?: string;
connectionKind?: string;
costGroup?: string;
category?: string;
level?: string;
roomId?: string;
roomNumberSnapshot?: string;
roomNameSnapshot?: string;
quantity: number;
powerPerUnit: number;
simultaneityFactor: number;
cosPhi?: number;
remark?: string;
overriddenFields?: string;
}
export interface ProjectDeviceRowSyncStore {
updateLinkedRows(
projectDeviceId: string,
changes: Array<{ rowId: string; input: ProjectDeviceLinkedRowValues }>
): void;
disconnectLinkedRows(projectDeviceId: string, rowIds: string[]): void;
reconnectRows(projectDeviceId: string, rowIds: string[]): void;
}
@@ -1,5 +1,6 @@
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 {
projectDeviceSyncFields,
type ProjectDeviceSyncField,
@@ -14,10 +15,8 @@ type SyncDependencies = {
CircuitDeviceRowRepository,
| "listLinkedByProjectDevice"
| "listLinkStatesByIds"
| "updateLinkedRowsTransactional"
| "disconnectLinkedRowsTransactional"
| "reconnectRowsTransactional"
>;
deviceRowSyncStore: ProjectDeviceRowSyncStore;
};
export interface ProjectDeviceSyncRestoreRow {
@@ -71,10 +70,19 @@ 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) {
@@ -137,7 +145,7 @@ export class ProjectDeviceSyncService {
next.overriddenFields = serializeOverriddenFields(remainingOverrides);
changes.push({ rowId: row.id, input: next });
}
this.deviceRowRepository.updateLinkedRowsTransactional(projectDeviceId, changes);
this.getDeviceRowSyncStore().updateLinkedRows(projectDeviceId, changes);
return {
preview: await this.getPreview(projectId, projectDeviceId),
@@ -150,7 +158,10 @@ export class ProjectDeviceSyncService {
const selectedRows = this.resolveSelectedRows(linkedRows, rowIds);
const disconnectedRowIds = selectedRows.map((row) => row.id);
this.deviceRowRepository.disconnectLinkedRowsTransactional(projectDeviceId, disconnectedRowIds);
this.getDeviceRowSyncStore().disconnectLinkedRows(
projectDeviceId,
disconnectedRowIds
);
return { disconnectedRowIds, undo: { rowIds: disconnectedRowIds } };
}
@@ -175,7 +186,7 @@ export class ProjectDeviceSyncService {
return { rowId: row.id, input: next };
});
this.deviceRowRepository.updateLinkedRowsTransactional(projectDeviceId, changes);
this.getDeviceRowSyncStore().updateLinkedRows(projectDeviceId, changes);
return this.getPreview(projectId, projectDeviceId);
}
@@ -189,7 +200,7 @@ export class ProjectDeviceSyncService {
if (linkStates.length !== uniqueRowIds.length || linkStates.some((row) => row.linkedProjectDeviceId !== null)) {
throw new Error("One or more rows cannot be reconnected.");
}
this.deviceRowRepository.reconnectRowsTransactional(projectDeviceId, uniqueRowIds);
this.getDeviceRowSyncStore().reconnectRows(projectDeviceId, uniqueRowIds);
return this.getPreview(projectId, projectDeviceId);
}