moy-buhgalter/storage.js

148 lines
4.1 KiB
JavaScript

const fs = require("fs");
const path = require("path");
const FILE = path.join(__dirname, "data.json");
function read() {
try {
return JSON.parse(fs.readFileSync(FILE, "utf8"));
} catch (_) {
return { entries: [], lastTransport: {}, reminders: [] };
}
}
function write(data) {
data.updatedAt = new Date().toISOString();
fs.writeFileSync(FILE, JSON.stringify(data, null, 2));
}
function addEntry({ userId, type, amount, note, auto }) {
const data = read();
const entry = {
id: Date.now() + "_" + Math.random().toString(36).slice(2, 7),
userId,
type,
amount: Math.round(amount),
note: String(note || "").trim(),
auto: !!auto,
createdAt: new Date().toISOString()
};
data.entries.push(entry);
write(data);
return entry;
}
function getEntries(userId) {
return read()
.entries.filter((e) => e.userId === userId)
.sort((a, b) => new Date(a.createdAt) - new Date(b.createdAt));
}
function removeById(userId, id) {
const data = read();
const before = data.entries.length;
data.entries = data.entries.filter((e) => !(e.userId === userId && e.id === id));
write(data);
return data.entries.length < before;
}
function updateById(userId, id, patch) {
const data = read();
const e = data.entries.find((x) => x.userId === userId && x.id === id);
if (!e) return false;
if (typeof patch.amount === "number") e.amount = Math.round(patch.amount);
if (typeof patch.note === "string") e.note = patch.note.trim();
if (typeof patch.type === "string" && (patch.type === "income" || patch.type === "expense")) {
e.type = patch.type;
}
write(data);
return true;
}
function getBalance(userId) {
return getEntries(userId).reduce((bal, e) =>
e.type === "income" ? bal + e.amount : bal - e.amount, 0);
}
function getMonthlySummary(userId, year, month) {
const list = getEntries(userId).filter((e) => {
const d = new Date(e.createdAt);
return d.getFullYear() === year && d.getMonth() + 1 === month;
});
const income = list.filter((e) => e.type === "income").reduce((s, e) => s + e.amount, 0);
const expense = list.filter((e) => e.type === "expense").reduce((s, e) => s + e.amount, 0);
return { income, expense, net: income - expense };
}
function getPeriodSummary(userId, from, to) {
const list = getEntries(userId).filter((e) => {
const d = new Date(e.createdAt);
return d >= from && d <= to;
});
const income = list.filter((e) => e.type === "income").reduce((s, e) => s + e.amount, 0);
const expense = list.filter((e) => e.type === "expense").reduce((s, e) => s + e.amount, 0);
const byCat = {};
for (const e of list) {
const cat = e.note || "Без категории";
byCat[cat] = byCat[cat] || { income: 0, expense: 0 };
if (e.type === "income") byCat[cat].income += e.amount;
else byCat[cat].expense += e.amount;
}
// Динамика по дням внутри периода
const byDay = {};
for (const e of list) {
const d = new Date(e.createdAt);
const key = d.toISOString().slice(0, 10);
byDay[key] = byDay[key] || { income: 0, expense: 0 };
if (e.type === "income") byDay[key].income += e.amount;
else byDay[key].expense += e.amount;
}
return { income, expense, net: income - expense, byCat, byDay, count: list.length };
}
function addReminder(userId, dateISO, text) {
const data = read();
data.reminders.push({
id: Date.now() + "_" + Math.random().toString(36).slice(2, 7),
userId,
at: dateISO,
text: String(text || "").trim(),
done: false
});
write(data);
}
function getDueReminders(userId, now) {
const data = read();
const due = data.reminders.filter(
(r) => r.userId === userId && !r.done && new Date(r.at) <= now
);
for (const r of due) r.done = true;
if (due.length) write(data);
return due;
}
function listReminders(userId) {
return read()
.reminders.filter((r) => r.userId === userId && !r.done)
.sort((a, b) => new Date(a.at) - new Date(b.at));
}
module.exports = {
read,
write,
addEntry,
getEntries,
removeById,
updateById,
getBalance,
getMonthlySummary,
getPeriodSummary,
addReminder,
getDueReminders,
listReminders
};