Proposal: cable-sizing module (on-demand DIN VDE 0298-4 cross-section calculator) #1

Open
Grovy311 wants to merge 10 commits from Grovy311/leistungsbilanz-ts:feature/cable-sizing-module into main
Showing only changes of commit 01a024f048 - Show all commits

View file

@ -26,7 +26,9 @@ export async function calculateCableSizingHandler(req: Request, res: Response) {
const result = calculateCableSizing(input); const result = calculateCableSizing(input);
const alerts = buildCableSizingAlerts(input, result); const alerts = buildCableSizingAlerts(input, result);
const entry = await cableSizingCalculationRepository.create({ let entry;
try {
entry = await cableSizingCalculationRepository.create({
id: randomUUID(), id: randomUUID(),
projectId: context?.projectId ?? null, projectId: context?.projectId ?? null,
circuitId: context?.circuitId ?? null, circuitId: context?.circuitId ?? null,
@ -35,6 +37,23 @@ export async function calculateCableSizingHandler(req: Request, res: Response) {
result, result,
appliedToCircuit: 0, appliedToCircuit: 0,
}); });
} catch (error) {
// context.circuitId/projectId are caller-supplied and only used for the
// audit-log entry, not the calculation itself - a stale or unknown id
// (e.g. a circuit deleted between page load and this request) should be
// a normal 400, not a raw 500 from the foreign-key constraint.
if (
error &&
typeof error === "object" &&
"code" in error &&
(error as { code?: string }).code === "SQLITE_CONSTRAINT_FOREIGNKEY"
) {
return res
.status(400)
.json({ error: "Unknown context.projectId or context.circuitId" });
}
throw error;
}
return res.status(201).json({ calculationId: entry.id, result, alerts }); return res.status(201).json({ calculationId: entry.id, result, alerts });
} }