Keep manual quantity consistent when synchronizing device rows
Synchronizing a project device pushes its quantity onto every linked device row, but manualQuantity is not part of the sync snapshot and was left untouched. A device whose quantity is lower than the row's left the row at manualQuantity > quantity, violating the snapshot invariant. The violation surfaced far from its cause: the invariant is only checked when a full state snapshot is read, and the automatic snapshot runs every 25 revisions. A project could therefore accumulate the broken row silently and then reject every subsequent command, because the failing validation rolls back the whole transaction including the revision bump. Recompute manualQuantity as the synchronized quantity minus the total of the linked external objects, matching the invariant that assertCircuitDeviceRowQuantity enforces elsewhere, and reject a synchronized quantity that falls below that total. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
This commit is contained in:
parent
9504905c8f
commit
c6cdfc42d5
2 changed files with 208 additions and 1 deletions
|
|
@ -14,6 +14,7 @@ import type { AppDatabase } from "../database-context.js";
|
|||
import { circuitDeviceRows } from "../schema/circuit-device-rows.js";
|
||||
import { circuitLists } from "../schema/circuit-lists.js";
|
||||
import { circuits } from "../schema/circuits.js";
|
||||
import { externalModelObjects } from "../schema/external-model-objects.js";
|
||||
import { projectDevices } from "../schema/project-devices.js";
|
||||
import { executeProjectCommandTransaction } from "./project-command-transaction.persistence.js";
|
||||
import { updateDerivedCircuitVoltage } from "./project-voltage.persistence.js";
|
||||
|
|
@ -119,9 +120,32 @@ export class ProjectDeviceRowSyncProjectCommandRepository
|
|||
);
|
||||
|
||||
for (const assignment of input.command.payload.rows) {
|
||||
const values: typeof assignment.target & {
|
||||
manualQuantity?: number;
|
||||
} = { ...assignment.target };
|
||||
if (assignment.target.quantity !== assignment.expected.quantity) {
|
||||
const externalTotal = tx
|
||||
.select({ planningValues: externalModelObjects.planningValues })
|
||||
.from(externalModelObjects)
|
||||
.where(
|
||||
eq(externalModelObjects.circuitDeviceRowId, assignment.rowId)
|
||||
)
|
||||
.all()
|
||||
.reduce(
|
||||
(sum, object) => sum + object.planningValues.effectiveQuantity,
|
||||
0
|
||||
);
|
||||
const manualQuantity = assignment.target.quantity - externalTotal;
|
||||
if (manualQuantity < 0) {
|
||||
throw new Error(
|
||||
"Synchronized quantity is below the total quantity of linked external objects."
|
||||
);
|
||||
}
|
||||
values.manualQuantity = manualQuantity;
|
||||
}
|
||||
const updated = tx
|
||||
.update(circuitDeviceRows)
|
||||
.set(assignment.target)
|
||||
.set(values)
|
||||
.where(eq(circuitDeviceRows.id, assignment.rowId))
|
||||
.run();
|
||||
if (updated.changes !== 1) {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue