refactor(api): extract rate card procedures

This commit is contained in:
2026-03-31 19:56:14 +02:00
parent 4586e94c95
commit cec4169bea
4 changed files with 621 additions and 291 deletions
@@ -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("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([
{ 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 } },
]);
const result = await findMany({
where: {},
include: { _count: { select: { lines: true } } },
orderBy: [{ isActive: "desc" }, { effectiveFrom: "desc" }, { name: "asc" }],
const caller = createControllerCaller({
rateCard: { findMany },
});
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[0]._count.lines).toBe(5);
});
});
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({
id: "rc_new",
name: "Q1 2026 Rates",
@@ -51,16 +79,21 @@ describe("rateCard router", () => {
],
});
const result = await create({
data: {
name: "Q1 2026 Rates",
currency: "EUR",
lines: {
create: [{ costRateCents: 9500, billRateCents: 14000, chapter: "Digital Content Production" }],
},
},
const caller = createManagerCaller({
rateCard: { create },
});
const result = await caller.create({
name: "Q1 2026 Rates",
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.lines).toHaveLength(1);
expect(result.lines[0].costRateCents).toBe(9500);
@@ -211,19 +244,34 @@ describe("rateCard router", () => {
});
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 createLine = vi.fn()
.mockResolvedValueOnce({ id: "rcl_new_1", costRateCents: 8000 })
.mockResolvedValueOnce({ id: "rcl_new_2", costRateCents: 9500 });
await deleteMany({ where: { rateCardId: "rc_1" } });
const line1 = await createLine({ data: { rateCardId: "rc_1", costRateCents: 8000 } });
const line2 = await createLine({ data: { rateCardId: "rc_1", costRateCents: 9500 } });
const caller = createManagerCaller({
rateCard: {
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(line1.id).toBe("rcl_new_1");
expect(line2.id).toBe("rcl_new_2");
expect(result).toEqual([
{ id: "rcl_new_1", costRateCents: 8000 },
{ id: "rcl_new_2", costRateCents: 9500 },
]);
});
});
});