Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add getAccountBalance() API. #2930

Merged
merged 6 commits into from
Jul 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion packages/api/methods.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,7 @@ describe('API CRUD operations', () => {
);
});

//apis: createAccount, getAccounts, updateAccount, closeAccount, deleteAccount, reopenAccount
//apis: createAccount, getAccounts, updateAccount, closeAccount, deleteAccount, reopenAccount, getAccountBalance
test('Accounts: successfully complete account operators', async () => {
const accountId1 = await api.createAccount(
{ name: 'test-account1', offbudget: true },
Expand All @@ -272,6 +272,9 @@ describe('API CRUD operations', () => {
]),
);

expect(await api.getAccountBalance(accountId1)).toEqual(1000);
expect(await api.getAccountBalance(accountId2)).toEqual(0);

await api.updateAccount(accountId1, { offbudget: false });
await api.closeAccount(accountId1, accountId2, null);
await api.deleteAccount(accountId2);
Expand Down Expand Up @@ -569,6 +572,11 @@ describe('API CRUD operations', () => {
});
expect(addResult).toBe('ok');

expect(await api.getAccountBalance(accountId)).toEqual(200);
expect(
await api.getAccountBalance(accountId, new Date(2023, 10, 2)),
).toEqual(0);

// confirm added transactions exist
let transactions = await api.getTransactions(
accountId,
Expand Down
4 changes: 4 additions & 0 deletions packages/api/methods.ts
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,10 @@ export function deleteAccount(id) {
return send('api/account-delete', { id });
}

export function getAccountBalance(id, cutoff?) {
return send('api/account-balance', { id, cutoff });
}

export function getCategoryGroups() {
return send('api/category-groups-get');
}
Expand Down
8 changes: 8 additions & 0 deletions packages/loot-core/src/server/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -538,6 +538,14 @@ handlers['api/account-delete'] = withMutation(async function ({ id }) {
return handlers['account-close']({ id, forced: true });
});

handlers['api/account-balance'] = withMutation(async function ({
id,
cutoff = new Date(),
}) {
checkFileOpen();
return handlers['account-balance']({ id, cutoff });
});

handlers['api/categories-get'] = async function ({
grouped,
}: { grouped? } = {}) {
Expand Down
9 changes: 9 additions & 0 deletions packages/loot-core/src/server/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import { logger } from '../platform/server/log';
import * as sqlite from '../platform/server/sqlite';
import { isNonProductionEnvironment } from '../shared/environment';
import * as monthUtils from '../shared/months';
import { dayFromDate } from '../shared/months';
import { q, Query } from '../shared/query';
import { amountToInteger, stringToInteger } from '../shared/util';
import { type Budget } from '../types/budget';
Expand Down Expand Up @@ -578,6 +579,14 @@ handlers['accounts-get'] = async function () {
return db.getAccounts();
};

handlers['account-balance'] = async function ({ id, cutoff }) {
const { balance } = await db.first(
'SELECT sum(amount) as balance FROM transactions WHERE acct = ? AND isParent = 0 AND tombstone = 0 AND date <= ?',
[id, db.toDateRepr(dayFromDate(cutoff))],
);
return balance ? balance : 0;
};

handlers['account-properties'] = async function ({ id }) {
const { balance } = await db.first(
'SELECT sum(amount) as balance FROM transactions WHERE acct = ? AND isParent = 0 AND tombstone = 0',
Expand Down
5 changes: 5 additions & 0 deletions packages/loot-core/src/types/api-handlers.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,11 @@ export interface ApiHandlers {

'api/account-delete': (arg: { id }) => Promise<unknown>;

'api/account-balance': (arg: {
id: string;
cutoff?: Date;
}) => Promise<number>;

'api/categories-get': (arg: {
grouped;
}) => Promise<Array<APICategoryGroupEntity | APICategoryEntity>>;
Expand Down
6 changes: 6 additions & 0 deletions upcoming-release-notes/2930.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
category: Features
authors: [psybers]
---

Add getAccountBalance() API.
Loading