Skip to content

Commit b495287

Browse files
jhfclaude
andcommitted
fix: Auto-derive region_version_id and pre-validate stat_for_unit period
Bug 1: Location saves failed with "violates check constraint location_region_version_consistency" because the UI sends region_id without region_version_id. Added BEFORE INSERT OR UPDATE trigger that auto-derives region_version_id from region.version_id via PK lookup. Bug 2: stat_for_unit inserts for periods outside the parent unit's valid range produced a cryptic temporal FK error. Added pre-validation in upsertTemporalRecord's INSERT branch with a friendly error message showing the unit's actual valid range. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
1 parent bee1fca commit b495287

File tree

3 files changed

+57
-0
lines changed

3 files changed

+57
-0
lines changed

app/src/app/legal-units/[id]/update-legal-unit-server-actions.ts

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -430,6 +430,36 @@ export async function upsertTemporalRecord({
430430
};
431431
}
432432
} else {
433+
// Check that the parent unit exists for the requested period
434+
const parentTable = unitType;
435+
const { data: parentRows, error: parentError } = await client
436+
.from(parentTable)
437+
.select("valid_from, valid_to")
438+
.eq("id", unitId)
439+
.lte("valid_from", valid_from)
440+
.gte("valid_to", valid_to);
441+
442+
if (parentError) {
443+
return { status: "error", message: parentError.message };
444+
}
445+
446+
if (!parentRows || parentRows.length === 0) {
447+
const { data: allRows } = await client
448+
.from(parentTable)
449+
.select("valid_from, valid_to")
450+
.eq("id", unitId)
451+
.order("valid_from");
452+
453+
const rangeInfo = allRows?.length
454+
? ` The ${unitType.replace("_", " ")} exists for: ${allRows.map((r) => `${r.valid_from} to ${r.valid_to ?? "infinity"}`).join(", ")}.`
455+
: "";
456+
457+
return {
458+
status: "error",
459+
message: `Cannot add ${tableName.replace("_", " ")} for the period ${valid_from} to ${valid_to}. The ${unitType.replace("_", " ")} does not exist for this period.${rangeInfo}`,
460+
};
461+
}
462+
433463
let insertPayload = { ...payload, ...naturalKeyValues };
434464
let templateQuery = client.from(tableName).select("id").limit(1);
435465
for (const key in naturalKeyValues) {
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
BEGIN;
2+
DROP TRIGGER IF EXISTS location_set_region_version_id_trigger ON public.location;
3+
DROP FUNCTION IF EXISTS public.location_set_region_version_id();
4+
END;
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
BEGIN;
2+
3+
CREATE OR REPLACE FUNCTION public.location_set_region_version_id()
4+
RETURNS TRIGGER
5+
LANGUAGE plpgsql
6+
SECURITY DEFINER
7+
SET search_path = public, pg_temp
8+
AS $location_set_region_version_id$
9+
BEGIN
10+
NEW.region_version_id := (
11+
SELECT r.version_id FROM public.region AS r WHERE r.id = NEW.region_id
12+
);
13+
RETURN NEW;
14+
END;
15+
$location_set_region_version_id$;
16+
17+
CREATE TRIGGER location_set_region_version_id_trigger
18+
BEFORE INSERT OR UPDATE OF region_id ON public.location
19+
FOR EACH ROW
20+
WHEN (NEW.region_id IS NOT NULL AND NEW.region_version_id IS NULL)
21+
EXECUTE FUNCTION public.location_set_region_version_id();
22+
23+
END;

0 commit comments

Comments
 (0)