transactions table Add description column to the Transactions page with ellipsis styling for long text Update default limit from 20 to 10 across all paginated endpoints (deals, transactions, and upload confirmation)
145 lines
3.5 KiB
JavaScript
145 lines
3.5 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 = 10,
|
|
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",
|
|
"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 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.${safeSort} ${safeOrder}
|
|
LIMIT ? OFFSET ?
|
|
`,
|
|
)
|
|
.all(...params, limit, offset);
|
|
|
|
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 = ?
|
|
`,
|
|
)
|
|
.all(deal.id);
|
|
}
|
|
|
|
res.json({
|
|
deals,
|
|
total,
|
|
page: Number(page),
|
|
totalPages: Math.ceil(total / limit),
|
|
});
|
|
});
|
|
|
|
// PUT /api/deals/:id — 编辑分类/确认
|
|
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");
|
|
params.push(req.params.id);
|
|
db.prepare("UPDATE deals SET " + sets.join(", ") + " WHERE id = ?").run(
|
|
...params,
|
|
);
|
|
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;
|