From c8a741a9543898e36eaa17b498cc0a03a714dd75 Mon Sep 17 00:00:00 2001 From: Matiss Janis Aboltins Date: Wed, 3 Apr 2024 17:39:11 +0100 Subject: [PATCH] Rename external -> bankSync and a bit of cleanup --- .../loot-core/src/server/accounts/sync.ts | 94 +++++-------------- packages/loot-core/src/server/main.ts | 48 ---------- .../loot-core/src/types/server-handlers.d.ts | 7 -- 3 files changed, 25 insertions(+), 124 deletions(-) diff --git a/packages/loot-core/src/server/accounts/sync.ts b/packages/loot-core/src/server/accounts/sync.ts index d1ff9fa3385..3c8c44639fb 100644 --- a/packages/loot-core/src/server/accounts/sync.ts +++ b/packages/loot-core/src/server/accounts/sync.ts @@ -328,7 +328,7 @@ async function normalizeTransactions( return { normalized, payeesToCreate }; } -async function normalizeExternalTransactions(transactions, acctId) { +async function normalizeBankSyncTransactions(transactions, acctId) { const payeesToCreate = new Map(); const normalized = []; @@ -440,7 +440,7 @@ async function createNewPayees(payeesToCreate, addsAndUpdates) { export async function reconcileTransactions( acctId, transactions, - isExternalAccount = false, + isBankSyncAccount = false, ) { console.log('Performing transaction reconciliation'); @@ -448,8 +448,8 @@ export async function reconcileTransactions( const updated = []; const added = []; - const transactionNormalization = isExternalAccount - ? normalizeExternalTransactions + const transactionNormalization = isBankSyncAccount + ? normalizeBankSyncTransactions : normalizeTransactions; const { normalized, payeesToCreate } = await transactionNormalization( @@ -583,7 +583,7 @@ export async function reconcileTransactions( // Update the transaction const updates = { - ...(isExternalAccount ? {} : { date: trans.date }), + ...(isBankSyncAccount ? {} : { date: trans.date }), imported_id: trans.imported_id || null, payee: existing.payee || trans.payee || null, category: existing.category || trans.category || null, @@ -712,9 +712,6 @@ export async function syncAccount( ); const acctRow = await db.select('accounts', id); - const isExternalAccount = - acctRow.account_sync_source === 'simpleFin' || - acctRow.account_sync_source === 'goCardless'; if (latestTransaction) { const startingTransaction = await db.first( @@ -786,76 +783,39 @@ export async function syncAccount( })); return runMutator(async () => { - const result = await reconcileTransactions( - id, - transactions, - isExternalAccount, - ); + const result = await reconcileTransactions(id, transactions, true); await updateAccountBalance(id, accountBalance); return result; }); } else { - let balanceToUse: number; - let transactions; - - if (isExternalAccount) { - let download; - - // Otherwise, download transaction for the past 90 days - const startingDay = monthUtils.subDays(monthUtils.currentDay(), 90); + let download; - if (acctRow.account_sync_source === 'simpleFin') { - download = await downloadSimpleFinTransactions(acctId, startingDay); - } else if (acctRow.account_sync_source === 'goCardless') { - download = await downloadGoCardlessTransactions( - userId, - userKey, - acctId, - bankId, - startingDay, - ); - } + // Otherwise, download transaction for the past 90 days + const startingDay = monthUtils.subDays(monthUtils.currentDay(), 90); - transactions = download.transactions; - balanceToUse = download.startingBalance; - - if (acctRow.account_sync_source === 'simpleFin') { - const currentBalance = download.startingBalance; - const previousBalance = transactions.reduce((total, trans) => { - return ( - total - parseInt(trans.transactionAmount.amount.replace('.', '')) - ); - }, currentBalance); - balanceToUse = previousBalance; - } - } else { - // Otherwise, download transaction for the last few days if it's an - // on-budget account, or for the past 30 days if off-budget - const startingDay = monthUtils.subDays( - monthUtils.currentDay(), - acctRow.offbudget === 0 ? 1 : 30, - ); - - const download = await downloadTransactions( + if (acctRow.account_sync_source === 'simpleFin') { + download = await downloadSimpleFinTransactions(acctId, startingDay); + } else if (acctRow.account_sync_source === 'goCardless') { + download = await downloadGoCardlessTransactions( userId, userKey, acctId, bankId, - dateFns.format(dateFns.parseISO(startingDay), 'yyyy-MM-dd'), + startingDay, ); + } - transactions = download.transactions; - - // We need to add a transaction that represents the starting - // balance for everything to balance out. In order to get balance - // before the first imported transaction, we need to get the - // current balance from the accounts table and subtract all the - // imported transactions. - const currentBalance = acctRow.balance_current; + const transactions = download.transactions; + let balanceToUse = download.startingBalance; - balanceToUse = transactions.reduce((total, trans) => { - return total - trans.amount; + if (acctRow.account_sync_source === 'simpleFin') { + const currentBalance = download.startingBalance; + const previousBalance = transactions.reduce((total, trans) => { + return ( + total - parseInt(trans.transactionAmount.amount.replace('.', '')) + ); }, currentBalance); + balanceToUse = previousBalance; } const oldestTransaction = transactions[transactions.length - 1]; @@ -878,11 +838,7 @@ export async function syncAccount( starting_balance_flag: true, }); - const result = await reconcileTransactions( - id, - transactions, - isExternalAccount, - ); + const result = await reconcileTransactions(id, transactions, true); return { ...result, added: [initialId, ...result.added], diff --git a/packages/loot-core/src/server/main.ts b/packages/loot-core/src/server/main.ts index 3342871763e..f648a0b19a1 100644 --- a/packages/loot-core/src/server/main.ts +++ b/packages/loot-core/src/server/main.ts @@ -610,54 +610,6 @@ handlers['account-properties'] = async function ({ id }) { return { balance: balance || 0, numTransactions: count }; }; -handlers['accounts-link'] = async function ({ - institution, - publicToken, - accountId, - upgradingId, -}) { - const bankId = await link.handoffPublicToken(institution, publicToken); - - const [[, userId], [, userKey]] = await asyncStorage.multiGet([ - 'user-id', - 'user-key', - ]); - - // Get all the available accounts and find the selected one - const accounts = await bankSync.getGoCardlessAccounts( - userId, - userKey, - bankId, - ); - const account = accounts.find(acct => acct.account_id === accountId); - - await db.update('accounts', { - id: upgradingId, - account_id: account.account_id, - official_name: account.official_name, - balance_current: amountToInteger(account.balances.current), - balance_available: amountToInteger(account.balances.available), - balance_limit: amountToInteger(account.balances.limit), - mask: account.mask, - bank: bankId, - }); - - await bankSync.syncAccount( - userId, - userKey, - upgradingId, - account.account_id, - bankId, - ); - - connection.send('sync-event', { - type: 'success', - tables: ['transactions'], - }); - - return 'ok'; -}; - handlers['gocardless-accounts-link'] = async function ({ requisitionId, account, diff --git a/packages/loot-core/src/types/server-handlers.d.ts b/packages/loot-core/src/types/server-handlers.d.ts index eff46e2f601..7704b7582a8 100644 --- a/packages/loot-core/src/types/server-handlers.d.ts +++ b/packages/loot-core/src/types/server-handlers.d.ts @@ -149,13 +149,6 @@ export interface ServerHandlers { id; }) => Promise<{ balance: number; numTransactions: number }>; - 'accounts-link': (arg: { - institution; - publicToken; - accountId; - upgradingId; - }) => Promise<'ok'>; - 'gocardless-accounts-link': (arg: { requisitionId; account;