feat(platform): harden access scoping and delivery baseline

This commit is contained in:
2026-03-30 00:27:31 +02:00
parent 00b936fa1f
commit 819345acfa
109 changed files with 26142 additions and 8081 deletions
@@ -1,3 +1,4 @@
import { listAssignmentBookings } from "@capakraken/application";
import { SystemRole } from "@capakraken/shared";
import { describe, expect, it, vi } from "vitest";
import { staffingRouter } from "../router/staffing.js";
@@ -245,6 +246,303 @@ describe("staffing.getSuggestions", () => {
});
});
describe("staffing.getProjectStaffingSuggestions", () => {
it("returns canonical project-scoped staffing suggestions with defaults and role filter", async () => {
vi.mocked(listAssignmentBookings).mockResolvedValueOnce([]);
const db = {
project: {
findUnique: vi.fn().mockResolvedValue({
id: "project_1",
shortCode: "GDM",
name: "Gelddruckmaschine",
startDate: new Date("2026-01-06T00:00:00.000Z"),
endDate: new Date("2026-01-06T00:00:00.000Z"),
}),
},
resource: {
findMany: vi.fn().mockResolvedValue([
sampleResource({
id: "res_by",
displayName: "Bavaria",
eid: "BY-1",
areaRole: { name: "Consultant" },
country: { code: "DE", name: "Germany" },
}),
sampleResource({
id: "res_hh",
displayName: "Hamburg",
eid: "HH-1",
federalState: "HH",
areaRole: { name: "Artist" },
country: { code: "DE", name: "Germany" },
}),
]),
},
};
const caller = createProtectedCaller(db);
const result = await caller.getProjectStaffingSuggestions({
projectId: "project_1",
roleName: "artist",
limit: 5,
});
expect(result).toEqual({
project: "Gelddruckmaschine (GDM)",
period: "2026-01-06 to 2026-01-06",
suggestions: [
{
id: "res_hh",
name: "Hamburg",
eid: "HH-1",
role: "Artist",
chapter: "VFX",
fte: expect.any(Number),
lcr: "75,00 EUR",
workingDays: expect.any(Number),
availableHours: expect.any(Number),
bookedHours: 0,
availableHoursPerDay: expect.any(Number),
utilization: 0,
},
],
});
expect(db.project.findUnique).toHaveBeenCalledWith({
where: { id: "project_1" },
select: {
id: true,
shortCode: true,
name: true,
startDate: true,
endDate: true,
},
});
});
});
describe("staffing.getBestProjectResourceDetail", () => {
it("returns canonical project resource ranking with holiday-aware capacity details", async () => {
const assignmentFindMany = vi
.fn()
.mockResolvedValueOnce([
{
resourceId: "res_carol",
hoursPerDay: 2,
startDate: new Date("2026-01-05T00:00:00.000Z"),
endDate: new Date("2026-01-16T00:00:00.000Z"),
status: "PROPOSED",
resource: {
id: "res_carol",
eid: "carol.danvers",
displayName: "Carol Danvers",
chapter: "Delivery",
lcrCents: 7664,
availability: { monday: 8, tuesday: 8, wednesday: 8, thursday: 8, friday: 8 },
countryId: "country_de",
federalState: "HH",
metroCityId: "city_hamburg",
country: { code: "DE", name: "Deutschland" },
metroCity: { name: "Hamburg" },
areaRole: { name: "Artist" },
},
},
{
resourceId: "res_steve",
hoursPerDay: 4,
startDate: new Date("2026-01-05T00:00:00.000Z"),
endDate: new Date("2026-01-16T00:00:00.000Z"),
status: "CONFIRMED",
resource: {
id: "res_steve",
eid: "steve.rogers",
displayName: "Steve Rogers",
chapter: "Delivery",
lcrCents: 13377,
availability: { monday: 8, tuesday: 8, wednesday: 8, thursday: 8, friday: 8 },
countryId: "country_de",
federalState: "BY",
metroCityId: "city_augsburg",
country: { code: "DE", name: "Deutschland" },
metroCity: { name: "Augsburg" },
areaRole: { name: "Artist" },
},
},
])
.mockResolvedValueOnce([
{
resourceId: "res_carol",
projectId: "project_lari",
hoursPerDay: 2,
startDate: new Date("2026-01-05T00:00:00.000Z"),
endDate: new Date("2026-01-16T00:00:00.000Z"),
status: "PROPOSED",
project: { name: "Gelddruckmaschine", shortCode: "LARI" },
},
{
resourceId: "res_steve",
projectId: "project_lari",
hoursPerDay: 4,
startDate: new Date("2026-01-05T00:00:00.000Z"),
endDate: new Date("2026-01-16T00:00:00.000Z"),
status: "CONFIRMED",
project: { name: "Gelddruckmaschine", shortCode: "LARI" },
},
]);
const db = {
project: {
findUnique: vi.fn().mockResolvedValue({
id: "project_lari",
name: "Gelddruckmaschine",
shortCode: "LARI",
status: "ACTIVE",
responsiblePerson: "Larissa Joos",
}),
},
assignment: {
findMany: assignmentFindMany,
},
vacation: {
findMany: vi.fn().mockResolvedValue([]),
},
};
const caller = createProtectedCaller(db);
const result = await caller.getBestProjectResourceDetail({
projectId: "project_lari",
startDate: new Date("2026-01-05T00:00:00.000Z"),
endDate: new Date("2026-01-16T00:00:00.000Z"),
minHoursPerDay: 3,
rankingMode: "lowest_lcr",
});
expect(result.project).toEqual({
id: "project_lari",
name: "Gelddruckmaschine",
shortCode: "LARI",
status: "ACTIVE",
responsiblePerson: "Larissa Joos",
});
expect(result.period).toEqual({
startDate: "2026-01-05",
endDate: "2026-01-16",
minHoursPerDay: 3,
rankingMode: "lowest_lcr",
});
expect(result.filters).toEqual({
chapter: null,
roleName: null,
});
expect(result.candidateCount).toBe(2);
expect(result.bestMatch).toEqual(
expect.objectContaining({
name: "Carol Danvers",
remainingHoursPerDay: 6,
lcrCents: 7664,
federalState: "HH",
metroCity: "Hamburg",
baseAvailableHours: 80,
holidaySummary: expect.objectContaining({ count: 0 }),
}),
);
expect(result.candidates).toEqual([
expect.objectContaining({
name: "Carol Danvers",
remainingHoursPerDay: 6,
workingDays: 10,
baseAvailableHours: 80,
holidaySummary: expect.objectContaining({ count: 0, hoursDeduction: 0 }),
capacityBreakdown: expect.objectContaining({ holidayHoursDeduction: 0 }),
}),
expect.objectContaining({
name: "Steve Rogers",
remainingHoursPerDay: 4,
workingDays: 9,
baseAvailableHours: 80,
holidaySummary: expect.objectContaining({ count: 1, hoursDeduction: 8 }),
capacityBreakdown: expect.objectContaining({ holidayHoursDeduction: 8 }),
}),
]);
});
});
describe("staffing.searchCapacity", () => {
it("returns holiday-aware capacity across multiple resources", async () => {
vi.mocked(listAssignmentBookings).mockResolvedValueOnce([]);
const db = {
resource: {
findMany: vi.fn().mockResolvedValue([
sampleResource({
id: "res_by",
displayName: "Bavaria",
eid: "BY-1",
chapter: "CGI",
areaRole: { name: "Consultant" },
federalState: "BY",
}),
sampleResource({
id: "res_hh",
displayName: "Hamburg",
eid: "HH-1",
chapter: "CGI",
areaRole: { name: "Consultant" },
federalState: "HH",
}),
]),
},
};
const caller = createProtectedCaller(db);
const result = await caller.searchCapacity({
startDate: new Date("2026-01-06"),
endDate: new Date("2026-01-06"),
minHoursPerDay: 1,
});
expect(result.results).toHaveLength(1);
expect(result.results[0]).toEqual(
expect.objectContaining({
name: "Hamburg",
availableHours: 8,
availableHoursPerDay: 8,
}),
);
});
it("applies role and chapter filters in the resource query", async () => {
vi.mocked(listAssignmentBookings).mockResolvedValueOnce([]);
const db = {
resource: {
findMany: vi.fn().mockResolvedValue([]),
},
};
const caller = createProtectedCaller(db);
await caller.searchCapacity({
startDate: new Date("2026-04-01"),
endDate: new Date("2026-04-02"),
minHoursPerDay: 4,
roleName: "Consult",
chapter: "CG",
limit: 5,
});
expect(db.resource.findMany).toHaveBeenCalledWith(
expect.objectContaining({
where: expect.objectContaining({
isActive: true,
areaRole: { name: { contains: "Consult", mode: "insensitive" } },
chapter: { contains: "CG", mode: "insensitive" },
}),
take: 100,
}),
);
});
});
// ─── analyzeUtilization ──────────────────────────────────────────────────────
describe("staffing.analyzeUtilization", () => {