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;