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

This commit is contained in:
Robert Dyer
2024-07-11 03:27:32 -05:00
committed by GitHub
parent aa3cbd881b
commit ab4639f48f
6 changed files with 75 additions and 0 deletions

View File

@@ -58,6 +58,19 @@ describe('API CRUD operations', () => {
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
test('CategoryGroups: successfully update category groups', async () => {
const month = '2023-10';

View File

@@ -31,6 +31,10 @@ export async function downloadBudget(syncId, { password }: { password? } = {}) {
return send('api/download-budget', { syncId, password });
}
export async function getBudgets() {
return send('api/get-budgets');
}
export async function sync() {
return send('api/sync');
}

View File

@@ -1,3 +1,4 @@
import { Budget } from '../types/budget';
import type {
AccountEntity,
CategoryEntity,
@@ -5,6 +6,7 @@ import type {
PayeeEntity,
} from '../types/models';
import { RemoteFile } from './cloud-storage';
import * as models from './models';
export type APIAccountEntity = Pick<AccountEntity, 'id' | 'name'> & {
@@ -114,3 +116,39 @@ export const payeeModel = {
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;
},
};

View File

@@ -22,9 +22,11 @@ import { ServerHandlers } from '../types/server-handlers';
import { addTransactions } from './accounts/sync';
import {
accountModel,
budgetModel,
categoryModel,
categoryGroupModel,
payeeModel,
remoteFileModel,
} from './api-models';
import { runQuery as aqlQuery } from './aql';
import * as cloudStorage from './cloud-storage';
@@ -226,6 +228,15 @@ handlers['api/download-budget'] = async function ({ syncId, password }) {
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 () {
const { id } = prefs.getPrefs();
const result = await handlers['sync-budget']();

View File

@@ -3,6 +3,7 @@ import type {
APIAccountEntity,
APICategoryEntity,
APICategoryGroupEntity,
APIFileEntity,
APIPayeeEntity,
} from '../server/api-models';
@@ -23,6 +24,8 @@ export interface ApiHandlers {
password?: string;
}) => Promise<void>;
'api/get-budgets': () => Promise<APIFileEntity[]>;
'api/start-import': (arg: { budgetName: string }) => Promise<void>;
'api/finish-import': () => Promise<void>;

View File

@@ -0,0 +1,6 @@
---
category: Enhancements
authors: [psybers]
---
API: add getBudgets() method to list all local/remote budgets.