- 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
113 lines
3.9 KiB
JavaScript
113 lines
3.9 KiB
JavaScript
const express = require('express');
|
|
const { getDB } = require('../db');
|
|
const router = express.Router();
|
|
|
|
// GET /api/deals — 获取所有对账关联
|
|
router.get('/', (req, res) => {
|
|
const db = getDB();
|
|
const { page = 1, limit = 50 } = req.query;
|
|
const offset = (page - 1) * limit;
|
|
|
|
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
|
|
GROUP BY d.id
|
|
ORDER BY d.deal_date DESC, ABS(d.amount) DESC
|
|
LIMIT ? OFFSET ?
|
|
`).all(limit, offset);
|
|
|
|
const total = db.prepare('SELECT COUNT(*) as c FROM deals').get().c;
|
|
|
|
// 加载每笔 deal 的关联交易
|
|
for (const deal of deals) {
|
|
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 = ?
|
|
ORDER BY t.transaction_date
|
|
`).all(deal.id);
|
|
}
|
|
|
|
res.json({ deals, total, page: Number(page), totalPages: Math.ceil(total / limit) });
|
|
});
|
|
|
|
// POST /api/deals/generate — 根据现有 matched 关系生成 deals
|
|
router.post('/generate', (req, res) => {
|
|
const db = getDB();
|
|
|
|
// 清除旧数据
|
|
db.prepare('DELETE FROM deal_transactions').run();
|
|
db.prepare('DELETE FROM deals').run();
|
|
|
|
// 查找所有已匹配的交易对(只取一方避免重复)
|
|
const pairs = db.prepare(`
|
|
SELECT DISTINCT
|
|
MIN(t1.id, t2.id) as id1,
|
|
MAX(t1.id, t2.id) as id2,
|
|
t1.transaction_date as d1, t2.transaction_date as d2,
|
|
t1.amount as a1, t2.amount as a2,
|
|
t1.counterparty as cp1, t2.counterparty as cp2,
|
|
t1.description as desc1, t2.description as desc2,
|
|
t1.match_score
|
|
FROM transactions t1
|
|
JOIN transactions t2 ON t2.id = t1.matched_transaction_id
|
|
WHERE t1.matched_transaction_id IS NOT NULL
|
|
AND t1.id < t1.matched_transaction_id
|
|
ORDER BY ABS(t1.amount) DESC
|
|
`).all();
|
|
|
|
const insertDeal = db.prepare('INSERT INTO deals (deal_date, amount, counterparty, description) VALUES (?,?,?,?)');
|
|
const insertDt = db.prepare('INSERT OR IGNORE INTO deal_transactions (deal_id, transaction_id) VALUES (?,?)');
|
|
|
|
let count = 0;
|
|
for (const p of pairs) {
|
|
const dealDate = p.d1 || p.d2;
|
|
const amount = p.a1 || p.a2;
|
|
const cp = [p.cp1, p.cp2].filter(Boolean).join(' / ');
|
|
const desc = [p.desc1, p.desc2].filter(Boolean).join(' / ');
|
|
const r = insertDeal.run(dealDate, amount, cp, desc);
|
|
insertDt.run(r.lastInsertRowid, p.id1);
|
|
insertDt.run(r.lastInsertRowid, p.id2);
|
|
count++;
|
|
}
|
|
|
|
res.json({ success: true, generated: count, total: pairs.length });
|
|
});
|
|
|
|
// POST /api/deals/refresh — 刷新 deals(增量更新)
|
|
router.post('/refresh', (req, res) => {
|
|
const db = getDB();
|
|
|
|
// 找到有 matched 但不在 deals 中的交易
|
|
const matched = db.prepare(`
|
|
SELECT t1.id as id1, t2.id as id2, t1.transaction_date, t1.amount,
|
|
t1.counterparty as cp1, t2.counterparty as cp2,
|
|
t1.description as desc1, t2.description as desc2
|
|
FROM transactions t1
|
|
JOIN transactions t2 ON t2.id = t1.matched_transaction_id
|
|
WHERE t1.id < t1.matched_transaction_id
|
|
AND t1.id NOT IN (SELECT transaction_id FROM deal_transactions)
|
|
`).all();
|
|
|
|
if (matched.length === 0) return res.json({ success: true, added: 0 });
|
|
|
|
const insertDeal = db.prepare('INSERT INTO deals (deal_date, amount, counterparty, description) VALUES (?,?,?,?)');
|
|
const insertDt = db.prepare('INSERT OR IGNORE INTO deal_transactions (deal_id, transaction_id) VALUES (?,?)');
|
|
|
|
for (const p of matched) {
|
|
const r = insertDeal.run(p.transaction_date, p.amount,
|
|
[p.cp1, p.cp2].filter(Boolean).join(' / '),
|
|
[p.desc1, p.desc2].filter(Boolean).join(' / '));
|
|
insertDt.run(r.lastInsertRowid, p.id1);
|
|
insertDt.run(r.lastInsertRowid, p.id2);
|
|
}
|
|
|
|
res.json({ success: true, added: matched.length });
|
|
});
|
|
|
|
module.exports = router;
|