refactor(application): extract vacation management into application use-cases
Moves approve, reject, cancel, and request vacation business logic out of the tRPC procedure layer into packages/application, matching the pattern used by allocation use-cases. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -35,7 +35,7 @@ vi.mock("@capakraken/application", async (importOriginal) => {
|
||||
};
|
||||
});
|
||||
|
||||
import { executeTool, type ToolContext } from "../router/assistant-tools.js";
|
||||
import { executeTool } from "../router/assistant-tools.js";
|
||||
import { createToolContext } from "./assistant-tools-vacation-entitlement-test-helpers.js";
|
||||
|
||||
describe("assistant vacation approval error paths", () => {
|
||||
@@ -67,13 +67,22 @@ describe("assistant vacation approval error paths", () => {
|
||||
});
|
||||
|
||||
it("returns a stable assistant error when vacation approval violates lifecycle preconditions", async () => {
|
||||
const alreadyApprovedVacation = {
|
||||
id: "vac_approved",
|
||||
status: "APPROVED",
|
||||
resource: { displayName: "Alice Example" },
|
||||
};
|
||||
const ctx = createToolContext(
|
||||
{
|
||||
user: {
|
||||
findUnique: vi.fn().mockResolvedValue({ id: "mgr_1", systemRole: "MANAGER" }),
|
||||
},
|
||||
vacation: {
|
||||
findUnique: vi.fn().mockResolvedValue({
|
||||
id: "vac_approved",
|
||||
resource: { displayName: "Alice Example" },
|
||||
}),
|
||||
findUnique: vi.fn().mockResolvedValue(alreadyApprovedVacation),
|
||||
},
|
||||
resource: {
|
||||
findUnique: vi.fn().mockResolvedValue(null),
|
||||
count: vi.fn().mockResolvedValue(0),
|
||||
},
|
||||
},
|
||||
{ userRole: SystemRole.MANAGER },
|
||||
@@ -82,30 +91,7 @@ describe("assistant vacation approval error paths", () => {
|
||||
const result = await executeTool(
|
||||
"approve_vacation",
|
||||
JSON.stringify({ vacationId: "vac_approved" }),
|
||||
{
|
||||
...ctx,
|
||||
db: {
|
||||
...ctx.db,
|
||||
vacation: {
|
||||
...((ctx.db as Record<string, unknown>).vacation as Record<string, unknown>),
|
||||
findUnique: vi.fn()
|
||||
.mockResolvedValueOnce({
|
||||
id: "vac_approved",
|
||||
resource: { displayName: "Alice Example" },
|
||||
})
|
||||
.mockResolvedValueOnce({
|
||||
id: "vac_approved",
|
||||
resource: { displayName: "Alice Example" },
|
||||
}),
|
||||
update: vi.fn().mockRejectedValue(
|
||||
new TRPCError({
|
||||
code: "BAD_REQUEST",
|
||||
message: "Only PENDING, CANCELLED, or REJECTED vacations can be approved",
|
||||
}),
|
||||
),
|
||||
},
|
||||
} as ToolContext["db"],
|
||||
},
|
||||
ctx,
|
||||
);
|
||||
|
||||
expect(JSON.parse(result.content)).toEqual({
|
||||
|
||||
@@ -36,7 +36,7 @@ vi.mock("@capakraken/application", async (importOriginal) => {
|
||||
};
|
||||
});
|
||||
|
||||
import { executeTool, type ToolContext } from "../router/assistant-tools.js";
|
||||
import { executeTool } from "../router/assistant-tools.js";
|
||||
import { createToolContext } from "./assistant-tools-vacation-entitlement-test-helpers.js";
|
||||
|
||||
describe("assistant vacation cancellation error paths", () => {
|
||||
@@ -68,14 +68,19 @@ describe("assistant vacation cancellation error paths", () => {
|
||||
});
|
||||
|
||||
it("returns a stable assistant error when vacation cancellation violates lifecycle preconditions", async () => {
|
||||
const alreadyCancelledVacation = {
|
||||
id: "vac_cancelled",
|
||||
status: VacationStatus.CANCELLED,
|
||||
requestedById: "user_1",
|
||||
resource: { displayName: "Alice Example", userId: "user_1" },
|
||||
};
|
||||
const ctx = createToolContext(
|
||||
{
|
||||
user: {
|
||||
findUnique: vi.fn().mockResolvedValue({ id: "user_1", systemRole: "USER" }),
|
||||
},
|
||||
vacation: {
|
||||
findUnique: vi.fn().mockResolvedValue({
|
||||
id: "vac_cancelled",
|
||||
requestedById: "user_1",
|
||||
resource: { displayName: "Alice Example", userId: "user_1" },
|
||||
}),
|
||||
findUnique: vi.fn().mockResolvedValue(alreadyCancelledVacation),
|
||||
},
|
||||
},
|
||||
{ userRole: SystemRole.USER, permissions: [] },
|
||||
@@ -84,32 +89,7 @@ describe("assistant vacation cancellation error paths", () => {
|
||||
const result = await executeTool(
|
||||
"cancel_vacation",
|
||||
JSON.stringify({ vacationId: "vac_cancelled" }),
|
||||
{
|
||||
...ctx,
|
||||
db: {
|
||||
...ctx.db,
|
||||
vacation: {
|
||||
...((ctx.db as Record<string, unknown>).vacation as Record<string, unknown>),
|
||||
findUnique: vi.fn()
|
||||
.mockResolvedValueOnce({
|
||||
id: "vac_cancelled",
|
||||
requestedById: "user_1",
|
||||
resource: { displayName: "Alice Example", userId: "user_1" },
|
||||
})
|
||||
.mockResolvedValueOnce({
|
||||
id: "vac_cancelled",
|
||||
status: VacationStatus.CANCELLED,
|
||||
resource: { displayName: "Alice Example" },
|
||||
}),
|
||||
update: vi.fn().mockRejectedValue(
|
||||
new TRPCError({
|
||||
code: "BAD_REQUEST",
|
||||
message: "Already cancelled",
|
||||
}),
|
||||
),
|
||||
},
|
||||
} as ToolContext["db"],
|
||||
},
|
||||
ctx,
|
||||
);
|
||||
|
||||
expect(JSON.parse(result.content)).toEqual({
|
||||
|
||||
@@ -72,13 +72,13 @@ describe("assistant vacation mutation tools", () => {
|
||||
message: "Rejected vacation for Alice Example: Capacity freeze",
|
||||
}),
|
||||
);
|
||||
expect(db.vacation.updateMany).toHaveBeenCalledWith(
|
||||
expect(db.vacation.update).toHaveBeenCalledWith(
|
||||
expect.objectContaining({
|
||||
where: expect.objectContaining({ id: "vac_cancelled" }),
|
||||
data: expect.objectContaining({ status: "APPROVED" }),
|
||||
}),
|
||||
);
|
||||
expect(db.vacation.updateMany).toHaveBeenCalledWith(
|
||||
expect(db.vacation.update).toHaveBeenCalledWith(
|
||||
expect.objectContaining({
|
||||
where: expect.objectContaining({ id: "vac_pending" }),
|
||||
data: expect.objectContaining({ status: "REJECTED", rejectionReason: "Capacity freeze" }),
|
||||
|
||||
@@ -312,6 +312,7 @@ describe("vacation router", () => {
|
||||
it("logs and swallows async notification failures during approval", async () => {
|
||||
vi.mocked(createNotification).mockRejectedValueOnce(new Error("notification down"));
|
||||
|
||||
const approvedVacation = { ...sampleVacation, status: VacationStatus.APPROVED };
|
||||
const db = createVacationDb({
|
||||
user: {
|
||||
findUnique: vi.fn().mockResolvedValue({ id: "mgr_1", systemRole: "MANAGER" }),
|
||||
@@ -332,11 +333,7 @@ describe("vacation router", () => {
|
||||
...sampleVacation,
|
||||
status: VacationStatus.PENDING,
|
||||
}),
|
||||
findUniqueOrThrow: vi.fn().mockResolvedValue({
|
||||
...sampleVacation,
|
||||
status: VacationStatus.APPROVED,
|
||||
}),
|
||||
updateMany: vi.fn().mockResolvedValue({ count: 1 }),
|
||||
update: vi.fn().mockResolvedValue(approvedVacation),
|
||||
},
|
||||
});
|
||||
|
||||
@@ -361,6 +358,7 @@ describe("vacation router", () => {
|
||||
it("logs and swallows webhook failures during approval", async () => {
|
||||
vi.mocked(dispatchWebhooks).mockRejectedValueOnce(new Error("webhook down"));
|
||||
|
||||
const approvedVacation = { ...sampleVacation, status: VacationStatus.APPROVED };
|
||||
const db = createVacationDb({
|
||||
user: {
|
||||
findUnique: vi.fn().mockResolvedValue({ id: "mgr_1", systemRole: "MANAGER" }),
|
||||
@@ -381,11 +379,7 @@ describe("vacation router", () => {
|
||||
...sampleVacation,
|
||||
status: VacationStatus.PENDING,
|
||||
}),
|
||||
findUniqueOrThrow: vi.fn().mockResolvedValue({
|
||||
...sampleVacation,
|
||||
status: VacationStatus.APPROVED,
|
||||
}),
|
||||
updateMany: vi.fn().mockResolvedValue({ count: 1 }),
|
||||
update: vi.fn().mockResolvedValue(approvedVacation),
|
||||
},
|
||||
});
|
||||
|
||||
@@ -899,8 +893,7 @@ describe("vacation router", () => {
|
||||
const db = createVacationDb({
|
||||
vacation: {
|
||||
findUnique: vi.fn().mockResolvedValue(sampleVacation),
|
||||
findUniqueOrThrow: vi.fn().mockResolvedValue(updatedVacation),
|
||||
updateMany: vi.fn().mockResolvedValue({ count: 1 }),
|
||||
update: vi.fn().mockResolvedValue(updatedVacation),
|
||||
},
|
||||
user: {
|
||||
findUnique: vi.fn().mockResolvedValue({ id: "mgr_1" }),
|
||||
@@ -914,7 +907,7 @@ describe("vacation router", () => {
|
||||
const result = await caller.approve({ id: "vac_1" });
|
||||
|
||||
expect(result.status).toBe(VacationStatus.APPROVED);
|
||||
expect(db.vacation.updateMany).toHaveBeenCalledWith(
|
||||
expect(db.vacation.update).toHaveBeenCalledWith(
|
||||
expect.objectContaining({
|
||||
where: expect.objectContaining({ id: "vac_1" }),
|
||||
data: expect.objectContaining({
|
||||
@@ -1020,8 +1013,7 @@ describe("vacation router", () => {
|
||||
const db = createVacationDb({
|
||||
vacation: {
|
||||
findUnique: vi.fn().mockResolvedValue(sampleVacation),
|
||||
findUniqueOrThrow: vi.fn().mockResolvedValue(updatedVacation),
|
||||
updateMany: vi.fn().mockResolvedValue({ count: 1 }),
|
||||
update: vi.fn().mockResolvedValue(updatedVacation),
|
||||
},
|
||||
resource: {
|
||||
findUnique: vi.fn().mockResolvedValue(null),
|
||||
@@ -1032,7 +1024,7 @@ describe("vacation router", () => {
|
||||
const result = await caller.reject({ id: "vac_1", rejectionReason: "Team conflict" });
|
||||
|
||||
expect(result.status).toBe(VacationStatus.REJECTED);
|
||||
expect(db.vacation.updateMany).toHaveBeenCalledWith(
|
||||
expect(db.vacation.update).toHaveBeenCalledWith(
|
||||
expect.objectContaining({
|
||||
where: expect.objectContaining({ id: "vac_1" }),
|
||||
data: expect.objectContaining({
|
||||
|
||||
Reference in New Issue
Block a user