Add user authentication with JWT login and registration
This commit is contained in:
+51
-37
@@ -1,4 +1,4 @@
|
||||
const { getDB } = require('../db');
|
||||
const { getDB } = require("../db");
|
||||
|
||||
/**
|
||||
* 交易匹配服务
|
||||
@@ -13,7 +13,7 @@ const { getDB } = require('../db');
|
||||
* 2. 日期相近(±1天) + 相同金额 + 描述相似
|
||||
* 3. 日期相近(±2天) + 金额相近(±1元) + 对方名称相似
|
||||
*/
|
||||
function findMatches(newTransaction) {
|
||||
function findMatches(newTransaction, userId) {
|
||||
const db = getDB();
|
||||
|
||||
const absAmount = Math.abs(newTransaction.amount);
|
||||
@@ -21,25 +21,31 @@ function findMatches(newTransaction) {
|
||||
// 只匹配金额 > 0.01 的交易
|
||||
if (absAmount < 0.01) return [];
|
||||
|
||||
const candidates = db.prepare(`
|
||||
const candidates = db
|
||||
.prepare(
|
||||
`
|
||||
SELECT id, transaction_code, transaction_date, amount, counterparty,
|
||||
description, source_type, direction, category
|
||||
FROM transactions
|
||||
WHERE id != ?
|
||||
AND source_id != ?
|
||||
AND user_id = ?
|
||||
AND reviewed = 0
|
||||
AND ABS(ABS(amount) - ?) <= 1.0
|
||||
AND transaction_date BETWEEN DATE(?, '-2 days') AND DATE(?, '+2 days')
|
||||
ORDER BY ABS(julianday(transaction_date) - julianday(?)) ASC
|
||||
LIMIT 20
|
||||
`).all(
|
||||
newTransaction.id || -1,
|
||||
newTransaction.source_id || -1,
|
||||
absAmount,
|
||||
newTransaction.transaction_date,
|
||||
newTransaction.transaction_date,
|
||||
newTransaction.transaction_date
|
||||
);
|
||||
`,
|
||||
)
|
||||
.all(
|
||||
newTransaction.id || -1,
|
||||
newTransaction.source_id || -1,
|
||||
userId,
|
||||
absAmount,
|
||||
newTransaction.transaction_date,
|
||||
newTransaction.transaction_date,
|
||||
newTransaction.transaction_date,
|
||||
);
|
||||
|
||||
if (candidates.length === 0) return [];
|
||||
|
||||
@@ -53,66 +59,70 @@ function findMatches(newTransaction) {
|
||||
const amountDiff = Math.abs(Math.abs(candidate.amount) - absAmount);
|
||||
if (amountDiff < 0.01) {
|
||||
score += 40;
|
||||
reasons.push('金额完全一致');
|
||||
reasons.push("金额完全一致");
|
||||
} else if (amountDiff < 0.1) {
|
||||
score += 35;
|
||||
reasons.push('金额基本一致');
|
||||
reasons.push("金额基本一致");
|
||||
} else if (amountDiff < 1.0) {
|
||||
score += 20;
|
||||
reasons.push('金额接近');
|
||||
reasons.push("金额接近");
|
||||
}
|
||||
|
||||
// 日期得分
|
||||
const dateDiff = Math.abs(
|
||||
new Date(candidate.transaction_date) - new Date(newTransaction.transaction_date)
|
||||
) / (1000 * 60 * 60 * 24);
|
||||
const dateDiff =
|
||||
Math.abs(
|
||||
new Date(candidate.transaction_date) -
|
||||
new Date(newTransaction.transaction_date),
|
||||
) /
|
||||
(1000 * 60 * 60 * 24);
|
||||
|
||||
if (dateDiff < 0.01) {
|
||||
score += 30;
|
||||
reasons.push('日期相同');
|
||||
reasons.push("日期相同");
|
||||
} else if (dateDiff <= 1) {
|
||||
score += 20;
|
||||
reasons.push('日期差1天');
|
||||
reasons.push("日期差1天");
|
||||
} else if (dateDiff <= 2) {
|
||||
score += 10;
|
||||
reasons.push('日期差2天');
|
||||
reasons.push("日期差2天");
|
||||
}
|
||||
|
||||
// 对方名称相似度
|
||||
const nameSim = stringSimilarity(
|
||||
(candidate.counterparty || '').toLowerCase(),
|
||||
(newTransaction.counterparty || '').toLowerCase()
|
||||
(candidate.counterparty || "").toLowerCase(),
|
||||
(newTransaction.counterparty || "").toLowerCase(),
|
||||
);
|
||||
if (nameSim > 0.8) {
|
||||
score += 20;
|
||||
reasons.push('对方名称高度相似');
|
||||
reasons.push("对方名称高度相似");
|
||||
} else if (nameSim > 0.5) {
|
||||
score += 10;
|
||||
reasons.push('对方名称部分相似');
|
||||
reasons.push("对方名称部分相似");
|
||||
}
|
||||
|
||||
// 描述相似度
|
||||
const descSim = stringSimilarity(
|
||||
(candidate.description || '').toLowerCase(),
|
||||
(newTransaction.description || '').toLowerCase()
|
||||
(candidate.description || "").toLowerCase(),
|
||||
(newTransaction.description || "").toLowerCase(),
|
||||
);
|
||||
if (descSim > 0.7) {
|
||||
score += 10;
|
||||
reasons.push('描述相似');
|
||||
reasons.push("描述相似");
|
||||
}
|
||||
|
||||
// 方向互补(一个支出一个收入,说明是转账匹配)
|
||||
if (candidate.direction !== newTransaction.direction) {
|
||||
score += 5;
|
||||
reasons.push('收支方向互补');
|
||||
reasons.push("收支方向互补");
|
||||
}
|
||||
|
||||
if (score >= 40) { // 最低匹配阈值
|
||||
if (score >= 40) {
|
||||
// 最低匹配阈值
|
||||
matches.push({
|
||||
transaction_id: candidate.id,
|
||||
match_score: Math.min(score, 100) / 100,
|
||||
reasons: reasons.join(';'),
|
||||
candidate
|
||||
reasons: reasons.join(";"),
|
||||
candidate,
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -124,17 +134,21 @@ function findMatches(newTransaction) {
|
||||
}
|
||||
|
||||
/**
|
||||
* 运行全局自动对账
|
||||
* 运行全局自动对账(限定当前用户)
|
||||
* 对所有未匹配的交易尝试匹配
|
||||
*/
|
||||
function runAutoReconciliation() {
|
||||
function runAutoReconciliation(userId) {
|
||||
const db = getDB();
|
||||
|
||||
const unmatched = db.prepare(`
|
||||
const unmatched = db
|
||||
.prepare(
|
||||
`
|
||||
SELECT * FROM transactions
|
||||
WHERE matched_transaction_id IS NULL
|
||||
WHERE matched_transaction_id IS NULL AND user_id = ?
|
||||
ORDER BY transaction_date DESC
|
||||
`).all();
|
||||
`,
|
||||
)
|
||||
.all(userId);
|
||||
|
||||
let matchCount = 0;
|
||||
|
||||
@@ -149,7 +163,7 @@ function runAutoReconciliation() {
|
||||
for (const tx of unmatched) {
|
||||
if (processed.has(tx.id)) continue;
|
||||
|
||||
const matches = findMatches(tx);
|
||||
const matches = findMatches(tx, userId);
|
||||
if (matches.length > 0 && matches[0].match_score >= 0.7) {
|
||||
const bestMatch = matches[0];
|
||||
|
||||
|
||||
Reference in New Issue
Block a user