brendmanager/src/opportunities.js

68 lines
3.6 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), email: text(b.email),
};
}
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://";
if (o.email && !/^[^\s@,;<>]+@[^\s@,;<>]+\.[^\s@,;<>]+$/.test(o.email)) return "err.email";
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 LEGACY = { "Найдена": "Идея", "Подготовка заявки": "Написали организатору", "Заявка отправлена": "Ждём ответа", "Ответ получен": "Согласовано", "Выступление состоялось": "Выступили", "Отклонена/неактуальна": "Отказ / неактуально" };
for (const it of data.items) if (LEGACY[it.stage]) it.stage = LEGACY[it.stage];
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;
},
};
}