Refactor Transactions page and add deals UI

This commit is contained in:
2026-05-05 23:21:38 +08:00
parent 4d842fac1a
commit de73990415
3 changed files with 967 additions and 208 deletions
+75 -23
View File
@@ -80,6 +80,8 @@ router.get("/", (req, res) => {
"amount",
"created_at",
"counterparty",
"direction",
"source_type",
];
const safeSortBy = allowedSortFields.includes(sort_by)
? sort_by
@@ -114,54 +116,104 @@ router.get("/", (req, res) => {
router.get("/needs-classify", (req, res) => {
const db = getDB();
const cats = db.prepare("SELECT name FROM categories").all().map(r => r.name);
const cats = db
.prepare("SELECT name FROM categories")
.all()
.map((r) => r.name);
if (!cats.length) return res.json({ ids: [], total: 0 });
const ph = cats.map(() => "?").join(",");
const stale = db.prepare(`SELECT id FROM deals WHERE category IS NULL OR category NOT IN (${ph})`).all(...cats);
res.json({ ids: stale.map(r => r.id), total: stale.length });
const stale = db
.prepare(
`SELECT id FROM deals WHERE category IS NULL OR category NOT IN (${ph})`,
)
.all(...cats);
res.json({ ids: stale.map((r) => r.id), total: stale.length });
});
// POST /api/transactions/reconcile - 全量清除后重新对账
router.post('/reconcile', (req, res) => {
router.post("/reconcile", (req, res) => {
const db = getDB();
db.prepare('DELETE FROM deal_transactions').run();
db.prepare('DELETE FROM deals').run();
db.prepare('UPDATE transactions SET matched_transaction_id=NULL, match_score=NULL').run();
db.prepare("DELETE FROM deal_transactions").run();
db.prepare("DELETE FROM deals").run();
db.prepare(
"UPDATE transactions SET matched_transaction_id=NULL, match_score=NULL",
).run();
const result = runAutoReconciliation();
const allTxs = db.prepare('SELECT id,transaction_date,transaction_time,amount,direction,counterparty,description,matched_transaction_id FROM transactions ORDER BY transaction_date,transaction_time').all();
const visited = new Set(); const groups = [];
const allTxs = db
.prepare(
"SELECT id,transaction_date,transaction_time,amount,direction,counterparty,description,matched_transaction_id FROM transactions ORDER BY transaction_date,transaction_time",
)
.all();
const visited = new Set();
const groups = [];
for (const tx of allTxs) {
if (visited.has(tx.id)) continue; visited.add(tx.id);
if (visited.has(tx.id)) continue;
visited.add(tx.id);
const group = [tx];
for (const other of allTxs) {
if (visited.has(other.id)) continue;
if (other.matched_transaction_id===tx.id||tx.matched_transaction_id===other.id||(tx.matched_transaction_id&&tx.matched_transaction_id===other.matched_transaction_id)) {
group.push(other); visited.add(other.id);
if (
other.matched_transaction_id === tx.id ||
tx.matched_transaction_id === other.id ||
(tx.matched_transaction_id &&
tx.matched_transaction_id === other.matched_transaction_id)
) {
group.push(other);
visited.add(other.id);
}
}
groups.push(group);
}
const insD=db.prepare('INSERT INTO deals(deal_date,amount,direction,counterparty,description,match_count)VALUES(?,?,?,?,?,?)');
const insT=db.prepare('INSERT INTO deal_transactions(deal_id,transaction_id)VALUES(?,?)');
let dc=0;
for(const group of groups){
const e=group.reduce((a,b)=>(a.transaction_time||a.transaction_date)<(b.transaction_time||b.transaction_date)?a:b);
const r=insD.run(e.transaction_time||e.transaction_date,e.amount,e.direction,e.counterparty,e.description,group.length);
for(const tx of group)insT.run(r.lastInsertRowid,tx.id);
const insD = db.prepare(
"INSERT INTO deals(deal_date,amount,direction,counterparty,description,match_count)VALUES(?,?,?,?,?,?)",
);
const insT = db.prepare(
"INSERT INTO deal_transactions(deal_id,transaction_id)VALUES(?,?)",
);
let dc = 0;
for (const group of groups) {
const e = group.reduce((a, b) =>
(a.transaction_time || a.transaction_date) <
(b.transaction_time || b.transaction_date)
? a
: b,
);
const r = insD.run(
e.transaction_time || e.transaction_date,
e.amount,
e.direction,
e.counterparty,
e.description,
group.length,
);
for (const tx of group) insT.run(r.lastInsertRowid, tx.id);
dc++;
}
res.json({success:true,matched:result.matched,total:result.total,deals:dc});
res.json({
success: true,
matched: result.matched,
total: result.total,
deals: dc,
});
});
// POST /api/transactions/classify-all — 重分类 deals
router.post("/classify-all", async (req, res) => {
const db = getDB();
const cats = db.prepare("SELECT name FROM categories").all().map(r => r.name);
const cats = db
.prepare("SELECT name FROM categories")
.all()
.map((r) => r.name);
if (!cats.length) return res.json({ classified: 0 });
const ph = cats.map(() => "?").join(",");
const stale = db.prepare(`SELECT id FROM deals WHERE category IS NULL OR category NOT IN (${ph})`).all(...cats);
const stale = db
.prepare(
`SELECT id FROM deals WHERE category IS NULL OR category NOT IN (${ph})`,
)
.all(...cats);
if (!stale.length) return res.json({ classified: 0, total: 0 });
const ids = stale.map(r => r.id), B = 50;
const ids = stale.map((r) => r.id),
B = 50;
let n = 0;
for (let i = 0; i < ids.length; i += B) {
const r = await classifyDeals(ids.slice(i, i + B));