// BRM-163: карточка согласования и журнал отправки у записи «Плана и банка идей». // Собирает сервер (приёмка смотрит страницу до скриптов). Данные — запись плана, карточка проверочного // пути с этой записью (testRun.view()), публикации записи. Кнопки — та же форма /bot/test-card, // что на «Бот и отчёты», только с возвратом к записи (back=plan). import { t } from "./i18n.js"; import { almatyTime } from "./deploy-status.js"; import { OFFSET_MS, SLOT } from "./test-run.js"; const ACTIONS = ["approve", "rework", "reject"]; const esc = (s) => String(s ?? "").replace(/&/g, "&").replace(//g, ">").replace(/"/g, """); const multiline = (s) => esc(s).replace(/\n/g, "
"); // Время публикации по Алматы, которое агент назначит записи с датой (SLOT). export const slotTime = (date) => new Date(Date.parse(`${date}T${SLOT}:00Z`) - OFFSET_MS).toISOString(); // Какую запись показать: ?id, иначе ближайшую с датой от сегодня, иначе первую по дате, иначе любую. export function pickIdea(ideas, id, today) { const byId = ideas.find((i) => String(i.id) === String(id ?? "")); if (byId) return byId; const dated = ideas.filter((i) => i.date).sort((a, b) => a.date.localeCompare(b.date) || a.id - b.id); return dated.find((i) => i.date >= today) ?? dated[0] ?? ideas[0] ?? null; } const cardsOf = (tr, idea) => (tr?.cards ?? []).filter((c) => c.ideaId === idea.id); // свежие первыми export function planCard(lang, { ideas = [], idea, tr, assets = [], publications = [], why = () => "", wordSet = false, q = "", msg = null }) { const L = (k, v) => esc(t(lang, "plan.card." + k, v)); const B = (k, v) => esc(t(lang, "tr." + k, v)); const P = (k, v) => esc(t(lang, "botp.test." + k, v)); const choose = ideas.length ? `
${q ? `` : ""}
` : ""; const note = msg ? `

${P("card.msg." + msg)}

` : ""; const offBtns = (reason) => `

${ACTIONS.map((a) => ``).join(" ")}

${reason}

`; if (!idea) { return `

${P("mark")} ${L("empty")}

${B("card.text")}: ${P("card.later")}

${B("card.assets")}: ${P("card.later")}

${tr?.channel ? B("card.channel", { title: tr.channel.title }) : P("card.noChannel")}

${P("card.timeLater")}

${offBtns(L("why.noIdea"))}

${L("h.log")}

${P("card.nothing.closed")}

${L("count", { test: 0, real: 0 })}

`; } const [c] = cardsOf(tr, idea); const text = c ? c.post : [idea.title, idea.description].map((s) => String(s ?? "").trim()).filter(Boolean).join("\n\n"); const att = c ? c.attachments ?? [] : assets; const mats = att.length ? att.map((a) => `«${esc(a.title)}»`).join(", ") : B("card.noAssets"); const channel = c ? B("card.channel", { title: c.chat?.title ?? "" }) : tr?.channel ? B("card.channel", { title: tr.channel.title }) : P("card.noChannel"); const time = c ? B("card.time", { time: almatyTime(c.publishAt) }) : idea.date ? B("card.time", { time: almatyTime(slotTime(idea.date)) }) : L("noDate"); const state = c ? B("state." + c.status) : esc(t(lang, "ap.status." + (idea.status ?? "draft"))); let actions; if (c && c.status === "waiting" && wordSet) { actions = `

${ACTIONS.map((a) => ``).join(" ")}

${P("card.same")}

`; } else { const reason = c && c.status === "waiting" ? L("why.noWord") : c ? L("why.closed", { state: B("state." + c.status) }) : ["approved", "published"].includes(idea.status) ? L("why.agreed") : !idea.date ? L("why.noDate") : tr?.approved ? L("why.wait", { time: almatyTime(slotTime(idea.date)) }) : L("why.strategy"); actions = offBtns(reason); } const card = `

${P("mark")} ${L("record")}: ${esc(idea.title)}

${P("card.state")}: ${state}

${B("card.text")}:

${multiline(text)}

${B("card.assets")}: ${mats}

${channel}

${time}

${c && c.status === "waiting" ? `

${B("card.deadline", { time: almatyTime(c.expiresAt) })}

` : ""}${actions}
`; // Журнал записи: публикации (тест / настоящая) и события проверочного пути по её карточкам. const pubs = publications.filter((p) => p.ideaId === idea.id); const tests = pubs.filter((p) => p.test), reals = pubs.filter((p) => !p.test); const ids = new Set(cardsOf(tr, idea).map((x) => x.id)); const events = (tr?.log ?? []).filter((e) => e.ideaId === idea.id || (e.card != null && ids.has(e.card))); const sentEv = events.find((e) => e.kind === "sent"); const sentCard = cardsOf(tr, idea).find((x) => x.status === "sent"); const lastTest = tests.at(-1); const sent = sentEv ? { messageId: sentEv.messageId, at: sentEv.sentAt ?? sentEv.at, link: sentEv.link } : sentCard ? { messageId: sentCard.messageId, at: sentCard.sentAt, link: sentCard.link } : lastTest?.messageId ? { messageId: lastTest.messageId, at: lastTest.createdAt, link: lastTest.link } : null; const open = (link) => (link ? ` ${P("card.open")}` : ""); const nothing = !c ? (["approved", "published"].includes(idea.status) ? "nothing.agreed" : "nothing.notApproved") : c.status === "waiting" ? "nothing.waiting" : c.status === "approved" ? "nothing.approved" : "nothing.closed"; const sum = sent ? `

${P("mark")} ${L("sent", { messageId: sent.messageId, time: almatyTime(sent.at) })}${open(sent.link)}

` : reals.length ? `

${L("realOnly")}

` : `

${L(nothing, { time: c ? almatyTime(c.publishAt) : "" })}

`; const pubRows = pubs.map((p) => `
  • ${p.test ? `${P("mark")}` : `${L("real")}`} ${esc(t(lang, "ap.channel." + p.channel))}, ${esc(p.date)}${p.messageId ? ", " + L("msgNo", { messageId: p.messageId, time: almatyTime(p.createdAt) }) : p.test ? "" : ", " + L("manual")}${open(p.link)}
  • `); const evRows = events.map((e) => { const v = { ...e, publishAt: e.publishAt ? almatyTime(e.publishAt) : "", sentAt: e.sentAt ? almatyTime(e.sentAt) : "", title: e.title ?? "" }; let s = P("k." + e.kind, v); if (e.kind === "blocked") s += ": " + (e.problems ?? []).map((x) => esc(why(x))).join(" "); return `
  • ${esc(almatyTime(e.at))} — ${P("mark")} ${s}${open(e.link)}
  • `; }); const list = pubRows.length || evRows.length ? `${pubRows.length ? `

    ${L("h.pubs")}

    ` : ""}${evRows.length ? `

    ${L("h.events")}

    ` : ""}` : ""; return `${note}${choose}${card}

    ${L("h.log")}

    ${sum}

    ${L("count", { test: tests.length, real: reals.length })}

    ${list}

    ${L("toBot")}

    `; }