Files
CapaKraken/packages/api/src/router/blueprint-validation.ts
T
Hartmut 4a49ec4f05 fix(sanity): resolve 15 gaps from sanity check audit (G-01 through G-15)
- G-01: ProjectWizard renders blueprint fieldDefs with DynamicFieldInput component
- G-02: Blueprint rolePresets validated via RolePresetsSchema in wizard; API keeps loose schema
- G-03: ProjectWizard step 2/3 validation (role, hoursPerDay, headcount required)
- G-04: EstimateWizard validates baseCurrency and demand line cost rates
- G-05: Project lifecycle transition guards with ALLOWED_TRANSITIONS map
- G-06: Blueprint validator extended for minLength/maxLength/pattern and DATE range checks
- G-07: assertBlueprintDynamicFields merges global blueprint fieldDefs into validation
- G-08: (tracked — chapter managed dropdown; deferred to backend ticket)
- G-09: JSDoc added to lcrCents/ucrCents clarifying LCR/UCR terminology
- G-10: Dispo route redirect already in place — closed as done
- G-11: packages/ui empty by design — closed as documented
- G-12: @deprecated JSDoc added to CreateAllocationSchema and UpdateAllocationSchema
- G-13: ProjectWizard review step enhanced with blueprint name, field values, skills, assignments
- G-14: ProjectWizard handleSubmit collects per-item warnings instead of silent swallowing
- G-15: Vacation cancel reverses usedDays entitlement for APPROVED ANNUAL/OTHER vacations

Tests: all 1575 passing (1 pre-existing failure in insights-summary unrelated to these changes)

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-04-06 00:11:12 +02:00

81 lines
2.5 KiB
TypeScript

import { validateCustomFields } from "@capakraken/engine";
import { BlueprintTarget, type BlueprintFieldDefinition } from "@capakraken/shared";
import { TRPCError } from "@trpc/server";
import { findUniqueOrThrow } from "../db/helpers.js";
interface BlueprintLookup {
blueprint: {
findUnique: (args: {
where: { id: string };
select: { fieldDefs: true; target: true };
}) => Promise<{ fieldDefs: unknown; target: string } | null>;
findMany: (args: {
where: { target: BlueprintTarget; isGlobal: boolean; isActive: boolean };
select: { fieldDefs: true };
}) => Promise<Array<{ fieldDefs: unknown }>>;
};
}
interface AssertBlueprintDynamicFieldsInput {
db: BlueprintLookup;
blueprintId: string | undefined;
dynamicFields: Record<string, unknown>;
target: BlueprintTarget;
}
export async function assertBlueprintDynamicFields({
db,
blueprintId,
dynamicFields,
target,
}: AssertBlueprintDynamicFieldsInput): Promise<void> {
// Collect field defs from the entity's specific blueprint (if any)
let specificFieldDefs: BlueprintFieldDefinition[] = [];
if (blueprintId) {
const blueprint = await findUniqueOrThrow(
db.blueprint.findUnique({
where: { id: blueprintId },
select: { fieldDefs: true, target: true },
}),
"Blueprint",
);
if (blueprint.target !== target) {
throw new TRPCError({
code: "BAD_REQUEST",
message: `${target} entities require a ${target.toLowerCase()} blueprint`,
});
}
specificFieldDefs = blueprint.fieldDefs as BlueprintFieldDefinition[];
}
// Also collect field defs from all active global blueprints for this target
const globalBlueprints = await db.blueprint.findMany({
where: { target, isGlobal: true, isActive: true },
select: { fieldDefs: true },
});
const globalFieldDefs = globalBlueprints.flatMap(
(bp) => bp.fieldDefs as BlueprintFieldDefinition[],
);
// Merge: specific blueprint fields + global fields (specific takes precedence for same key)
const specificKeys = new Set(specificFieldDefs.map((f) => f.key));
const mergedFieldDefs = [
...specificFieldDefs,
...globalFieldDefs.filter((f) => !specificKeys.has(f.key)),
];
if (mergedFieldDefs.length === 0) return;
const errors = validateCustomFields(mergedFieldDefs, dynamicFields);
if (errors.length > 0) {
throw new TRPCError({
code: "UNPROCESSABLE_CONTENT",
message: errors.map((error) => error.message).join("; "),
});
}
}