Skip to content

Commit

Permalink
feat: get single account with balance
Browse files Browse the repository at this point in the history
  • Loading branch information
acifani committed May 23, 2024
1 parent a1da34b commit bd7e598
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 0 deletions.
23 changes: 23 additions & 0 deletions actual.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,29 @@ export async function getAccounts() {
return api.getAccounts();
}

export async function getAccount(id: string) {
const accounts = await api.getAccounts();
const account = accounts.find((a: any) => a.id === id);
if (!account) {
return null;
}

const balance = await getAccountBalance(id);
return { ...account, balance };
}

export async function getAccountBalance(
accountID: string,
): Promise<number | null> {
const query = api
.q('transactions')
.filter({ account: accountID })
.select({ amount: { $sum: '$amount' } });
const { data } = await api.runQuery(query);

return data?.[0]?.amount ?? null;
}

export async function getTransactions(accountID: string) {
return api.getTransactions(accountID);
}
Expand Down
5 changes: 5 additions & 0 deletions routes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,11 @@ router.get('/accounts', async (_, res) => {
res.status(200).json(accounts);
});

router.get('/accounts/:accountid', async (req, res) => {
const account = await actual.getAccount(req.params.accountid);
res.status(200).json(account);
});

router.get(
'/accounts/:accountid/transactions',
async (req, res, next) => {
Expand Down

0 comments on commit bd7e598

Please sign in to comment.