A local-first personal finance app
:sparkles: (api) ability to run bank sync via the API (#2683)
Matiss Janis Aboltinsf8352808c417
diff --git a/packages/api/methods.ts b/packages/api/methods.tsindex bc69223e26..63f23ae30d 100644--- a/packages/api/methods.ts+++ b/packages/api/methods.ts@@ -35,6 +35,10 @@ export async function sync() {return send('api/sync');}+export async function runBankSync(args?: { accountId: string }) {+ return send('api/bank-sync', args);+}+export async function batchBudgetUpdates(func) {await send('api/batch-budget-start');try {diff --git a/packages/loot-core/src/client/actions/account.ts b/packages/loot-core/src/client/actions/account.tsindex 52b41608d1..c0d89874a3 100644--- a/packages/loot-core/src/client/actions/account.ts+++ b/packages/loot-core/src/client/actions/account.ts@@ -105,7 +105,7 @@ export function syncAccounts(id?: string) {// Perform sync operationconst { errors, newTransactions, matchedTransactions, updatedAccounts } =- await send('gocardless-accounts-sync', {+ await send('accounts-bank-sync', {id: accountId,});diff --git a/packages/loot-core/src/server/api.ts b/packages/loot-core/src/server/api.tsindex 93457027fd..b49ef58f18 100644--- a/packages/loot-core/src/server/api.ts+++ b/packages/loot-core/src/server/api.ts@@ -3,6 +3,7 @@ import { getClock } from '@actual-app/crdt';import * as connection from '../platform/server/connection';import {+ getBankSyncError,getDownloadError,getSyncError,getTestKeyError,@@ -233,6 +234,17 @@ handlers['api/sync'] = async function () {}};+handlers['api/bank-sync'] = async function (args) {+ const { errors } = await handlers['accounts-bank-sync']({+ id: args?.accountId,+ });++ const [firstError] = errors;+ if (firstError) {+ throw new Error(getBankSyncError(firstError));+ }+};+handlers['api/start-import'] = async function ({ budgetName }) {// Notify UI to close budgetawait handlers['close-budget']();diff --git a/packages/loot-core/src/server/main.ts b/packages/loot-core/src/server/main.tsindex 5b0ee4f930..33d30933c5 100644--- a/packages/loot-core/src/server/main.ts+++ b/packages/loot-core/src/server/main.ts@@ -1037,7 +1037,7 @@ handlers['gocardless-create-web-token'] = async function ({}};-handlers['gocardless-accounts-sync'] = async function ({ id }) {+handlers['accounts-bank-sync'] = async function ({ id }) {const [[, userId], [, userKey]] = await asyncStorage.multiGet(['user-id','user-key',@@ -1045,8 +1045,8 @@ handlers['gocardless-accounts-sync'] = async function ({ id }) {const accounts = await db.runQuery(`SELECT a.*, b.bank_id as bankId FROM accounts aLEFT JOIN banks b ON a.bank = b.id- WHERE a.tombstone = 0 AND a.closed = 0 AND a.id = ?`,- [id],+ WHERE a.tombstone = 0 AND a.closed = 0 ${id ? 'AND a.id = ?' : ''}`,+ id ? [id] : [],true,);@@ -1059,7 +1059,7 @@ handlers['gocardless-accounts-sync'] = async function ({ id }) {const acct = accounts[i];if (acct.bankId) {try {- console.group('Bank Sync operation');+ console.group('Bank Sync operation for account:', acct.name);const res = await bankSync.syncAccount(userId,userKey,diff --git a/packages/loot-core/src/shared/errors.ts b/packages/loot-core/src/shared/errors.tsindex e081481ff2..a6489b1e8b 100644--- a/packages/loot-core/src/shared/errors.ts+++ b/packages/loot-core/src/shared/errors.ts@@ -92,6 +92,10 @@ export function getSyncError(error, id) {}}+export function getBankSyncError(error: { message?: string }) {+ return error.message || 'We had an unknown problem syncing the account.';+}+export class LazyLoadFailedError extends Error {type = 'app-init-failure';meta = {};diff --git a/packages/loot-core/src/types/api-handlers.d.ts b/packages/loot-core/src/types/api-handlers.d.tsindex d5fbd3ba5c..7be7f4119e 100644--- a/packages/loot-core/src/types/api-handlers.d.ts+++ b/packages/loot-core/src/types/api-handlers.d.ts@@ -96,6 +96,8 @@ export interface ApiHandlers {'api/sync': () => Promise<void>;+ 'api/bank-sync': (arg?: { accountId: string }) => Promise<void>;+'api/accounts-get': () => Promise<APIAccountEntity[]>;'api/account-create': (arg: { account; initialBalance? }) => Promise<string>;diff --git a/packages/loot-core/src/types/server-handlers.d.ts b/packages/loot-core/src/types/server-handlers.d.tsindex 88370eaf38..8a643803ba 100644--- a/packages/loot-core/src/types/server-handlers.d.ts+++ b/packages/loot-core/src/types/server-handlers.d.ts@@ -213,7 +213,7 @@ export interface ServerHandlers {| { error: 'failed' }>;- 'gocardless-accounts-sync': (arg: { id: string }) => Promise<{+ 'accounts-bank-sync': (arg: { id?: string }) => Promise<{errors;newTransactions;matchedTransactions;diff --git a/upcoming-release-notes/2683.md b/upcoming-release-notes/2683.mdnew file mode 100644index 0000000000..79717b7822--- /dev/null+++ b/upcoming-release-notes/2683.md@@ -0,0 +1,6 @@+---+category: Features+authors: [MatissJanis]+---++API: ability to run 3rd party bank-sync (GoCardless, SimpleFin) programmatically using `@actual-app/api`.