Skip to content

Commit

Permalink
feat: accounts are returned sorted by usage
Browse files Browse the repository at this point in the history
  • Loading branch information
kajyr committed Nov 21, 2021
1 parent abdd3dd commit 4b2f637
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 1 deletion.
3 changes: 2 additions & 1 deletion backend/config/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { filename, readFile } from '../dal';
import getAccountsSorted from '../helpers/get-accounts-sorted';

export default function (fastify, opts, done) {
const routes = [
Expand All @@ -9,7 +10,7 @@ export default function (fastify, opts, done) {
const data = await readFile();
return {
file: filename,
accounts: data.accounts,
accounts: getAccountsSorted(data.transactions),
commodities: data.commodities,
};
},
Expand Down
21 changes: 21 additions & 0 deletions backend/helpers/get-accounts-sorted.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { Transaction } from 'pta-journal';

type Temp = { k: string; n: number };

function getAccountsSorted(trxs: Transaction[]): string[] {
const obj = trxs
.flatMap((trx) => trx.entries.map((entry) => entry.account))
.reduce((acc, cur) => {
acc[cur] = (acc[cur] || 0) + 1;
return acc;
}, {} as Record<string, number>);

let a: Temp[] = [];
for (let k in obj) {
a.push({ k: k, n: obj[k] });
}

return a.sort((a, b) => b.n - a.n).map((a) => a.k);
}

export default getAccountsSorted;

0 comments on commit 4b2f637

Please sign in to comment.