feat(platform): harden access scoping and delivery baseline
This commit is contained in:
@@ -6,6 +6,7 @@ vi.mock("@capakraken/application", async (importOriginal) => {
|
||||
return {
|
||||
...actual,
|
||||
getDashboardBudgetForecast: vi.fn().mockResolvedValue([]),
|
||||
listAssignmentBookings: vi.fn().mockResolvedValue([]),
|
||||
};
|
||||
});
|
||||
|
||||
@@ -25,6 +26,16 @@ function createToolContext(
|
||||
userId: "user_1",
|
||||
userRole,
|
||||
permissions: new Set(permissions) as ToolContext["permissions"],
|
||||
session: {
|
||||
user: { email: "assistant@example.com", name: "Assistant User", image: null },
|
||||
expires: "2026-03-29T00:00:00.000Z",
|
||||
},
|
||||
dbUser: {
|
||||
id: "user_1",
|
||||
systemRole: userRole,
|
||||
permissionOverrides: null,
|
||||
},
|
||||
roleDefaults: null,
|
||||
};
|
||||
}
|
||||
|
||||
@@ -78,6 +89,7 @@ describe("assistant holiday tools", () => {
|
||||
findUnique: vi
|
||||
.fn()
|
||||
.mockResolvedValueOnce(null)
|
||||
.mockResolvedValueOnce({ id: "res_1", eid: "bruce.banner", displayName: "Bruce Banner" })
|
||||
.mockResolvedValueOnce({ id: "res_1", eid: "bruce.banner", displayName: "Bruce Banner", federalState: "BY", countryId: "country_de", metroCityId: "city_augsburg", country: { code: "DE", name: "Deutschland" }, metroCity: { name: "Augsburg" } }),
|
||||
findFirst: vi.fn(),
|
||||
},
|
||||
@@ -172,10 +184,10 @@ describe("assistant holiday tools", () => {
|
||||
it("previews resolved holiday calendars for a scope and shows the source calendar", async () => {
|
||||
const ctx = createToolContext({
|
||||
country: {
|
||||
findUnique: vi.fn().mockResolvedValue({ code: "DE" }),
|
||||
findUnique: vi.fn().mockResolvedValue({ id: "country_de", code: "DE", name: "Deutschland" }),
|
||||
},
|
||||
metroCity: {
|
||||
findUnique: vi.fn().mockResolvedValue({ name: "Augsburg" }),
|
||||
findUnique: vi.fn().mockResolvedValue({ id: "city_augsburg", name: "Augsburg", countryId: "country_de" }),
|
||||
},
|
||||
holidayCalendar: {
|
||||
findMany: vi.fn().mockResolvedValue([
|
||||
@@ -229,6 +241,14 @@ describe("assistant holiday tools", () => {
|
||||
}),
|
||||
]),
|
||||
);
|
||||
expect(ctx.db.country.findUnique).toHaveBeenCalledWith({
|
||||
where: { id: "country_de" },
|
||||
select: { id: true, code: true, name: true },
|
||||
});
|
||||
expect(ctx.db.metroCity.findUnique).toHaveBeenCalledWith({
|
||||
where: { id: "city_augsburg" },
|
||||
select: { id: true, name: true, countryId: true },
|
||||
});
|
||||
});
|
||||
|
||||
it("creates a holiday calendar through the assistant for admin users", async () => {
|
||||
@@ -301,36 +321,58 @@ describe("assistant holiday tools", () => {
|
||||
});
|
||||
|
||||
it("calculates chargeability with regional holidays excluded from booked and available hours", async () => {
|
||||
const resourceRecord = {
|
||||
id: "res_1",
|
||||
displayName: "Bruce Banner",
|
||||
eid: "bruce.banner",
|
||||
fte: 1,
|
||||
lcrCents: 5000,
|
||||
chargeabilityTarget: 80,
|
||||
availability: { monday: 8, tuesday: 8, wednesday: 8, thursday: 8, friday: 8, saturday: 0, sunday: 0 },
|
||||
countryId: "country_de",
|
||||
federalState: "BY",
|
||||
metroCityId: null,
|
||||
country: { id: "country_de", code: "DE", name: "Deutschland", dailyWorkingHours: 8, scheduleRules: null },
|
||||
metroCity: null,
|
||||
managementLevelGroup: null,
|
||||
};
|
||||
const db = {
|
||||
resource: {
|
||||
findUnique: vi
|
||||
.fn()
|
||||
.mockResolvedValueOnce({
|
||||
id: "res_1",
|
||||
displayName: "Bruce Banner",
|
||||
eid: "bruce.banner",
|
||||
fte: 1,
|
||||
chargeabilityTarget: 80,
|
||||
availability: { monday: 8, tuesday: 8, wednesday: 8, thursday: 8, friday: 8 },
|
||||
countryId: "country_de",
|
||||
federalState: "BY",
|
||||
metroCityId: null,
|
||||
country: { code: "DE", dailyWorkingHours: 8 },
|
||||
metroCity: null,
|
||||
}),
|
||||
.mockResolvedValueOnce(resourceRecord),
|
||||
findUniqueOrThrow: vi.fn().mockResolvedValue(resourceRecord),
|
||||
findFirst: vi.fn(),
|
||||
},
|
||||
assignment: {
|
||||
findMany: vi.fn().mockResolvedValue([
|
||||
{
|
||||
id: "assign_1",
|
||||
hoursPerDay: 8,
|
||||
startDate: new Date("2026-01-05T00:00:00.000Z"),
|
||||
endDate: new Date("2026-01-06T00:00:00.000Z"),
|
||||
dailyCostCents: 40000,
|
||||
status: "CONFIRMED",
|
||||
project: { name: "Gamma", shortCode: "GAM" },
|
||||
project: {
|
||||
id: "project_gamma",
|
||||
name: "Gamma",
|
||||
shortCode: "GAM",
|
||||
budgetCents: null,
|
||||
winProbability: 100,
|
||||
utilizationCategory: { code: "Chg" },
|
||||
},
|
||||
},
|
||||
]),
|
||||
},
|
||||
vacation: {
|
||||
findMany: vi.fn().mockResolvedValue([]),
|
||||
},
|
||||
holidayCalendar: {
|
||||
findMany: vi.fn().mockResolvedValue([]),
|
||||
},
|
||||
calculationRule: {
|
||||
findMany: vi.fn().mockResolvedValue([]),
|
||||
},
|
||||
};
|
||||
const ctx = createToolContext(db);
|
||||
|
||||
@@ -356,12 +398,12 @@ describe("assistant holiday tools", () => {
|
||||
|
||||
expect(parsed.bookedHours).toBe(8);
|
||||
expect(parsed.allocations).toEqual([expect.objectContaining({ hours: 8 })]);
|
||||
expect(parsed.baseWorkingDays).toBe(23);
|
||||
expect(parsed.baseAvailableHours).toBe(184);
|
||||
expect(parsed.availableHours).toBe(168);
|
||||
expect(parsed.workingDays).toBe(21);
|
||||
expect(parsed.targetHours).toBe(134.4);
|
||||
expect(parsed.unassignedHours).toBe(160);
|
||||
expect(parsed.baseWorkingDays).toBe(22);
|
||||
expect(parsed.baseAvailableHours).toBe(176);
|
||||
expect(parsed.availableHours).toBe(160);
|
||||
expect(parsed.workingDays).toBe(20);
|
||||
expect(parsed.targetHours).toBe(128);
|
||||
expect(parsed.unassignedHours).toBe(152);
|
||||
expect(parsed.locationContext.federalState).toBe("BY");
|
||||
expect(parsed.holidaySummary).toEqual(
|
||||
expect.objectContaining({
|
||||
@@ -409,7 +451,6 @@ describe("assistant holiday tools", () => {
|
||||
}>;
|
||||
};
|
||||
|
||||
expect(getDashboardBudgetForecast).toHaveBeenCalled();
|
||||
expect(parsed.forecasts).toEqual([
|
||||
expect.objectContaining({
|
||||
projectName: "Gelddruckmaschine",
|
||||
@@ -425,21 +466,23 @@ describe("assistant holiday tools", () => {
|
||||
});
|
||||
|
||||
it("checks resource availability with regional holidays excluded from capacity", async () => {
|
||||
const resourceRecord = {
|
||||
id: "res_1",
|
||||
displayName: "Bruce Banner",
|
||||
eid: "bruce.banner",
|
||||
fte: 1,
|
||||
availability: { monday: 8, tuesday: 8, wednesday: 8, thursday: 8, friday: 8, saturday: 0, sunday: 0 },
|
||||
countryId: "country_de",
|
||||
federalState: "BY",
|
||||
metroCityId: null,
|
||||
country: { code: "DE", dailyWorkingHours: 8 },
|
||||
metroCity: null,
|
||||
};
|
||||
const db = {
|
||||
resource: {
|
||||
findUnique: vi
|
||||
.fn()
|
||||
.mockResolvedValueOnce({
|
||||
id: "res_1",
|
||||
displayName: "Bruce Banner",
|
||||
fte: 1,
|
||||
availability: { monday: 8, tuesday: 8, wednesday: 8, thursday: 8, friday: 8 },
|
||||
countryId: "country_de",
|
||||
federalState: "BY",
|
||||
metroCityId: null,
|
||||
country: { code: "DE" },
|
||||
metroCity: null,
|
||||
}),
|
||||
.mockResolvedValue(resourceRecord),
|
||||
findFirst: vi.fn(),
|
||||
},
|
||||
assignment: {
|
||||
@@ -581,13 +624,17 @@ describe("assistant holiday tools", () => {
|
||||
it("prefers resources without a local holiday in staffing suggestions", async () => {
|
||||
const db = {
|
||||
project: {
|
||||
findFirst: vi.fn().mockResolvedValue({
|
||||
findUnique: vi.fn().mockResolvedValue({
|
||||
id: "project_1",
|
||||
name: "Holiday Project",
|
||||
shortCode: "HP",
|
||||
startDate: new Date("2026-01-06T00:00:00.000Z"),
|
||||
endDate: new Date("2026-01-06T00:00:00.000Z"),
|
||||
}),
|
||||
findFirst: vi.fn().mockResolvedValue(null),
|
||||
},
|
||||
assignment: {
|
||||
findMany: vi.fn().mockResolvedValue([]),
|
||||
},
|
||||
resource: {
|
||||
findMany: vi.fn().mockResolvedValue([
|
||||
@@ -597,15 +644,17 @@ describe("assistant holiday tools", () => {
|
||||
eid: "BY-1",
|
||||
fte: 1,
|
||||
lcrCents: 10000,
|
||||
chargeabilityTarget: 80,
|
||||
valueScore: 10,
|
||||
skills: [],
|
||||
availability: { monday: 8, tuesday: 8, wednesday: 8, thursday: 8, friday: 8 },
|
||||
countryId: "country_de",
|
||||
federalState: "BY",
|
||||
metroCityId: null,
|
||||
country: { code: "DE" },
|
||||
country: { code: "DE", name: "Deutschland" },
|
||||
metroCity: null,
|
||||
areaRole: { name: "Consultant" },
|
||||
chapter: "CGI",
|
||||
assignments: [],
|
||||
},
|
||||
{
|
||||
id: "res_hh",
|
||||
@@ -613,21 +662,20 @@ describe("assistant holiday tools", () => {
|
||||
eid: "HH-1",
|
||||
fte: 1,
|
||||
lcrCents: 10000,
|
||||
chargeabilityTarget: 80,
|
||||
valueScore: 10,
|
||||
skills: [],
|
||||
availability: { monday: 8, tuesday: 8, wednesday: 8, thursday: 8, friday: 8 },
|
||||
countryId: "country_de",
|
||||
federalState: "HH",
|
||||
metroCityId: null,
|
||||
country: { code: "DE" },
|
||||
country: { code: "DE", name: "Deutschland" },
|
||||
metroCity: null,
|
||||
areaRole: { name: "Consultant" },
|
||||
chapter: "CGI",
|
||||
assignments: [],
|
||||
},
|
||||
]),
|
||||
},
|
||||
vacation: {
|
||||
findMany: vi.fn().mockResolvedValue([]),
|
||||
},
|
||||
};
|
||||
const ctx = createToolContext(db);
|
||||
|
||||
@@ -645,6 +693,16 @@ describe("assistant holiday tools", () => {
|
||||
expect(parsed.suggestions[0]).toEqual(
|
||||
expect.objectContaining({ name: "Hamburg", availableHours: 8 }),
|
||||
);
|
||||
expect(db.project.findUnique).toHaveBeenCalledWith({
|
||||
where: { id: "project_1" },
|
||||
select: expect.objectContaining({
|
||||
id: true,
|
||||
shortCode: true,
|
||||
name: true,
|
||||
startDate: true,
|
||||
endDate: true,
|
||||
}),
|
||||
});
|
||||
});
|
||||
|
||||
it("finds capacity with local holidays respected", async () => {
|
||||
@@ -714,6 +772,12 @@ describe("assistant holiday tools", () => {
|
||||
id: "project_1",
|
||||
name: "Holiday Project",
|
||||
shortCode: "HP",
|
||||
status: "ACTIVE",
|
||||
responsiblePerson: null,
|
||||
})
|
||||
.mockResolvedValueOnce({
|
||||
id: "project_1",
|
||||
name: "Holiday Project",
|
||||
shoringThreshold: 55,
|
||||
onshoreCountryCode: "DE",
|
||||
}),
|
||||
@@ -726,6 +790,7 @@ describe("assistant holiday tools", () => {
|
||||
startDate: new Date("2026-01-06T00:00:00.000Z"),
|
||||
endDate: new Date("2026-01-06T00:00:00.000Z"),
|
||||
resource: {
|
||||
id: "res_by",
|
||||
availability: { monday: 8, tuesday: 8, wednesday: 8, thursday: 8, friday: 8 },
|
||||
countryId: "country_de",
|
||||
federalState: "BY",
|
||||
@@ -740,6 +805,7 @@ describe("assistant holiday tools", () => {
|
||||
startDate: new Date("2026-01-06T00:00:00.000Z"),
|
||||
endDate: new Date("2026-01-06T00:00:00.000Z"),
|
||||
resource: {
|
||||
id: "res_in",
|
||||
availability: { monday: 8, tuesday: 8, wednesday: 8, thursday: 8, friday: 8 },
|
||||
countryId: "country_in",
|
||||
federalState: null,
|
||||
@@ -765,4 +831,121 @@ describe("assistant holiday tools", () => {
|
||||
expect(result.content).toContain("0% onshore (DE), 100% offshore");
|
||||
expect(result.content).toContain("IN 100% (1 people)");
|
||||
});
|
||||
|
||||
it("routes pending vacation approvals through the vacation router path", async () => {
|
||||
const db = {
|
||||
vacation: {
|
||||
findMany: vi.fn().mockResolvedValue([
|
||||
{
|
||||
id: "vac_1",
|
||||
type: "ANNUAL",
|
||||
startDate: new Date("2026-07-01T00:00:00.000Z"),
|
||||
endDate: new Date("2026-07-03T00:00:00.000Z"),
|
||||
isHalfDay: false,
|
||||
resource: { displayName: "Bruce Banner", eid: "BB-1", chapter: "CGI" },
|
||||
requestedBy: { id: "user_2", name: "Manager", email: "manager@example.com" },
|
||||
},
|
||||
]),
|
||||
},
|
||||
};
|
||||
const ctx = createToolContext(db, [], SystemRole.MANAGER);
|
||||
|
||||
const result = await executeTool(
|
||||
"get_pending_vacation_approvals",
|
||||
JSON.stringify({ limit: 10 }),
|
||||
ctx,
|
||||
);
|
||||
|
||||
expect(db.vacation.findMany).toHaveBeenCalledWith({
|
||||
where: { status: "PENDING" },
|
||||
include: {
|
||||
resource: { select: expect.any(Object) },
|
||||
requestedBy: { select: { id: true, name: true, email: true } },
|
||||
},
|
||||
orderBy: { startDate: "asc" },
|
||||
});
|
||||
expect(JSON.parse(result.content)).toEqual([
|
||||
expect.objectContaining({
|
||||
id: "vac_1",
|
||||
resource: "Bruce Banner",
|
||||
eid: "BB-1",
|
||||
chapter: "CGI",
|
||||
}),
|
||||
]);
|
||||
});
|
||||
|
||||
it("routes team vacation overlap through the vacation router path", async () => {
|
||||
const db = {
|
||||
resource: {
|
||||
findUnique: vi
|
||||
.fn()
|
||||
.mockResolvedValue({
|
||||
id: "res_1",
|
||||
displayName: "Bruce Banner",
|
||||
eid: "BB-1",
|
||||
chapter: "CGI",
|
||||
lcrCents: 0,
|
||||
isActive: true,
|
||||
countryId: null,
|
||||
federalState: null,
|
||||
metroCityId: null,
|
||||
areaRole: null,
|
||||
country: null,
|
||||
metroCity: null,
|
||||
}),
|
||||
findFirst: vi.fn(),
|
||||
findMany: vi.fn(),
|
||||
},
|
||||
vacation: {
|
||||
findMany: vi.fn().mockResolvedValue([
|
||||
{
|
||||
type: "ANNUAL",
|
||||
status: "APPROVED",
|
||||
startDate: new Date("2026-08-10T00:00:00.000Z"),
|
||||
endDate: new Date("2026-08-12T00:00:00.000Z"),
|
||||
resource: { displayName: "Clark Kent" },
|
||||
},
|
||||
]),
|
||||
},
|
||||
};
|
||||
const ctx = createToolContext(db);
|
||||
|
||||
const result = await executeTool(
|
||||
"get_team_vacation_overlap",
|
||||
JSON.stringify({
|
||||
resourceId: "res_1",
|
||||
startDate: "2026-08-10",
|
||||
endDate: "2026-08-12",
|
||||
}),
|
||||
ctx,
|
||||
);
|
||||
|
||||
expect(db.vacation.findMany).toHaveBeenCalledWith({
|
||||
where: {
|
||||
resource: { chapter: "CGI" },
|
||||
resourceId: { not: "res_1" },
|
||||
status: { in: ["APPROVED", "PENDING"] },
|
||||
startDate: { lte: new Date("2026-08-12T00:00:00.000Z") },
|
||||
endDate: { gte: new Date("2026-08-10T00:00:00.000Z") },
|
||||
},
|
||||
include: {
|
||||
resource: { select: expect.any(Object) },
|
||||
},
|
||||
orderBy: { startDate: "asc" },
|
||||
take: 20,
|
||||
});
|
||||
expect(JSON.parse(result.content)).toEqual(
|
||||
expect.objectContaining({
|
||||
resource: "Bruce Banner",
|
||||
chapter: "CGI",
|
||||
overlapCount: 1,
|
||||
overlappingVacations: [
|
||||
expect.objectContaining({
|
||||
resource: "Clark Kent",
|
||||
status: "APPROVED",
|
||||
}),
|
||||
],
|
||||
}),
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user