2026-05-16 21:36:04 -05:00
|
|
|
const router = require('express').Router();
|
|
|
|
|
const { standardizeError } = require('../middleware/errorFormatter');
|
2026-05-29 03:02:36 -05:00
|
|
|
const { getDb } = require('../db/database');
|
2026-06-07 01:05:48 -05:00
|
|
|
const { applyBankPaymentAsSourceOfTruth, reactivatePaymentsOverriddenBy } = require('../services/paymentAccountingService');
|
2026-05-16 21:36:04 -05:00
|
|
|
const {
|
|
|
|
|
listMatchSuggestions,
|
|
|
|
|
rejectMatchSuggestion,
|
|
|
|
|
} = require('../services/matchSuggestionService');
|
2026-06-06 18:30:21 -05:00
|
|
|
const { learnMerchantRuleFromMatch } = require('../services/billMerchantRuleService');
|
refactor(match): one canonical writer for transaction match state (IMP-CODE-03)
match_status, matched_bill_id and ignored must move together, but they were
updated by copy-pasted inline UPDATEs across six routes/services — exactly how
they drift apart (QA-B5-04 left match_status='matched' with a NULL bill).
Add services/transactionMatchState.js (markMatched / markUnmatched / markIgnored,
each ownership-scoped, returning rows changed) and route the six single-
transaction transitions through it: matchTransactionToBill, unmatchTransaction,
ignoreTransaction, unignoreTransaction (transactionMatchService), the match/
unmatch handlers (routes/matches), and unmatch-on-payment-delete (routes/
transactions, routes/payments).
Guarded bulk auto-match sweeps (subscription tracking, merchant-rule matching,
historical import) and the retention purge intentionally keep their own queries
— their WHERE clauses carry idempotency guards (AND match_status='unmatched')
the simple helper must not silently drop.
Test: tests/transactionMatchState.test.js (transitions + ownership scoping).
transactionMatchService/subscriptionService regression suites still pass;
server 122 pass.
Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-07-03 13:02:10 -05:00
|
|
|
const { markMatched, markUnmatched } = require('../services/transactionMatchState');
|
2026-06-11 20:12:31 -05:00
|
|
|
const { serializePayment } = require('../services/paymentValidation');
|
2026-06-10 19:42:51 -05:00
|
|
|
const { todayLocal } = require('../utils/dates');
|
2026-05-16 21:36:04 -05:00
|
|
|
|
|
|
|
|
function sendMatchError(res, err, fallbackMessage = 'Match operation failed') {
|
|
|
|
|
if (err.status) {
|
|
|
|
|
return res.status(err.status).json(standardizeError(err.message, err.code || 'MATCH_ERROR', err.field));
|
|
|
|
|
}
|
|
|
|
|
console.error('[matches] service error:', err.stack || err.message);
|
|
|
|
|
return res.status(500).json(standardizeError(fallbackMessage, 'MATCH_ERROR'));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// GET /api/matches/suggestions
|
|
|
|
|
router.get('/suggestions', (req, res) => {
|
|
|
|
|
try {
|
|
|
|
|
res.json(listMatchSuggestions(req.user.id, req.query));
|
|
|
|
|
} catch (err) {
|
|
|
|
|
return sendMatchError(res, err, 'Match suggestions failed');
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
// POST /api/matches/:id/reject
|
|
|
|
|
router.post('/:id/reject', (req, res) => {
|
|
|
|
|
try {
|
|
|
|
|
res.json(rejectMatchSuggestion(req.user.id, req.params.id));
|
|
|
|
|
} catch (err) {
|
|
|
|
|
return sendMatchError(res, err, 'Rejecting match suggestion failed');
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
2026-05-29 03:02:36 -05:00
|
|
|
// POST /api/matches/confirm — link a transaction to a bill and record a payment
|
|
|
|
|
router.post('/confirm', (req, res) => {
|
|
|
|
|
const txId = parseInt(req.body?.transaction_id, 10);
|
|
|
|
|
const billId = parseInt(req.body?.bill_id, 10);
|
|
|
|
|
if (!Number.isInteger(txId) || !Number.isInteger(billId)) {
|
|
|
|
|
return res.status(400).json(standardizeError('transaction_id and bill_id are required integers', 'VALIDATION_ERROR'));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const db = getDb();
|
|
|
|
|
const tx = db.prepare('SELECT * FROM transactions WHERE id = ? AND user_id = ?').get(txId, req.user.id);
|
|
|
|
|
if (!tx) return res.status(404).json(standardizeError('Transaction not found', 'NOT_FOUND', 'transaction_id'));
|
|
|
|
|
if (tx.match_status === 'matched') {
|
|
|
|
|
return res.status(409).json(standardizeError('Transaction is already matched to a bill', 'ALREADY_MATCHED', 'transaction_id'));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const bill = db.prepare('SELECT * FROM bills WHERE id = ? AND user_id = ? AND deleted_at IS NULL').get(billId, req.user.id);
|
|
|
|
|
if (!bill) return res.status(404).json(standardizeError('Bill not found', 'NOT_FOUND', 'bill_id'));
|
|
|
|
|
|
|
|
|
|
const existing = db.prepare('SELECT id FROM payments WHERE transaction_id = ? AND deleted_at IS NULL').get(txId);
|
|
|
|
|
if (existing) return res.status(409).json(standardizeError('A payment is already linked to this transaction', 'DUPLICATE_MATCH'));
|
|
|
|
|
|
2026-06-10 19:42:51 -05:00
|
|
|
const paidDate = tx.posted_date || (tx.transacted_at ? tx.transacted_at.slice(0, 10) : todayLocal());
|
2026-06-11 20:12:31 -05:00
|
|
|
const amount = Math.round(Math.abs(tx.amount)); // tx.amount and payments.amount are both cents
|
2026-05-29 03:02:36 -05:00
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
db.exec('BEGIN');
|
|
|
|
|
const payResult = db.prepare(
|
2026-06-07 01:05:48 -05:00
|
|
|
"INSERT INTO payments (bill_id, amount, paid_date, payment_source, transaction_id) VALUES (?, ?, ?, 'transaction_match', ?)"
|
|
|
|
|
).run(billId, amount, paidDate, txId);
|
2026-06-03 22:16:51 -05:00
|
|
|
|
2026-06-07 01:05:48 -05:00
|
|
|
const paymentForAccounting = db.prepare('SELECT * FROM payments WHERE id = ?').get(payResult.lastInsertRowid);
|
|
|
|
|
applyBankPaymentAsSourceOfTruth(db, bill, paymentForAccounting);
|
2026-05-29 03:02:36 -05:00
|
|
|
|
refactor(match): one canonical writer for transaction match state (IMP-CODE-03)
match_status, matched_bill_id and ignored must move together, but they were
updated by copy-pasted inline UPDATEs across six routes/services — exactly how
they drift apart (QA-B5-04 left match_status='matched' with a NULL bill).
Add services/transactionMatchState.js (markMatched / markUnmatched / markIgnored,
each ownership-scoped, returning rows changed) and route the six single-
transaction transitions through it: matchTransactionToBill, unmatchTransaction,
ignoreTransaction, unignoreTransaction (transactionMatchService), the match/
unmatch handlers (routes/matches), and unmatch-on-payment-delete (routes/
transactions, routes/payments).
Guarded bulk auto-match sweeps (subscription tracking, merchant-rule matching,
historical import) and the retention purge intentionally keep their own queries
— their WHERE clauses carry idempotency guards (AND match_status='unmatched')
the simple helper must not silently drop.
Test: tests/transactionMatchState.test.js (transitions + ownership scoping).
transactionMatchService/subscriptionService regression suites still pass;
server 122 pass.
Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-07-03 13:02:10 -05:00
|
|
|
markMatched(db, req.user.id, txId, billId);
|
2026-06-06 18:30:21 -05:00
|
|
|
|
|
|
|
|
// Learn a merchant→bill rule from this explicit confirmation so future
|
|
|
|
|
// synced transactions from the same merchant auto-match. Best-effort.
|
|
|
|
|
learnMerchantRuleFromMatch(db, req.user.id, billId, tx);
|
|
|
|
|
|
2026-05-29 03:02:36 -05:00
|
|
|
db.exec('COMMIT');
|
|
|
|
|
|
|
|
|
|
const payment = db.prepare('SELECT * FROM payments WHERE id = ?').get(payResult.lastInsertRowid);
|
|
|
|
|
const updated = db.prepare(`
|
|
|
|
|
SELECT t.*, b.name AS matched_bill_name
|
|
|
|
|
FROM transactions t
|
|
|
|
|
LEFT JOIN bills b ON b.id = t.matched_bill_id AND b.deleted_at IS NULL
|
|
|
|
|
WHERE t.id = ?
|
|
|
|
|
`).get(txId);
|
2026-06-11 20:12:31 -05:00
|
|
|
res.json({ transaction: updated, payment: serializePayment(payment) });
|
2026-05-29 03:02:36 -05:00
|
|
|
} catch (err) {
|
|
|
|
|
try { db.exec('ROLLBACK'); } catch {}
|
|
|
|
|
return sendMatchError(res, err, 'Failed to confirm match');
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
// POST /api/matches/:transactionId/unmatch — remove a manual match
|
|
|
|
|
router.post('/:transactionId/unmatch', (req, res) => {
|
|
|
|
|
const txId = parseInt(req.params.transactionId, 10);
|
|
|
|
|
if (!Number.isInteger(txId)) {
|
|
|
|
|
return res.status(400).json(standardizeError('transactionId must be an integer', 'VALIDATION_ERROR'));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const db = getDb();
|
|
|
|
|
const tx = db.prepare('SELECT * FROM transactions WHERE id = ? AND user_id = ?').get(txId, req.user.id);
|
|
|
|
|
if (!tx) return res.status(404).json(standardizeError('Transaction not found', 'NOT_FOUND'));
|
|
|
|
|
if (tx.match_status !== 'matched') {
|
|
|
|
|
return res.status(409).json(standardizeError('Transaction is not matched', 'NOT_MATCHED'));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
db.exec('BEGIN');
|
2026-06-07 01:05:48 -05:00
|
|
|
const matchedPayments = db.prepare(`
|
|
|
|
|
SELECT *
|
|
|
|
|
FROM payments
|
|
|
|
|
WHERE transaction_id = ? AND payment_source = 'transaction_match' AND deleted_at IS NULL
|
|
|
|
|
`).all(txId);
|
|
|
|
|
for (const payment of matchedPayments) {
|
|
|
|
|
reactivatePaymentsOverriddenBy(db, payment.id);
|
|
|
|
|
}
|
2026-05-29 03:02:36 -05:00
|
|
|
db.prepare(`
|
|
|
|
|
UPDATE payments SET deleted_at = datetime('now'), updated_at = datetime('now')
|
|
|
|
|
WHERE transaction_id = ? AND payment_source = 'transaction_match' AND deleted_at IS NULL
|
|
|
|
|
`).run(txId);
|
refactor(match): one canonical writer for transaction match state (IMP-CODE-03)
match_status, matched_bill_id and ignored must move together, but they were
updated by copy-pasted inline UPDATEs across six routes/services — exactly how
they drift apart (QA-B5-04 left match_status='matched' with a NULL bill).
Add services/transactionMatchState.js (markMatched / markUnmatched / markIgnored,
each ownership-scoped, returning rows changed) and route the six single-
transaction transitions through it: matchTransactionToBill, unmatchTransaction,
ignoreTransaction, unignoreTransaction (transactionMatchService), the match/
unmatch handlers (routes/matches), and unmatch-on-payment-delete (routes/
transactions, routes/payments).
Guarded bulk auto-match sweeps (subscription tracking, merchant-rule matching,
historical import) and the retention purge intentionally keep their own queries
— their WHERE clauses carry idempotency guards (AND match_status='unmatched')
the simple helper must not silently drop.
Test: tests/transactionMatchState.test.js (transitions + ownership scoping).
transactionMatchService/subscriptionService regression suites still pass;
server 122 pass.
Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-07-03 13:02:10 -05:00
|
|
|
markUnmatched(db, req.user.id, txId);
|
2026-05-29 03:02:36 -05:00
|
|
|
db.exec('COMMIT');
|
|
|
|
|
res.json({ ok: true });
|
|
|
|
|
} catch (err) {
|
|
|
|
|
try { db.exec('ROLLBACK'); } catch {}
|
|
|
|
|
return sendMatchError(res, err, 'Failed to unmatch transaction');
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
2026-05-16 21:36:04 -05:00
|
|
|
module.exports = router;
|