Make single row moves atomic
This commit is contained in:
@@ -316,7 +316,18 @@ export class CircuitWriteService {
|
||||
throw new Error("Invalid target circuit id.");
|
||||
}
|
||||
|
||||
// Placeholder-target move creates a new circuit explicitly; no implicit renumbering of others.
|
||||
let createTargetCircuit:
|
||||
| {
|
||||
circuitListId: string;
|
||||
sectionId: string;
|
||||
equipmentIdentifier: string;
|
||||
displayName: string;
|
||||
sortOrder: number;
|
||||
}
|
||||
| undefined;
|
||||
|
||||
// Placeholder-target move prepares a new circuit explicitly. Its creation and
|
||||
// the row assignment are committed together without renumbering other circuits.
|
||||
if (!targetCircuit) {
|
||||
if (!input.targetSectionId || !input.createNewCircuit) {
|
||||
throw new Error("Invalid move target.");
|
||||
@@ -326,76 +337,27 @@ export class CircuitWriteService {
|
||||
const sectionCircuits = await this.circuitRepository.listBySection(section.id);
|
||||
const nextSortOrder =
|
||||
sectionCircuits.length > 0 ? Math.max(...sectionCircuits.map((circuit) => circuit.sortOrder)) + 10 : 10;
|
||||
const createdId = await this.circuitRepository.create({
|
||||
createTargetCircuit = {
|
||||
circuitListId: sourceCircuit.circuitListId,
|
||||
sectionId: section.id,
|
||||
equipmentIdentifier: nextIdentifier,
|
||||
displayName: "Neuer Stromkreis",
|
||||
sortOrder: nextSortOrder,
|
||||
isReserve: false,
|
||||
});
|
||||
targetCircuit = await this.circuitRepository.findById(createdId);
|
||||
if (!targetCircuit) {
|
||||
throw new Error("Failed to create target circuit.");
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
if (targetCircuit.circuitListId !== sourceCircuit.circuitListId) {
|
||||
if (targetCircuit && targetCircuit.circuitListId !== sourceCircuit.circuitListId) {
|
||||
throw new Error("Target circuit does not belong to same circuit list.");
|
||||
}
|
||||
if (targetCircuit.id === sourceCircuit.id) {
|
||||
if (targetCircuit?.id === sourceCircuit.id) {
|
||||
return this.deviceRowRepository.findById(rowId);
|
||||
}
|
||||
|
||||
const targetCount = await this.deviceRowRepository.countByCircuit(targetCircuit.id);
|
||||
await this.deviceRowRepository.moveToCircuit(rowId, targetCircuit.id, (targetCount + 1) * 10);
|
||||
|
||||
const sourceRemaining = await this.deviceRowRepository.countByCircuit(sourceCircuit.id);
|
||||
// Source circuit becomes reserve when all rows are moved away.
|
||||
if (sourceRemaining === 0) {
|
||||
await this.circuitRepository.update(sourceCircuit.id, {
|
||||
sectionId: sourceCircuit.sectionId,
|
||||
equipmentIdentifier: sourceCircuit.equipmentIdentifier,
|
||||
displayName: sourceCircuit.displayName ?? undefined,
|
||||
sortOrder: sourceCircuit.sortOrder,
|
||||
protectionType: sourceCircuit.protectionType ?? undefined,
|
||||
protectionRatedCurrent: sourceCircuit.protectionRatedCurrent ?? undefined,
|
||||
protectionCharacteristic: sourceCircuit.protectionCharacteristic ?? undefined,
|
||||
cableType: sourceCircuit.cableType ?? undefined,
|
||||
cableCrossSection: sourceCircuit.cableCrossSection ?? undefined,
|
||||
cableLength: sourceCircuit.cableLength ?? undefined,
|
||||
rcdAssignment: sourceCircuit.rcdAssignment ?? undefined,
|
||||
terminalDesignation: sourceCircuit.terminalDesignation ?? undefined,
|
||||
voltage: sourceCircuit.voltage ?? undefined,
|
||||
controlRequirement: sourceCircuit.controlRequirement ?? undefined,
|
||||
status: sourceCircuit.status ?? undefined,
|
||||
isReserve: true,
|
||||
remark: sourceCircuit.remark ?? undefined,
|
||||
});
|
||||
}
|
||||
|
||||
// Target circuit is no longer reserve once it receives moved rows.
|
||||
if (Boolean(targetCircuit.isReserve)) {
|
||||
await this.circuitRepository.update(targetCircuit.id, {
|
||||
sectionId: targetCircuit.sectionId,
|
||||
equipmentIdentifier: targetCircuit.equipmentIdentifier,
|
||||
displayName: targetCircuit.displayName ?? undefined,
|
||||
sortOrder: targetCircuit.sortOrder,
|
||||
protectionType: targetCircuit.protectionType ?? undefined,
|
||||
protectionRatedCurrent: targetCircuit.protectionRatedCurrent ?? undefined,
|
||||
protectionCharacteristic: targetCircuit.protectionCharacteristic ?? undefined,
|
||||
cableType: targetCircuit.cableType ?? undefined,
|
||||
cableCrossSection: targetCircuit.cableCrossSection ?? undefined,
|
||||
cableLength: targetCircuit.cableLength ?? undefined,
|
||||
rcdAssignment: targetCircuit.rcdAssignment ?? undefined,
|
||||
terminalDesignation: targetCircuit.terminalDesignation ?? undefined,
|
||||
voltage: targetCircuit.voltage ?? undefined,
|
||||
controlRequirement: targetCircuit.controlRequirement ?? undefined,
|
||||
status: targetCircuit.status ?? undefined,
|
||||
isReserve: false,
|
||||
remark: targetCircuit.remark ?? undefined,
|
||||
});
|
||||
}
|
||||
this.deviceRowRepository.moveRowsTransactional({
|
||||
rows: [{ id: row.id, expectedCircuitId: row.circuitId }],
|
||||
targetCircuitId: targetCircuit?.id,
|
||||
createTargetCircuit,
|
||||
});
|
||||
|
||||
return this.deviceRowRepository.findById(rowId);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user