brendmanager/src/plan.js
2026-11-18 15:51:27 +05:00

61 lines
3.1 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

// Чистая логика банка идей и 60-дневного календаря (BRM-13).
import { APPROVERS, CHANNELS, STATUSES, pending } from "./approval.js";
export const DAYS = 60;
export const RUBRIC_NAMES = ["Полезные советы", "Истории и кейсы", "Новости рынка", "О команде и бренде", "Акции и предложения"];
export const DEFAULT_SETTINGS = {
threshold: 10,
rubrics: [40, 25, 20, 10, 5].map((share, i) => ({ id: i + 1, name: RUBRIC_NAMES[i], share })),
};
// BRM-112: заглушки «Рубрика N» в сохранённом плане заменяются названиями; названия, которые задал владелец, не трогаем.
export const isPlaceholderName = (n) => /^Рубрика \d+$/.test(String(n).trim());
export function renameStubRubrics(settings) {
for (const r of settings.rubrics) if (isPlaceholderName(r.name) && RUBRIC_NAMES[r.id - 1]) r.name = RUBRIC_NAMES[r.id - 1];
}
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) };
}