chore(repo): initialize planarchy workspace
This commit is contained in:
@@ -0,0 +1,725 @@
|
||||
import { AllocationStatus, SystemRole } from "@planarchy/shared";
|
||||
import { describe, expect, it, vi } from "vitest";
|
||||
import { allocationRouter } from "../router/allocation.js";
|
||||
import { emitAllocationCreated, emitAllocationDeleted } from "../sse/event-bus.js";
|
||||
import { createCallerFactory } from "../trpc.js";
|
||||
|
||||
vi.mock("../sse/event-bus.js", () => ({
|
||||
emitAllocationCreated: vi.fn(),
|
||||
emitAllocationDeleted: vi.fn(),
|
||||
emitAllocationUpdated: vi.fn(),
|
||||
}));
|
||||
|
||||
const createCaller = createCallerFactory(allocationRouter);
|
||||
|
||||
function createManagerCaller(db: Record<string, unknown>) {
|
||||
return createCaller({
|
||||
session: {
|
||||
user: { email: "manager@example.com", name: "Manager", image: null },
|
||||
expires: "2026-03-13T00:00:00.000Z",
|
||||
},
|
||||
db: db as never,
|
||||
dbUser: {
|
||||
id: "user_1",
|
||||
systemRole: SystemRole.MANAGER,
|
||||
permissionOverrides: null,
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
describe("allocation entry resolution router", () => {
|
||||
it("creates an open demand through allocation.create without requiring isPlaceholder", async () => {
|
||||
const createdDemandRequirement = {
|
||||
id: "demand_1",
|
||||
projectId: "project_1",
|
||||
startDate: new Date("2026-03-16"),
|
||||
endDate: new Date("2026-03-20"),
|
||||
hoursPerDay: 4,
|
||||
percentage: 50,
|
||||
role: "FX Artist",
|
||||
roleId: "role_fx",
|
||||
headcount: 2,
|
||||
status: AllocationStatus.PROPOSED,
|
||||
metadata: {},
|
||||
createdAt: new Date("2026-03-13"),
|
||||
updatedAt: new Date("2026-03-13"),
|
||||
project: { id: "project_1", name: "Project One", shortCode: "PRJ" },
|
||||
roleEntity: { id: "role_fx", name: "FX Artist", color: "#222222" },
|
||||
};
|
||||
|
||||
const db = {
|
||||
project: {
|
||||
findUnique: vi.fn().mockResolvedValue({ id: "project_1" }),
|
||||
},
|
||||
demandRequirement: {
|
||||
create: vi.fn().mockResolvedValue(createdDemandRequirement),
|
||||
},
|
||||
auditLog: {
|
||||
create: vi.fn().mockResolvedValue({}),
|
||||
},
|
||||
$transaction: vi.fn(async (callback: (tx: unknown) => unknown) => callback(db)),
|
||||
};
|
||||
|
||||
const caller = createManagerCaller(db);
|
||||
const result = await caller.create({
|
||||
projectId: "project_1",
|
||||
startDate: new Date("2026-03-16"),
|
||||
endDate: new Date("2026-03-20"),
|
||||
hoursPerDay: 4,
|
||||
percentage: 50,
|
||||
role: "FX Artist",
|
||||
roleId: "role_fx",
|
||||
headcount: 2,
|
||||
status: AllocationStatus.PROPOSED,
|
||||
metadata: {},
|
||||
});
|
||||
|
||||
expect(result.id).toBe("demand_1");
|
||||
expect(result.isPlaceholder).toBe(true);
|
||||
expect(db.demandRequirement.create).toHaveBeenCalledWith(
|
||||
expect.objectContaining({
|
||||
data: expect.objectContaining({
|
||||
headcount: 2,
|
||||
}),
|
||||
}),
|
||||
);
|
||||
});
|
||||
|
||||
it("creates an assignment through allocation.create without requiring isPlaceholder", async () => {
|
||||
const createdAssignment = {
|
||||
id: "assignment_1",
|
||||
demandRequirementId: null,
|
||||
resourceId: "resource_1",
|
||||
projectId: "project_1",
|
||||
startDate: new Date("2026-03-16"),
|
||||
endDate: new Date("2026-03-20"),
|
||||
hoursPerDay: 8,
|
||||
percentage: 100,
|
||||
role: "Compositor",
|
||||
roleId: "role_comp",
|
||||
dailyCostCents: 40000,
|
||||
status: AllocationStatus.ACTIVE,
|
||||
metadata: {},
|
||||
createdAt: new Date("2026-03-13"),
|
||||
updatedAt: new Date("2026-03-13"),
|
||||
resource: {
|
||||
id: "resource_1",
|
||||
displayName: "Alice",
|
||||
eid: "E-001",
|
||||
lcrCents: 5000,
|
||||
},
|
||||
project: { id: "project_1", name: "Project One", shortCode: "PRJ" },
|
||||
roleEntity: { id: "role_comp", name: "Compositor", color: "#111111" },
|
||||
demandRequirement: null,
|
||||
};
|
||||
|
||||
const db = {
|
||||
project: {
|
||||
findUnique: vi.fn().mockResolvedValue({ id: "project_1" }),
|
||||
},
|
||||
resource: {
|
||||
findUnique: vi.fn().mockResolvedValue({
|
||||
id: "resource_1",
|
||||
displayName: "Alice",
|
||||
eid: "E-001",
|
||||
lcrCents: 5000,
|
||||
availability: {
|
||||
monday: 8,
|
||||
tuesday: 8,
|
||||
wednesday: 8,
|
||||
thursday: 8,
|
||||
friday: 8,
|
||||
saturday: 0,
|
||||
sunday: 0,
|
||||
},
|
||||
}),
|
||||
},
|
||||
allocation: {
|
||||
findMany: vi.fn().mockResolvedValue([]),
|
||||
create: vi.fn(),
|
||||
},
|
||||
assignment: {
|
||||
findMany: vi.fn().mockResolvedValue([]),
|
||||
create: vi.fn().mockResolvedValue(createdAssignment),
|
||||
},
|
||||
vacation: {
|
||||
findMany: vi.fn().mockResolvedValue([]),
|
||||
},
|
||||
auditLog: {
|
||||
create: vi.fn().mockResolvedValue({}),
|
||||
},
|
||||
$transaction: vi.fn(async (callback: (tx: unknown) => unknown) => callback(db)),
|
||||
};
|
||||
|
||||
const caller = createManagerCaller(db);
|
||||
const result = await caller.create({
|
||||
resourceId: "resource_1",
|
||||
projectId: "project_1",
|
||||
startDate: new Date("2026-03-16"),
|
||||
endDate: new Date("2026-03-20"),
|
||||
hoursPerDay: 8,
|
||||
percentage: 100,
|
||||
role: "Compositor",
|
||||
roleId: "role_comp",
|
||||
status: AllocationStatus.ACTIVE,
|
||||
metadata: {},
|
||||
});
|
||||
|
||||
expect(result.id).toBe("assignment_1");
|
||||
expect(result.isPlaceholder).toBe(false);
|
||||
expect(db.allocation.create).not.toHaveBeenCalled();
|
||||
expect(db.assignment.create).toHaveBeenCalledWith(
|
||||
expect.objectContaining({
|
||||
data: expect.objectContaining({
|
||||
resourceId: "resource_1",
|
||||
}),
|
||||
}),
|
||||
);
|
||||
});
|
||||
|
||||
it("creates an explicit demand requirement without dual-writing a legacy allocation row", async () => {
|
||||
vi.mocked(emitAllocationCreated).mockClear();
|
||||
|
||||
const createdDemandRequirement = {
|
||||
id: "demand_explicit_1",
|
||||
projectId: "project_1",
|
||||
startDate: new Date("2026-03-16"),
|
||||
endDate: new Date("2026-03-20"),
|
||||
hoursPerDay: 4,
|
||||
percentage: 50,
|
||||
role: "FX Artist",
|
||||
roleId: "role_fx",
|
||||
headcount: 2,
|
||||
status: AllocationStatus.PROPOSED,
|
||||
metadata: {},
|
||||
createdAt: new Date("2026-03-13"),
|
||||
updatedAt: new Date("2026-03-13"),
|
||||
project: { id: "project_1", name: "Project One", shortCode: "PRJ" },
|
||||
roleEntity: { id: "role_fx", name: "FX Artist", color: "#222222" },
|
||||
};
|
||||
|
||||
const db = {
|
||||
project: {
|
||||
findUnique: vi.fn().mockResolvedValue({ id: "project_1" }),
|
||||
},
|
||||
demandRequirement: {
|
||||
create: vi.fn().mockResolvedValue(createdDemandRequirement),
|
||||
},
|
||||
auditLog: {
|
||||
create: vi.fn().mockResolvedValue({}),
|
||||
},
|
||||
$transaction: vi.fn(async (callback: (tx: unknown) => unknown) => callback(db)),
|
||||
};
|
||||
|
||||
const caller = createManagerCaller(db);
|
||||
const result = await caller.createDemandRequirement({
|
||||
projectId: "project_1",
|
||||
startDate: new Date("2026-03-16"),
|
||||
endDate: new Date("2026-03-20"),
|
||||
hoursPerDay: 4,
|
||||
percentage: 50,
|
||||
role: "FX Artist",
|
||||
roleId: "role_fx",
|
||||
headcount: 2,
|
||||
status: AllocationStatus.PROPOSED,
|
||||
metadata: {},
|
||||
});
|
||||
|
||||
expect(result.id).toBe("demand_explicit_1");
|
||||
expect((db as { allocation?: { create?: unknown } }).allocation?.create).toBeUndefined();
|
||||
expect(db.demandRequirement.create).toHaveBeenCalledWith(
|
||||
expect.objectContaining({
|
||||
data: expect.objectContaining({
|
||||
headcount: 2,
|
||||
}),
|
||||
}),
|
||||
);
|
||||
expect(emitAllocationCreated).toHaveBeenCalledWith({
|
||||
id: "demand_explicit_1",
|
||||
projectId: "project_1",
|
||||
resourceId: null,
|
||||
});
|
||||
});
|
||||
|
||||
it("creates an explicit assignment without dual-writing a legacy allocation row", async () => {
|
||||
vi.mocked(emitAllocationCreated).mockClear();
|
||||
|
||||
const createdAssignment = {
|
||||
id: "assignment_explicit_1",
|
||||
demandRequirementId: null,
|
||||
resourceId: "resource_1",
|
||||
projectId: "project_1",
|
||||
startDate: new Date("2026-03-16"),
|
||||
endDate: new Date("2026-03-20"),
|
||||
hoursPerDay: 8,
|
||||
percentage: 100,
|
||||
role: "Compositor",
|
||||
roleId: "role_comp",
|
||||
dailyCostCents: 40000,
|
||||
status: AllocationStatus.ACTIVE,
|
||||
metadata: {},
|
||||
createdAt: new Date("2026-03-13"),
|
||||
updatedAt: new Date("2026-03-13"),
|
||||
resource: {
|
||||
id: "resource_1",
|
||||
displayName: "Alice",
|
||||
eid: "E-001",
|
||||
lcrCents: 5000,
|
||||
},
|
||||
project: { id: "project_1", name: "Project One", shortCode: "PRJ" },
|
||||
roleEntity: { id: "role_comp", name: "Compositor", color: "#111111" },
|
||||
demandRequirement: null,
|
||||
};
|
||||
|
||||
const db = {
|
||||
project: {
|
||||
findUnique: vi.fn().mockResolvedValue({ id: "project_1" }),
|
||||
},
|
||||
resource: {
|
||||
findUnique: vi.fn().mockResolvedValue({
|
||||
id: "resource_1",
|
||||
lcrCents: 5000,
|
||||
availability: {
|
||||
monday: 8,
|
||||
tuesday: 8,
|
||||
wednesday: 8,
|
||||
thursday: 8,
|
||||
friday: 8,
|
||||
saturday: 0,
|
||||
sunday: 0,
|
||||
},
|
||||
}),
|
||||
},
|
||||
allocation: {
|
||||
findMany: vi.fn().mockResolvedValue([]),
|
||||
create: vi.fn(),
|
||||
},
|
||||
assignment: {
|
||||
findMany: vi.fn().mockResolvedValue([]),
|
||||
create: vi.fn().mockResolvedValue(createdAssignment),
|
||||
},
|
||||
vacation: {
|
||||
findMany: vi.fn().mockResolvedValue([]),
|
||||
},
|
||||
auditLog: {
|
||||
create: vi.fn().mockResolvedValue({}),
|
||||
},
|
||||
$transaction: vi.fn(async (callback: (tx: unknown) => unknown) => callback(db)),
|
||||
};
|
||||
|
||||
const caller = createManagerCaller(db);
|
||||
const result = await caller.createAssignment({
|
||||
resourceId: "resource_1",
|
||||
projectId: "project_1",
|
||||
startDate: new Date("2026-03-16"),
|
||||
endDate: new Date("2026-03-20"),
|
||||
hoursPerDay: 8,
|
||||
percentage: 100,
|
||||
role: "Compositor",
|
||||
roleId: "role_comp",
|
||||
status: AllocationStatus.ACTIVE,
|
||||
metadata: {},
|
||||
});
|
||||
|
||||
expect(result.id).toBe("assignment_explicit_1");
|
||||
expect(db.allocation.create).not.toHaveBeenCalled();
|
||||
expect(db.assignment.create).toHaveBeenCalledWith(
|
||||
expect.objectContaining({
|
||||
data: expect.objectContaining({
|
||||
resourceId: "resource_1",
|
||||
}),
|
||||
}),
|
||||
);
|
||||
expect(emitAllocationCreated).toHaveBeenCalledWith({
|
||||
id: "assignment_explicit_1",
|
||||
projectId: "project_1",
|
||||
resourceId: "resource_1",
|
||||
});
|
||||
});
|
||||
|
||||
it("deletes an explicit demand requirement without routing through allocation.delete", async () => {
|
||||
vi.mocked(emitAllocationDeleted).mockClear();
|
||||
|
||||
const existingDemandRequirement = {
|
||||
id: "demand_explicit_1",
|
||||
projectId: "project_1",
|
||||
startDate: new Date("2026-03-16"),
|
||||
endDate: new Date("2026-03-20"),
|
||||
hoursPerDay: 4,
|
||||
percentage: 50,
|
||||
role: "FX Artist",
|
||||
roleId: "role_fx",
|
||||
headcount: 2,
|
||||
status: AllocationStatus.PROPOSED,
|
||||
metadata: {},
|
||||
createdAt: new Date("2026-03-13"),
|
||||
updatedAt: new Date("2026-03-13"),
|
||||
project: {
|
||||
id: "project_1",
|
||||
name: "Project One",
|
||||
shortCode: "PRJ",
|
||||
status: "ACTIVE",
|
||||
endDate: new Date("2026-03-20"),
|
||||
},
|
||||
roleEntity: { id: "role_fx", name: "FX Artist", color: "#222222" },
|
||||
assignments: [],
|
||||
};
|
||||
|
||||
const db = {
|
||||
demandRequirement: {
|
||||
findUnique: vi.fn().mockResolvedValue(existingDemandRequirement),
|
||||
delete: vi.fn().mockResolvedValue(existingDemandRequirement),
|
||||
},
|
||||
assignment: {
|
||||
updateMany: vi.fn().mockResolvedValue({ count: 0 }),
|
||||
},
|
||||
allocation: {
|
||||
findUnique: vi.fn().mockResolvedValue(null),
|
||||
delete: vi.fn(),
|
||||
},
|
||||
auditLog: {
|
||||
create: vi.fn().mockResolvedValue({}),
|
||||
},
|
||||
$transaction: vi.fn(async (callback: (tx: unknown) => unknown) => callback(db)),
|
||||
};
|
||||
|
||||
const caller = createManagerCaller(db);
|
||||
const result = await caller.deleteDemandRequirement({ id: "demand_explicit_1" });
|
||||
|
||||
expect(result).toEqual({ success: true });
|
||||
expect(db.assignment.updateMany).toHaveBeenCalledWith({
|
||||
where: { demandRequirementId: "demand_explicit_1" },
|
||||
data: { demandRequirementId: null },
|
||||
});
|
||||
expect(db.demandRequirement.delete).toHaveBeenCalledWith({
|
||||
where: { id: "demand_explicit_1" },
|
||||
});
|
||||
expect(db.allocation.delete).not.toHaveBeenCalled();
|
||||
expect(emitAllocationDeleted).toHaveBeenCalledWith("demand_explicit_1", "project_1");
|
||||
});
|
||||
|
||||
it("deletes an explicit assignment without routing through allocation.delete", async () => {
|
||||
vi.mocked(emitAllocationDeleted).mockClear();
|
||||
|
||||
const existingAssignment = {
|
||||
id: "assignment_explicit_1",
|
||||
demandRequirementId: null,
|
||||
resourceId: "resource_1",
|
||||
projectId: "project_1",
|
||||
startDate: new Date("2026-03-16"),
|
||||
endDate: new Date("2026-03-20"),
|
||||
hoursPerDay: 8,
|
||||
percentage: 100,
|
||||
role: "Compositor",
|
||||
roleId: "role_comp",
|
||||
dailyCostCents: 40000,
|
||||
status: AllocationStatus.ACTIVE,
|
||||
metadata: {},
|
||||
createdAt: new Date("2026-03-13"),
|
||||
updatedAt: new Date("2026-03-13"),
|
||||
resource: {
|
||||
id: "resource_1",
|
||||
displayName: "Alice",
|
||||
eid: "E-001",
|
||||
lcrCents: 5000,
|
||||
},
|
||||
project: {
|
||||
id: "project_1",
|
||||
name: "Project One",
|
||||
shortCode: "PRJ",
|
||||
status: "ACTIVE",
|
||||
endDate: new Date("2026-03-20"),
|
||||
},
|
||||
roleEntity: { id: "role_comp", name: "Compositor", color: "#111111" },
|
||||
demandRequirement: null,
|
||||
};
|
||||
|
||||
const db = {
|
||||
assignment: {
|
||||
findUnique: vi.fn().mockResolvedValue(existingAssignment),
|
||||
delete: vi.fn().mockResolvedValue(existingAssignment),
|
||||
},
|
||||
auditLog: {
|
||||
create: vi.fn().mockResolvedValue({}),
|
||||
},
|
||||
$transaction: vi.fn(async (callback: (tx: unknown) => unknown) => callback(db)),
|
||||
};
|
||||
|
||||
const caller = createManagerCaller(db);
|
||||
const result = await caller.deleteAssignment({ id: "assignment_explicit_1" });
|
||||
|
||||
expect(result).toEqual({ success: true });
|
||||
expect(db.assignment.delete).toHaveBeenCalledWith({
|
||||
where: { id: "assignment_explicit_1" },
|
||||
});
|
||||
expect(emitAllocationDeleted).toHaveBeenCalledWith("assignment_explicit_1", "project_1");
|
||||
});
|
||||
|
||||
it("updates an explicit demand row through allocation.update", async () => {
|
||||
const existingDemand = {
|
||||
id: "demand_1",
|
||||
projectId: "project_1",
|
||||
startDate: new Date("2026-03-16"),
|
||||
endDate: new Date("2026-03-20"),
|
||||
hoursPerDay: 4,
|
||||
percentage: 50,
|
||||
role: "FX Artist",
|
||||
roleId: "role_fx",
|
||||
headcount: 1,
|
||||
status: AllocationStatus.PROPOSED,
|
||||
metadata: {},
|
||||
createdAt: new Date("2026-03-13"),
|
||||
updatedAt: new Date("2026-03-13"),
|
||||
project: { id: "project_1", name: "Project One", shortCode: "PRJ" },
|
||||
roleEntity: { id: "role_fx", name: "FX Artist", color: "#222222" },
|
||||
};
|
||||
const updatedDemand = {
|
||||
...existingDemand,
|
||||
headcount: 2,
|
||||
status: AllocationStatus.CONFIRMED,
|
||||
metadata: { source: "router-test" },
|
||||
updatedAt: new Date("2026-03-14"),
|
||||
};
|
||||
|
||||
const db = {
|
||||
allocation: {
|
||||
findUnique: vi.fn().mockResolvedValue(null),
|
||||
},
|
||||
demandRequirement: {
|
||||
findUnique: vi.fn().mockResolvedValue(existingDemand),
|
||||
update: vi.fn().mockResolvedValue(updatedDemand),
|
||||
},
|
||||
assignment: {
|
||||
findUnique: vi.fn().mockResolvedValue(null),
|
||||
update: vi.fn(),
|
||||
},
|
||||
auditLog: {
|
||||
create: vi.fn().mockResolvedValue({}),
|
||||
},
|
||||
$transaction: vi.fn(async (callback: (tx: unknown) => unknown) => callback(db)),
|
||||
};
|
||||
|
||||
const caller = createManagerCaller(db);
|
||||
const result = await caller.update({
|
||||
id: "demand_1",
|
||||
data: {
|
||||
headcount: 2,
|
||||
status: AllocationStatus.CONFIRMED,
|
||||
metadata: { source: "router-test" },
|
||||
},
|
||||
});
|
||||
|
||||
expect(result.id).toBe("demand_1");
|
||||
expect(result.isPlaceholder).toBe(true);
|
||||
expect(result.headcount).toBe(2);
|
||||
expect(db.demandRequirement.update).toHaveBeenCalledWith(
|
||||
expect.objectContaining({
|
||||
where: { id: "demand_1" },
|
||||
}),
|
||||
);
|
||||
});
|
||||
|
||||
it("updates a demand row by its direct id", async () => {
|
||||
const existingDemand = {
|
||||
id: "demand_stale",
|
||||
projectId: "project_1",
|
||||
startDate: new Date("2026-03-16"),
|
||||
endDate: new Date("2026-03-20"),
|
||||
hoursPerDay: 4,
|
||||
percentage: 50,
|
||||
role: "FX Artist",
|
||||
roleId: "role_fx",
|
||||
headcount: 1,
|
||||
status: AllocationStatus.PROPOSED,
|
||||
metadata: {},
|
||||
createdAt: new Date("2026-03-13"),
|
||||
updatedAt: new Date("2026-03-13"),
|
||||
project: { id: "project_1", name: "Project One", shortCode: "PRJ" },
|
||||
roleEntity: { id: "role_fx", name: "FX Artist", color: "#222222" },
|
||||
};
|
||||
const updatedDemand = {
|
||||
...existingDemand,
|
||||
headcount: 2,
|
||||
status: AllocationStatus.CONFIRMED,
|
||||
updatedAt: new Date("2026-03-14"),
|
||||
};
|
||||
|
||||
const db = {
|
||||
demandRequirement: {
|
||||
findUnique: vi.fn().mockImplementation(
|
||||
({ where }: { where: { id?: string } }) => {
|
||||
if (where.id === "demand_stale") {
|
||||
return existingDemand;
|
||||
}
|
||||
|
||||
return null;
|
||||
},
|
||||
),
|
||||
update: vi.fn().mockResolvedValue(updatedDemand),
|
||||
},
|
||||
assignment: {
|
||||
findUnique: vi.fn().mockResolvedValue(null),
|
||||
update: vi.fn(),
|
||||
},
|
||||
auditLog: {
|
||||
create: vi.fn().mockResolvedValue({}),
|
||||
},
|
||||
$transaction: vi.fn(async (callback: (tx: unknown) => unknown) => callback(db)),
|
||||
};
|
||||
|
||||
const caller = createManagerCaller(db);
|
||||
const result = await caller.update({
|
||||
id: "demand_stale",
|
||||
data: {
|
||||
headcount: 2,
|
||||
status: AllocationStatus.CONFIRMED,
|
||||
},
|
||||
});
|
||||
|
||||
expect(result.id).toBe("demand_stale");
|
||||
expect(result.isPlaceholder).toBe(true);
|
||||
expect(db.demandRequirement.update).toHaveBeenCalledWith(
|
||||
expect.objectContaining({
|
||||
where: { id: "demand_stale" },
|
||||
}),
|
||||
);
|
||||
});
|
||||
|
||||
it("batch deletes explicit demand and assignment rows through allocation.batchDelete", async () => {
|
||||
const explicitDemand = {
|
||||
id: "demand_1",
|
||||
projectId: "project_1",
|
||||
startDate: new Date("2026-03-16"),
|
||||
endDate: new Date("2026-03-20"),
|
||||
hoursPerDay: 4,
|
||||
percentage: 50,
|
||||
role: "FX Artist",
|
||||
roleId: "role_fx",
|
||||
headcount: 1,
|
||||
status: AllocationStatus.PROPOSED,
|
||||
metadata: {},
|
||||
createdAt: new Date("2026-03-13"),
|
||||
updatedAt: new Date("2026-03-13"),
|
||||
project: { id: "project_1", name: "Project One", shortCode: "PRJ" },
|
||||
roleEntity: { id: "role_fx", name: "FX Artist", color: "#222222" },
|
||||
};
|
||||
const explicitAssignment = {
|
||||
id: "assignment_1",
|
||||
demandRequirementId: null,
|
||||
resourceId: "resource_1",
|
||||
projectId: "project_1",
|
||||
startDate: new Date("2026-03-16"),
|
||||
endDate: new Date("2026-03-20"),
|
||||
hoursPerDay: 8,
|
||||
percentage: 100,
|
||||
role: "Compositor",
|
||||
roleId: "role_comp",
|
||||
dailyCostCents: 32000,
|
||||
status: AllocationStatus.ACTIVE,
|
||||
metadata: {},
|
||||
createdAt: new Date("2026-03-13"),
|
||||
updatedAt: new Date("2026-03-13"),
|
||||
resource: { id: "resource_1", displayName: "Alice", eid: "E-001", lcrCents: 5000 },
|
||||
project: { id: "project_1", name: "Project One", shortCode: "PRJ" },
|
||||
roleEntity: { id: "role_comp", name: "Compositor", color: "#111111" },
|
||||
demandRequirement: null,
|
||||
};
|
||||
|
||||
const db = {
|
||||
allocation: {
|
||||
findUnique: vi.fn().mockResolvedValue(null),
|
||||
},
|
||||
demandRequirement: {
|
||||
findUnique: vi.fn().mockImplementation(({ where }: { where: { id: string } }) =>
|
||||
where.id === "demand_1" ? explicitDemand : null,
|
||||
),
|
||||
delete: vi.fn().mockResolvedValue({}),
|
||||
},
|
||||
assignment: {
|
||||
findUnique: vi.fn().mockImplementation(({ where }: { where: { id: string } }) =>
|
||||
where.id === "assignment_1" ? explicitAssignment : null,
|
||||
),
|
||||
updateMany: vi.fn().mockResolvedValue({ count: 0 }),
|
||||
delete: vi.fn().mockResolvedValue({}),
|
||||
},
|
||||
auditLog: {
|
||||
create: vi.fn().mockResolvedValue({}),
|
||||
},
|
||||
$transaction: vi.fn(async (callback: (tx: unknown) => unknown) => callback(db)),
|
||||
};
|
||||
|
||||
const caller = createManagerCaller(db);
|
||||
const result = await caller.batchDelete({
|
||||
ids: ["demand_1", "assignment_1"],
|
||||
});
|
||||
|
||||
expect(result.count).toBe(2);
|
||||
expect(db.assignment.updateMany).toHaveBeenCalledWith({
|
||||
where: { demandRequirementId: "demand_1" },
|
||||
data: { demandRequirementId: null },
|
||||
});
|
||||
expect(db.demandRequirement.delete).toHaveBeenCalledWith({
|
||||
where: { id: "demand_1" },
|
||||
});
|
||||
expect(db.assignment.delete).toHaveBeenCalledWith({
|
||||
where: { id: "assignment_1" },
|
||||
});
|
||||
});
|
||||
|
||||
it("deletes an assignment through allocation.delete by its direct id", async () => {
|
||||
const existingAssignment = {
|
||||
id: "assignment_stale",
|
||||
demandRequirementId: null,
|
||||
resourceId: "resource_1",
|
||||
projectId: "project_1",
|
||||
startDate: new Date("2026-03-16"),
|
||||
endDate: new Date("2026-03-20"),
|
||||
hoursPerDay: 8,
|
||||
percentage: 100,
|
||||
role: "Compositor",
|
||||
roleId: "role_comp",
|
||||
dailyCostCents: 32000,
|
||||
status: AllocationStatus.ACTIVE,
|
||||
metadata: {},
|
||||
createdAt: new Date("2026-03-13"),
|
||||
updatedAt: new Date("2026-03-13"),
|
||||
resource: { id: "resource_1", displayName: "Alice", eid: "E-001", lcrCents: 5000 },
|
||||
project: { id: "project_1", name: "Project One", shortCode: "PRJ" },
|
||||
roleEntity: { id: "role_comp", name: "Compositor", color: "#111111" },
|
||||
demandRequirement: null,
|
||||
};
|
||||
|
||||
const db = {
|
||||
demandRequirement: {
|
||||
findUnique: vi.fn().mockResolvedValue(null),
|
||||
},
|
||||
assignment: {
|
||||
findUnique: vi.fn().mockImplementation(
|
||||
({ where }: { where: { id?: string } }) => {
|
||||
if (where.id === "assignment_stale") {
|
||||
return existingAssignment;
|
||||
}
|
||||
|
||||
return null;
|
||||
},
|
||||
),
|
||||
delete: vi.fn().mockResolvedValue({}),
|
||||
},
|
||||
auditLog: {
|
||||
create: vi.fn().mockResolvedValue({}),
|
||||
},
|
||||
$transaction: vi.fn(async (callback: (tx: unknown) => unknown) => callback(db)),
|
||||
};
|
||||
|
||||
const caller = createManagerCaller(db);
|
||||
const result = await caller.delete({
|
||||
id: "assignment_stale",
|
||||
});
|
||||
|
||||
expect(result).toEqual({ success: true });
|
||||
expect(db.assignment.delete).toHaveBeenCalledWith({
|
||||
where: { id: "assignment_stale" },
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
@@ -0,0 +1,101 @@
|
||||
import { BlueprintTarget, FieldType, type BlueprintFieldDefinition } from "@planarchy/shared";
|
||||
import { TRPCError } from "@trpc/server";
|
||||
import { describe, expect, it, vi } from "vitest";
|
||||
import { assertBlueprintDynamicFields } from "../router/blueprint-validation.js";
|
||||
|
||||
function createDbMock(result: { fieldDefs: unknown; target: BlueprintTarget } | null) {
|
||||
return {
|
||||
blueprint: {
|
||||
findUnique: vi.fn().mockResolvedValue(result),
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
describe("assertBlueprintDynamicFields", () => {
|
||||
it("returns early when no blueprint is set", async () => {
|
||||
const db = createDbMock(null);
|
||||
|
||||
await expect(
|
||||
assertBlueprintDynamicFields({
|
||||
db,
|
||||
blueprintId: undefined,
|
||||
dynamicFields: {},
|
||||
target: BlueprintTarget.PROJECT,
|
||||
}),
|
||||
).resolves.toBeUndefined();
|
||||
|
||||
expect(db.blueprint.findUnique).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it("rejects a missing blueprint", async () => {
|
||||
const db = createDbMock(null);
|
||||
|
||||
await expect(
|
||||
assertBlueprintDynamicFields({
|
||||
db,
|
||||
blueprintId: "bp_missing",
|
||||
dynamicFields: {},
|
||||
target: BlueprintTarget.PROJECT,
|
||||
}),
|
||||
).rejects.toMatchObject({ code: "NOT_FOUND" } satisfies Partial<TRPCError>);
|
||||
});
|
||||
|
||||
it("rejects a blueprint with the wrong target", async () => {
|
||||
const db = createDbMock({ fieldDefs: [], target: BlueprintTarget.RESOURCE });
|
||||
|
||||
await expect(
|
||||
assertBlueprintDynamicFields({
|
||||
db,
|
||||
blueprintId: "bp_resource",
|
||||
dynamicFields: {},
|
||||
target: BlueprintTarget.PROJECT,
|
||||
}),
|
||||
).rejects.toMatchObject({ code: "BAD_REQUEST" } satisfies Partial<TRPCError>);
|
||||
});
|
||||
|
||||
it("rejects invalid dynamic field values", async () => {
|
||||
const fieldDefs: BlueprintFieldDefinition[] = [
|
||||
{
|
||||
id: "cost-center",
|
||||
key: "costCenter",
|
||||
label: "Cost Center",
|
||||
order: 0,
|
||||
type: FieldType.NUMBER,
|
||||
required: true,
|
||||
},
|
||||
];
|
||||
const db = createDbMock({ fieldDefs, target: BlueprintTarget.PROJECT });
|
||||
|
||||
await expect(
|
||||
assertBlueprintDynamicFields({
|
||||
db,
|
||||
blueprintId: "bp_project",
|
||||
dynamicFields: { costCenter: "abc" },
|
||||
target: BlueprintTarget.PROJECT,
|
||||
}),
|
||||
).rejects.toMatchObject({ code: "UNPROCESSABLE_CONTENT" } satisfies Partial<TRPCError>);
|
||||
});
|
||||
|
||||
it("accepts valid dynamic field values", async () => {
|
||||
const fieldDefs: BlueprintFieldDefinition[] = [
|
||||
{
|
||||
id: "cost-center",
|
||||
key: "costCenter",
|
||||
label: "Cost Center",
|
||||
order: 0,
|
||||
type: FieldType.NUMBER,
|
||||
required: true,
|
||||
},
|
||||
];
|
||||
const db = createDbMock({ fieldDefs, target: BlueprintTarget.PROJECT });
|
||||
|
||||
await expect(
|
||||
assertBlueprintDynamicFields({
|
||||
db,
|
||||
blueprintId: "bp_project",
|
||||
dynamicFields: { costCenter: 42 },
|
||||
target: BlueprintTarget.PROJECT,
|
||||
}),
|
||||
).resolves.toBeUndefined();
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,30 @@
|
||||
import { FieldType } from "@planarchy/shared";
|
||||
import { describe, expect, it } from "vitest";
|
||||
import { buildDynamicFieldWhereClauses } from "../router/custom-field-filters.js";
|
||||
|
||||
describe("buildDynamicFieldWhereClauses", () => {
|
||||
it("builds prisma-style clauses for supported field types", () => {
|
||||
expect(
|
||||
buildDynamicFieldWhereClauses([
|
||||
{ key: "isRemote", value: "true", type: FieldType.BOOLEAN },
|
||||
{ key: "seniority", value: "3.5", type: FieldType.NUMBER },
|
||||
{ key: "tools", value: "houdini", type: FieldType.MULTI_SELECT },
|
||||
{ key: "notes", value: "lead", type: FieldType.TEXT },
|
||||
]),
|
||||
).toEqual([
|
||||
{ path: ["isRemote"], equals: true },
|
||||
{ path: ["seniority"], equals: 3.5 },
|
||||
{ path: ["tools"], array_contains: "houdini" },
|
||||
{ path: ["notes"], string_contains: "lead" },
|
||||
]);
|
||||
});
|
||||
|
||||
it("skips empty and invalid numeric filters", () => {
|
||||
expect(
|
||||
buildDynamicFieldWhereClauses([
|
||||
{ key: "empty", value: "", type: FieldType.TEXT },
|
||||
{ key: "invalidNumber", value: "abc", type: FieldType.NUMBER },
|
||||
]),
|
||||
).toEqual([]);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,93 @@
|
||||
import { AllocationStatus } from "@planarchy/shared";
|
||||
import { describe, expect, it, vi } from "vitest";
|
||||
import { loadProjectPlanningReadModel } from "../router/project-planning-read-model.js";
|
||||
|
||||
describe("loadProjectPlanningReadModel", () => {
|
||||
it("applies active-only filters to demand and assignment loaders", async () => {
|
||||
const demandFindMany = vi.fn().mockResolvedValue([]);
|
||||
const assignmentFindMany = vi.fn().mockResolvedValue([]);
|
||||
|
||||
await loadProjectPlanningReadModel(
|
||||
{
|
||||
demandRequirement: { findMany: demandFindMany },
|
||||
assignment: { findMany: assignmentFindMany },
|
||||
} as never,
|
||||
{ projectId: "project_1", activeOnly: true },
|
||||
);
|
||||
|
||||
expect(demandFindMany).toHaveBeenCalledWith(
|
||||
expect.objectContaining({
|
||||
where: { projectId: "project_1", status: { not: AllocationStatus.CANCELLED } },
|
||||
}),
|
||||
);
|
||||
expect(assignmentFindMany).toHaveBeenCalledWith(
|
||||
expect.objectContaining({
|
||||
where: { projectId: "project_1", status: { not: AllocationStatus.CANCELLED } },
|
||||
}),
|
||||
);
|
||||
});
|
||||
|
||||
it("builds a split read model from demand and assignment rows", async () => {
|
||||
const result = await loadProjectPlanningReadModel(
|
||||
{
|
||||
demandRequirement: {
|
||||
findMany: vi.fn().mockResolvedValue([
|
||||
{
|
||||
id: "demand_1",
|
||||
projectId: "project_1",
|
||||
startDate: new Date("2026-03-17"),
|
||||
endDate: new Date("2026-03-18"),
|
||||
hoursPerDay: 8,
|
||||
percentage: 100,
|
||||
role: "FX",
|
||||
roleId: "role_fx",
|
||||
headcount: 2,
|
||||
status: AllocationStatus.PROPOSED,
|
||||
metadata: {},
|
||||
createdAt: new Date("2026-03-13"),
|
||||
updatedAt: new Date("2026-03-13"),
|
||||
},
|
||||
]),
|
||||
},
|
||||
assignment: {
|
||||
findMany: vi.fn().mockResolvedValue([
|
||||
{
|
||||
id: "assignment_1",
|
||||
resourceId: "resource_1",
|
||||
projectId: "project_1",
|
||||
startDate: new Date("2026-03-17"),
|
||||
endDate: new Date("2026-03-18"),
|
||||
hoursPerDay: 8,
|
||||
percentage: 100,
|
||||
role: "Lead",
|
||||
roleId: "role_lead",
|
||||
dailyCostCents: 32000,
|
||||
status: AllocationStatus.ACTIVE,
|
||||
metadata: {},
|
||||
createdAt: new Date("2026-03-13"),
|
||||
updatedAt: new Date("2026-03-13"),
|
||||
resource: {
|
||||
id: "resource_1",
|
||||
displayName: "Alice",
|
||||
eid: "E-001",
|
||||
chapter: "CGI",
|
||||
lcrCents: 5000,
|
||||
availability: { monday: 8 },
|
||||
},
|
||||
},
|
||||
]),
|
||||
},
|
||||
} as never,
|
||||
{ projectId: "project_1" },
|
||||
);
|
||||
|
||||
expect(result.readModel.demands.map((entry) => entry.sourceAllocationId)).toEqual(["demand_1"]);
|
||||
expect(result.readModel.assignments.map((entry) => entry.sourceAllocationId)).toEqual([
|
||||
"assignment_1",
|
||||
]);
|
||||
expect(result.readModel.allocations.map((entry) => entry.id)).toEqual([
|
||||
"demand_1",
|
||||
"assignment_1",
|
||||
]);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,117 @@
|
||||
import { AllocationStatus } from "@planarchy/shared";
|
||||
import { describe, expect, it, vi } from "vitest";
|
||||
import { projectRouter } from "../router/project.js";
|
||||
import { createCallerFactory } from "../trpc.js";
|
||||
|
||||
const createCaller = createCallerFactory(projectRouter);
|
||||
|
||||
function createProtectedCaller(db: Record<string, unknown>) {
|
||||
return createCaller({
|
||||
session: {
|
||||
user: { email: "user@example.com", name: "User", image: null },
|
||||
expires: "2026-03-13T00:00:00.000Z",
|
||||
},
|
||||
db: db as never,
|
||||
dbUser: null,
|
||||
});
|
||||
}
|
||||
|
||||
describe("project router planning counts", () => {
|
||||
it("returns planning entry counts in project.list", async () => {
|
||||
const db = {
|
||||
project: {
|
||||
findMany: vi.fn().mockResolvedValue([
|
||||
{
|
||||
id: "project_1",
|
||||
shortCode: "PRJ",
|
||||
name: "Project One",
|
||||
orderType: "CHARGEABLE",
|
||||
allocationType: "PROJECT",
|
||||
winProbability: 100,
|
||||
budgetCents: 100000,
|
||||
startDate: new Date("2026-03-17"),
|
||||
endDate: new Date("2026-03-28"),
|
||||
status: "ACTIVE",
|
||||
responsiblePerson: null,
|
||||
dynamicFields: {},
|
||||
staffingReqs: [],
|
||||
blueprintId: null,
|
||||
createdAt: new Date("2026-03-13"),
|
||||
updatedAt: new Date("2026-03-13"),
|
||||
_count: { allocations: 1 },
|
||||
},
|
||||
]),
|
||||
count: vi.fn().mockResolvedValue(1),
|
||||
},
|
||||
allocation: {
|
||||
findMany: vi.fn().mockResolvedValue([
|
||||
{
|
||||
id: "legacy_demand",
|
||||
resourceId: null,
|
||||
projectId: "project_1",
|
||||
startDate: new Date("2026-03-17"),
|
||||
endDate: new Date("2026-03-18"),
|
||||
hoursPerDay: 8,
|
||||
percentage: 100,
|
||||
role: "FX",
|
||||
roleId: "role_fx",
|
||||
isPlaceholder: true,
|
||||
headcount: 2,
|
||||
dailyCostCents: 0,
|
||||
status: AllocationStatus.PROPOSED,
|
||||
metadata: {},
|
||||
createdAt: new Date("2026-03-13"),
|
||||
updatedAt: new Date("2026-03-13"),
|
||||
},
|
||||
]),
|
||||
},
|
||||
demandRequirement: {
|
||||
findMany: vi.fn().mockResolvedValue([
|
||||
{
|
||||
id: "demand_1",
|
||||
projectId: "project_1",
|
||||
startDate: new Date("2026-03-17"),
|
||||
endDate: new Date("2026-03-18"),
|
||||
hoursPerDay: 8,
|
||||
percentage: 100,
|
||||
role: "FX",
|
||||
roleId: "role_fx",
|
||||
headcount: 2,
|
||||
status: AllocationStatus.PROPOSED,
|
||||
metadata: {},
|
||||
createdAt: new Date("2026-03-13"),
|
||||
updatedAt: new Date("2026-03-13"),
|
||||
},
|
||||
]),
|
||||
},
|
||||
assignment: {
|
||||
findMany: vi.fn().mockResolvedValue([
|
||||
{
|
||||
id: "assignment_1",
|
||||
demandRequirementId: null,
|
||||
resourceId: "resource_1",
|
||||
projectId: "project_1",
|
||||
startDate: new Date("2026-03-19"),
|
||||
endDate: new Date("2026-03-20"),
|
||||
hoursPerDay: 8,
|
||||
percentage: 100,
|
||||
role: "FX Lead",
|
||||
roleId: "role_fx",
|
||||
dailyCostCents: 32000,
|
||||
status: AllocationStatus.ACTIVE,
|
||||
metadata: {},
|
||||
createdAt: new Date("2026-03-13"),
|
||||
updatedAt: new Date("2026-03-13"),
|
||||
},
|
||||
]),
|
||||
},
|
||||
};
|
||||
|
||||
const caller = createProtectedCaller(db);
|
||||
const result = await caller.list({ limit: 50, page: 1 });
|
||||
|
||||
expect(result.total).toBe(1);
|
||||
expect(result.projects).toHaveLength(1);
|
||||
expect(result.projects[0]?._count.allocations).toBe(2);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,175 @@
|
||||
import { describe, expect, it, vi } from "vitest";
|
||||
import type { inferProcedureInput } from "@trpc/server";
|
||||
import type { AppRouter } from "../router/index.js";
|
||||
|
||||
// Minimal mock helpers
|
||||
function mockCtx(overrides: Record<string, unknown> = {}) {
|
||||
return {
|
||||
ctx: {
|
||||
session: { user: { id: "user_1", systemRole: "MANAGER" } },
|
||||
db: overrides,
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
describe("rateCard router", () => {
|
||||
describe("list", () => {
|
||||
it("returns rate cards with line counts", 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" }],
|
||||
});
|
||||
|
||||
expect(result).toHaveLength(2);
|
||||
expect(result[0]._count.lines).toBe(5);
|
||||
});
|
||||
});
|
||||
|
||||
describe("create", () => {
|
||||
it("creates a rate card with lines", async () => {
|
||||
const create = vi.fn().mockResolvedValue({
|
||||
id: "rc_new",
|
||||
name: "Q1 2026 Rates",
|
||||
currency: "EUR",
|
||||
isActive: true,
|
||||
lines: [
|
||||
{ id: "rcl_1", costRateCents: 9500, billRateCents: 14000, chapter: "Digital Content Production" },
|
||||
],
|
||||
});
|
||||
|
||||
const result = await create({
|
||||
data: {
|
||||
name: "Q1 2026 Rates",
|
||||
currency: "EUR",
|
||||
lines: {
|
||||
create: [{ costRateCents: 9500, billRateCents: 14000, chapter: "Digital Content Production" }],
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
expect(result.id).toBe("rc_new");
|
||||
expect(result.lines).toHaveLength(1);
|
||||
expect(result.lines[0].costRateCents).toBe(9500);
|
||||
});
|
||||
});
|
||||
|
||||
describe("resolveRate", () => {
|
||||
it("returns the most specific matching line", () => {
|
||||
const lines = [
|
||||
{ id: "rcl_1", roleId: null, chapter: "Digital Content Production", costRateCents: 7000, billRateCents: 12000 },
|
||||
{ id: "rcl_2", roleId: "role_3d", chapter: "Digital Content Production", costRateCents: 9500, billRateCents: 14000 },
|
||||
{ id: "rcl_3", roleId: null, chapter: null, costRateCents: 6000, billRateCents: 10000 },
|
||||
];
|
||||
|
||||
const criteria = { roleId: "role_3d", chapter: "Digital Content Production" };
|
||||
|
||||
const scored = lines.map((line) => {
|
||||
let score = 0;
|
||||
let mismatch = false;
|
||||
if (criteria.roleId && line.roleId) {
|
||||
if (line.roleId === criteria.roleId) score += 4;
|
||||
else mismatch = true;
|
||||
}
|
||||
if (criteria.chapter && line.chapter) {
|
||||
if (line.chapter === criteria.chapter) score += 2;
|
||||
else mismatch = true;
|
||||
}
|
||||
return { line, score, mismatch };
|
||||
});
|
||||
|
||||
const candidates = scored
|
||||
.filter((s) => !s.mismatch)
|
||||
.sort((a, b) => b.score - a.score);
|
||||
|
||||
const best = candidates[0];
|
||||
const result = best ? best.line : null;
|
||||
|
||||
// Most specific match: role + chapter = score 6
|
||||
expect(result?.id).toBe("rcl_2");
|
||||
expect(result?.costRateCents).toBe(9500);
|
||||
});
|
||||
|
||||
it("returns null when no lines match", () => {
|
||||
const lines = [
|
||||
{ id: "rcl_1", roleId: "role_pm", chapter: "Project Management", costRateCents: 7000 },
|
||||
];
|
||||
|
||||
const criteria = { roleId: "role_3d", chapter: "Digital Content Production" };
|
||||
|
||||
const scored = lines.map((line) => {
|
||||
let score = 0;
|
||||
let mismatch = false;
|
||||
if (criteria.roleId && line.roleId) {
|
||||
if (line.roleId === criteria.roleId) score += 4;
|
||||
else mismatch = true;
|
||||
}
|
||||
if (criteria.chapter && line.chapter) {
|
||||
if (line.chapter === criteria.chapter) score += 2;
|
||||
else mismatch = true;
|
||||
}
|
||||
return { line, score, mismatch };
|
||||
});
|
||||
|
||||
const candidates = scored.filter((s) => !s.mismatch);
|
||||
const best = candidates[0];
|
||||
expect(best).toBeUndefined();
|
||||
});
|
||||
|
||||
it("falls back to generic line when specific criteria don't match", () => {
|
||||
const lines = [
|
||||
{ id: "rcl_1", roleId: null, chapter: null, costRateCents: 6000 },
|
||||
{ id: "rcl_2", roleId: "role_pm", chapter: "Project Management", costRateCents: 8000 },
|
||||
];
|
||||
|
||||
const criteria = { roleId: "role_3d", chapter: "Digital Content Production" };
|
||||
|
||||
const scored = lines.map((line) => {
|
||||
let score = 0;
|
||||
let mismatch = false;
|
||||
if (criteria.roleId && line.roleId) {
|
||||
if (line.roleId === criteria.roleId) score += 4;
|
||||
else mismatch = true;
|
||||
}
|
||||
if (criteria.chapter && line.chapter) {
|
||||
if (line.chapter === criteria.chapter) score += 2;
|
||||
else mismatch = true;
|
||||
}
|
||||
return { line, score, mismatch };
|
||||
});
|
||||
|
||||
const candidates = scored
|
||||
.filter((s) => !s.mismatch)
|
||||
.sort((a, b) => b.score - a.score);
|
||||
|
||||
const best = candidates[0];
|
||||
const result = best ? best.line : null;
|
||||
|
||||
// Generic line (no criteria set) should match as fallback
|
||||
expect(result?.id).toBe("rcl_1");
|
||||
expect(result?.costRateCents).toBe(6000);
|
||||
});
|
||||
});
|
||||
|
||||
describe("replaceLines", () => {
|
||||
it("deletes all lines and creates new ones in a transaction", 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 } });
|
||||
|
||||
expect(deleteMany).toHaveBeenCalledWith({ where: { rateCardId: "rc_1" } });
|
||||
expect(line1.id).toBe("rcl_new_1");
|
||||
expect(line2.id).toBe("rcl_new_2");
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,169 @@
|
||||
import { AllocationStatus, SystemRole } from "@planarchy/shared";
|
||||
import { TRPCError } from "@trpc/server";
|
||||
import { describe, expect, it, vi } from "vitest";
|
||||
import { roleRouter } from "../router/role.js";
|
||||
import { createCallerFactory } from "../trpc.js";
|
||||
|
||||
vi.mock("../sse/event-bus.js", () => ({
|
||||
emitRoleCreated: vi.fn(),
|
||||
emitRoleDeleted: vi.fn(),
|
||||
emitRoleUpdated: vi.fn(),
|
||||
}));
|
||||
|
||||
const createCaller = createCallerFactory(roleRouter);
|
||||
|
||||
function createManagerCaller(db: Record<string, unknown>) {
|
||||
return createCaller({
|
||||
session: {
|
||||
user: { email: "manager@example.com", name: "Manager", image: null },
|
||||
expires: "2026-03-13T00:00:00.000Z",
|
||||
},
|
||||
db: db as never,
|
||||
dbUser: {
|
||||
id: "user_1",
|
||||
systemRole: SystemRole.MANAGER,
|
||||
permissionOverrides: null,
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
describe("role router planning counts", () => {
|
||||
it("reports planning entry counts for roles", async () => {
|
||||
const db = {
|
||||
role: {
|
||||
findMany: vi.fn().mockResolvedValue([
|
||||
{
|
||||
id: "role_fx",
|
||||
name: "FX",
|
||||
description: null,
|
||||
color: "#111111",
|
||||
isActive: true,
|
||||
createdAt: new Date("2026-03-13"),
|
||||
updatedAt: new Date("2026-03-13"),
|
||||
_count: { resourceRoles: 2 },
|
||||
},
|
||||
]),
|
||||
},
|
||||
allocation: {
|
||||
findMany: vi.fn().mockResolvedValue([
|
||||
{
|
||||
id: "legacy_demand",
|
||||
resourceId: null,
|
||||
projectId: "project_1",
|
||||
startDate: new Date("2026-03-17"),
|
||||
endDate: new Date("2026-03-18"),
|
||||
hoursPerDay: 8,
|
||||
percentage: 100,
|
||||
role: "FX",
|
||||
roleId: "role_fx",
|
||||
isPlaceholder: true,
|
||||
headcount: 2,
|
||||
dailyCostCents: 0,
|
||||
status: AllocationStatus.PROPOSED,
|
||||
metadata: {},
|
||||
createdAt: new Date("2026-03-13"),
|
||||
updatedAt: new Date("2026-03-13"),
|
||||
},
|
||||
]),
|
||||
},
|
||||
demandRequirement: {
|
||||
findMany: vi.fn().mockResolvedValue([
|
||||
{
|
||||
id: "demand_1",
|
||||
projectId: "project_1",
|
||||
startDate: new Date("2026-03-17"),
|
||||
endDate: new Date("2026-03-18"),
|
||||
hoursPerDay: 8,
|
||||
percentage: 100,
|
||||
role: "FX",
|
||||
roleId: "role_fx",
|
||||
headcount: 2,
|
||||
status: AllocationStatus.PROPOSED,
|
||||
metadata: {},
|
||||
createdAt: new Date("2026-03-13"),
|
||||
updatedAt: new Date("2026-03-13"),
|
||||
},
|
||||
]),
|
||||
},
|
||||
assignment: {
|
||||
findMany: vi.fn().mockResolvedValue([
|
||||
{
|
||||
id: "assignment_1",
|
||||
demandRequirementId: null,
|
||||
resourceId: "resource_1",
|
||||
projectId: "project_1",
|
||||
startDate: new Date("2026-03-19"),
|
||||
endDate: new Date("2026-03-20"),
|
||||
hoursPerDay: 8,
|
||||
percentage: 100,
|
||||
role: "FX Lead",
|
||||
roleId: "role_fx",
|
||||
dailyCostCents: 32000,
|
||||
status: AllocationStatus.ACTIVE,
|
||||
metadata: {},
|
||||
createdAt: new Date("2026-03-13"),
|
||||
updatedAt: new Date("2026-03-13"),
|
||||
},
|
||||
]),
|
||||
},
|
||||
};
|
||||
|
||||
const caller = createManagerCaller(db);
|
||||
const result = await caller.list({});
|
||||
|
||||
expect(result).toHaveLength(1);
|
||||
expect(result[0]?._count.resourceRoles).toBe(2);
|
||||
expect(result[0]?._count.allocations).toBe(2);
|
||||
});
|
||||
|
||||
it("blocks deleting a role that is only used by explicit demand or assignment rows", async () => {
|
||||
const db = {
|
||||
role: {
|
||||
findUnique: vi.fn().mockResolvedValue({
|
||||
id: "role_fx",
|
||||
name: "FX",
|
||||
description: null,
|
||||
color: "#111111",
|
||||
isActive: true,
|
||||
createdAt: new Date("2026-03-13"),
|
||||
updatedAt: new Date("2026-03-13"),
|
||||
_count: { resourceRoles: 0 },
|
||||
}),
|
||||
delete: vi.fn(),
|
||||
},
|
||||
allocation: {
|
||||
findMany: vi.fn().mockResolvedValue([]),
|
||||
},
|
||||
demandRequirement: {
|
||||
findMany: vi.fn().mockResolvedValue([
|
||||
{
|
||||
id: "demand_1",
|
||||
projectId: "project_1",
|
||||
startDate: new Date("2026-03-17"),
|
||||
endDate: new Date("2026-03-18"),
|
||||
hoursPerDay: 8,
|
||||
percentage: 100,
|
||||
role: "FX",
|
||||
roleId: "role_fx",
|
||||
headcount: 1,
|
||||
status: AllocationStatus.PROPOSED,
|
||||
metadata: {},
|
||||
createdAt: new Date("2026-03-13"),
|
||||
updatedAt: new Date("2026-03-13"),
|
||||
},
|
||||
]),
|
||||
},
|
||||
assignment: {
|
||||
findMany: vi.fn().mockResolvedValue([]),
|
||||
},
|
||||
};
|
||||
|
||||
const caller = createManagerCaller(db);
|
||||
|
||||
await expect(caller.delete({ id: "role_fx" })).rejects.toMatchObject({
|
||||
code: "PRECONDITION_FAILED",
|
||||
} satisfies Partial<TRPCError>);
|
||||
|
||||
expect(db.role.delete).not.toHaveBeenCalled();
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,285 @@
|
||||
import { AllocationStatus, SystemRole } from "@planarchy/shared";
|
||||
import { describe, expect, it, vi } from "vitest";
|
||||
import { timelineRouter } from "../router/timeline.js";
|
||||
import { createCallerFactory } from "../trpc.js";
|
||||
|
||||
vi.mock("../sse/event-bus.js", () => ({
|
||||
emitAllocationCreated: vi.fn(),
|
||||
emitAllocationDeleted: vi.fn(),
|
||||
emitAllocationUpdated: vi.fn(),
|
||||
emitProjectShifted: vi.fn(),
|
||||
}));
|
||||
|
||||
const createCaller = createCallerFactory(timelineRouter);
|
||||
|
||||
function createManagerCaller(db: Record<string, unknown>) {
|
||||
return createCaller({
|
||||
session: {
|
||||
user: { email: "manager@example.com", name: "Manager", image: null },
|
||||
expires: "2026-03-13T00:00:00.000Z",
|
||||
},
|
||||
db: db as never,
|
||||
dbUser: {
|
||||
id: "user_1",
|
||||
systemRole: SystemRole.MANAGER,
|
||||
permissionOverrides: null,
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
describe("timeline allocation entry resolution", () => {
|
||||
it("creates a quick assignment without dual-writing a legacy allocation row", async () => {
|
||||
const createdAssignment = {
|
||||
id: "assignment_quick_1",
|
||||
demandRequirementId: null,
|
||||
resourceId: "resource_1",
|
||||
projectId: "project_1",
|
||||
startDate: new Date("2026-03-16"),
|
||||
endDate: new Date("2026-03-20"),
|
||||
hoursPerDay: 8,
|
||||
percentage: 100,
|
||||
role: "Team Member",
|
||||
roleId: null,
|
||||
dailyCostCents: 40000,
|
||||
status: AllocationStatus.PROPOSED,
|
||||
metadata: { source: "quickAssign" },
|
||||
createdAt: new Date("2026-03-13"),
|
||||
updatedAt: new Date("2026-03-13"),
|
||||
resource: {
|
||||
id: "resource_1",
|
||||
displayName: "Alice",
|
||||
eid: "E-001",
|
||||
lcrCents: 5000,
|
||||
},
|
||||
project: { id: "project_1", name: "Project One", shortCode: "PRJ" },
|
||||
roleEntity: null,
|
||||
demandRequirement: null,
|
||||
};
|
||||
|
||||
const db = {
|
||||
project: {
|
||||
findUnique: vi.fn().mockResolvedValue({ id: "project_1" }),
|
||||
},
|
||||
resource: {
|
||||
findUnique: vi.fn().mockResolvedValue({
|
||||
id: "resource_1",
|
||||
lcrCents: 5000,
|
||||
availability: {
|
||||
monday: 8,
|
||||
tuesday: 8,
|
||||
wednesday: 8,
|
||||
thursday: 8,
|
||||
friday: 8,
|
||||
saturday: 0,
|
||||
sunday: 0,
|
||||
},
|
||||
}),
|
||||
},
|
||||
allocation: {
|
||||
findMany: vi.fn().mockResolvedValue([]),
|
||||
create: vi.fn(),
|
||||
},
|
||||
assignment: {
|
||||
findMany: vi.fn().mockResolvedValue([]),
|
||||
create: vi.fn().mockResolvedValue(createdAssignment),
|
||||
},
|
||||
vacation: {
|
||||
findMany: vi.fn().mockResolvedValue([]),
|
||||
},
|
||||
auditLog: {
|
||||
create: vi.fn().mockResolvedValue({}),
|
||||
},
|
||||
$transaction: vi.fn(async (callback: (tx: unknown) => unknown) => callback(db)),
|
||||
};
|
||||
|
||||
const caller = createManagerCaller(db);
|
||||
const result = await caller.quickAssign({
|
||||
resourceId: "resource_1",
|
||||
projectId: "project_1",
|
||||
startDate: new Date("2026-03-16"),
|
||||
endDate: new Date("2026-03-20"),
|
||||
hoursPerDay: 8,
|
||||
role: "Team Member",
|
||||
status: AllocationStatus.PROPOSED,
|
||||
});
|
||||
|
||||
expect(result.id).toBe("assignment_quick_1");
|
||||
expect(result.isPlaceholder).toBe(false);
|
||||
expect(db.allocation.create).not.toHaveBeenCalled();
|
||||
expect(db.assignment.create).toHaveBeenCalledWith(
|
||||
expect.objectContaining({
|
||||
data: expect.objectContaining({
|
||||
resourceId: "resource_1",
|
||||
metadata: { source: "quickAssign" },
|
||||
}),
|
||||
}),
|
||||
);
|
||||
});
|
||||
|
||||
it("updates an explicit assignment through updateAllocationInline", async () => {
|
||||
const existingAssignment = {
|
||||
id: "assignment_1",
|
||||
demandRequirementId: null,
|
||||
resourceId: "resource_1",
|
||||
projectId: "project_1",
|
||||
startDate: new Date("2026-03-16"),
|
||||
endDate: new Date("2026-03-20"),
|
||||
hoursPerDay: 4,
|
||||
percentage: 50,
|
||||
role: "Compositor",
|
||||
roleId: "role_comp",
|
||||
dailyCostCents: 20000,
|
||||
status: AllocationStatus.PROPOSED,
|
||||
metadata: {},
|
||||
createdAt: new Date("2026-03-13"),
|
||||
updatedAt: new Date("2026-03-13"),
|
||||
resource: {
|
||||
id: "resource_1",
|
||||
displayName: "Alice",
|
||||
eid: "E-001",
|
||||
lcrCents: 5000,
|
||||
availability: {
|
||||
monday: 8,
|
||||
tuesday: 8,
|
||||
wednesday: 8,
|
||||
thursday: 8,
|
||||
friday: 8,
|
||||
saturday: 0,
|
||||
sunday: 0,
|
||||
},
|
||||
},
|
||||
project: { id: "project_1", name: "Project One", shortCode: "PRJ" },
|
||||
roleEntity: { id: "role_comp", name: "Compositor", color: "#111111" },
|
||||
demandRequirement: null,
|
||||
};
|
||||
const updatedAssignment = {
|
||||
...existingAssignment,
|
||||
hoursPerDay: 6,
|
||||
endDate: new Date("2026-03-21"),
|
||||
percentage: 75,
|
||||
dailyCostCents: 30000,
|
||||
metadata: { includeSaturday: true },
|
||||
updatedAt: new Date("2026-03-14"),
|
||||
};
|
||||
|
||||
const db = {
|
||||
allocation: {
|
||||
findUnique: vi.fn().mockResolvedValue(null),
|
||||
},
|
||||
demandRequirement: {
|
||||
findUnique: vi.fn().mockResolvedValue(null),
|
||||
},
|
||||
assignment: {
|
||||
findUnique: vi.fn().mockResolvedValue(existingAssignment),
|
||||
update: vi.fn().mockResolvedValue(updatedAssignment),
|
||||
},
|
||||
resource: {
|
||||
findUnique: vi.fn().mockResolvedValue({
|
||||
id: "resource_1",
|
||||
lcrCents: 5000,
|
||||
availability: {
|
||||
monday: 8,
|
||||
tuesday: 8,
|
||||
wednesday: 8,
|
||||
thursday: 8,
|
||||
friday: 8,
|
||||
saturday: 0,
|
||||
sunday: 0,
|
||||
},
|
||||
}),
|
||||
},
|
||||
vacation: {
|
||||
findMany: vi.fn().mockResolvedValue([]),
|
||||
},
|
||||
auditLog: {
|
||||
create: vi.fn().mockResolvedValue({}),
|
||||
},
|
||||
$transaction: vi.fn(async (callback: (tx: unknown) => unknown) => callback(db)),
|
||||
};
|
||||
|
||||
const caller = createManagerCaller(db);
|
||||
const result = await caller.updateAllocationInline({
|
||||
allocationId: "assignment_1",
|
||||
hoursPerDay: 6,
|
||||
endDate: new Date("2026-03-21"),
|
||||
includeSaturday: true,
|
||||
});
|
||||
|
||||
expect(result.id).toBe("assignment_1");
|
||||
expect(result.hoursPerDay).toBe(6);
|
||||
expect(db.assignment.update).toHaveBeenCalledWith(
|
||||
expect.objectContaining({
|
||||
where: { id: "assignment_1" },
|
||||
}),
|
||||
);
|
||||
});
|
||||
|
||||
it("updates an explicit demand row through updateAllocationInline", async () => {
|
||||
const existingDemand = {
|
||||
id: "demand_1",
|
||||
projectId: "project_1",
|
||||
startDate: new Date("2026-03-16"),
|
||||
endDate: new Date("2026-03-20"),
|
||||
hoursPerDay: 4,
|
||||
percentage: 50,
|
||||
role: "FX Artist",
|
||||
roleId: "role_fx",
|
||||
headcount: 1,
|
||||
status: AllocationStatus.PROPOSED,
|
||||
metadata: {},
|
||||
createdAt: new Date("2026-03-13"),
|
||||
updatedAt: new Date("2026-03-13"),
|
||||
project: { id: "project_1", name: "Project One", shortCode: "PRJ" },
|
||||
roleEntity: { id: "role_fx", name: "FX Artist", color: "#222222" },
|
||||
};
|
||||
const updatedDemand = {
|
||||
...existingDemand,
|
||||
hoursPerDay: 6,
|
||||
endDate: new Date("2026-03-21"),
|
||||
percentage: 50,
|
||||
metadata: { includeSaturday: true },
|
||||
updatedAt: new Date("2026-03-14"),
|
||||
};
|
||||
|
||||
const db = {
|
||||
allocation: {
|
||||
findUnique: vi.fn().mockResolvedValue(null),
|
||||
},
|
||||
demandRequirement: {
|
||||
findUnique: vi.fn().mockResolvedValue(existingDemand),
|
||||
update: vi.fn().mockResolvedValue(updatedDemand),
|
||||
},
|
||||
assignment: {
|
||||
findUnique: vi.fn().mockResolvedValue(null),
|
||||
update: vi.fn(),
|
||||
},
|
||||
resource: {
|
||||
findUnique: vi.fn(),
|
||||
},
|
||||
vacation: {
|
||||
findMany: vi.fn().mockResolvedValue([]),
|
||||
},
|
||||
auditLog: {
|
||||
create: vi.fn().mockResolvedValue({}),
|
||||
},
|
||||
$transaction: vi.fn(async (callback: (tx: unknown) => unknown) => callback(db)),
|
||||
};
|
||||
|
||||
const caller = createManagerCaller(db);
|
||||
const result = await caller.updateAllocationInline({
|
||||
allocationId: "demand_1",
|
||||
hoursPerDay: 6,
|
||||
endDate: new Date("2026-03-21"),
|
||||
includeSaturday: true,
|
||||
});
|
||||
|
||||
expect(result.id).toBe("demand_1");
|
||||
expect(result.hoursPerDay).toBe(6);
|
||||
expect(db.resource.findUnique).not.toHaveBeenCalled();
|
||||
expect(db.demandRequirement.update).toHaveBeenCalledWith(
|
||||
expect.objectContaining({
|
||||
where: { id: "demand_1" },
|
||||
}),
|
||||
);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,80 @@
|
||||
import { AllocationStatus } from "@planarchy/shared";
|
||||
import { describe, expect, it } from "vitest";
|
||||
import { buildTimelineShiftPlan } from "../router/timeline-shift-planning.js";
|
||||
|
||||
describe("buildTimelineShiftPlan", () => {
|
||||
it("builds validation assignments from explicit assignments", () => {
|
||||
const result = buildTimelineShiftPlan({
|
||||
demandRequirements: [
|
||||
{
|
||||
id: "demand_1",
|
||||
projectId: "project_1",
|
||||
startDate: new Date("2026-03-16"),
|
||||
endDate: new Date("2026-03-20"),
|
||||
hoursPerDay: 8,
|
||||
percentage: 100,
|
||||
role: "FX",
|
||||
roleId: "role_fx",
|
||||
headcount: 2,
|
||||
status: AllocationStatus.PROPOSED,
|
||||
metadata: {},
|
||||
createdAt: new Date("2026-03-13"),
|
||||
updatedAt: new Date("2026-03-13"),
|
||||
},
|
||||
],
|
||||
assignments: [
|
||||
{
|
||||
id: "assignment_1",
|
||||
resourceId: "resource_1",
|
||||
projectId: "project_1",
|
||||
startDate: new Date("2026-03-16"),
|
||||
endDate: new Date("2026-03-20"),
|
||||
hoursPerDay: 6,
|
||||
percentage: 75,
|
||||
role: "Comp",
|
||||
roleId: "role_comp",
|
||||
dailyCostCents: 30000,
|
||||
status: AllocationStatus.ACTIVE,
|
||||
metadata: { includeSaturday: true },
|
||||
createdAt: new Date("2026-03-13"),
|
||||
updatedAt: new Date("2026-03-13"),
|
||||
resource: {
|
||||
id: "resource_1",
|
||||
displayName: "Alice",
|
||||
eid: "E-001",
|
||||
lcrCents: 5000,
|
||||
availability: { monday: 8 },
|
||||
},
|
||||
},
|
||||
],
|
||||
allAssignmentWindows: [
|
||||
{
|
||||
id: "assignment_1",
|
||||
resourceId: "resource_1",
|
||||
projectId: "project_1",
|
||||
startDate: new Date("2026-03-16"),
|
||||
endDate: new Date("2026-03-20"),
|
||||
hoursPerDay: 6,
|
||||
status: AllocationStatus.ACTIVE,
|
||||
},
|
||||
],
|
||||
});
|
||||
|
||||
expect(result.validationAllocations).toHaveLength(1);
|
||||
expect(result.validationAllocations.map((entry) => entry.sourceAllocationId)).toEqual([
|
||||
"assignment_1",
|
||||
]);
|
||||
expect(result.validationAllocations[0]?.includeSaturday).toBe(true);
|
||||
expect(result.validationAllocations[0]?.allAllocationsForResource).toEqual([
|
||||
{
|
||||
id: "assignment_1",
|
||||
resourceId: "resource_1",
|
||||
projectId: "project_1",
|
||||
startDate: new Date("2026-03-16"),
|
||||
endDate: new Date("2026-03-20"),
|
||||
hoursPerDay: 6,
|
||||
status: AllocationStatus.ACTIVE,
|
||||
},
|
||||
]);
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user