feat(planning): ship holiday-aware planning and assistant upgrades
This commit is contained in:
@@ -26,6 +26,7 @@ import {
|
||||
emitAllocationUpdated,
|
||||
emitProjectShifted,
|
||||
} from "../sse/event-bus.js";
|
||||
import { asHolidayResolverDb, getResolvedCalendarHolidays } from "../lib/holiday-availability.js";
|
||||
import { buildTimelineShiftPlan } from "./timeline-shift-planning.js";
|
||||
import { createTRPCRouter, managerProcedure, protectedProcedure, requirePermission } from "../trpc.js";
|
||||
import { anonymizeResource, getAnonymizationDirectory } from "../lib/anonymization.js";
|
||||
@@ -37,7 +38,7 @@ type ShiftDbClient = Pick<
|
||||
|
||||
type TimelineEntriesDbClient = Pick<
|
||||
PrismaClient,
|
||||
"demandRequirement" | "assignment" | "resource" | "project"
|
||||
"demandRequirement" | "assignment" | "resource" | "project" | "holidayCalendar" | "country" | "metroCity"
|
||||
>;
|
||||
|
||||
type TimelineEntriesFilters = {
|
||||
@@ -325,6 +326,116 @@ export const timelineRouter = createTRPCRouter({
|
||||
};
|
||||
}),
|
||||
|
||||
getHolidayOverlays: protectedProcedure
|
||||
.input(
|
||||
z.object({
|
||||
startDate: z.coerce.date(),
|
||||
endDate: z.coerce.date(),
|
||||
resourceIds: z.array(z.string()).optional(),
|
||||
projectIds: z.array(z.string()).optional(),
|
||||
clientIds: z.array(z.string()).optional(),
|
||||
chapters: z.array(z.string()).optional(),
|
||||
eids: z.array(z.string()).optional(),
|
||||
countryCodes: z.array(z.string()).optional(),
|
||||
}),
|
||||
)
|
||||
.query(async ({ ctx, input }) => {
|
||||
const readModel = await loadTimelineEntriesReadModel(ctx.db, input);
|
||||
const resourceIds = [...new Set(
|
||||
readModel.assignments
|
||||
.map((assignment) => assignment.resourceId)
|
||||
.filter((resourceId): resourceId is string => typeof resourceId === "string" && resourceId.length > 0),
|
||||
)];
|
||||
|
||||
if (input.resourceIds && input.resourceIds.length > 0) {
|
||||
for (const resourceId of input.resourceIds) {
|
||||
if (resourceId && !resourceIds.includes(resourceId)) {
|
||||
resourceIds.push(resourceId);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const hasResourceFilters =
|
||||
(input.chapters?.length ?? 0) > 0 ||
|
||||
(input.eids?.length ?? 0) > 0 ||
|
||||
(input.countryCodes?.length ?? 0) > 0;
|
||||
|
||||
if (hasResourceFilters) {
|
||||
const andConditions: Record<string, unknown>[] = [];
|
||||
if (input.chapters && input.chapters.length > 0) {
|
||||
andConditions.push({ chapter: { in: input.chapters } });
|
||||
}
|
||||
if (input.eids && input.eids.length > 0) {
|
||||
andConditions.push({ eid: { in: input.eids } });
|
||||
}
|
||||
if (input.countryCodes && input.countryCodes.length > 0) {
|
||||
andConditions.push({ country: { code: { in: input.countryCodes } } });
|
||||
}
|
||||
|
||||
const matchingResources = await ctx.db.resource.findMany({
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
where: (andConditions.length === 1 ? andConditions[0]! : { AND: andConditions }) as any,
|
||||
select: { id: true },
|
||||
});
|
||||
|
||||
for (const resource of matchingResources) {
|
||||
if (!resourceIds.includes(resource.id)) {
|
||||
resourceIds.push(resource.id);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (resourceIds.length === 0) {
|
||||
return [];
|
||||
}
|
||||
|
||||
const resources = await ctx.db.resource.findMany({
|
||||
where: { id: { in: resourceIds } },
|
||||
select: {
|
||||
id: true,
|
||||
countryId: true,
|
||||
federalState: true,
|
||||
metroCityId: true,
|
||||
country: { select: { code: true } },
|
||||
metroCity: { select: { name: true } },
|
||||
},
|
||||
});
|
||||
|
||||
const overlays = await Promise.all(
|
||||
resources.map(async (resource) => {
|
||||
const holidays = await getResolvedCalendarHolidays(asHolidayResolverDb(ctx.db), {
|
||||
periodStart: input.startDate,
|
||||
periodEnd: input.endDate,
|
||||
countryId: resource.countryId,
|
||||
countryCode: resource.country?.code ?? null,
|
||||
federalState: resource.federalState,
|
||||
metroCityId: resource.metroCityId,
|
||||
metroCityName: resource.metroCity?.name ?? null,
|
||||
});
|
||||
|
||||
return holidays.map((holiday) => {
|
||||
const holidayDate = new Date(`${holiday.date}T00:00:00.000Z`);
|
||||
return {
|
||||
id: `calendar-holiday:${resource.id}:${holiday.date}`,
|
||||
resourceId: resource.id,
|
||||
type: VacationType.PUBLIC_HOLIDAY,
|
||||
status: "APPROVED" as const,
|
||||
startDate: holidayDate,
|
||||
endDate: holidayDate,
|
||||
note: holiday.name,
|
||||
};
|
||||
});
|
||||
}),
|
||||
);
|
||||
|
||||
return overlays.flat().sort((left, right) => {
|
||||
if (left.resourceId !== right.resourceId) {
|
||||
return left.resourceId.localeCompare(right.resourceId);
|
||||
}
|
||||
return left.startDate.getTime() - right.startDate.getTime();
|
||||
});
|
||||
}),
|
||||
|
||||
/**
|
||||
* Get full project context for a project:
|
||||
* - project with staffingReqs and budget
|
||||
|
||||
Reference in New Issue
Block a user