diff --git a/packages/desktop-client/src/accounts/accountsSlice.ts b/packages/desktop-client/src/accounts/accountsSlice.ts index e95c686dc7..1ddc2bb995 100644 --- a/packages/desktop-client/src/accounts/accountsSlice.ts +++ b/packages/desktop-client/src/accounts/accountsSlice.ts @@ -233,25 +233,41 @@ export const syncAccounts = createAppAsyncThunk( return false; } - const batchSync = !id; - - // Build an array of IDs for accounts to sync.. if no `id` provided - // then we assume that all accounts should be synced - const queriesState = getState().queries; - let accountIdsToSync = !batchSync - ? [id] - : queriesState.accounts - .filter( - ({ bank, closed, tombstone }) => !!bank && !closed && !tombstone, - ) - .sort((a, b) => - a.offbudget === b.offbudget - ? a.sort_order - b.sort_order - : a.offbudget - b.offbudget, - ) - .map(({ id }) => id); - const { setAccountsSyncing } = accountsSlice.actions; + + if (id === 'uncategorized') { + // Sync no accounts + dispatch(setAccountsSyncing({ ids: [] })); + return false; + } + + const queriesState = getState().queries; + let accountIdsToSync: string[]; + if (id === 'offbudget' || id === 'onbudget') { + const targetOffbudget = id === 'offbudget' ? 1 : 0; + accountIdsToSync = queriesState.accounts + .filter( + ({ bank, closed, tombstone, offbudget }) => + !!bank && !closed && !tombstone && offbudget === targetOffbudget, + ) + .sort((a, b) => a.sort_order - b.sort_order) + .map(({ id }) => id); + } else if (id) { + accountIdsToSync = [id]; + } else { + // Default: all accounts + accountIdsToSync = queriesState.accounts + .filter( + ({ bank, closed, tombstone }) => !!bank && !closed && !tombstone, + ) + .sort((a, b) => + a.offbudget === b.offbudget + ? a.sort_order - b.sort_order + : a.offbudget - b.offbudget, + ) + .map(({ id }) => id); + } + dispatch(setAccountsSyncing({ ids: accountIdsToSync })); // TODO: Force cast to AccountEntity. @@ -260,7 +276,9 @@ export const syncAccounts = createAppAsyncThunk( 'accounts-get', )) as unknown as AccountEntity[]; const simpleFinAccounts = accountsData.filter( - a => a.account_sync_source === 'simpleFin', + a => + a.account_sync_source === 'simpleFin' && + accountIdsToSync.includes(a.id), ); let isSyncSuccess = false; @@ -268,7 +286,7 @@ export const syncAccounts = createAppAsyncThunk( const matchedTransactions: Array = []; const updatedAccounts: Array = []; - if (batchSync && simpleFinAccounts.length > 0) { + if (simpleFinAccounts.length > 0) { console.log('Using SimpleFin batch sync'); const res = await send('simplefin-batch-sync', { diff --git a/packages/desktop-client/src/app/appSlice.ts b/packages/desktop-client/src/app/appSlice.ts index 5f42f95999..7bb05b1955 100644 --- a/packages/desktop-client/src/app/appSlice.ts +++ b/packages/desktop-client/src/app/appSlice.ts @@ -121,7 +121,9 @@ export const syncAndDownload = createAppAsyncThunk( return { error: syncState.error }; } - const hasDownloaded = await dispatch(syncAccounts({ id: accountId })); + const hasDownloaded = await dispatch( + syncAccounts({ id: accountId }), + ).unwrap(); if (hasDownloaded) { // Sync again afterwards if new transactions were created diff --git a/packages/desktop-client/src/components/accounts/Account.tsx b/packages/desktop-client/src/components/accounts/Account.tsx index 72eb8d2dc8..892d5efb1e 100644 --- a/packages/desktop-client/src/components/accounts/Account.tsx +++ b/packages/desktop-client/src/components/accounts/Account.tsx @@ -583,7 +583,7 @@ class AccountInternal extends PureComponent< const account = this.props.accounts.find(acct => acct.id === accountId); await this.props.dispatch( - syncAndDownload({ accountId: account ? account.id : undefined }), + syncAndDownload({ accountId: account ? account.id : accountId }), ); }; diff --git a/upcoming-release-notes/5124.md b/upcoming-release-notes/5124.md new file mode 100644 index 0000000000..7ac5428c0c --- /dev/null +++ b/upcoming-release-notes/5124.md @@ -0,0 +1,6 @@ +--- +category: Bugfix +authors: [MatissJanis] +--- + +Refactor account synchronization logic in accountsSlice and improve syncAndDownload handling