Prevent stale cell update overwrites

This commit is contained in:
2026-07-23 17:43:11 +02:00
parent 0800f984bb
commit 1c57df46fa
5 changed files with 128 additions and 47 deletions
@@ -30,6 +30,10 @@ export interface CircuitDeviceRowUpdateInput {
overriddenFields?: string;
}
export type CircuitDeviceRowPatchInput = Partial<CircuitDeviceRowUpdateInput> & {
sortOrder?: number;
};
export interface CircuitDeviceRowCreateInput extends CircuitDeviceRowUpdateInput {
circuitId: string;
linkedProjectDeviceId?: string;
@@ -78,6 +82,39 @@ function toUpdateValues(input: CircuitDeviceRowUpdateInput) {
};
}
function toPatchValues(input: CircuitDeviceRowPatchInput) {
const values: Partial<typeof circuitDeviceRows.$inferInsert> = {};
const has = (field: keyof CircuitDeviceRowPatchInput) =>
Object.prototype.hasOwnProperty.call(input, field);
if (has("linkedProjectDeviceId")) {
values.linkedProjectDeviceId = input.linkedProjectDeviceId ?? null;
}
if (input.name !== undefined) values.name = input.name;
if (input.displayName !== undefined) values.displayName = input.displayName;
if (has("phaseType")) values.phaseType = input.phaseType ?? null;
if (has("connectionKind")) values.connectionKind = input.connectionKind ?? null;
if (has("costGroup")) values.costGroup = input.costGroup ?? null;
if (has("category")) values.category = input.category ?? null;
if (has("level")) values.level = input.level ?? null;
if (has("roomId")) values.roomId = input.roomId ?? null;
if (has("roomNumberSnapshot")) {
values.roomNumberSnapshot = input.roomNumberSnapshot ?? null;
}
if (has("roomNameSnapshot")) values.roomNameSnapshot = input.roomNameSnapshot ?? null;
if (input.quantity !== undefined) values.quantity = input.quantity;
if (input.powerPerUnit !== undefined) values.powerPerUnit = input.powerPerUnit;
if (input.simultaneityFactor !== undefined) {
values.simultaneityFactor = input.simultaneityFactor;
}
if (has("cosPhi")) values.cosPhi = input.cosPhi ?? null;
if (has("remark")) values.remark = input.remark ?? null;
if (has("overriddenFields")) values.overriddenFields = input.overriddenFields ?? null;
if (input.sortOrder !== undefined) values.sortOrder = input.sortOrder;
return values;
}
function toCreateValues(id: string, input: CircuitDeviceRowCreateInput) {
return {
id,
@@ -256,6 +293,14 @@ export class CircuitDeviceRowRepository {
.where(eq(circuitDeviceRows.id, rowId));
}
async updateFields(rowId: string, input: CircuitDeviceRowPatchInput) {
const values = toPatchValues(input);
if (Object.keys(values).length === 0) {
return;
}
await db.update(circuitDeviceRows).set(values).where(eq(circuitDeviceRows.id, rowId));
}
updateLinkedRowsTransactional(
projectDeviceId: string,
changes: Array<{ rowId: string; input: CircuitDeviceRowUpdateInput }>
+64
View File
@@ -24,6 +24,26 @@ export interface CircuitCreatePersistenceInput {
isReserve?: boolean;
}
export interface CircuitPatchPersistenceInput {
sectionId?: string;
equipmentIdentifier?: string;
displayName?: string;
sortOrder?: number;
protectionType?: string;
protectionRatedCurrent?: number;
protectionCharacteristic?: string;
cableType?: string;
cableCrossSection?: string;
cableLength?: number;
voltage?: number;
controlRequirement?: string;
remark?: string;
rcdAssignment?: string;
terminalDesignation?: string;
status?: string;
isReserve?: boolean;
}
export function toCircuitCreateValues(id: string, input: CircuitCreatePersistenceInput) {
return {
id,
@@ -48,6 +68,42 @@ export function toCircuitCreateValues(id: string, input: CircuitCreatePersistenc
};
}
function toCircuitPatchValues(input: CircuitPatchPersistenceInput) {
const values: Partial<typeof circuits.$inferInsert> = {};
const has = (field: keyof CircuitPatchPersistenceInput) =>
Object.prototype.hasOwnProperty.call(input, field);
if (input.sectionId !== undefined) values.sectionId = input.sectionId;
if (input.equipmentIdentifier !== undefined) {
values.equipmentIdentifier = input.equipmentIdentifier;
}
if (has("displayName")) values.displayName = input.displayName ?? null;
if (input.sortOrder !== undefined) values.sortOrder = input.sortOrder;
if (has("protectionType")) values.protectionType = input.protectionType ?? null;
if (has("protectionRatedCurrent")) {
values.protectionRatedCurrent = input.protectionRatedCurrent ?? null;
}
if (has("protectionCharacteristic")) {
values.protectionCharacteristic = input.protectionCharacteristic ?? null;
}
if (has("cableType")) values.cableType = input.cableType ?? null;
if (has("cableCrossSection")) values.cableCrossSection = input.cableCrossSection ?? null;
if (has("cableLength")) values.cableLength = input.cableLength ?? null;
if (has("rcdAssignment")) values.rcdAssignment = input.rcdAssignment ?? null;
if (has("terminalDesignation")) {
values.terminalDesignation = input.terminalDesignation ?? null;
}
if (has("voltage")) values.voltage = input.voltage ?? null;
if (has("controlRequirement")) {
values.controlRequirement = input.controlRequirement ?? null;
}
if (has("status")) values.status = input.status ?? null;
if (input.isReserve !== undefined) values.isReserve = input.isReserve ? 1 : 0;
if (has("remark")) values.remark = input.remark ?? null;
return values;
}
export class CircuitRepository {
async findById(circuitId: string) {
const [row] = await db.select().from(circuits).where(eq(circuits.id, circuitId)).limit(1);
@@ -114,6 +170,14 @@ export class CircuitRepository {
.where(eq(circuits.id, circuitId));
}
async updateFields(circuitId: string, input: CircuitPatchPersistenceInput) {
const values = toCircuitPatchValues(input);
if (Object.keys(values).length === 0) {
return;
}
await db.update(circuits).set(values).where(eq(circuits.id, circuitId));
}
async delete(circuitId: string) {
await db.delete(circuits).where(eq(circuits.id, circuitId));
}