61 lines
2.8 KiB
JavaScript
61 lines
2.8 KiB
JavaScript
// Путь записи календаря от черновика до выхода (BRM-28). Чистая логика: функции меняют запись и
|
|
// возвращают { err } (ключ текста из locales) либо { ok: true }. Сама система ничего не публикует.
|
|
import { isDate } from "./plan.js";
|
|
|
|
export const STATUSES = ["draft", "review", "approved", "published"];
|
|
export const APPROVERS = ["Татьяна", "Команда"];
|
|
export const CHANNELS = ["telegram", "linkedin", "site", "other"];
|
|
|
|
export const withApproval = (idea) => ({ status: "draft", approver: null, submittedAt: null, comments: [], history: [], ...idea });
|
|
|
|
function move(idea, to, by, at) {
|
|
idea.history.push({ from: idea.status, to, by, at });
|
|
idea.status = to;
|
|
}
|
|
|
|
export function submit(idea, { approver, by }, now) {
|
|
if (!APPROVERS.includes(approver)) return { err: "err.approver" };
|
|
if (idea.status === "approved" || idea.status === "published") return { err: "err.cannotSubmit" };
|
|
if (idea.status === "review") { idea.approver = approver; return { ok: true }; } // смена согласующего
|
|
idea.approver = approver;
|
|
idea.submittedAt = now;
|
|
move(idea, "review", by || approver, now);
|
|
return { ok: true };
|
|
}
|
|
|
|
export function decide(idea, { action, comment, by }, now) {
|
|
if (idea.status !== "review") return { err: "err.notInReview" };
|
|
if (by !== idea.approver) return { err: "err.notApprover" };
|
|
if (action === "approve") {
|
|
move(idea, "approved", by, now);
|
|
return { ok: true };
|
|
}
|
|
if (action === "return") {
|
|
const text = String(comment ?? "").trim();
|
|
if (!text) return { err: "err.commentRequired" };
|
|
idea.comments.push({ text, by, at: now });
|
|
move(idea, "draft", by, now);
|
|
idea.submittedAt = null;
|
|
return { ok: true };
|
|
}
|
|
return { err: "err.action" };
|
|
}
|
|
|
|
// Правка согласованной записи возвращает её на согласование.
|
|
export function touchedByEdit(idea, now) {
|
|
if (idea.status === "approved") { move(idea, "review", "system", now); idea.submittedAt = now; }
|
|
}
|
|
|
|
export function publish(idea, publications, { channel, date, by }, now) {
|
|
if (idea.status !== "approved" && idea.status !== "published") return { err: "err.notApproved" };
|
|
if (!CHANNELS.includes(channel)) return { err: "err.channel" };
|
|
if (!isDate(date)) return { err: "err.pubDate" };
|
|
const pub = { id: publications.reduce((m, p) => Math.max(m, p.id), 0) + 1, ideaId: idea.id, channel, date, by: by || "", createdAt: now };
|
|
publications.push(pub);
|
|
if (idea.status === "approved") move(idea, "published", by || "", now);
|
|
return { ok: true, publication: pub };
|
|
}
|
|
|
|
export const pending = (ideas) =>
|
|
ideas.filter((i) => i.status === "review").map((i) => ({ id: i.id, title: i.title, approver: i.approver, submittedAt: i.submittedAt }));
|