Skip to content

Commit

Permalink
feat: csv export
Browse files Browse the repository at this point in the history
  • Loading branch information
acifani committed May 8, 2024
1 parent 62bac26 commit dc80eb4
Showing 1 changed file with 56 additions and 9 deletions.
65 changes: 56 additions & 9 deletions routes.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,29 @@
import * as actual from './actual';
import { q, runQuery } from '@actual-app/api';
import express from 'express';
const router = express.Router();

const transactionsQuery = q('transactions').select([
'*',
'account.closed',
'account.name',
'account.offbudget',
'category.name',
'category.is_income',
'payee.name',
'schedule.name',
]);

const formatHandler: express.RequestHandler = (req, res) => {
const format = req.query['format'] === 'csv' ? 'csv' : 'json';
if (format === 'csv') {
const data = jsonToCSV(res.locals.data);
res.status(200).header('Content-Type', 'text/csv').send(data);
} else if (format === 'json') {
res.status(200).json(res.locals.data);
}
};

router.get('/budget/:month', async (req, res) => {
const budget = await actual.getBudgetAtMonth(req.params.month);
res.status(200).json(budget);
Expand All @@ -12,15 +34,40 @@ router.get('/accounts', async (_, res) => {
res.status(200).json(accounts);
});

router.get('/accounts/:accountid/transactions', async (req, res) => {
const transactions = await actual.getTransactions(req.params.accountid);
res.status(200).json(transactions);
});
router.get(
'/accounts/:accountid/transactions',
async (req, res, next) => {
const { data } = await runQuery(
transactionsQuery.filter({ account: req.params.accountid }),
);
res.locals.data = data;
next();
},
formatHandler,
);

router.get('/transactions', async (req, res) => {
const offbudget = req.query['offbudget'] === 'true';
const transactions = await actual.getAllTransactions(offbudget);
res.status(200).json(transactions);
});
router.get(
'/transactions',
async (_, res, next) => {
const { data } = await runQuery(transactionsQuery);
res.locals.data = data;
next();
},
formatHandler,
);

export { router };

function jsonToCSV(data: any) {
let csv = '';
csv += Object.keys(data?.[0]).join(',') + '\n';

for (const row of data) {
csv += Object.values(row)
.map((v) => JSON.stringify(v))
.join(',');
csv += '\n';
}

return csv;
}

0 comments on commit dc80eb4

Please sign in to comment.