154 lines
6.5 KiB
JavaScript
154 lines
6.5 KiB
JavaScript
const express = require('express');
|
|
const router = express.Router();
|
|
const { getDb, ensureUserDefaultCategories } = require('../db/database');
|
|
const { standardizeError } = require('../middleware/errorFormatter');
|
|
const {
|
|
createSubscriptionFromRecommendation,
|
|
declineRecommendation,
|
|
decorateSubscription,
|
|
getSubscriptionRecommendations,
|
|
getSubscriptionSummary,
|
|
getSubscriptions,
|
|
} = require('../services/subscriptionService');
|
|
const { addMerchantRule, applyMerchantRules } = require('../services/billMerchantRuleService');
|
|
|
|
router.get('/', (req, res) => {
|
|
const db = getDb();
|
|
ensureUserDefaultCategories(req.user.id);
|
|
const subscriptions = getSubscriptions(db, req.user.id);
|
|
res.json({
|
|
summary: getSubscriptionSummary(subscriptions),
|
|
subscriptions,
|
|
});
|
|
});
|
|
|
|
router.get('/recommendations', (req, res) => {
|
|
const db = getDb();
|
|
res.json({
|
|
recommendations: getSubscriptionRecommendations(db, req.user.id),
|
|
});
|
|
});
|
|
|
|
router.post('/recommendations/decline', (req, res) => {
|
|
const { decline_key } = req.body || {};
|
|
if (!decline_key || typeof decline_key !== 'string' || decline_key.length > 200) {
|
|
return res.status(400).json(standardizeError('decline_key is required', 'VALIDATION_ERROR', 'decline_key'));
|
|
}
|
|
try {
|
|
declineRecommendation(getDb(), req.user.id, decline_key);
|
|
res.json({ ok: true });
|
|
} catch (err) {
|
|
res.status(500).json(standardizeError(err.message || 'Failed to decline recommendation', 'DECLINE_ERROR'));
|
|
}
|
|
});
|
|
|
|
// POST /api/subscriptions/recommendations/match-bill
|
|
// Link an existing bill to all transactions in a recommendation (no new bill created).
|
|
router.post('/recommendations/match-bill', (req, res) => {
|
|
const billId = parseInt(req.body?.bill_id, 10);
|
|
const rawIds = Array.isArray(req.body?.transaction_ids) ? req.body.transaction_ids : [];
|
|
const txIds = rawIds.map(id => parseInt(id, 10)).filter(n => Number.isInteger(n) && n > 0).slice(0, 50);
|
|
|
|
if (!Number.isInteger(billId) || billId < 1) {
|
|
return res.status(400).json(standardizeError('bill_id is required', 'VALIDATION_ERROR', 'bill_id'));
|
|
}
|
|
if (txIds.length === 0) {
|
|
return res.status(400).json(standardizeError('transaction_ids must be a non-empty array', 'VALIDATION_ERROR', 'transaction_ids'));
|
|
}
|
|
|
|
const db = getDb();
|
|
const bill = db.prepare('SELECT id, name 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 update = db.prepare(`
|
|
UPDATE transactions
|
|
SET matched_bill_id = ?, match_status = 'matched', updated_at = CURRENT_TIMESTAMP
|
|
WHERE id = ? AND user_id = ? AND ignored = 0 AND match_status != 'matched'
|
|
`);
|
|
let matchedCount = 0;
|
|
db.transaction(() => {
|
|
for (const id of txIds) {
|
|
matchedCount += update.run(billId, id, req.user.id).changes;
|
|
}
|
|
})();
|
|
|
|
// Store merchant rule for ongoing auto-matching on future syncs
|
|
const merchant = typeof req.body?.merchant === 'string' ? req.body.merchant.trim() : '';
|
|
if (merchant) addMerchantRule(db, req.user.id, billId, merchant);
|
|
|
|
// Apply rules immediately to catch any unmatched transactions beyond the explicit list
|
|
const { matched: autoMatched } = applyMerchantRules(db, req.user.id);
|
|
|
|
res.json({ ok: true, matched_count: matchedCount + autoMatched, bill_name: bill.name });
|
|
});
|
|
|
|
router.post('/recommendations/create', (req, res) => {
|
|
const db = getDb();
|
|
ensureUserDefaultCategories(req.user.id);
|
|
if (req.body?.category_id) {
|
|
const categoryId = parseInt(req.body.category_id, 10);
|
|
const category = Number.isInteger(categoryId)
|
|
? db.prepare('SELECT id FROM categories WHERE id = ? AND user_id = ? AND deleted_at IS NULL').get(categoryId, req.user.id)
|
|
: null;
|
|
if (!category) {
|
|
return res.status(400).json(standardizeError('category_id is invalid for this user', 'VALIDATION_ERROR', 'category_id'));
|
|
}
|
|
}
|
|
try {
|
|
const created = createSubscriptionFromRecommendation(db, req.user.id, req.body || {});
|
|
// Store merchant rule so future SimpleFIN transactions auto-match this bill
|
|
if (req.body?.merchant) addMerchantRule(db, req.user.id, created.id, req.body.merchant);
|
|
res.status(201).json(created);
|
|
} catch (err) {
|
|
res.status(err.status || 400).json(standardizeError(err.message || 'Could not create subscription', err.status ? 'VALIDATION_ERROR' : 'SUBSCRIPTION_CREATE_ERROR', err.field || null));
|
|
}
|
|
});
|
|
|
|
router.patch('/:id', (req, res) => {
|
|
const db = getDb();
|
|
const billId = parseInt(req.params.id, 10);
|
|
if (!Number.isInteger(billId)) {
|
|
return res.status(400).json(standardizeError('bill_id must be an integer', 'VALIDATION_ERROR', 'bill_id'));
|
|
}
|
|
|
|
const existing = db.prepare('SELECT * FROM bills WHERE id = ? AND user_id = ? AND deleted_at IS NULL').get(billId, req.user.id);
|
|
if (!existing) return res.status(404).json(standardizeError('Bill not found', 'NOT_FOUND', 'bill_id'));
|
|
|
|
const allowedTypes = new Set(['streaming', 'software', 'cloud', 'music', 'news', 'fitness', 'gaming', 'utilities', 'insurance', 'other']);
|
|
const next = {
|
|
is_subscription: req.body.is_subscription !== undefined ? (req.body.is_subscription ? 1 : 0) : existing.is_subscription,
|
|
subscription_type: req.body.subscription_type !== undefined
|
|
? (allowedTypes.has(req.body.subscription_type) ? req.body.subscription_type : 'other')
|
|
: existing.subscription_type,
|
|
reminder_days_before: req.body.reminder_days_before !== undefined
|
|
? Number(req.body.reminder_days_before)
|
|
: existing.reminder_days_before,
|
|
active: req.body.active !== undefined ? (req.body.active ? 1 : 0) : existing.active,
|
|
};
|
|
|
|
if (!Number.isInteger(next.reminder_days_before) || next.reminder_days_before < 0 || next.reminder_days_before > 30) {
|
|
return res.status(400).json(standardizeError('reminder_days_before must be between 0 and 30', 'VALIDATION_ERROR', 'reminder_days_before'));
|
|
}
|
|
|
|
db.prepare(`
|
|
UPDATE bills
|
|
SET is_subscription = ?,
|
|
subscription_type = ?,
|
|
reminder_days_before = ?,
|
|
active = ?,
|
|
updated_at = datetime('now')
|
|
WHERE id = ? AND user_id = ?
|
|
`).run(next.is_subscription, next.subscription_type, next.reminder_days_before, next.active, billId, req.user.id);
|
|
|
|
const updated = db.prepare(`
|
|
SELECT b.*, c.name AS category_name
|
|
FROM bills b
|
|
LEFT JOIN categories c ON c.id = b.category_id AND c.user_id = b.user_id AND c.deleted_at IS NULL
|
|
WHERE b.id = ? AND b.user_id = ?
|
|
`).get(billId, req.user.id);
|
|
|
|
res.json(decorateSubscription(updated));
|
|
});
|
|
|
|
module.exports = router;
|