import type { DatabaseSync } from "node:sqlite"; import { ValidationError } from "./vacancies.js"; /** Стадии: подходит → ответил → прошёл скрининг → пришёл на встречу → вышел на обучение. Произведение по умолчанию 0.05. */ export const DEFAULT_CONVERSIONS = [0.5, 0.5, 0.5, 0.5, 0.8]; export function validateConversions(c: unknown): number[] { if (!Array.isArray(c) || c.length === 0 || c.some((x) => typeof x !== "number" || !(x > 0) || x > 1)) throw new ValidationError("конверсии: непустой массив чисел в (0; 1], нулевая конверсия недопустима"); return c as number[]; } export function computePlan(target: number, conversions: number[], workdays: number) { if (!Number.isInteger(workdays) || workdays < 1) throw new ValidationError("дней должно быть целым числом от 1"); const product = validateConversions(conversions).reduce((a, b) => a * b, 1); const resumes = Math.ceil(Math.round((target / product) * 1e6) / 1e6); return { resumes, perDay: Math.ceil(resumes / workdays), conversion: product }; } const SUITABLE = ["qualified", "awaiting_slot", "meeting_set"]; export function dailyReport(db: DatabaseSync, vacancyId: number, plannedPerDay: number) { const day = new Date().toISOString().slice(0, 10); const found = ( db.prepare("SELECT COALESCE(SUM(found),0) AS n FROM runs WHERE vacancy_id=? AND date(started_at)=?").get(vacancyId, day) as { n: number } ).n; const byStatus = Object.fromEntries( (db.prepare("SELECT status, COUNT(*) AS n FROM candidates WHERE vacancy_id=? GROUP BY status").all(vacancyId) as { status: string; n: number }[]).map( (r) => [r.status, r.n], ), ); const suitable = db .prepare( `SELECT id, full_name AS fullName, phone, channel, score, status, ready_for_training AS readyForTraining FROM candidates WHERE vacancy_id=? AND status IN (${SUITABLE.map(() => "?").join(",")}) ORDER BY score DESC`, ) .all(vacancyId, ...SUITABLE); const meetings = db .prepare("SELECT m.starts_at AS startsAt, c.full_name AS fullName FROM meetings m JOIN candidates c ON c.id=m.candidate_id WHERE c.vacancy_id=? ORDER BY m.starts_at") .all(vacancyId); const failedSources = JSON.parse( ((db.prepare("SELECT failed_sources AS f FROM runs WHERE vacancy_id=? ORDER BY id DESC LIMIT 1").get(vacancyId) as { f: string } | undefined)?.f) ?? "[]", ) as string[]; return { date: day, found, planned: plannedPerDay, summary: `${found} из ${plannedPerDay}`, byStatus, suitable, meetings, failedSources }; }