refactor(api): extract rate card procedures
This commit is contained in:
@@ -0,0 +1,151 @@
|
|||||||
|
import { SystemRole } from "@capakraken/shared";
|
||||||
|
import { beforeEach, describe, expect, it, vi } from "vitest";
|
||||||
|
|
||||||
|
const { createAuditEntry } = vi.hoisted(() => ({
|
||||||
|
createAuditEntry: vi.fn(),
|
||||||
|
}));
|
||||||
|
|
||||||
|
vi.mock("../lib/audit.js", () => ({
|
||||||
|
createAuditEntry,
|
||||||
|
}));
|
||||||
|
|
||||||
|
import {
|
||||||
|
addRateCardLine,
|
||||||
|
createRateCard,
|
||||||
|
replaceRateCardLines,
|
||||||
|
} from "../router/rate-card-procedure-support.js";
|
||||||
|
|
||||||
|
function createManagerContext(db: Record<string, unknown>) {
|
||||||
|
return {
|
||||||
|
db: db as never,
|
||||||
|
dbUser: {
|
||||||
|
id: "manager_1",
|
||||||
|
systemRole: SystemRole.MANAGER,
|
||||||
|
permissionOverrides: null,
|
||||||
|
},
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
describe("rate-card procedure support", () => {
|
||||||
|
beforeEach(() => {
|
||||||
|
vi.clearAllMocks();
|
||||||
|
});
|
||||||
|
|
||||||
|
it("creates a rate card and writes a summarized audit entry", async () => {
|
||||||
|
const create = vi.fn().mockResolvedValue({
|
||||||
|
id: "rc_1",
|
||||||
|
name: "Standard 2026",
|
||||||
|
lines: [{ id: "line_1" }],
|
||||||
|
});
|
||||||
|
|
||||||
|
const result = await createRateCard(createManagerContext({
|
||||||
|
rateCard: { create },
|
||||||
|
}), {
|
||||||
|
name: "Standard 2026",
|
||||||
|
currency: "EUR",
|
||||||
|
lines: [{
|
||||||
|
costRateCents: 9_500,
|
||||||
|
billRateCents: 14_000,
|
||||||
|
chapter: "Delivery",
|
||||||
|
attributes: {},
|
||||||
|
}],
|
||||||
|
});
|
||||||
|
|
||||||
|
expect(result).toEqual({
|
||||||
|
id: "rc_1",
|
||||||
|
name: "Standard 2026",
|
||||||
|
lines: [{ id: "line_1" }],
|
||||||
|
});
|
||||||
|
expect(create).toHaveBeenCalledWith(expect.objectContaining({
|
||||||
|
data: expect.objectContaining({
|
||||||
|
name: "Standard 2026",
|
||||||
|
currency: "EUR",
|
||||||
|
}),
|
||||||
|
}));
|
||||||
|
expect(createAuditEntry).toHaveBeenCalledWith(expect.objectContaining({
|
||||||
|
entityType: "RateCard",
|
||||||
|
entityId: "rc_1",
|
||||||
|
action: "CREATE",
|
||||||
|
after: {
|
||||||
|
name: "Standard 2026",
|
||||||
|
currency: "EUR",
|
||||||
|
lineCount: 1,
|
||||||
|
},
|
||||||
|
}));
|
||||||
|
});
|
||||||
|
|
||||||
|
it("adds a line and uses the rate card name in audit metadata", async () => {
|
||||||
|
const lineCreate = vi.fn().mockResolvedValue({
|
||||||
|
id: "line_1",
|
||||||
|
rateCardId: "rc_1",
|
||||||
|
chapter: "Delivery",
|
||||||
|
costRateCents: 9_500,
|
||||||
|
billRateCents: 14_000,
|
||||||
|
});
|
||||||
|
|
||||||
|
const result = await addRateCardLine(createManagerContext({
|
||||||
|
rateCard: {
|
||||||
|
findUnique: vi.fn().mockResolvedValue({ id: "rc_1", name: "Standard 2026" }),
|
||||||
|
},
|
||||||
|
rateCardLine: {
|
||||||
|
create: lineCreate,
|
||||||
|
},
|
||||||
|
}), {
|
||||||
|
rateCardId: "rc_1",
|
||||||
|
line: {
|
||||||
|
costRateCents: 9_500,
|
||||||
|
billRateCents: 14_000,
|
||||||
|
chapter: "Delivery",
|
||||||
|
attributes: {},
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
expect(result.id).toBe("line_1");
|
||||||
|
expect(lineCreate).toHaveBeenCalledWith(expect.objectContaining({
|
||||||
|
data: expect.objectContaining({
|
||||||
|
rateCardId: "rc_1",
|
||||||
|
chapter: "Delivery",
|
||||||
|
}),
|
||||||
|
}));
|
||||||
|
expect(createAuditEntry).toHaveBeenCalledWith(expect.objectContaining({
|
||||||
|
entityType: "RateCardLine",
|
||||||
|
entityId: "line_1",
|
||||||
|
entityName: "Standard 2026 - Delivery",
|
||||||
|
}));
|
||||||
|
});
|
||||||
|
|
||||||
|
it("replaces lines inside a transaction and audits the replacement count", async () => {
|
||||||
|
const deleteMany = vi.fn().mockResolvedValue({ count: 2 });
|
||||||
|
const create = vi.fn()
|
||||||
|
.mockResolvedValueOnce({ id: "line_1", rateCardId: "rc_1", costRateCents: 8_000 })
|
||||||
|
.mockResolvedValueOnce({ id: "line_2", rateCardId: "rc_1", costRateCents: 9_500 });
|
||||||
|
|
||||||
|
const db = {
|
||||||
|
rateCard: {
|
||||||
|
findUnique: vi.fn().mockResolvedValue({ id: "rc_1", name: "Standard 2026" }),
|
||||||
|
},
|
||||||
|
rateCardLine: {},
|
||||||
|
$transaction: vi.fn(async (callback: (tx: { rateCardLine: { deleteMany: typeof deleteMany; create: typeof create } }) => unknown) => callback({
|
||||||
|
rateCardLine: { deleteMany, create },
|
||||||
|
})),
|
||||||
|
};
|
||||||
|
|
||||||
|
const result = await replaceRateCardLines(createManagerContext(db), {
|
||||||
|
rateCardId: "rc_1",
|
||||||
|
lines: [
|
||||||
|
{ costRateCents: 8_000, chapter: "Delivery", attributes: {} },
|
||||||
|
{ costRateCents: 9_500, chapter: "Pipeline", attributes: {} },
|
||||||
|
],
|
||||||
|
});
|
||||||
|
|
||||||
|
expect(deleteMany).toHaveBeenCalledWith({ where: { rateCardId: "rc_1" } });
|
||||||
|
expect(create).toHaveBeenCalledTimes(2);
|
||||||
|
expect(result).toHaveLength(2);
|
||||||
|
expect(createAuditEntry).toHaveBeenCalledWith(expect.objectContaining({
|
||||||
|
entityType: "RateCard",
|
||||||
|
entityId: "rc_1",
|
||||||
|
summary: "Replaced all lines with 2 new lines",
|
||||||
|
after: { replacedLineCount: 2 },
|
||||||
|
}));
|
||||||
|
});
|
||||||
|
});
|
||||||
@@ -20,27 +20,55 @@ function createControllerCaller(db: Record<string, unknown>) {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function createManagerCaller(db: Record<string, unknown>) {
|
||||||
|
return createCaller({
|
||||||
|
session: {
|
||||||
|
user: { email: "manager@example.com", name: "Manager", image: null },
|
||||||
|
expires: "2099-01-01T00:00:00.000Z",
|
||||||
|
},
|
||||||
|
db: db as never,
|
||||||
|
dbUser: {
|
||||||
|
id: "user_2",
|
||||||
|
systemRole: SystemRole.MANAGER,
|
||||||
|
permissionOverrides: null,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
describe("rateCard router", () => {
|
describe("rateCard router", () => {
|
||||||
describe("list", () => {
|
describe("list", () => {
|
||||||
it("returns rate cards with line counts", async () => {
|
it("returns rate cards with line counts through the router", async () => {
|
||||||
const findMany = vi.fn().mockResolvedValue([
|
const findMany = vi.fn().mockResolvedValue([
|
||||||
{ id: "rc_1", name: "Standard 2026", currency: "EUR", isActive: true, _count: { lines: 5 } },
|
{ id: "rc_1", name: "Standard 2026", currency: "EUR", isActive: true, _count: { lines: 5 } },
|
||||||
{ id: "rc_2", name: "India Rates", currency: "INR", isActive: true, _count: { lines: 3 } },
|
{ id: "rc_2", name: "India Rates", currency: "INR", isActive: true, _count: { lines: 3 } },
|
||||||
]);
|
]);
|
||||||
|
|
||||||
const result = await findMany({
|
const caller = createControllerCaller({
|
||||||
where: {},
|
rateCard: { findMany },
|
||||||
include: { _count: { select: { lines: true } } },
|
});
|
||||||
orderBy: [{ isActive: "desc" }, { effectiveFrom: "desc" }, { name: "asc" }],
|
const result = await caller.list({
|
||||||
|
search: "Standard",
|
||||||
|
isActive: true,
|
||||||
});
|
});
|
||||||
|
|
||||||
|
expect(findMany).toHaveBeenCalledWith({
|
||||||
|
where: {
|
||||||
|
isActive: true,
|
||||||
|
name: { contains: "Standard", mode: "insensitive" },
|
||||||
|
},
|
||||||
|
include: {
|
||||||
|
_count: { select: { lines: true } },
|
||||||
|
client: { select: { id: true, name: true, code: true } },
|
||||||
|
},
|
||||||
|
orderBy: [{ isActive: "desc" }, { effectiveFrom: "desc" }, { name: "asc" }],
|
||||||
|
});
|
||||||
expect(result).toHaveLength(2);
|
expect(result).toHaveLength(2);
|
||||||
expect(result[0]._count.lines).toBe(5);
|
expect(result[0]._count.lines).toBe(5);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
describe("create", () => {
|
describe("create", () => {
|
||||||
it("creates a rate card with lines", async () => {
|
it("creates a rate card with lines through the router", async () => {
|
||||||
const create = vi.fn().mockResolvedValue({
|
const create = vi.fn().mockResolvedValue({
|
||||||
id: "rc_new",
|
id: "rc_new",
|
||||||
name: "Q1 2026 Rates",
|
name: "Q1 2026 Rates",
|
||||||
@@ -51,16 +79,21 @@ describe("rateCard router", () => {
|
|||||||
],
|
],
|
||||||
});
|
});
|
||||||
|
|
||||||
const result = await create({
|
const caller = createManagerCaller({
|
||||||
data: {
|
rateCard: { create },
|
||||||
name: "Q1 2026 Rates",
|
});
|
||||||
currency: "EUR",
|
const result = await caller.create({
|
||||||
lines: {
|
name: "Q1 2026 Rates",
|
||||||
create: [{ costRateCents: 9500, billRateCents: 14000, chapter: "Digital Content Production" }],
|
currency: "EUR",
|
||||||
},
|
lines: [{ costRateCents: 9500, billRateCents: 14000, chapter: "Digital Content Production", attributes: {} }],
|
||||||
},
|
|
||||||
});
|
});
|
||||||
|
|
||||||
|
expect(create).toHaveBeenCalledWith(expect.objectContaining({
|
||||||
|
data: expect.objectContaining({
|
||||||
|
name: "Q1 2026 Rates",
|
||||||
|
currency: "EUR",
|
||||||
|
}),
|
||||||
|
}));
|
||||||
expect(result.id).toBe("rc_new");
|
expect(result.id).toBe("rc_new");
|
||||||
expect(result.lines).toHaveLength(1);
|
expect(result.lines).toHaveLength(1);
|
||||||
expect(result.lines[0].costRateCents).toBe(9500);
|
expect(result.lines[0].costRateCents).toBe(9500);
|
||||||
@@ -211,19 +244,34 @@ describe("rateCard router", () => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
describe("replaceLines", () => {
|
describe("replaceLines", () => {
|
||||||
it("deletes all lines and creates new ones in a transaction", async () => {
|
it("deletes all lines and creates new ones in a transaction through the router", async () => {
|
||||||
const deleteMany = vi.fn().mockResolvedValue({ count: 3 });
|
const deleteMany = vi.fn().mockResolvedValue({ count: 3 });
|
||||||
const createLine = vi.fn()
|
const createLine = vi.fn()
|
||||||
.mockResolvedValueOnce({ id: "rcl_new_1", costRateCents: 8000 })
|
.mockResolvedValueOnce({ id: "rcl_new_1", costRateCents: 8000 })
|
||||||
.mockResolvedValueOnce({ id: "rcl_new_2", costRateCents: 9500 });
|
.mockResolvedValueOnce({ id: "rcl_new_2", costRateCents: 9500 });
|
||||||
|
|
||||||
await deleteMany({ where: { rateCardId: "rc_1" } });
|
const caller = createManagerCaller({
|
||||||
const line1 = await createLine({ data: { rateCardId: "rc_1", costRateCents: 8000 } });
|
rateCard: {
|
||||||
const line2 = await createLine({ data: { rateCardId: "rc_1", costRateCents: 9500 } });
|
findUnique: vi.fn().mockResolvedValue({ id: "rc_1", name: "Standard 2026" }),
|
||||||
|
},
|
||||||
|
rateCardLine: {},
|
||||||
|
$transaction: vi.fn(async (callback: (tx: { rateCardLine: { deleteMany: typeof deleteMany; create: typeof createLine } }) => unknown) => callback({
|
||||||
|
rateCardLine: { deleteMany, create: createLine },
|
||||||
|
})),
|
||||||
|
});
|
||||||
|
const result = await caller.replaceLines({
|
||||||
|
rateCardId: "rc_1",
|
||||||
|
lines: [
|
||||||
|
{ costRateCents: 8000, chapter: "Delivery", attributes: {} },
|
||||||
|
{ costRateCents: 9500, chapter: "Pipeline", attributes: {} },
|
||||||
|
],
|
||||||
|
});
|
||||||
|
|
||||||
expect(deleteMany).toHaveBeenCalledWith({ where: { rateCardId: "rc_1" } });
|
expect(deleteMany).toHaveBeenCalledWith({ where: { rateCardId: "rc_1" } });
|
||||||
expect(line1.id).toBe("rcl_new_1");
|
expect(result).toEqual([
|
||||||
expect(line2.id).toBe("rcl_new_2");
|
{ id: "rcl_new_1", costRateCents: 8000 },
|
||||||
|
{ id: "rcl_new_2", costRateCents: 9500 },
|
||||||
|
]);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -0,0 +1,355 @@
|
|||||||
|
import {
|
||||||
|
CreateRateCardLineSchema,
|
||||||
|
CreateRateCardSchema,
|
||||||
|
UpdateRateCardLineSchema,
|
||||||
|
UpdateRateCardSchema,
|
||||||
|
} from "@capakraken/shared";
|
||||||
|
import { z } from "zod";
|
||||||
|
import { findUniqueOrThrow } from "../db/helpers.js";
|
||||||
|
import { createAuditEntry } from "../lib/audit.js";
|
||||||
|
import type { TRPCContext } from "../trpc.js";
|
||||||
|
import {
|
||||||
|
lookupBestRateMatch,
|
||||||
|
rateCardLineSelect,
|
||||||
|
resolveBestRate,
|
||||||
|
resolveBestRateLineMatch,
|
||||||
|
} from "./rate-card-read.js";
|
||||||
|
import {
|
||||||
|
buildRateCardCreateAuditAfter,
|
||||||
|
buildRateCardCreateData,
|
||||||
|
buildRateCardLineCreateAuditAfter,
|
||||||
|
buildRateCardLineCreateData,
|
||||||
|
buildRateCardLineUpdateData,
|
||||||
|
buildRateCardListWhere,
|
||||||
|
buildRateCardNestedLineCreateData,
|
||||||
|
buildRateCardReplaceLinesAuditAfter,
|
||||||
|
buildRateCardUpdateData,
|
||||||
|
rateCardCreateInclude,
|
||||||
|
rateCardDetailInclude,
|
||||||
|
rateCardSummaryInclude,
|
||||||
|
} from "./rate-card-write-support.js";
|
||||||
|
|
||||||
|
type RateCardProcedureContext = Pick<TRPCContext, "db" | "dbUser">;
|
||||||
|
|
||||||
|
function withAuditUser(userId: string | undefined) {
|
||||||
|
return userId ? { userId } : {};
|
||||||
|
}
|
||||||
|
|
||||||
|
export const rateCardListInputSchema = z.object({
|
||||||
|
isActive: z.boolean().optional(),
|
||||||
|
search: z.string().optional(),
|
||||||
|
clientId: z.string().optional(),
|
||||||
|
effectiveAt: z.coerce.date().optional(),
|
||||||
|
}).optional();
|
||||||
|
|
||||||
|
export const rateCardByIdInputSchema = z.object({ id: z.string() });
|
||||||
|
|
||||||
|
export const rateCardLookupBestMatchInputSchema = z.object({
|
||||||
|
clientId: z.string().optional(),
|
||||||
|
chapter: z.string().optional(),
|
||||||
|
managementLevelId: z.string().optional(),
|
||||||
|
roleName: z.string().optional(),
|
||||||
|
seniority: z.string().optional(),
|
||||||
|
});
|
||||||
|
|
||||||
|
export const rateCardResolveBestRateInputSchema = z.object({
|
||||||
|
resourceId: z.string().optional(),
|
||||||
|
roleName: z.string().optional(),
|
||||||
|
date: z.coerce.date().optional(),
|
||||||
|
});
|
||||||
|
|
||||||
|
export const rateCardUpdateInputSchema = z.object({
|
||||||
|
id: z.string(),
|
||||||
|
data: UpdateRateCardSchema,
|
||||||
|
});
|
||||||
|
|
||||||
|
export const rateCardAddLineInputSchema = z.object({
|
||||||
|
rateCardId: z.string(),
|
||||||
|
line: CreateRateCardLineSchema,
|
||||||
|
});
|
||||||
|
|
||||||
|
export const rateCardUpdateLineInputSchema = z.object({
|
||||||
|
lineId: z.string(),
|
||||||
|
data: UpdateRateCardLineSchema,
|
||||||
|
});
|
||||||
|
|
||||||
|
export const rateCardDeleteLineInputSchema = z.object({ lineId: z.string() });
|
||||||
|
|
||||||
|
export const rateCardReplaceLinesInputSchema = z.object({
|
||||||
|
rateCardId: z.string(),
|
||||||
|
lines: z.array(CreateRateCardLineSchema),
|
||||||
|
});
|
||||||
|
|
||||||
|
export const rateCardResolveLineInputSchema = z.object({
|
||||||
|
rateCardId: z.string(),
|
||||||
|
roleId: z.string().optional(),
|
||||||
|
chapter: z.string().optional(),
|
||||||
|
location: z.string().optional(),
|
||||||
|
seniority: z.string().optional(),
|
||||||
|
workType: z.string().optional(),
|
||||||
|
});
|
||||||
|
|
||||||
|
type RateCardListInput = z.infer<typeof rateCardListInputSchema>;
|
||||||
|
type RateCardByIdInput = z.infer<typeof rateCardByIdInputSchema>;
|
||||||
|
type RateCardLookupBestMatchInput = z.infer<typeof rateCardLookupBestMatchInputSchema>;
|
||||||
|
type RateCardResolveBestRateInput = z.infer<typeof rateCardResolveBestRateInputSchema>;
|
||||||
|
type RateCardCreateInput = z.infer<typeof CreateRateCardSchema>;
|
||||||
|
type RateCardUpdateInput = z.infer<typeof rateCardUpdateInputSchema>;
|
||||||
|
type RateCardDeactivateInput = z.infer<typeof rateCardByIdInputSchema>;
|
||||||
|
type RateCardAddLineInput = z.infer<typeof rateCardAddLineInputSchema>;
|
||||||
|
type RateCardUpdateLineInput = z.infer<typeof rateCardUpdateLineInputSchema>;
|
||||||
|
type RateCardDeleteLineInput = z.infer<typeof rateCardDeleteLineInputSchema>;
|
||||||
|
type RateCardReplaceLinesInput = z.infer<typeof rateCardReplaceLinesInputSchema>;
|
||||||
|
type RateCardResolveLineInput = z.infer<typeof rateCardResolveLineInputSchema>;
|
||||||
|
|
||||||
|
export async function listRateCards(
|
||||||
|
ctx: RateCardProcedureContext,
|
||||||
|
input: RateCardListInput,
|
||||||
|
) {
|
||||||
|
return ctx.db.rateCard.findMany({
|
||||||
|
where: buildRateCardListWhere(input),
|
||||||
|
include: rateCardSummaryInclude,
|
||||||
|
orderBy: [{ isActive: "desc" }, { effectiveFrom: "desc" }, { name: "asc" }],
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
export async function getRateCardById(
|
||||||
|
ctx: RateCardProcedureContext,
|
||||||
|
input: RateCardByIdInput,
|
||||||
|
) {
|
||||||
|
return findUniqueOrThrow(
|
||||||
|
ctx.db.rateCard.findUnique({
|
||||||
|
where: { id: input.id },
|
||||||
|
include: rateCardDetailInclude,
|
||||||
|
}),
|
||||||
|
"Rate card",
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
export async function lookupRateCardBestMatch(
|
||||||
|
ctx: RateCardProcedureContext,
|
||||||
|
input: RateCardLookupBestMatchInput,
|
||||||
|
) {
|
||||||
|
return lookupBestRateMatch(ctx.db, input);
|
||||||
|
}
|
||||||
|
|
||||||
|
export async function resolveRateCard(
|
||||||
|
ctx: RateCardProcedureContext,
|
||||||
|
input: RateCardResolveBestRateInput,
|
||||||
|
) {
|
||||||
|
return resolveBestRate(ctx.db, input);
|
||||||
|
}
|
||||||
|
|
||||||
|
export async function createRateCard(
|
||||||
|
ctx: RateCardProcedureContext,
|
||||||
|
input: RateCardCreateInput,
|
||||||
|
) {
|
||||||
|
const { lines, name, currency } = input;
|
||||||
|
|
||||||
|
const rateCard = await ctx.db.rateCard.create({
|
||||||
|
data: buildRateCardCreateData(input),
|
||||||
|
include: rateCardCreateInclude,
|
||||||
|
});
|
||||||
|
|
||||||
|
void createAuditEntry({
|
||||||
|
db: ctx.db,
|
||||||
|
entityType: "RateCard",
|
||||||
|
entityId: rateCard.id,
|
||||||
|
entityName: rateCard.name,
|
||||||
|
action: "CREATE",
|
||||||
|
...withAuditUser(ctx.dbUser?.id),
|
||||||
|
after: buildRateCardCreateAuditAfter({ name, currency, lines }),
|
||||||
|
source: "ui",
|
||||||
|
});
|
||||||
|
|
||||||
|
return rateCard;
|
||||||
|
}
|
||||||
|
|
||||||
|
export async function updateRateCard(
|
||||||
|
ctx: RateCardProcedureContext,
|
||||||
|
input: RateCardUpdateInput,
|
||||||
|
) {
|
||||||
|
const before = await findUniqueOrThrow(
|
||||||
|
ctx.db.rateCard.findUnique({ where: { id: input.id } }),
|
||||||
|
"Rate card",
|
||||||
|
);
|
||||||
|
|
||||||
|
const updated = await ctx.db.rateCard.update({
|
||||||
|
where: { id: input.id },
|
||||||
|
data: buildRateCardUpdateData(input.data),
|
||||||
|
include: rateCardSummaryInclude,
|
||||||
|
});
|
||||||
|
|
||||||
|
void createAuditEntry({
|
||||||
|
db: ctx.db,
|
||||||
|
entityType: "RateCard",
|
||||||
|
entityId: input.id,
|
||||||
|
entityName: updated.name,
|
||||||
|
action: "UPDATE",
|
||||||
|
...withAuditUser(ctx.dbUser?.id),
|
||||||
|
before: before as unknown as Record<string, unknown>,
|
||||||
|
after: updated as unknown as Record<string, unknown>,
|
||||||
|
source: "ui",
|
||||||
|
});
|
||||||
|
|
||||||
|
return updated;
|
||||||
|
}
|
||||||
|
|
||||||
|
export async function deactivateRateCard(
|
||||||
|
ctx: RateCardProcedureContext,
|
||||||
|
input: RateCardDeactivateInput,
|
||||||
|
) {
|
||||||
|
const deactivated = await ctx.db.rateCard.update({
|
||||||
|
where: { id: input.id },
|
||||||
|
data: { isActive: false },
|
||||||
|
});
|
||||||
|
|
||||||
|
void createAuditEntry({
|
||||||
|
db: ctx.db,
|
||||||
|
entityType: "RateCard",
|
||||||
|
entityId: input.id,
|
||||||
|
entityName: deactivated.name,
|
||||||
|
action: "DELETE",
|
||||||
|
...withAuditUser(ctx.dbUser?.id),
|
||||||
|
source: "ui",
|
||||||
|
summary: "Deactivated rate card",
|
||||||
|
});
|
||||||
|
|
||||||
|
return deactivated;
|
||||||
|
}
|
||||||
|
|
||||||
|
export async function addRateCardLine(
|
||||||
|
ctx: RateCardProcedureContext,
|
||||||
|
input: RateCardAddLineInput,
|
||||||
|
) {
|
||||||
|
const rateCard = await findUniqueOrThrow(
|
||||||
|
ctx.db.rateCard.findUnique({ where: { id: input.rateCardId } }),
|
||||||
|
"Rate card",
|
||||||
|
);
|
||||||
|
|
||||||
|
const line = await ctx.db.rateCardLine.create({
|
||||||
|
data: buildRateCardLineCreateData(input.rateCardId, input.line),
|
||||||
|
select: rateCardLineSelect,
|
||||||
|
});
|
||||||
|
|
||||||
|
void createAuditEntry({
|
||||||
|
db: ctx.db,
|
||||||
|
entityType: "RateCardLine",
|
||||||
|
entityId: line.id,
|
||||||
|
entityName: `${rateCard.name} - ${input.line.chapter ?? "line"}`,
|
||||||
|
action: "CREATE",
|
||||||
|
...withAuditUser(ctx.dbUser?.id),
|
||||||
|
after: buildRateCardLineCreateAuditAfter({
|
||||||
|
rateCardId: input.rateCardId,
|
||||||
|
line: input.line,
|
||||||
|
}),
|
||||||
|
source: "ui",
|
||||||
|
});
|
||||||
|
|
||||||
|
return line;
|
||||||
|
}
|
||||||
|
|
||||||
|
export async function updateRateCardLine(
|
||||||
|
ctx: RateCardProcedureContext,
|
||||||
|
input: RateCardUpdateLineInput,
|
||||||
|
) {
|
||||||
|
const before = await findUniqueOrThrow(
|
||||||
|
ctx.db.rateCardLine.findUnique({ where: { id: input.lineId } }),
|
||||||
|
"Rate card line",
|
||||||
|
);
|
||||||
|
|
||||||
|
const updated = await ctx.db.rateCardLine.update({
|
||||||
|
where: { id: input.lineId },
|
||||||
|
data: buildRateCardLineUpdateData(input.data),
|
||||||
|
select: rateCardLineSelect,
|
||||||
|
});
|
||||||
|
|
||||||
|
void createAuditEntry({
|
||||||
|
db: ctx.db,
|
||||||
|
entityType: "RateCardLine",
|
||||||
|
entityId: input.lineId,
|
||||||
|
action: "UPDATE",
|
||||||
|
...withAuditUser(ctx.dbUser?.id),
|
||||||
|
before: before as unknown as Record<string, unknown>,
|
||||||
|
after: updated as unknown as Record<string, unknown>,
|
||||||
|
source: "ui",
|
||||||
|
});
|
||||||
|
|
||||||
|
return updated;
|
||||||
|
}
|
||||||
|
|
||||||
|
export async function deleteRateCardLine(
|
||||||
|
ctx: RateCardProcedureContext,
|
||||||
|
input: RateCardDeleteLineInput,
|
||||||
|
) {
|
||||||
|
const line = await findUniqueOrThrow(
|
||||||
|
ctx.db.rateCardLine.findUnique({ where: { id: input.lineId } }),
|
||||||
|
"Rate card line",
|
||||||
|
);
|
||||||
|
|
||||||
|
await ctx.db.rateCardLine.delete({ where: { id: input.lineId } });
|
||||||
|
|
||||||
|
void createAuditEntry({
|
||||||
|
db: ctx.db,
|
||||||
|
entityType: "RateCardLine",
|
||||||
|
entityId: input.lineId,
|
||||||
|
action: "DELETE",
|
||||||
|
...withAuditUser(ctx.dbUser?.id),
|
||||||
|
before: line as unknown as Record<string, unknown>,
|
||||||
|
source: "ui",
|
||||||
|
});
|
||||||
|
|
||||||
|
return { deleted: true };
|
||||||
|
}
|
||||||
|
|
||||||
|
export async function replaceRateCardLines(
|
||||||
|
ctx: RateCardProcedureContext,
|
||||||
|
input: RateCardReplaceLinesInput,
|
||||||
|
) {
|
||||||
|
const rateCard = await findUniqueOrThrow(
|
||||||
|
ctx.db.rateCard.findUnique({ where: { id: input.rateCardId } }),
|
||||||
|
"Rate card",
|
||||||
|
);
|
||||||
|
|
||||||
|
const result = await ctx.db.$transaction(async (tx) => {
|
||||||
|
await tx.rateCardLine.deleteMany({ where: { rateCardId: input.rateCardId } });
|
||||||
|
|
||||||
|
return Promise.all(
|
||||||
|
input.lines.map((line) =>
|
||||||
|
tx.rateCardLine.create({
|
||||||
|
data: {
|
||||||
|
rateCardId: input.rateCardId,
|
||||||
|
...buildRateCardNestedLineCreateData(line),
|
||||||
|
},
|
||||||
|
select: rateCardLineSelect,
|
||||||
|
}),
|
||||||
|
),
|
||||||
|
);
|
||||||
|
});
|
||||||
|
|
||||||
|
void createAuditEntry({
|
||||||
|
db: ctx.db,
|
||||||
|
entityType: "RateCard",
|
||||||
|
entityId: input.rateCardId,
|
||||||
|
entityName: rateCard.name,
|
||||||
|
action: "UPDATE",
|
||||||
|
...withAuditUser(ctx.dbUser?.id),
|
||||||
|
after: buildRateCardReplaceLinesAuditAfter(result.length),
|
||||||
|
source: "ui",
|
||||||
|
summary: `Replaced all lines with ${result.length} new lines`,
|
||||||
|
});
|
||||||
|
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
export async function resolveRateCardLine(
|
||||||
|
ctx: RateCardProcedureContext,
|
||||||
|
input: RateCardResolveLineInput,
|
||||||
|
) {
|
||||||
|
const { rateCardId, ...criteria } = input;
|
||||||
|
const lines = await ctx.db.rateCardLine.findMany({
|
||||||
|
where: { rateCardId },
|
||||||
|
select: rateCardLineSelect,
|
||||||
|
});
|
||||||
|
return resolveBestRateLineMatch(lines, criteria);
|
||||||
|
}
|
||||||
@@ -1,306 +1,82 @@
|
|||||||
import {
|
import { CreateRateCardSchema } from "@capakraken/shared";
|
||||||
CreateRateCardLineSchema,
|
|
||||||
CreateRateCardSchema,
|
|
||||||
UpdateRateCardLineSchema,
|
|
||||||
UpdateRateCardSchema,
|
|
||||||
} from "@capakraken/shared";
|
|
||||||
import { z } from "zod";
|
|
||||||
import { findUniqueOrThrow } from "../db/helpers.js";
|
|
||||||
import { createTRPCRouter, controllerProcedure, managerProcedure } from "../trpc.js";
|
import { createTRPCRouter, controllerProcedure, managerProcedure } from "../trpc.js";
|
||||||
import { createAuditEntry } from "../lib/audit.js";
|
|
||||||
import {
|
import {
|
||||||
lookupBestRateMatch,
|
addRateCardLine,
|
||||||
rateCardLineSelect,
|
createRateCard,
|
||||||
resolveBestRate,
|
deactivateRateCard,
|
||||||
resolveBestRateLineMatch,
|
deleteRateCardLine,
|
||||||
} from "./rate-card-read.js";
|
getRateCardById,
|
||||||
import {
|
listRateCards,
|
||||||
buildRateCardCreateAuditAfter,
|
lookupRateCardBestMatch,
|
||||||
buildRateCardCreateData,
|
rateCardAddLineInputSchema,
|
||||||
buildRateCardLineCreateAuditAfter,
|
rateCardByIdInputSchema,
|
||||||
buildRateCardLineCreateData,
|
rateCardDeleteLineInputSchema,
|
||||||
buildRateCardLineUpdateData,
|
rateCardListInputSchema,
|
||||||
buildRateCardListWhere,
|
rateCardLookupBestMatchInputSchema,
|
||||||
buildRateCardReplaceLinesAuditAfter,
|
rateCardReplaceLinesInputSchema,
|
||||||
buildRateCardUpdateData,
|
rateCardResolveBestRateInputSchema,
|
||||||
rateCardCreateInclude,
|
rateCardResolveLineInputSchema,
|
||||||
rateCardDetailInclude,
|
rateCardUpdateInputSchema,
|
||||||
buildRateCardNestedLineCreateData,
|
rateCardUpdateLineInputSchema,
|
||||||
rateCardSummaryInclude,
|
replaceRateCardLines,
|
||||||
} from "./rate-card-write-support.js";
|
resolveRateCard,
|
||||||
|
resolveRateCardLine,
|
||||||
|
updateRateCard,
|
||||||
|
updateRateCardLine,
|
||||||
|
} from "./rate-card-procedure-support.js";
|
||||||
|
|
||||||
export const rateCardRouter = createTRPCRouter({
|
export const rateCardRouter = createTRPCRouter({
|
||||||
list: controllerProcedure
|
list: controllerProcedure
|
||||||
.input(
|
.input(rateCardListInputSchema)
|
||||||
z.object({
|
.query(({ ctx, input }) => listRateCards(ctx, input)),
|
||||||
isActive: z.boolean().optional(),
|
|
||||||
search: z.string().optional(),
|
|
||||||
clientId: z.string().optional(),
|
|
||||||
effectiveAt: z.coerce.date().optional(),
|
|
||||||
}).optional(),
|
|
||||||
)
|
|
||||||
.query(async ({ ctx, input }) => {
|
|
||||||
return ctx.db.rateCard.findMany({
|
|
||||||
where: buildRateCardListWhere(input),
|
|
||||||
include: rateCardSummaryInclude,
|
|
||||||
orderBy: [{ isActive: "desc" }, { effectiveFrom: "desc" }, { name: "asc" }],
|
|
||||||
});
|
|
||||||
}),
|
|
||||||
|
|
||||||
getById: controllerProcedure
|
getById: controllerProcedure
|
||||||
.input(z.object({ id: z.string() }))
|
.input(rateCardByIdInputSchema)
|
||||||
.query(async ({ ctx, input }) => {
|
.query(({ ctx, input }) => getRateCardById(ctx, input)),
|
||||||
const rateCard = await findUniqueOrThrow(
|
|
||||||
ctx.db.rateCard.findUnique({
|
|
||||||
where: { id: input.id },
|
|
||||||
include: rateCardDetailInclude,
|
|
||||||
}),
|
|
||||||
"Rate card",
|
|
||||||
);
|
|
||||||
return rateCard;
|
|
||||||
}),
|
|
||||||
|
|
||||||
lookupBestMatch: controllerProcedure
|
lookupBestMatch: controllerProcedure
|
||||||
.input(z.object({
|
.input(rateCardLookupBestMatchInputSchema)
|
||||||
clientId: z.string().optional(),
|
.query(({ ctx, input }) => lookupRateCardBestMatch(ctx, input)),
|
||||||
chapter: z.string().optional(),
|
|
||||||
managementLevelId: z.string().optional(),
|
|
||||||
roleName: z.string().optional(),
|
|
||||||
seniority: z.string().optional(),
|
|
||||||
}))
|
|
||||||
.query(async ({ ctx, input }) => lookupBestRateMatch(ctx.db, input)),
|
|
||||||
|
|
||||||
resolveBestRate: controllerProcedure
|
resolveBestRate: controllerProcedure
|
||||||
.input(z.object({
|
.input(rateCardResolveBestRateInputSchema)
|
||||||
resourceId: z.string().optional(),
|
.query(({ ctx, input }) => resolveRateCard(ctx, input)),
|
||||||
roleName: z.string().optional(),
|
|
||||||
date: z.coerce.date().optional(),
|
|
||||||
}))
|
|
||||||
.query(async ({ ctx, input }) => resolveBestRate(ctx.db, input)),
|
|
||||||
|
|
||||||
create: managerProcedure
|
create: managerProcedure
|
||||||
.input(CreateRateCardSchema)
|
.input(CreateRateCardSchema)
|
||||||
.mutation(async ({ ctx, input }) => {
|
.mutation(({ ctx, input }) => createRateCard(ctx, input)),
|
||||||
const { lines, name, currency } = input;
|
|
||||||
|
|
||||||
const rateCard = await ctx.db.rateCard.create({
|
|
||||||
data: buildRateCardCreateData(input),
|
|
||||||
include: rateCardCreateInclude,
|
|
||||||
});
|
|
||||||
|
|
||||||
void createAuditEntry({
|
|
||||||
db: ctx.db,
|
|
||||||
entityType: "RateCard",
|
|
||||||
entityId: rateCard.id,
|
|
||||||
entityName: rateCard.name,
|
|
||||||
action: "CREATE",
|
|
||||||
userId: ctx.dbUser?.id,
|
|
||||||
after: buildRateCardCreateAuditAfter({ name, currency, lines }),
|
|
||||||
source: "ui",
|
|
||||||
});
|
|
||||||
|
|
||||||
return rateCard;
|
|
||||||
}),
|
|
||||||
|
|
||||||
update: managerProcedure
|
update: managerProcedure
|
||||||
.input(z.object({ id: z.string(), data: UpdateRateCardSchema }))
|
.input(rateCardUpdateInputSchema)
|
||||||
.mutation(async ({ ctx, input }) => {
|
.mutation(({ ctx, input }) => updateRateCard(ctx, input)),
|
||||||
const before = await findUniqueOrThrow(
|
|
||||||
ctx.db.rateCard.findUnique({ where: { id: input.id } }),
|
|
||||||
"Rate card",
|
|
||||||
);
|
|
||||||
|
|
||||||
const updated = await ctx.db.rateCard.update({
|
|
||||||
where: { id: input.id },
|
|
||||||
data: buildRateCardUpdateData(input.data),
|
|
||||||
include: rateCardSummaryInclude,
|
|
||||||
});
|
|
||||||
|
|
||||||
void createAuditEntry({
|
|
||||||
db: ctx.db,
|
|
||||||
entityType: "RateCard",
|
|
||||||
entityId: input.id,
|
|
||||||
entityName: updated.name,
|
|
||||||
action: "UPDATE",
|
|
||||||
userId: ctx.dbUser?.id,
|
|
||||||
before: before as unknown as Record<string, unknown>,
|
|
||||||
after: updated as unknown as Record<string, unknown>,
|
|
||||||
source: "ui",
|
|
||||||
});
|
|
||||||
|
|
||||||
return updated;
|
|
||||||
}),
|
|
||||||
|
|
||||||
deactivate: managerProcedure
|
deactivate: managerProcedure
|
||||||
.input(z.object({ id: z.string() }))
|
.input(rateCardByIdInputSchema)
|
||||||
.mutation(async ({ ctx, input }) => {
|
.mutation(({ ctx, input }) => deactivateRateCard(ctx, input)),
|
||||||
const deactivated = await ctx.db.rateCard.update({
|
|
||||||
where: { id: input.id },
|
|
||||||
data: { isActive: false },
|
|
||||||
});
|
|
||||||
|
|
||||||
void createAuditEntry({
|
|
||||||
db: ctx.db,
|
|
||||||
entityType: "RateCard",
|
|
||||||
entityId: input.id,
|
|
||||||
entityName: deactivated.name,
|
|
||||||
action: "DELETE",
|
|
||||||
userId: ctx.dbUser?.id,
|
|
||||||
source: "ui",
|
|
||||||
summary: "Deactivated rate card",
|
|
||||||
});
|
|
||||||
|
|
||||||
return deactivated;
|
|
||||||
}),
|
|
||||||
|
|
||||||
// ─── Line CRUD ─────────────────────────────────────────────────────────────
|
// ─── Line CRUD ─────────────────────────────────────────────────────────────
|
||||||
|
|
||||||
addLine: managerProcedure
|
addLine: managerProcedure
|
||||||
.input(z.object({ rateCardId: z.string(), line: CreateRateCardLineSchema }))
|
.input(rateCardAddLineInputSchema)
|
||||||
.mutation(async ({ ctx, input }) => {
|
.mutation(({ ctx, input }) => addRateCardLine(ctx, input)),
|
||||||
const rateCard = await findUniqueOrThrow(
|
|
||||||
ctx.db.rateCard.findUnique({ where: { id: input.rateCardId } }),
|
|
||||||
"Rate card",
|
|
||||||
);
|
|
||||||
|
|
||||||
const line = await ctx.db.rateCardLine.create({
|
|
||||||
data: buildRateCardLineCreateData(input.rateCardId, input.line),
|
|
||||||
select: rateCardLineSelect,
|
|
||||||
});
|
|
||||||
|
|
||||||
void createAuditEntry({
|
|
||||||
db: ctx.db,
|
|
||||||
entityType: "RateCardLine",
|
|
||||||
entityId: line.id,
|
|
||||||
entityName: `${rateCard.name} — ${input.line.chapter ?? "line"}`,
|
|
||||||
action: "CREATE",
|
|
||||||
userId: ctx.dbUser?.id,
|
|
||||||
after: buildRateCardLineCreateAuditAfter({
|
|
||||||
rateCardId: input.rateCardId,
|
|
||||||
line: input.line,
|
|
||||||
}),
|
|
||||||
source: "ui",
|
|
||||||
});
|
|
||||||
|
|
||||||
return line;
|
|
||||||
}),
|
|
||||||
|
|
||||||
updateLine: managerProcedure
|
updateLine: managerProcedure
|
||||||
.input(z.object({ lineId: z.string(), data: UpdateRateCardLineSchema }))
|
.input(rateCardUpdateLineInputSchema)
|
||||||
.mutation(async ({ ctx, input }) => {
|
.mutation(({ ctx, input }) => updateRateCardLine(ctx, input)),
|
||||||
const before = await findUniqueOrThrow(
|
|
||||||
ctx.db.rateCardLine.findUnique({ where: { id: input.lineId } }),
|
|
||||||
"Rate card line",
|
|
||||||
);
|
|
||||||
|
|
||||||
const updated = await ctx.db.rateCardLine.update({
|
|
||||||
where: { id: input.lineId },
|
|
||||||
data: buildRateCardLineUpdateData(input.data),
|
|
||||||
select: rateCardLineSelect,
|
|
||||||
});
|
|
||||||
|
|
||||||
void createAuditEntry({
|
|
||||||
db: ctx.db,
|
|
||||||
entityType: "RateCardLine",
|
|
||||||
entityId: input.lineId,
|
|
||||||
action: "UPDATE",
|
|
||||||
userId: ctx.dbUser?.id,
|
|
||||||
before: before as unknown as Record<string, unknown>,
|
|
||||||
after: updated as unknown as Record<string, unknown>,
|
|
||||||
source: "ui",
|
|
||||||
});
|
|
||||||
|
|
||||||
return updated;
|
|
||||||
}),
|
|
||||||
|
|
||||||
deleteLine: managerProcedure
|
deleteLine: managerProcedure
|
||||||
.input(z.object({ lineId: z.string() }))
|
.input(rateCardDeleteLineInputSchema)
|
||||||
.mutation(async ({ ctx, input }) => {
|
.mutation(({ ctx, input }) => deleteRateCardLine(ctx, input)),
|
||||||
const line = await findUniqueOrThrow(
|
|
||||||
ctx.db.rateCardLine.findUnique({ where: { id: input.lineId } }),
|
|
||||||
"Rate card line",
|
|
||||||
);
|
|
||||||
|
|
||||||
await ctx.db.rateCardLine.delete({ where: { id: input.lineId } });
|
|
||||||
|
|
||||||
void createAuditEntry({
|
|
||||||
db: ctx.db,
|
|
||||||
entityType: "RateCardLine",
|
|
||||||
entityId: input.lineId,
|
|
||||||
action: "DELETE",
|
|
||||||
userId: ctx.dbUser?.id,
|
|
||||||
before: line as unknown as Record<string, unknown>,
|
|
||||||
source: "ui",
|
|
||||||
});
|
|
||||||
|
|
||||||
return { deleted: true };
|
|
||||||
}),
|
|
||||||
|
|
||||||
// ─── Batch operations ──────────────────────────────────────────────────────
|
// ─── Batch operations ──────────────────────────────────────────────────────
|
||||||
|
|
||||||
replaceLines: managerProcedure
|
replaceLines: managerProcedure
|
||||||
.input(z.object({
|
.input(rateCardReplaceLinesInputSchema)
|
||||||
rateCardId: z.string(),
|
.mutation(({ ctx, input }) => replaceRateCardLines(ctx, input)),
|
||||||
lines: z.array(CreateRateCardLineSchema),
|
|
||||||
}))
|
|
||||||
.mutation(async ({ ctx, input }) => {
|
|
||||||
const rateCard = await findUniqueOrThrow(
|
|
||||||
ctx.db.rateCard.findUnique({ where: { id: input.rateCardId } }),
|
|
||||||
"Rate card",
|
|
||||||
);
|
|
||||||
|
|
||||||
const result = await ctx.db.$transaction(async (tx) => {
|
|
||||||
await tx.rateCardLine.deleteMany({ where: { rateCardId: input.rateCardId } });
|
|
||||||
|
|
||||||
const created = await Promise.all(
|
|
||||||
input.lines.map((line) =>
|
|
||||||
tx.rateCardLine.create({
|
|
||||||
data: {
|
|
||||||
rateCardId: input.rateCardId,
|
|
||||||
...buildRateCardNestedLineCreateData(line),
|
|
||||||
},
|
|
||||||
select: rateCardLineSelect,
|
|
||||||
}),
|
|
||||||
),
|
|
||||||
);
|
|
||||||
|
|
||||||
return created;
|
|
||||||
});
|
|
||||||
|
|
||||||
void createAuditEntry({
|
|
||||||
db: ctx.db,
|
|
||||||
entityType: "RateCard",
|
|
||||||
entityId: input.rateCardId,
|
|
||||||
entityName: rateCard.name,
|
|
||||||
action: "UPDATE",
|
|
||||||
userId: ctx.dbUser?.id,
|
|
||||||
after: buildRateCardReplaceLinesAuditAfter(result.length),
|
|
||||||
source: "ui",
|
|
||||||
summary: `Replaced all lines with ${result.length} new lines`,
|
|
||||||
});
|
|
||||||
|
|
||||||
return result;
|
|
||||||
}),
|
|
||||||
|
|
||||||
// ─── Rate resolution ───────────────────────────────────────────────────────
|
// ─── Rate resolution ───────────────────────────────────────────────────────
|
||||||
|
|
||||||
resolveRateLine: controllerProcedure
|
resolveRateLine: controllerProcedure
|
||||||
.input(z.object({
|
.input(rateCardResolveLineInputSchema)
|
||||||
rateCardId: z.string(),
|
.query(({ ctx, input }) => resolveRateCardLine(ctx, input)),
|
||||||
roleId: z.string().optional(),
|
|
||||||
chapter: z.string().optional(),
|
|
||||||
location: z.string().optional(),
|
|
||||||
seniority: z.string().optional(),
|
|
||||||
workType: z.string().optional(),
|
|
||||||
}))
|
|
||||||
.query(async ({ ctx, input }) => {
|
|
||||||
const { rateCardId, ...criteria } = input;
|
|
||||||
|
|
||||||
// Find the most specific matching line (most criteria matched wins)
|
|
||||||
const lines = await ctx.db.rateCardLine.findMany({
|
|
||||||
where: { rateCardId },
|
|
||||||
select: rateCardLineSelect,
|
|
||||||
});
|
|
||||||
return resolveBestRateLineMatch(lines, criteria);
|
|
||||||
}),
|
|
||||||
});
|
});
|
||||||
|
|||||||
Reference in New Issue
Block a user