61 lines
2.0 KiB
JavaScript
61 lines
2.0 KiB
JavaScript
const router = require('express').Router();
|
|
const { getDb } = require('../db/database');
|
|
const { standardizeError } = require('../middleware/errorFormatter');
|
|
const { decorateDataSource, ensureManualDataSource } = require('../services/transactionService');
|
|
|
|
const VALID_TYPES = new Set(['manual', 'file_import', 'provider_sync']);
|
|
const VALID_STATUSES = new Set(['active', 'inactive', 'error']);
|
|
|
|
function cleanFilter(value) {
|
|
return typeof value === 'string' ? value.trim() : '';
|
|
}
|
|
|
|
// GET /api/data-sources?type=&status=
|
|
router.get('/', (req, res) => {
|
|
const db = getDb();
|
|
ensureManualDataSource(db, req.user.id);
|
|
|
|
const type = cleanFilter(req.query.type);
|
|
const status = cleanFilter(req.query.status);
|
|
|
|
if (type && !VALID_TYPES.has(type)) {
|
|
return res.status(400).json(standardizeError('type must be manual, file_import, or provider_sync', 'VALIDATION_ERROR', 'type'));
|
|
}
|
|
if (status && !VALID_STATUSES.has(status)) {
|
|
return res.status(400).json(standardizeError('status must be active, inactive, or error', 'VALIDATION_ERROR', 'status'));
|
|
}
|
|
|
|
let query = `
|
|
SELECT
|
|
ds.id, ds.user_id, ds.type, ds.provider, ds.name, ds.status,
|
|
ds.config_json, ds.last_sync_at, ds.last_error, ds.created_at, ds.updated_at,
|
|
COUNT(DISTINCT fa.id) AS account_count,
|
|
COUNT(DISTINCT t.id) AS transaction_count
|
|
FROM data_sources ds
|
|
LEFT JOIN financial_accounts fa ON fa.data_source_id = ds.id AND fa.user_id = ds.user_id
|
|
LEFT JOIN transactions t ON t.data_source_id = ds.id AND t.user_id = ds.user_id
|
|
WHERE ds.user_id = ?
|
|
`;
|
|
const params = [req.user.id];
|
|
|
|
if (type) {
|
|
query += ' AND ds.type = ?';
|
|
params.push(type);
|
|
}
|
|
if (status) {
|
|
query += ' AND ds.status = ?';
|
|
params.push(status);
|
|
}
|
|
|
|
query += `
|
|
GROUP BY ds.id
|
|
ORDER BY
|
|
CASE WHEN ds.type = 'manual' THEN 0 ELSE 1 END,
|
|
ds.name COLLATE NOCASE ASC
|
|
`;
|
|
|
|
res.json(db.prepare(query).all(...params).map(decorateDataSource));
|
|
});
|
|
|
|
module.exports = router;
|