Prevent stale cell update overwrites
This commit is contained in:
@@ -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));
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user