93 lines
2.3 KiB
TypeScript
93 lines
2.3 KiB
TypeScript
import { buildSplitAllocationReadModel } from "@planarchy/application";
|
|
import type { PrismaClient } from "@planarchy/db";
|
|
import { AllocationStatus } from "@planarchy/shared";
|
|
|
|
export const PROJECT_PLANNING_ALLOCATION_INCLUDE = {
|
|
resource: {
|
|
select: {
|
|
id: true,
|
|
displayName: true,
|
|
eid: true,
|
|
chapter: true,
|
|
lcrCents: true,
|
|
availability: true,
|
|
},
|
|
},
|
|
project: {
|
|
select: {
|
|
id: true,
|
|
name: true,
|
|
shortCode: true,
|
|
orderType: true,
|
|
budgetCents: true,
|
|
winProbability: true,
|
|
status: true,
|
|
startDate: true,
|
|
endDate: true,
|
|
staffingReqs: true,
|
|
responsiblePerson: true,
|
|
},
|
|
},
|
|
roleEntity: {
|
|
select: { id: true, name: true, color: true },
|
|
},
|
|
} as const;
|
|
|
|
export const PROJECT_PLANNING_DEMAND_INCLUDE = {
|
|
project: PROJECT_PLANNING_ALLOCATION_INCLUDE.project,
|
|
roleEntity: PROJECT_PLANNING_ALLOCATION_INCLUDE.roleEntity,
|
|
} as const;
|
|
|
|
export const PROJECT_PLANNING_ASSIGNMENT_INCLUDE = {
|
|
resource: PROJECT_PLANNING_ALLOCATION_INCLUDE.resource,
|
|
project: PROJECT_PLANNING_ALLOCATION_INCLUDE.project,
|
|
roleEntity: PROJECT_PLANNING_ALLOCATION_INCLUDE.roleEntity,
|
|
} as const;
|
|
|
|
type ProjectPlanningReadDbClient = Pick<
|
|
PrismaClient,
|
|
"demandRequirement" | "assignment"
|
|
>;
|
|
|
|
export interface LoadProjectPlanningReadModelInput {
|
|
projectId: string;
|
|
activeOnly?: boolean;
|
|
}
|
|
|
|
export async function loadProjectPlanningReadModel(
|
|
db: ProjectPlanningReadDbClient,
|
|
input: LoadProjectPlanningReadModelInput,
|
|
) {
|
|
const statusFilter = input.activeOnly
|
|
? { status: { not: AllocationStatus.CANCELLED } }
|
|
: {};
|
|
|
|
const [demandRequirements, assignments] = await Promise.all([
|
|
db.demandRequirement.findMany({
|
|
where: {
|
|
projectId: input.projectId,
|
|
...statusFilter,
|
|
},
|
|
include: PROJECT_PLANNING_DEMAND_INCLUDE,
|
|
orderBy: [{ startDate: "asc" }, { projectId: "asc" }],
|
|
}),
|
|
db.assignment.findMany({
|
|
where: {
|
|
projectId: input.projectId,
|
|
...statusFilter,
|
|
},
|
|
include: PROJECT_PLANNING_ASSIGNMENT_INCLUDE,
|
|
orderBy: [{ startDate: "asc" }, { resourceId: "asc" }],
|
|
}),
|
|
]);
|
|
|
|
return {
|
|
demandRequirements,
|
|
assignments,
|
|
readModel: buildSplitAllocationReadModel({
|
|
demandRequirements,
|
|
assignments,
|
|
}),
|
|
};
|
|
}
|