refactor(api): extract blueprint router support
This commit is contained in:
@@ -0,0 +1,108 @@
|
|||||||
|
import { BlueprintTarget, type BlueprintFieldDefinition, FieldType } from "@capakraken/shared";
|
||||||
|
import { TRPCError } from "@trpc/server";
|
||||||
|
import { describe, expect, it, vi } from "vitest";
|
||||||
|
import {
|
||||||
|
buildBlueprintCreateData,
|
||||||
|
buildBlueprintRolePresetsUpdateData,
|
||||||
|
buildBlueprintUpdateData,
|
||||||
|
expandGlobalBlueprintFieldDefs,
|
||||||
|
findBlueprintByIdentifier,
|
||||||
|
} from "../router/blueprint-support.js";
|
||||||
|
|
||||||
|
describe("blueprint support", () => {
|
||||||
|
it("resolves blueprints by exact then fuzzy name", async () => {
|
||||||
|
const db = {
|
||||||
|
blueprint: {
|
||||||
|
findUnique: vi.fn().mockResolvedValue(null),
|
||||||
|
findFirst: vi.fn()
|
||||||
|
.mockResolvedValueOnce(null)
|
||||||
|
.mockResolvedValueOnce({ id: "bp_1", name: "Consulting Blueprint" }),
|
||||||
|
},
|
||||||
|
} as never;
|
||||||
|
|
||||||
|
const result = await findBlueprintByIdentifier<{ id: string; name: string }>(
|
||||||
|
db,
|
||||||
|
" consulting ",
|
||||||
|
{ select: { id: true, name: true } },
|
||||||
|
);
|
||||||
|
|
||||||
|
expect(result).toEqual({ id: "bp_1", name: "Consulting Blueprint" });
|
||||||
|
expect(db.blueprint.findFirst).toHaveBeenNthCalledWith(2, {
|
||||||
|
where: { name: { contains: "consulting", mode: "insensitive" } },
|
||||||
|
select: { id: true, name: true },
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
it("throws when the blueprint cannot be resolved", async () => {
|
||||||
|
const db = {
|
||||||
|
blueprint: {
|
||||||
|
findUnique: vi.fn().mockResolvedValue(null),
|
||||||
|
findFirst: vi.fn().mockResolvedValue(null),
|
||||||
|
},
|
||||||
|
} as never;
|
||||||
|
|
||||||
|
await expect(findBlueprintByIdentifier(db, "missing", { select: { id: true } })).rejects.toBeInstanceOf(TRPCError);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("builds create, update, and role preset payloads", () => {
|
||||||
|
expect(buildBlueprintCreateData({
|
||||||
|
name: "Consulting Blueprint",
|
||||||
|
target: BlueprintTarget.PROJECT,
|
||||||
|
description: "Default setup",
|
||||||
|
fieldDefs: [],
|
||||||
|
defaults: { market: "EU" },
|
||||||
|
validationRules: [],
|
||||||
|
})).toEqual({
|
||||||
|
name: "Consulting Blueprint",
|
||||||
|
target: BlueprintTarget.PROJECT,
|
||||||
|
description: "Default setup",
|
||||||
|
fieldDefs: [],
|
||||||
|
defaults: { market: "EU" },
|
||||||
|
validationRules: [],
|
||||||
|
});
|
||||||
|
|
||||||
|
expect(buildBlueprintUpdateData({
|
||||||
|
description: "Updated",
|
||||||
|
fieldDefs: [{ key: "market", type: FieldType.TEXT }],
|
||||||
|
})).toEqual({
|
||||||
|
description: "Updated",
|
||||||
|
fieldDefs: [{ key: "market", type: FieldType.TEXT }],
|
||||||
|
});
|
||||||
|
|
||||||
|
expect(buildBlueprintRolePresetsUpdateData([{ roleId: "role_1" }])).toEqual({
|
||||||
|
rolePresets: [{ roleId: "role_1" }],
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
it("expands global field definitions with blueprint metadata", () => {
|
||||||
|
const fieldDefs: BlueprintFieldDefinition[] = [
|
||||||
|
{
|
||||||
|
id: "field_market",
|
||||||
|
key: "market",
|
||||||
|
label: "Market",
|
||||||
|
order: 0,
|
||||||
|
type: FieldType.TEXT,
|
||||||
|
required: false,
|
||||||
|
},
|
||||||
|
];
|
||||||
|
|
||||||
|
expect(expandGlobalBlueprintFieldDefs([
|
||||||
|
{
|
||||||
|
id: "bp_project_global",
|
||||||
|
name: "Global Project Blueprint",
|
||||||
|
fieldDefs,
|
||||||
|
},
|
||||||
|
])).toEqual([
|
||||||
|
{
|
||||||
|
id: "field_market",
|
||||||
|
key: "market",
|
||||||
|
label: "Market",
|
||||||
|
order: 0,
|
||||||
|
type: FieldType.TEXT,
|
||||||
|
required: false,
|
||||||
|
blueprintId: "bp_project_global",
|
||||||
|
blueprintName: "Global Project Blueprint",
|
||||||
|
},
|
||||||
|
]);
|
||||||
|
});
|
||||||
|
});
|
||||||
@@ -0,0 +1,111 @@
|
|||||||
|
import type { Prisma, PrismaClient } from "@capakraken/db";
|
||||||
|
import {
|
||||||
|
CreateBlueprintSchema,
|
||||||
|
UpdateBlueprintSchema,
|
||||||
|
type BlueprintFieldDefinition,
|
||||||
|
} from "@capakraken/shared";
|
||||||
|
import { TRPCError } from "@trpc/server";
|
||||||
|
import { z } from "zod";
|
||||||
|
|
||||||
|
type BlueprintIdentifierDb = Pick<PrismaClient, "blueprint">;
|
||||||
|
|
||||||
|
type BlueprintGlobalFieldDefinition = BlueprintFieldDefinition & {
|
||||||
|
blueprintId: string;
|
||||||
|
blueprintName: string;
|
||||||
|
};
|
||||||
|
|
||||||
|
type BlueprintFieldDefinitionSource = {
|
||||||
|
id: string;
|
||||||
|
name: string;
|
||||||
|
fieldDefs: unknown;
|
||||||
|
};
|
||||||
|
|
||||||
|
type CreateBlueprintInput = z.infer<typeof CreateBlueprintSchema>;
|
||||||
|
type UpdateBlueprintInput = z.infer<typeof UpdateBlueprintSchema>;
|
||||||
|
|
||||||
|
export async function findBlueprintByIdentifier<TBlueprint>(
|
||||||
|
db: BlueprintIdentifierDb,
|
||||||
|
identifier: string,
|
||||||
|
extraArgs: Record<string, unknown>,
|
||||||
|
): Promise<TBlueprint> {
|
||||||
|
const normalizedIdentifier = identifier.trim();
|
||||||
|
|
||||||
|
let blueprint = await db.blueprint.findUnique({
|
||||||
|
where: { id: normalizedIdentifier },
|
||||||
|
...extraArgs,
|
||||||
|
}) as TBlueprint | null;
|
||||||
|
|
||||||
|
if (!blueprint) {
|
||||||
|
blueprint = await db.blueprint.findFirst({
|
||||||
|
where: { name: { equals: normalizedIdentifier, mode: "insensitive" } },
|
||||||
|
...extraArgs,
|
||||||
|
}) as TBlueprint | null;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!blueprint) {
|
||||||
|
blueprint = await db.blueprint.findFirst({
|
||||||
|
where: { name: { contains: normalizedIdentifier, mode: "insensitive" } },
|
||||||
|
...extraArgs,
|
||||||
|
}) as TBlueprint | null;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!blueprint) {
|
||||||
|
throw new TRPCError({
|
||||||
|
code: "NOT_FOUND",
|
||||||
|
message: `Blueprint not found: ${normalizedIdentifier}`,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
return blueprint;
|
||||||
|
}
|
||||||
|
|
||||||
|
export function buildBlueprintCreateData(
|
||||||
|
input: CreateBlueprintInput,
|
||||||
|
): Prisma.BlueprintUncheckedCreateInput {
|
||||||
|
return {
|
||||||
|
name: input.name,
|
||||||
|
target: input.target,
|
||||||
|
description: input.description ?? null,
|
||||||
|
fieldDefs: input.fieldDefs as unknown as Prisma.InputJsonValue,
|
||||||
|
defaults: input.defaults as unknown as Prisma.InputJsonValue,
|
||||||
|
validationRules: input.validationRules as unknown as Prisma.InputJsonValue,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
export function buildBlueprintUpdateData(
|
||||||
|
input: UpdateBlueprintInput,
|
||||||
|
): Prisma.BlueprintUncheckedUpdateInput {
|
||||||
|
return {
|
||||||
|
...(input.name !== undefined ? { name: input.name } : {}),
|
||||||
|
...(input.description !== undefined ? { description: input.description } : {}),
|
||||||
|
...(input.fieldDefs !== undefined
|
||||||
|
? { fieldDefs: input.fieldDefs as unknown as Prisma.InputJsonValue }
|
||||||
|
: {}),
|
||||||
|
...(input.defaults !== undefined
|
||||||
|
? { defaults: input.defaults as unknown as Prisma.InputJsonValue }
|
||||||
|
: {}),
|
||||||
|
...(input.validationRules !== undefined
|
||||||
|
? { validationRules: input.validationRules as unknown as Prisma.InputJsonValue }
|
||||||
|
: {}),
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
export function buildBlueprintRolePresetsUpdateData(
|
||||||
|
rolePresets: unknown[],
|
||||||
|
): Prisma.BlueprintUncheckedUpdateInput {
|
||||||
|
return {
|
||||||
|
rolePresets: rolePresets as unknown as Prisma.InputJsonValue,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
export function expandGlobalBlueprintFieldDefs(
|
||||||
|
blueprints: BlueprintFieldDefinitionSource[],
|
||||||
|
): BlueprintGlobalFieldDefinition[] {
|
||||||
|
return blueprints.flatMap((blueprint) =>
|
||||||
|
(blueprint.fieldDefs as BlueprintFieldDefinition[]).map((fieldDef) => ({
|
||||||
|
...fieldDef,
|
||||||
|
blueprintId: blueprint.id,
|
||||||
|
blueprintName: blueprint.name,
|
||||||
|
})),
|
||||||
|
);
|
||||||
|
}
|
||||||
@@ -1,9 +1,34 @@
|
|||||||
import { BlueprintTarget, CreateBlueprintSchema, UpdateBlueprintSchema, type BlueprintFieldDefinition } from "@capakraken/shared";
|
import { BlueprintTarget, CreateBlueprintSchema, UpdateBlueprintSchema } from "@capakraken/shared";
|
||||||
import { TRPCError } from "@trpc/server";
|
|
||||||
import { z } from "zod";
|
import { z } from "zod";
|
||||||
import { findUniqueOrThrow } from "../db/helpers.js";
|
import { findUniqueOrThrow } from "../db/helpers.js";
|
||||||
import { adminProcedure, createTRPCRouter, planningReadProcedure, protectedProcedure } from "../trpc.js";
|
import { adminProcedure, createTRPCRouter, planningReadProcedure, protectedProcedure } from "../trpc.js";
|
||||||
import { createAuditEntry } from "../lib/audit.js";
|
import { createAuditEntry } from "../lib/audit.js";
|
||||||
|
import {
|
||||||
|
buildBlueprintCreateData,
|
||||||
|
buildBlueprintRolePresetsUpdateData,
|
||||||
|
buildBlueprintUpdateData,
|
||||||
|
expandGlobalBlueprintFieldDefs,
|
||||||
|
findBlueprintByIdentifier,
|
||||||
|
} from "./blueprint-support.js";
|
||||||
|
|
||||||
|
type BlueprintIdentifierReadModel = {
|
||||||
|
id: string;
|
||||||
|
name: string;
|
||||||
|
target: BlueprintTarget;
|
||||||
|
isActive: boolean;
|
||||||
|
};
|
||||||
|
|
||||||
|
type BlueprintDetailReadModel = {
|
||||||
|
id: string;
|
||||||
|
name: string;
|
||||||
|
target: BlueprintTarget;
|
||||||
|
description: string | null;
|
||||||
|
fieldDefs: unknown;
|
||||||
|
defaults: unknown;
|
||||||
|
validationRules: unknown;
|
||||||
|
rolePresets: unknown;
|
||||||
|
isActive: boolean;
|
||||||
|
};
|
||||||
|
|
||||||
export const blueprintRouter = createTRPCRouter({
|
export const blueprintRouter = createTRPCRouter({
|
||||||
listSummaries: planningReadProcedure
|
listSummaries: planningReadProcedure
|
||||||
@@ -48,79 +73,27 @@ export const blueprintRouter = createTRPCRouter({
|
|||||||
resolveByIdentifier: protectedProcedure
|
resolveByIdentifier: protectedProcedure
|
||||||
.input(z.object({ identifier: z.string().trim().min(1) }))
|
.input(z.object({ identifier: z.string().trim().min(1) }))
|
||||||
.query(async ({ ctx, input }) => {
|
.query(async ({ ctx, input }) => {
|
||||||
const identifier = input.identifier.trim();
|
return findBlueprintByIdentifier<BlueprintIdentifierReadModel>(ctx.db, input.identifier, {
|
||||||
const select = {
|
select: {
|
||||||
id: true,
|
id: true,
|
||||||
name: true,
|
name: true,
|
||||||
target: true,
|
target: true,
|
||||||
isActive: true,
|
isActive: true,
|
||||||
} as const;
|
},
|
||||||
|
|
||||||
let blueprint = await ctx.db.blueprint.findUnique({
|
|
||||||
where: { id: identifier },
|
|
||||||
select,
|
|
||||||
});
|
});
|
||||||
|
|
||||||
if (!blueprint) {
|
|
||||||
blueprint = await ctx.db.blueprint.findFirst({
|
|
||||||
where: { name: { equals: identifier, mode: "insensitive" } },
|
|
||||||
select,
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!blueprint) {
|
|
||||||
blueprint = await ctx.db.blueprint.findFirst({
|
|
||||||
where: { name: { contains: identifier, mode: "insensitive" } },
|
|
||||||
select,
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!blueprint) {
|
|
||||||
throw new TRPCError({ code: "NOT_FOUND", message: `Blueprint not found: ${identifier}` });
|
|
||||||
}
|
|
||||||
|
|
||||||
return blueprint;
|
|
||||||
}),
|
}),
|
||||||
|
|
||||||
getByIdentifier: planningReadProcedure
|
getByIdentifier: planningReadProcedure
|
||||||
.input(z.object({ identifier: z.string().trim().min(1) }))
|
.input(z.object({ identifier: z.string().trim().min(1) }))
|
||||||
.query(async ({ ctx, input }) => {
|
.query(async ({ ctx, input }) => {
|
||||||
const identifier = input.identifier.trim();
|
return findBlueprintByIdentifier<BlueprintDetailReadModel>(ctx.db, input.identifier, {});
|
||||||
let blueprint = await ctx.db.blueprint.findUnique({
|
|
||||||
where: { id: identifier },
|
|
||||||
});
|
|
||||||
|
|
||||||
if (!blueprint) {
|
|
||||||
blueprint = await ctx.db.blueprint.findFirst({
|
|
||||||
where: { name: { equals: identifier, mode: "insensitive" } },
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!blueprint) {
|
|
||||||
blueprint = await ctx.db.blueprint.findFirst({
|
|
||||||
where: { name: { contains: identifier, mode: "insensitive" } },
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!blueprint) {
|
|
||||||
throw new TRPCError({ code: "NOT_FOUND", message: `Blueprint not found: ${identifier}` });
|
|
||||||
}
|
|
||||||
|
|
||||||
return blueprint;
|
|
||||||
}),
|
}),
|
||||||
|
|
||||||
create: adminProcedure
|
create: adminProcedure
|
||||||
.input(CreateBlueprintSchema)
|
.input(CreateBlueprintSchema)
|
||||||
.mutation(async ({ ctx, input }) => {
|
.mutation(async ({ ctx, input }) => {
|
||||||
const blueprint = await ctx.db.blueprint.create({
|
const blueprint = await ctx.db.blueprint.create({
|
||||||
data: {
|
data: buildBlueprintCreateData(input),
|
||||||
name: input.name,
|
|
||||||
target: input.target,
|
|
||||||
description: input.description,
|
|
||||||
fieldDefs: input.fieldDefs as unknown as import("@capakraken/db").Prisma.InputJsonValue,
|
|
||||||
defaults: input.defaults as unknown as import("@capakraken/db").Prisma.InputJsonValue,
|
|
||||||
validationRules: input.validationRules as unknown as import("@capakraken/db").Prisma.InputJsonValue,
|
|
||||||
} as unknown as Parameters<typeof ctx.db.blueprint.create>[0]["data"],
|
|
||||||
});
|
});
|
||||||
|
|
||||||
void createAuditEntry({
|
void createAuditEntry({
|
||||||
@@ -147,13 +120,7 @@ export const blueprintRouter = createTRPCRouter({
|
|||||||
|
|
||||||
const updated = await ctx.db.blueprint.update({
|
const updated = await ctx.db.blueprint.update({
|
||||||
where: { id: input.id },
|
where: { id: input.id },
|
||||||
data: {
|
data: buildBlueprintUpdateData(input.data),
|
||||||
...(input.data.name !== undefined ? { name: input.data.name } : {}),
|
|
||||||
...(input.data.description !== undefined ? { description: input.data.description } : {}),
|
|
||||||
...(input.data.fieldDefs !== undefined ? { fieldDefs: input.data.fieldDefs as unknown as import("@capakraken/db").Prisma.InputJsonValue } : {}),
|
|
||||||
...(input.data.defaults !== undefined ? { defaults: input.data.defaults as unknown as import("@capakraken/db").Prisma.InputJsonValue } : {}),
|
|
||||||
...(input.data.validationRules !== undefined ? { validationRules: input.data.validationRules as unknown as import("@capakraken/db").Prisma.InputJsonValue } : {}),
|
|
||||||
} as unknown as Parameters<typeof ctx.db.blueprint.update>[0]["data"],
|
|
||||||
});
|
});
|
||||||
|
|
||||||
void createAuditEntry({
|
void createAuditEntry({
|
||||||
@@ -181,7 +148,7 @@ export const blueprintRouter = createTRPCRouter({
|
|||||||
);
|
);
|
||||||
const updated = await ctx.db.blueprint.update({
|
const updated = await ctx.db.blueprint.update({
|
||||||
where: { id: input.id },
|
where: { id: input.id },
|
||||||
data: { rolePresets: input.rolePresets as unknown as import("@capakraken/db").Prisma.InputJsonValue },
|
data: buildBlueprintRolePresetsUpdateData(input.rolePresets),
|
||||||
});
|
});
|
||||||
|
|
||||||
void createAuditEntry({
|
void createAuditEntry({
|
||||||
@@ -254,13 +221,7 @@ export const blueprintRouter = createTRPCRouter({
|
|||||||
where: { target: input.target, isGlobal: true, isActive: true },
|
where: { target: input.target, isGlobal: true, isActive: true },
|
||||||
select: { id: true, name: true, fieldDefs: true },
|
select: { id: true, name: true, fieldDefs: true },
|
||||||
});
|
});
|
||||||
return blueprints.flatMap((b) =>
|
return expandGlobalBlueprintFieldDefs(blueprints);
|
||||||
(b.fieldDefs as unknown as BlueprintFieldDefinition[]).map((f) => ({
|
|
||||||
...f,
|
|
||||||
blueprintId: b.id,
|
|
||||||
blueprintName: b.name,
|
|
||||||
})),
|
|
||||||
);
|
|
||||||
}),
|
}),
|
||||||
|
|
||||||
setGlobal: adminProcedure
|
setGlobal: adminProcedure
|
||||||
|
|||||||
Reference in New Issue
Block a user