brendmanager/src/opportunities.js
Peppa 57cbe48a45
All checks were successful
peppa/lint lint: success за 0с
peppa/test test: success за 0с
ci / checks (pull_request) Successful in 18s
deploy / deploy (pull_request) Successful in 2s
BRM-15: карточки PR-возможностей и воронка по стадиям
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
2026-11-15 14:56:47 +05:00

65 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.

import fs from "node:fs";
import path from "node:path";
export const STAGES = ["Найдена", "Подготовка заявки", "Заявка отправлена", "Ответ получен", "Выступление состоялось", "Отклонена/неактуальна"];
export const CLOSED_STAGES = ["Выступление состоялось", "Отклонена/неактуальна"];
export const TYPES = ["", "выступление", "подкаст", "мероприятие"];
const isDate = (s) => typeof s === "string" && /^\d{4}-\d{2}-\d{2}$/.test(s) && !Number.isNaN(Date.parse(s + "T00:00:00Z")) && new Date(s + "T00:00:00Z").toISOString().slice(0, 10) === s;
const text = (v) => (v == null ? "" : String(v).trim());
export function normalize(b) {
return {
title: text(b.title), organizer: text(b.organizer), deadline: b.deadline ? text(b.deadline) : null,
topic: text(b.topic), stage: b.stage == null || b.stage === "" ? STAGES[0] : text(b.stage),
nextStep: text(b.nextStep), type: text(b.type), url: text(b.url), notes: text(b.notes),
};
}
export function validate(o) {
if (!o.title) return "Название обязательно";
if (!STAGES.includes(o.stage)) return "Неизвестная стадия";
if (o.deadline !== null && !isDate(o.deadline)) return "Дедлайн в формате ГГГГ-ММ-ДД";
if (!TYPES.includes(o.type)) return "Неизвестный тип";
if (o.url && !/^https?:\/\/\S+$/i.test(o.url)) return "Ссылка должна начинаться с http:// или https://";
return null;
}
export function view(items, today, stage) {
return items
.filter((o) => !stage || o.stage === stage)
.map((o) => ({ ...o, overdue: !!o.deadline && o.deadline < today && !CLOSED_STAGES.includes(o.stage) }))
.sort((a, b) => (a.deadline && b.deadline ? (a.deadline < b.deadline ? -1 : a.deadline > b.deadline ? 1 : 0) : a.deadline ? -1 : b.deadline ? 1 : 0) || a.id - b.id);
}
export function createStore(file) {
let data;
try { data = JSON.parse(fs.readFileSync(file, "utf8")); } catch { data = { nextId: 1, items: [] }; }
const save = () => {
fs.mkdirSync(path.dirname(file), { recursive: true });
fs.writeFileSync(file + ".tmp", JSON.stringify(data, null, 2));
fs.renameSync(file + ".tmp", file);
};
return {
list: (today, stage) => view(data.items, today, stage),
get: (id) => data.items.find((x) => x.id === id),
create(b) {
const o = normalize(b); const err = validate(o);
if (err) return { err };
const item = { id: data.nextId++, ...o }; data.items.push(item); save(); return { item };
},
update(id, b) {
const i = data.items.findIndex((x) => x.id === id);
if (i < 0) return { notFound: true };
const o = normalize({ ...data.items[i], ...b }); const err = validate(o);
if (err) return { err };
data.items[i] = { id, ...o }; save(); return { item: data.items[i] };
},
remove(id) {
const i = data.items.findIndex((x) => x.id === id);
if (i < 0) return false;
data.items.splice(i, 1); save(); return true;
},
};
}