Add deals feature for reconciled transactions
- Create deals schema and routes to group matched transactions - Display deals in a new card on the Transactions page - Generate deals during reconciliation and refresh them on demand
This commit is contained in:
+47
-14
@@ -1,11 +1,11 @@
|
||||
const Database = require('better-sqlite3');
|
||||
const path = require('path');
|
||||
const Database = require("better-sqlite3");
|
||||
const path = require("path");
|
||||
|
||||
const DB_PATH = path.join(__dirname, '..', 'data', 'reco.db');
|
||||
const DB_PATH = path.join(__dirname, "..", "data", "reco.db");
|
||||
|
||||
// Ensure data directory exists
|
||||
const fs = require('fs');
|
||||
const dataDir = path.join(__dirname, '..', 'data');
|
||||
const fs = require("fs");
|
||||
const dataDir = path.join(__dirname, "..", "data");
|
||||
if (!fs.existsSync(dataDir)) {
|
||||
fs.mkdirSync(dataDir, { recursive: true });
|
||||
}
|
||||
@@ -15,8 +15,8 @@ let db;
|
||||
function getDB() {
|
||||
if (!db) {
|
||||
db = new Database(DB_PATH);
|
||||
db.pragma('journal_mode = WAL');
|
||||
db.pragma('foreign_keys = ON');
|
||||
db.pragma("journal_mode = WAL");
|
||||
db.pragma("foreign_keys = ON");
|
||||
}
|
||||
return db;
|
||||
}
|
||||
@@ -105,16 +105,47 @@ function initDB() {
|
||||
CREATE INDEX IF NOT EXISTS idx_tx_reviewed ON transactions(reviewed);
|
||||
CREATE INDEX IF NOT EXISTS idx_tx_matched ON transactions(matched_transaction_id);
|
||||
CREATE INDEX IF NOT EXISTS idx_tx_category ON transactions(category);
|
||||
CREATE TABLE IF NOT EXISTS deals (
|
||||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||
deal_date DATE NOT NULL,
|
||||
amount REAL NOT NULL,
|
||||
counterparty TEXT,
|
||||
description TEXT,
|
||||
match_count INTEGER DEFAULT 2,
|
||||
created_at DATETIME DEFAULT CURRENT_TIMESTAMP
|
||||
);
|
||||
|
||||
CREATE TABLE IF NOT EXISTS deal_transactions (
|
||||
deal_id INTEGER NOT NULL,
|
||||
transaction_id INTEGER NOT NULL,
|
||||
PRIMARY KEY (deal_id, transaction_id),
|
||||
FOREIGN KEY (deal_id) REFERENCES deals(id) ON DELETE CASCADE,
|
||||
FOREIGN KEY (transaction_id) REFERENCES transactions(id) ON DELETE CASCADE
|
||||
);
|
||||
|
||||
CREATE INDEX IF NOT EXISTS idx_deal_date ON deals(deal_date);
|
||||
CREATE INDEX IF NOT EXISTS idx_dt_deal ON deal_transactions(deal_id);
|
||||
|
||||
`);
|
||||
|
||||
// 仅首次初始化时插入默认分类(不再用固定ID)
|
||||
const catCount = database.prepare("SELECT COUNT(*) as c FROM categories").get().c;
|
||||
const catCount = database
|
||||
.prepare("SELECT COUNT(*) as c FROM categories")
|
||||
.get().c;
|
||||
if (catCount === 0) {
|
||||
const insertCategory = database.prepare("INSERT INTO categories (name, keywords) VALUES (?, ?)");
|
||||
const insertCategory = database.prepare(
|
||||
"INSERT INTO categories (name, keywords) VALUES (?, ?)",
|
||||
);
|
||||
const defaults = [
|
||||
["餐饮美食", '["餐厅","外卖","食堂","咖啡","奶茶","零食","水果","买菜"]'],
|
||||
["交通出行", '["公交","地铁","打车","出租车","加油","停车","高速","火车","飞机","骑行"]'],
|
||||
["购物消费", '["淘宝","京东","拼多多","超市","百货","服饰","数码","家电","日用品"]'],
|
||||
[
|
||||
"交通出行",
|
||||
'["公交","地铁","打车","出租车","加油","停车","高速","火车","飞机","骑行"]',
|
||||
],
|
||||
[
|
||||
"购物消费",
|
||||
'["淘宝","京东","拼多多","超市","百货","服饰","数码","家电","日用品"]',
|
||||
],
|
||||
["住房相关", '["房租","房贷","物业","水电","燃气","维修","装修"]'],
|
||||
["通讯网络", '["话费","宽带","流量"]'],
|
||||
["投资理财", '["基金","股票","理财","定期","转入","转出","收益"]'],
|
||||
@@ -124,13 +155,15 @@ function initDB() {
|
||||
["教育学习", '["学费","培训","书本","课程","考试"]'],
|
||||
["娱乐休闲", '["电影","演出","旅游","酒店","游戏","充值","会员"]'],
|
||||
["信用卡还款", '["还款","信用卡","花呗","借呗","白条"]'],
|
||||
["其他杂项", "[]"]
|
||||
["其他杂项", "[]"],
|
||||
];
|
||||
database.transaction(() => { defaults.forEach(c => insertCategory.run(c[0], c[1])); })();
|
||||
database.transaction(() => {
|
||||
defaults.forEach((c) => insertCategory.run(c[0], c[1]));
|
||||
})();
|
||||
console.log("[Reco] 已初始化默认分类");
|
||||
}
|
||||
|
||||
console.log('[Reco] 数据库初始化完成');
|
||||
console.log("[Reco] 数据库初始化完成");
|
||||
}
|
||||
|
||||
module.exports = { getDB, initDB };
|
||||
|
||||
Reference in New Issue
Block a user