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("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 },
|
||||
]);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user