Refactor Transactions page and add deals UI
This commit is contained in:
+97
-31
@@ -1,32 +1,68 @@
|
||||
const express = require('express');
|
||||
const { getDB } = require('../db');
|
||||
const express = require("express");
|
||||
const { getDB } = require("../db");
|
||||
const router = express.Router();
|
||||
|
||||
// GET /api/deals — 支持筛选和分页
|
||||
router.get('/', (req, res) => {
|
||||
router.get("/", (req, res) => {
|
||||
const db = getDB();
|
||||
const { page = 1, limit = 20, start_date, end_date, category, direction, keyword, sort_by = 'deal_date', sort_order = 'DESC' } = req.query;
|
||||
const {
|
||||
page = 1,
|
||||
limit = 20,
|
||||
start_date,
|
||||
end_date,
|
||||
category,
|
||||
direction,
|
||||
keyword,
|
||||
sort_by = "deal_date",
|
||||
sort_order = "DESC",
|
||||
} = req.query;
|
||||
const offset = (page - 1) * limit;
|
||||
|
||||
const conditions = [];
|
||||
const params = [];
|
||||
if (start_date) { conditions.push('d.deal_date >= ?'); params.push(start_date); }
|
||||
if (end_date) { conditions.push('d.deal_date <= ?'); params.push(end_date); }
|
||||
if (category) { conditions.push('d.category = ?'); params.push(category); }
|
||||
if (direction) { conditions.push('d.direction = ?'); params.push(direction); }
|
||||
if (keyword) {
|
||||
conditions.push('(d.counterparty LIKE ? OR d.description LIKE ? OR d.ai_description LIKE ?)');
|
||||
params.push('%' + keyword + '%', '%' + keyword + '%', '%' + keyword + '%');
|
||||
if (start_date) {
|
||||
conditions.push("d.deal_date >= ?");
|
||||
params.push(start_date);
|
||||
}
|
||||
const where = conditions.length ? 'WHERE ' + conditions.join(' AND ') : '';
|
||||
if (end_date) {
|
||||
conditions.push("d.deal_date <= ?");
|
||||
params.push(end_date);
|
||||
}
|
||||
if (category) {
|
||||
conditions.push("d.category = ?");
|
||||
params.push(category);
|
||||
}
|
||||
if (direction) {
|
||||
conditions.push("d.direction = ?");
|
||||
params.push(direction);
|
||||
}
|
||||
if (keyword) {
|
||||
conditions.push(
|
||||
"(d.counterparty LIKE ? OR d.description LIKE ? OR d.ai_description LIKE ?)",
|
||||
);
|
||||
params.push("%" + keyword + "%", "%" + keyword + "%", "%" + keyword + "%");
|
||||
}
|
||||
const where = conditions.length ? "WHERE " + conditions.join(" AND ") : "";
|
||||
|
||||
const allowedSort = ['id', 'deal_date', 'amount', 'counterparty', 'category', 'match_count'];
|
||||
const safeSort = allowedSort.includes(sort_by) ? sort_by : 'deal_date';
|
||||
const safeOrder = sort_order === 'ASC' ? 'ASC' : 'DESC';
|
||||
const allowedSort = [
|
||||
"id",
|
||||
"deal_date",
|
||||
"amount",
|
||||
"direction",
|
||||
"counterparty",
|
||||
"category",
|
||||
"match_count",
|
||||
];
|
||||
const safeSort = allowedSort.includes(sort_by) ? sort_by : "deal_date";
|
||||
const safeOrder = sort_order === "ASC" ? "ASC" : "DESC";
|
||||
|
||||
const total = db.prepare(`SELECT COUNT(*) as c FROM deals d ${where}`).get(...params).c;
|
||||
const total = db
|
||||
.prepare(`SELECT COUNT(*) as c FROM deals d ${where}`)
|
||||
.get(...params).c;
|
||||
|
||||
const deals = db.prepare(`
|
||||
const deals = db
|
||||
.prepare(
|
||||
`
|
||||
SELECT d.*, GROUP_CONCAT(t.transaction_code) as codes
|
||||
FROM deals d
|
||||
LEFT JOIN deal_transactions dt ON dt.deal_id = d.id
|
||||
@@ -35,35 +71,65 @@ router.get('/', (req, res) => {
|
||||
GROUP BY d.id
|
||||
ORDER BY d.${safeSort} ${safeOrder}
|
||||
LIMIT ? OFFSET ?
|
||||
`).all(...params, limit, offset);
|
||||
`,
|
||||
)
|
||||
.all(...params, limit, offset);
|
||||
|
||||
for (const deal of deals) {
|
||||
deal.transactions = db.prepare(`
|
||||
deal.transactions = db
|
||||
.prepare(
|
||||
`
|
||||
SELECT t.*, s.source_name
|
||||
FROM deal_transactions dt
|
||||
JOIN transactions t ON t.id = dt.transaction_id
|
||||
LEFT JOIN sources s ON s.id = t.source_id
|
||||
WHERE dt.deal_id = ?
|
||||
`).all(deal.id);
|
||||
`,
|
||||
)
|
||||
.all(deal.id);
|
||||
}
|
||||
|
||||
res.json({ deals, total, page: Number(page), totalPages: Math.ceil(total / limit) });
|
||||
res.json({
|
||||
deals,
|
||||
total,
|
||||
page: Number(page),
|
||||
totalPages: Math.ceil(total / limit),
|
||||
});
|
||||
});
|
||||
|
||||
// PUT /api/deals/:id — 编辑分类/确认
|
||||
router.put('/:id', (req, res) => {
|
||||
router.put("/:id", (req, res) => {
|
||||
const db = getDB();
|
||||
const { category, ai_description, reviewed, review_notes, tags } = req.body;
|
||||
const sets = []; const params = [];
|
||||
if (category !== undefined) { sets.push('category = ?'); params.push(category); }
|
||||
if (ai_description !== undefined) { sets.push('ai_description = ?'); params.push(ai_description); }
|
||||
if (reviewed !== undefined) { sets.push('reviewed = ?'); params.push(reviewed); if (reviewed) sets.push('reviewed_at = CURRENT_TIMESTAMP'); }
|
||||
if (review_notes !== undefined) { sets.push('review_notes = ?'); params.push(review_notes); }
|
||||
if (tags !== undefined) { sets.push('tags = ?'); params.push(JSON.stringify(tags)); }
|
||||
if (!sets.length) return res.status(400).json({ error: '无更新字段' });
|
||||
sets.push('updated_at = CURRENT_TIMESTAMP');
|
||||
const sets = [];
|
||||
const params = [];
|
||||
if (category !== undefined) {
|
||||
sets.push("category = ?");
|
||||
params.push(category);
|
||||
}
|
||||
if (ai_description !== undefined) {
|
||||
sets.push("ai_description = ?");
|
||||
params.push(ai_description);
|
||||
}
|
||||
if (reviewed !== undefined) {
|
||||
sets.push("reviewed = ?");
|
||||
params.push(reviewed);
|
||||
if (reviewed) sets.push("reviewed_at = CURRENT_TIMESTAMP");
|
||||
}
|
||||
if (review_notes !== undefined) {
|
||||
sets.push("review_notes = ?");
|
||||
params.push(review_notes);
|
||||
}
|
||||
if (tags !== undefined) {
|
||||
sets.push("tags = ?");
|
||||
params.push(JSON.stringify(tags));
|
||||
}
|
||||
if (!sets.length) return res.status(400).json({ error: "无更新字段" });
|
||||
sets.push("updated_at = CURRENT_TIMESTAMP");
|
||||
params.push(req.params.id);
|
||||
db.prepare('UPDATE deals SET ' + sets.join(', ') + ' WHERE id = ?').run(...params);
|
||||
db.prepare("UPDATE deals SET " + sets.join(", ") + " WHERE id = ?").run(
|
||||
...params,
|
||||
);
|
||||
res.json({ success: true });
|
||||
});
|
||||
|
||||
|
||||
@@ -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));
|
||||
|
||||
Reference in New Issue
Block a user