55 lines
2.5 KiB
JavaScript
55 lines
2.5 KiB
JavaScript
// Чистая логика банка идей и 60-дневного календаря (BRM-13).
|
||
import { APPROVERS, CHANNELS, STATUSES, pending } from "./approval.js";
|
||
export const DAYS = 60;
|
||
export const DEFAULT_SETTINGS = {
|
||
threshold: 10,
|
||
rubrics: [40, 25, 20, 10, 5].map((share, i) => ({ id: i + 1, name: `Рубрика ${i + 1}`, share })),
|
||
};
|
||
|
||
const DATE_RE = /^\d{4}-\d{2}-\d{2}$/;
|
||
export const isDate = (s) => typeof s === "string" && DATE_RE.test(s) && !Number.isNaN(Date.parse(s + "T00:00:00Z")) && addDays(s, 0) === s;
|
||
|
||
export function addDays(date, n) {
|
||
const d = new Date(date + "T00:00:00Z");
|
||
d.setUTCDate(d.getUTCDate() + n);
|
||
return d.toISOString().slice(0, 10);
|
||
}
|
||
|
||
export const windowDates = (start) => Array.from({ length: DAYS }, (_, i) => addDays(start, i));
|
||
export const inWindow = (start, date) => date >= start && date <= addDays(start, DAYS - 1);
|
||
|
||
// Возвращает текст ошибки или null.
|
||
export function validateIdea(input, plan) {
|
||
if (!input.title || !String(input.title).trim()) return "Укажите название";
|
||
if (!plan.settings.rubrics.some((r) => r.id === input.rubric)) return "Рубрика должна быть одной из пяти";
|
||
if (input.date != null) {
|
||
if (!isDate(input.date)) return "Дата в формате ГГГГ-ММ-ДД";
|
||
if (!inWindow(plan.start, input.date)) return `Дата должна быть в пределах 60 дней от ${plan.start}`;
|
||
}
|
||
return null;
|
||
}
|
||
|
||
export function buildCalendar(plan) {
|
||
return windowDates(plan.start).map((date) => ({ date, ideas: plan.ideas.filter((i) => i.date === date) }));
|
||
}
|
||
|
||
export const unscheduled = (plan) => plan.ideas.filter((i) => !i.date || !inWindow(plan.start, i.date));
|
||
|
||
export function buildSummary(plan) {
|
||
const planned = plan.ideas.filter((i) => i.date && inWindow(plan.start, i.date));
|
||
const total = planned.length;
|
||
return {
|
||
total,
|
||
rubrics: plan.settings.rubrics.map((r) => {
|
||
const count = planned.filter((i) => i.rubric === r.id).length;
|
||
const share = total ? Math.round((count / total) * 1000) / 10 : 0;
|
||
const skew = total > 0 && Math.abs(share - r.share) > plan.settings.threshold;
|
||
return { id: r.id, name: r.name, count, share, target: r.share, skew };
|
||
}),
|
||
};
|
||
}
|
||
|
||
export function buildState(plan) {
|
||
return { ...plan, pending: pending(plan.ideas), approvers: APPROVERS, channels: CHANNELS, statuses: STATUSES, calendar: buildCalendar(plan), unscheduled: unscheduled(plan), summary: buildSummary(plan) };
|
||
}
|