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 }>