// Письма организаторам (BRM-20): одно письмо на карточку, один получатель. // Стадии: draft → approved → sent. BRM письмо не отправляет: владелец делает это из своей почты. import fs from "node:fs"; import path from "node:path"; export const LETTER_STAGES = ["draft", "approved", "sent"]; const text = (v) => (v == null ? "" : String(v)); /** Что видит экран: ссылка для почтовой программы есть только у одобренного письма. */ export function letterView(l) { if (!l) return null; const mailto = l.status === "approved" && l.to ? `mailto:${encodeURIComponent(l.to)}?subject=${encodeURIComponent(l.subject)}&body=${encodeURIComponent(l.body)}` : null; return { ...l, mailto }; } export function createLetters(file) { let data; try { data = JSON.parse(fs.readFileSync(file, "utf8")); } catch { data = { items: {} }; } const save = () => { fs.mkdirSync(path.dirname(file), { recursive: true }); fs.writeFileSync(file + ".tmp", JSON.stringify(data, null, 2)); fs.renameSync(file + ".tmp", file); }; const toDraft = (l) => { l.status = "draft"; l.approvedAt = null; }; return { get: (oppId) => data.items[oppId] ?? null, create(oppId, { to, subject, body }, now) { if (data.items[oppId]) return data.items[oppId]; data.items[oppId] = { oppId, to, subject, body, status: "draft", createdAt: now, approvedAt: null, sentAt: null }; save(); return data.items[oppId]; }, /** Любая правка темы или текста сбрасывает одобрение. */ saveText(oppId, b) { const l = data.items[oppId]; if (!l) return { err: "err.letterNotFound", code: 404 }; if (l.status === "sent") return { err: "err.sentLocked", code: 409 }; const subject = text(b.subject).trim(), body = text(b.body).replace(/\r\n/g, "\n"); if (!subject || !body.trim()) return { err: "err.empty", code: 400 }; if (subject !== l.subject || body !== l.body) { l.subject = subject; l.body = body; toDraft(l); save(); } return { letter: l }; }, /** seen — тема и текст, которые владелец видел на экране: одобряется только прочитанное. */ approve(oppId, seen, now) { const l = data.items[oppId]; if (!l) return { err: "err.letterNotFound", code: 404 }; if (l.status === "sent") return { err: "err.sentLocked", code: 409 }; if (l.status === "approved") return { err: "err.alreadyApproved", code: 409 }; if (!l.to) return { err: "err.noRecipient", code: 400 }; if (text(seen.subject) !== l.subject || text(seen.body).replace(/\r\n/g, "\n") !== l.body) return { err: "err.changed", code: 409 }; l.status = "approved"; l.approvedAt = now; save(); return { letter: l }; }, markSent(oppId, now) { const l = data.items[oppId]; if (!l) return { err: "err.letterNotFound", code: 404 }; if (l.status === "sent") return { err: "err.sentLocked", code: 409 }; if (l.status !== "approved") return { err: "err.notApproved", code: 409 }; l.status = "sent"; l.sentAt = now; save(); return { letter: l }; }, /** Адрес в карточке изменился: неотправленное письмо получает новый адрес и теряет одобрение. */ syncRecipient(oppId, email) { const l = data.items[oppId]; if (!l || l.status === "sent" || l.to === email) return; l.to = email; toDraft(l); save(); }, remove(oppId) { if (data.items[oppId]) { delete data.items[oppId]; save(); } }, }; }