Rework LLM classification around user categories
Move category CRUD into the config route so renames sync historical deals, feed per-user categories into the LLM prompt, cache clients per user, and drop the separate classify route.
This commit is contained in:
+93
-20
@@ -93,36 +93,109 @@ router.get("/categories", (req, res) => {
|
||||
});
|
||||
|
||||
router.put("/categories/:id", (req, res) => {
|
||||
const db = getDB();
|
||||
const { name, keywords } = req.body;
|
||||
getDB()
|
||||
.prepare(
|
||||
"UPDATE categories SET name=COALESCE(?,name), keywords=COALESCE(?,keywords) WHERE id=? AND user_id=?",
|
||||
)
|
||||
.run(
|
||||
name,
|
||||
keywords ? JSON.stringify(keywords) : null,
|
||||
req.params.id,
|
||||
req.userId,
|
||||
);
|
||||
res.json({ success: true });
|
||||
const current = db
|
||||
.prepare("SELECT * FROM categories WHERE id = ? AND user_id = ?")
|
||||
.get(req.params.id, req.userId);
|
||||
if (!current) return res.status(404).json({ error: "分类不存在" });
|
||||
if (current.is_locked)
|
||||
return res.status(403).json({ error: "内置分类不可修改" });
|
||||
|
||||
const newName = name === undefined ? current.name : String(name).trim();
|
||||
if (!newName) return res.status(400).json({ error: "分类名称不能为空" });
|
||||
|
||||
const affectedIds = [];
|
||||
try {
|
||||
db.transaction(() => {
|
||||
if (newName !== current.name) {
|
||||
const dup = db
|
||||
.prepare(
|
||||
"SELECT id FROM categories WHERE user_id = ? AND name = ? AND id <> ?",
|
||||
)
|
||||
.get(req.userId, newName, req.params.id);
|
||||
if (dup) {
|
||||
const err = new Error("已存在同名分类");
|
||||
err.status = 409;
|
||||
throw err;
|
||||
}
|
||||
// 重命名:同步历史 deal 的分类名
|
||||
for (const row of db
|
||||
.prepare("SELECT id FROM deals WHERE user_id = ? AND category = ?")
|
||||
.all(req.userId, current.name))
|
||||
affectedIds.push(row.id);
|
||||
db.prepare(
|
||||
"UPDATE deals SET category = ?, updated_at = CURRENT_TIMESTAMP WHERE user_id = ? AND category = ?",
|
||||
).run(newName, req.userId, current.name);
|
||||
}
|
||||
const sets = [];
|
||||
const params = [];
|
||||
if (newName !== current.name) {
|
||||
sets.push("name = ?");
|
||||
params.push(newName);
|
||||
}
|
||||
if (keywords !== undefined) {
|
||||
sets.push("keywords = ?");
|
||||
params.push(JSON.stringify(Array.isArray(keywords) ? keywords : []));
|
||||
}
|
||||
if (sets.length) {
|
||||
db.prepare(
|
||||
`UPDATE categories SET ${sets.join(", ")} WHERE id = ? AND user_id = ?`,
|
||||
).run(...params, req.params.id, req.userId);
|
||||
}
|
||||
})();
|
||||
} catch (err) {
|
||||
return res
|
||||
.status(err.status || 500)
|
||||
.json({ error: err.message || "更新失败" });
|
||||
}
|
||||
|
||||
res.json({ success: true, affectedDeals: affectedIds.length, affectedIds });
|
||||
});
|
||||
|
||||
router.post("/categories", (req, res) => {
|
||||
const { name, keywords, parent_id } = req.body;
|
||||
if (!name) return res.status(400).json({ error: "分类名称不能为空" });
|
||||
const r = getDB()
|
||||
const db = getDB();
|
||||
const { name, keywords } = req.body;
|
||||
const trimmed = typeof name === "string" ? name.trim() : "";
|
||||
if (!trimmed) return res.status(400).json({ error: "分类名称不能为空" });
|
||||
const dup = db
|
||||
.prepare("SELECT id FROM categories WHERE user_id = ? AND name = ?")
|
||||
.get(req.userId, trimmed);
|
||||
if (dup) return res.status(409).json({ error: "已存在同名分类" });
|
||||
const r = db
|
||||
.prepare(
|
||||
"INSERT INTO categories (name,keywords,parent_id,user_id) VALUES (?,?,?,?)",
|
||||
"INSERT INTO categories (name, keywords, user_id) VALUES (?, ?, ?)",
|
||||
)
|
||||
.run(name, JSON.stringify(keywords || []), parent_id || null, req.userId);
|
||||
.run(trimmed, JSON.stringify(keywords || []), req.userId);
|
||||
res.json({ success: true, id: r.lastInsertRowid });
|
||||
});
|
||||
|
||||
router.delete("/categories/:id", (req, res) => {
|
||||
getDB()
|
||||
.prepare("DELETE FROM categories WHERE id=? AND user_id=?")
|
||||
.run(req.params.id, req.userId);
|
||||
res.json({ success: true });
|
||||
const db = getDB();
|
||||
const cat = db
|
||||
.prepare("SELECT name, is_locked FROM categories WHERE id = ? AND user_id = ?")
|
||||
.get(req.params.id, req.userId);
|
||||
if (!cat) return res.status(404).json({ error: "分类不存在" });
|
||||
if (cat.is_locked)
|
||||
return res.status(403).json({ error: "内置分类不可删除" });
|
||||
|
||||
const affectedIds = db
|
||||
.prepare("SELECT id FROM deals WHERE user_id = ? AND category = ?")
|
||||
.all(req.userId, cat.name)
|
||||
.map((r) => r.id);
|
||||
|
||||
db.transaction(() => {
|
||||
// 历史 deal 重置为待分类
|
||||
db.prepare(
|
||||
"UPDATE deals SET category = NULL, updated_at = CURRENT_TIMESTAMP WHERE user_id = ? AND category = ?",
|
||||
).run(req.userId, cat.name);
|
||||
db.prepare("DELETE FROM categories WHERE id = ? AND user_id = ?").run(
|
||||
req.params.id,
|
||||
req.userId,
|
||||
);
|
||||
})();
|
||||
|
||||
res.json({ success: true, affectedDeals: affectedIds.length, affectedIds });
|
||||
});
|
||||
|
||||
module.exports = router;
|
||||
|
||||
Reference in New Issue
Block a user