Add filtering, sorting and pagination to deals page

Add filtering, sorting, and pagination to deals page

Replace alert calls with toast notifications and add a clear deals
button with confirmation dialog
This commit is contained in:
2026-05-05 21:59:45 +08:00
parent 9bb4f1ca87
commit e23598e2e4
4 changed files with 158 additions and 39 deletions
+31 -6
View File
@@ -2,23 +2,40 @@ const express = require('express');
const { getDB } = require('../db');
const router = express.Router();
// GET /api/deals
// GET /api/deals — 支持筛选和分页
router.get('/', (req, res) => {
const db = getDB();
const { page = 1, limit = 50 } = 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 + '%');
}
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 total = db.prepare(`SELECT COUNT(*) as c FROM deals d ${where}`).get(...params).c;
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
LEFT JOIN transactions t ON t.id = dt.transaction_id
${where}
GROUP BY d.id
ORDER BY d.deal_date DESC, ABS(d.amount) DESC
ORDER BY d.${safeSort} ${safeOrder}
LIMIT ? OFFSET ?
`).all(limit, offset);
const total = db.prepare('SELECT COUNT(*) as c FROM deals').get().c;
`).all(...params, limit, offset);
for (const deal of deals) {
deal.transactions = db.prepare(`
@@ -50,4 +67,12 @@ router.put('/:id', (req, res) => {
res.json({ success: true });
});
// POST /api/deals/clear
router.post("/clear", (req, res) => {
const db = getDB();
db.prepare("DELETE FROM deal_transactions").run();
db.prepare("DELETE FROM deals").run();
res.json({ success: true });
});
module.exports = router;
+34 -1
View File
@@ -11,7 +11,7 @@ router.get("/", (req, res) => {
const {
page = 1,
limit = 50,
limit = 20,
start_date,
end_date,
source_type,
@@ -74,6 +74,7 @@ router.get("/", (req, res) => {
// 安全的白名单排序
const allowedSortFields = [
"id",
"transaction_time",
"transaction_date",
"amount",
@@ -120,6 +121,38 @@ router.get("/needs-classify", (req, res) => {
res.json({ ids: stale.map(r => r.id), total: stale.length });
});
// POST /api/transactions/reconcile - 全量清除后重新对账
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();
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 = [];
for (const tx of allTxs) {
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);
}
}
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);
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();