Skip to content
Snippets Groups Projects
Unverified Commit ab4639f4 authored by Robert Dyer's avatar Robert Dyer Committed by GitHub
Browse files

API: add getBudgets() methods to list all budgets in the local cache or remote server. (#2928)

parent aa3cbd88
No related branches found
No related tags found
No related merge requests found
...@@ -58,6 +58,19 @@ describe('API CRUD operations', () => { ...@@ -58,6 +58,19 @@ describe('API CRUD operations', () => {
await api.loadBudget(budgetName); await api.loadBudget(budgetName);
}); });
// api: getBudgets
test('getBudgets', async () => {
const budgets = await api.getBudgets();
expect(budgets).toEqual(
expect.arrayContaining([
expect.objectContaining({
id: 'test-budget',
name: 'Default Test Db',
}),
]),
);
});
// apis: getCategoryGroups, createCategoryGroup, updateCategoryGroup, deleteCategoryGroup // apis: getCategoryGroups, createCategoryGroup, updateCategoryGroup, deleteCategoryGroup
test('CategoryGroups: successfully update category groups', async () => { test('CategoryGroups: successfully update category groups', async () => {
const month = '2023-10'; const month = '2023-10';
......
...@@ -31,6 +31,10 @@ export async function downloadBudget(syncId, { password }: { password? } = {}) { ...@@ -31,6 +31,10 @@ export async function downloadBudget(syncId, { password }: { password? } = {}) {
return send('api/download-budget', { syncId, password }); return send('api/download-budget', { syncId, password });
} }
export async function getBudgets() {
return send('api/get-budgets');
}
export async function sync() { export async function sync() {
return send('api/sync'); return send('api/sync');
} }
......
import { Budget } from '../types/budget';
import type { import type {
AccountEntity, AccountEntity,
CategoryEntity, CategoryEntity,
...@@ -5,6 +6,7 @@ import type { ...@@ -5,6 +6,7 @@ import type {
PayeeEntity, PayeeEntity,
} from '../types/models'; } from '../types/models';
import { RemoteFile } from './cloud-storage';
import * as models from './models'; import * as models from './models';
export type APIAccountEntity = Pick<AccountEntity, 'id' | 'name'> & { export type APIAccountEntity = Pick<AccountEntity, 'id' | 'name'> & {
...@@ -114,3 +116,39 @@ export const payeeModel = { ...@@ -114,3 +116,39 @@ export const payeeModel = {
return payee as PayeeEntity; return payee as PayeeEntity;
}, },
}; };
export type APIFileEntity = Omit<RemoteFile, 'deleted' | 'fileId'> & {
id?: string;
cloudFileId: string;
state?: 'remote';
};
export const remoteFileModel = {
toExternal(file: RemoteFile): APIFileEntity | null {
if (file.deleted) {
return null;
}
return {
cloudFileId: file.fileId,
state: 'remote',
groupId: file.groupId,
name: file.name,
encryptKeyId: file.encryptKeyId,
hasKey: file.hasKey,
};
},
fromExternal(file: APIFileEntity) {
return { deleted: false, fileId: file.cloudFileId, ...file } as RemoteFile;
},
};
export const budgetModel = {
toExternal(file: Budget): APIFileEntity {
return file as APIFileEntity;
},
fromExternal(file: APIFileEntity) {
return file as Budget;
},
};
...@@ -22,9 +22,11 @@ import { ServerHandlers } from '../types/server-handlers'; ...@@ -22,9 +22,11 @@ import { ServerHandlers } from '../types/server-handlers';
import { addTransactions } from './accounts/sync'; import { addTransactions } from './accounts/sync';
import { import {
accountModel, accountModel,
budgetModel,
categoryModel, categoryModel,
categoryGroupModel, categoryGroupModel,
payeeModel, payeeModel,
remoteFileModel,
} from './api-models'; } from './api-models';
import { runQuery as aqlQuery } from './aql'; import { runQuery as aqlQuery } from './aql';
import * as cloudStorage from './cloud-storage'; import * as cloudStorage from './cloud-storage';
...@@ -226,6 +228,15 @@ handlers['api/download-budget'] = async function ({ syncId, password }) { ...@@ -226,6 +228,15 @@ handlers['api/download-budget'] = async function ({ syncId, password }) {
await handlers['load-budget']({ id: result.id }); await handlers['load-budget']({ id: result.id });
}; };
handlers['api/get-budgets'] = async function () {
const budgets = await handlers['get-budgets']();
const files = (await handlers['get-remote-files']()) || [];
return [
...budgets.map(file => budgetModel.toExternal(file)),
...files.map(file => remoteFileModel.toExternal(file)).filter(file => file),
];
};
handlers['api/sync'] = async function () { handlers['api/sync'] = async function () {
const { id } = prefs.getPrefs(); const { id } = prefs.getPrefs();
const result = await handlers['sync-budget'](); const result = await handlers['sync-budget']();
......
...@@ -3,6 +3,7 @@ import type { ...@@ -3,6 +3,7 @@ import type {
APIAccountEntity, APIAccountEntity,
APICategoryEntity, APICategoryEntity,
APICategoryGroupEntity, APICategoryGroupEntity,
APIFileEntity,
APIPayeeEntity, APIPayeeEntity,
} from '../server/api-models'; } from '../server/api-models';
...@@ -23,6 +24,8 @@ export interface ApiHandlers { ...@@ -23,6 +24,8 @@ export interface ApiHandlers {
password?: string; password?: string;
}) => Promise<void>; }) => Promise<void>;
'api/get-budgets': () => Promise<APIFileEntity[]>;
'api/start-import': (arg: { budgetName: string }) => Promise<void>; 'api/start-import': (arg: { budgetName: string }) => Promise<void>;
'api/finish-import': () => Promise<void>; 'api/finish-import': () => Promise<void>;
......
---
category: Enhancements
authors: [psybers]
---
API: add getBudgets() method to list all local/remote budgets.
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment