Files
khb_microservices/packages/khbuild_assistant_prep/src/inputs/build.ts
Yuri Yaryshev ebb1cb7950
Some checks failed
ci / test (push) Failing after 20s
ci / deploy (push) Has been skipped
Каркас monorepo: scheduler, data_collector, khbuild_assistant_prep, khb_common; миграции; CI/CD
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
2026-10-11 09:44:55 +03:00

71 lines
2.8 KiB
TypeScript

import { addDays, addWorkDays, weekBounds, workDaysBetween, type Queryable } from "khb_common";
import { input1, input2, input3, input4, input5, input7, type InputParams, type TaskRow } from "./queries.js";
export interface PackageItem {
input_no: 1 | 2 | 3 | 4 | 5 | 7;
jt_task_id: string | null;
name: string;
start_date: string | null;
end_date: string | null;
is_overdue: boolean;
is_purchase: boolean;
assignees: string[];
related_task_id: string | null;
days_until_start: number | null;
payload: unknown | null;
}
export function paramsFor(
jtJobId: string,
managerMembershipId: string,
today: string,
nonWorkdays: ReadonlySet<string>,
lookaheadWorkDays: number,
): InputParams {
const wk = weekBounds(today);
const next = weekBounds(addDays(wk.start, 7));
return {
jtJobId, managerMembershipId, today,
weekStart: wk.start, weekEnd: wk.end, nextWeekStart: next.start, nextWeekEnd: next.end,
horizon: addWorkDays(today, lookaheadWorkDays, nonWorkdays),
};
}
const effectiveEnd = (t: TaskRow) => t.end_date ?? t.start_date;
function toItem(no: PackageItem["input_no"], t: TaskRow, today: string, nonWorkdays: ReadonlySet<string>): PackageItem {
const start = t.start_date ?? t.end_date;
return {
input_no: no, jt_task_id: t.jt_task_id, name: t.name, start_date: t.start_date, end_date: t.end_date,
is_overdue: !!effectiveEnd(t) && effectiveEnd(t)! < today,
is_purchase: t.is_purchase, assignees: t.assignees, related_task_id: t.related_task_id,
days_until_start: no === 4 && start ? workDaysBetween(today, start, nonWorkdays) : null,
payload: null,
};
}
/** Собирает все входы (1)-(5), (7) для одного проекта по зеркалам и native-таблицам. */
export async function buildItems(
db: Queryable,
p: InputParams,
assistantProjectId: string,
nonWorkdays: ReadonlySet<string>,
historyDays = 14,
now = new Date(),
): Promise<PackageItem[]> {
const [i1, i2, i3] = [await input1(db, p), await input2(db, p), await input3(db, p)];
const managerIds = [...new Set([...i1, ...i2].map((t) => t.jt_task_id))];
const [i4, i5] = [await input4(db, p, managerIds), await input5(db, p, managerIds)];
const history = await input7(db, assistantProjectId, new Date(now.getTime() - historyDays * 86400_000));
const items: PackageItem[] = [];
for (const [no, rows] of [[1, i1], [2, i2], [3, i3], [4, i4], [5, i5]] as const)
for (const t of rows) items.push(toItem(no, t, p.today, nonWorkdays));
for (const h of history)
items.push({
input_no: 7, jt_task_id: h.jt_task_id, name: h.jt_task_id ?? "(без задачи)", start_date: null, end_date: null,
is_overdue: false, is_purchase: false, assignees: [], related_task_id: null, days_until_start: null, payload: h,
});
return items;
}