Full-codebase review turned up five real correctness/security bugs and
a dozen smaller inconsistencies; all are fixed here with matching test
coverage:
- BMK uniqueness silently allowed German-umlaut duplicates ("Ä1" vs
"ä1") because the DB's normalized index only folds ASCII case. Added
a shared Unicode-aware pre-check used by every circuit/component
insert and rename path (one of which had no pre-check at all).
- CircuitDeviceRow.simultaneityFactor had no upper bound at the row
level (command model and snapshot/restore schema), unlike every
sibling entity, letting a bad value silently corrupt power totals.
- Grid cell editing silently misread German thousands-separator input
("1.500" parsed as 1.5); "." is now rejected outright with a clear
message instead of guessing.
- The editor's shared command runner (runCommand/applyHistory) had no
re-entrancy guard, so a double click/drop could fire the same
command twice and race a BMK collision or revision conflict. Added a
synchronous ref guard plus isSaving on the buttons that lacked it.
- GET .../next-identifier leaked circuit-numbering state for sections
in other projects (no ownership check, 400 instead of 404). Moved
under /projects/:projectId and scoped it.
Also: added the missing circuits.section_id / circuit_device_rows.
circuit_id indexes (migration 0006), gave FormModal a focus trap /
Escape-to-close / focus restore and rebuilt ProjectSettingsModal on
top of it instead of duplicated markup, removed dead code (3 orphaned
domain model files, an unused persistence helper, a wrapper only used
by its own test), pointed the project page at GET /projects/:id
instead of listing+filtering client-side, closed the gap between the
documented 18 MB CSV limit and the ~17.17 MiB actually enforced, added
missing upper bounds on several free-text fields, filled in nine
missing German labels in the revision timeline, replaced a
key-order-fragile JSON.stringify equality check with a real field
comparison, made an implicit sort-order assumption in three
renumbering helpers explicit, cleared the sidebar's target selection
when it no longer resolves after a tree reload, and fixed
updateGlobalDevice to check-then-write instead of write-then-check.
Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
201 lines
6.3 KiB
TypeScript
201 lines
6.3 KiB
TypeScript
import type { SerializedProjectCommand } from "./project-command.model.js";
|
|
|
|
export const circuitDeviceRowInsertCommandType =
|
|
"circuit-device-row.insert" as const;
|
|
export const circuitDeviceRowDeleteCommandType =
|
|
"circuit-device-row.delete" as const;
|
|
export const circuitDeviceRowStructureCommandSchemaVersion = 1 as const;
|
|
|
|
export interface CircuitDeviceRowSnapshot {
|
|
id: string;
|
|
circuitId: string;
|
|
linkedProjectDeviceId: string | null;
|
|
sortOrder: number;
|
|
name: string;
|
|
displayName: string;
|
|
phaseType: string | null;
|
|
connectionKind: string | null;
|
|
costGroup: string | null;
|
|
category: string | null;
|
|
level: string | null;
|
|
roomId: string | null;
|
|
roomNumberSnapshot: string | null;
|
|
roomNameSnapshot: string | null;
|
|
quantity: number;
|
|
manualQuantity?: number;
|
|
powerPerUnit: number;
|
|
simultaneityFactor: number;
|
|
cosPhi: number | null;
|
|
remark: string | null;
|
|
overriddenFields: string | null;
|
|
}
|
|
|
|
export interface CircuitDeviceRowInsertCommandPayload {
|
|
row: CircuitDeviceRowSnapshot;
|
|
}
|
|
|
|
export interface CircuitDeviceRowDeleteCommandPayload {
|
|
rowId: string;
|
|
expectedCircuitId: string;
|
|
}
|
|
|
|
export interface CircuitDeviceRowInsertProjectCommand
|
|
extends SerializedProjectCommand<CircuitDeviceRowInsertCommandPayload> {
|
|
schemaVersion: typeof circuitDeviceRowStructureCommandSchemaVersion;
|
|
type: typeof circuitDeviceRowInsertCommandType;
|
|
}
|
|
|
|
export interface CircuitDeviceRowDeleteProjectCommand
|
|
extends SerializedProjectCommand<CircuitDeviceRowDeleteCommandPayload> {
|
|
schemaVersion: typeof circuitDeviceRowStructureCommandSchemaVersion;
|
|
type: typeof circuitDeviceRowDeleteCommandType;
|
|
}
|
|
|
|
export type CircuitDeviceRowStructureProjectCommand =
|
|
| CircuitDeviceRowInsertProjectCommand
|
|
| CircuitDeviceRowDeleteProjectCommand;
|
|
|
|
export function createCircuitDeviceRowInsertProjectCommand(
|
|
row: CircuitDeviceRowSnapshot
|
|
): CircuitDeviceRowInsertProjectCommand {
|
|
const normalizedRow = {
|
|
...row,
|
|
manualQuantity: row.manualQuantity ?? row.quantity,
|
|
};
|
|
const command: CircuitDeviceRowInsertProjectCommand = {
|
|
schemaVersion: circuitDeviceRowStructureCommandSchemaVersion,
|
|
type: circuitDeviceRowInsertCommandType,
|
|
payload: { row: normalizedRow },
|
|
};
|
|
assertCircuitDeviceRowInsertProjectCommand(command);
|
|
return command;
|
|
}
|
|
|
|
export function createCircuitDeviceRowDeleteProjectCommand(
|
|
rowId: string,
|
|
expectedCircuitId: string
|
|
): CircuitDeviceRowDeleteProjectCommand {
|
|
const command: CircuitDeviceRowDeleteProjectCommand = {
|
|
schemaVersion: circuitDeviceRowStructureCommandSchemaVersion,
|
|
type: circuitDeviceRowDeleteCommandType,
|
|
payload: { rowId, expectedCircuitId },
|
|
};
|
|
assertCircuitDeviceRowDeleteProjectCommand(command);
|
|
return command;
|
|
}
|
|
|
|
export function assertCircuitDeviceRowInsertProjectCommand(
|
|
command: SerializedProjectCommand<unknown>
|
|
): asserts command is CircuitDeviceRowInsertProjectCommand {
|
|
if (
|
|
command.schemaVersion !==
|
|
circuitDeviceRowStructureCommandSchemaVersion ||
|
|
command.type !== circuitDeviceRowInsertCommandType
|
|
) {
|
|
throw new Error("Unsupported circuit device-row insert command.");
|
|
}
|
|
if (!isPlainObject(command.payload)) {
|
|
throw new Error("Circuit device-row insert payload must be an object.");
|
|
}
|
|
const row = command.payload.row;
|
|
if (!isPlainObject(row)) {
|
|
throw new Error("Circuit device-row insert command requires a row.");
|
|
}
|
|
|
|
assertNonEmptyString(row.id, "row.id");
|
|
assertNonEmptyString(row.circuitId, "row.circuitId");
|
|
assertNullableString(row.linkedProjectDeviceId, "row.linkedProjectDeviceId");
|
|
assertFiniteNumber(row.sortOrder, "row.sortOrder");
|
|
assertNonEmptyString(row.name, "row.name");
|
|
assertNonEmptyString(row.displayName, "row.displayName");
|
|
for (const field of [
|
|
"phaseType",
|
|
"connectionKind",
|
|
"costGroup",
|
|
"category",
|
|
"level",
|
|
"roomId",
|
|
"roomNumberSnapshot",
|
|
"roomNameSnapshot",
|
|
"remark",
|
|
"overriddenFields",
|
|
] as const) {
|
|
assertNullableString(row[field], `row.${field}`);
|
|
}
|
|
const quantity = row.quantity;
|
|
assertNonNegativeNumber(quantity, "row.quantity");
|
|
const manualQuantity = row.manualQuantity ?? quantity;
|
|
assertNonNegativeNumber(manualQuantity, "row.manualQuantity");
|
|
if (manualQuantity > quantity) {
|
|
throw new Error("row.manualQuantity must not exceed row.quantity.");
|
|
}
|
|
assertNonNegativeNumber(row.powerPerUnit, "row.powerPerUnit");
|
|
assertNonNegativeNumber(
|
|
row.simultaneityFactor,
|
|
"row.simultaneityFactor"
|
|
);
|
|
if (row.simultaneityFactor > 1) {
|
|
throw new Error("row.simultaneityFactor must not exceed 1.");
|
|
}
|
|
if (row.cosPhi !== null) {
|
|
assertFiniteNumber(row.cosPhi, "row.cosPhi");
|
|
if (row.cosPhi <= 0) {
|
|
throw new Error("row.cosPhi must be positive or null.");
|
|
}
|
|
}
|
|
}
|
|
|
|
export function assertCircuitDeviceRowDeleteProjectCommand(
|
|
command: SerializedProjectCommand<unknown>
|
|
): asserts command is CircuitDeviceRowDeleteProjectCommand {
|
|
if (
|
|
command.schemaVersion !==
|
|
circuitDeviceRowStructureCommandSchemaVersion ||
|
|
command.type !== circuitDeviceRowDeleteCommandType
|
|
) {
|
|
throw new Error("Unsupported circuit device-row delete command.");
|
|
}
|
|
if (!isPlainObject(command.payload)) {
|
|
throw new Error("Circuit device-row delete payload must be an object.");
|
|
}
|
|
assertNonEmptyString(command.payload.rowId, "rowId");
|
|
assertNonEmptyString(
|
|
command.payload.expectedCircuitId,
|
|
"expectedCircuitId"
|
|
);
|
|
}
|
|
|
|
function assertNonEmptyString(value: unknown, field: string) {
|
|
if (typeof value !== "string" || !value.trim()) {
|
|
throw new Error(`${field} must be a non-empty string.`);
|
|
}
|
|
}
|
|
|
|
function assertNullableString(value: unknown, field: string) {
|
|
if (value !== null && typeof value !== "string") {
|
|
throw new Error(`${field} must be a string or null.`);
|
|
}
|
|
}
|
|
|
|
function assertFiniteNumber(
|
|
value: unknown,
|
|
field: string
|
|
): asserts value is number {
|
|
if (typeof value !== "number" || !Number.isFinite(value)) {
|
|
throw new Error(`${field} must be a finite number.`);
|
|
}
|
|
}
|
|
|
|
function assertNonNegativeNumber(
|
|
value: unknown,
|
|
field: string
|
|
): asserts value is number {
|
|
assertFiniteNumber(value, field);
|
|
if (value < 0) {
|
|
throw new Error(`${field} must not be negative.`);
|
|
}
|
|
}
|
|
|
|
function isPlainObject(value: unknown): value is Record<string, unknown> {
|
|
return value !== null && typeof value === "object" && !Array.isArray(value);
|
|
}
|