From 54559478d06128100414cceb58a10428f2dca5b2 Mon Sep 17 00:00:00 2001 From: Rian McGuire <rian@rian.id.au> Date: Sat, 21 Jan 2023 21:51:11 +1100 Subject: [PATCH] Fix some YNAB4 importer bugs (#88) --- packages/import-ynab4/importer.js | 63 ++++++++++++++++--------------- 1 file changed, 32 insertions(+), 31 deletions(-) diff --git a/packages/import-ynab4/importer.js b/packages/import-ynab4/importer.js index c98a17f72..2e77770d4 100644 --- a/packages/import-ynab4/importer.js +++ b/packages/import-ynab4/importer.js @@ -80,8 +80,10 @@ function getCurrentMonth() { // Importer async function importAccounts(data, entityIdMap) { + const accounts = sortByKey(data.accounts, 'sortableIndex'); + return Promise.all( - data.accounts.map(async account => { + accounts.map(async account => { if (!account.isTombstone) { const id = await actual.createAccount({ type: mapAccountType(account.accountType), @@ -182,6 +184,12 @@ async function importTransactions(data, entityIdMap) { // reliably resolve transfers for (let transaction of data.transactions) { entityIdMap.set(transaction.entityId, uuid.v4()); + + if (transaction.subTransactions) { + for (let subTransaction of transaction.subTransactions) { + entityIdMap.set(subTransaction.entityId, uuid.v4()); + } + } } let sortOrder = 1; @@ -198,17 +206,22 @@ async function importTransactions(data, entityIdMap) { } let id = entityIdMap.get(transaction.entityId); - let transferId = - entityIdMap.get(transaction.transferTransactionId) || null; - - let payee = null; - if (transferId) { - payee = payees.find( - p => - p.transfer_acct === entityIdMap.get(transaction.targetAccountId) - ).id; - } else { - payee = entityIdMap.get(transaction.payeeId); + + function transferProperties(t) { + let transferId = + entityIdMap.get(t.transferTransactionId) || null; + + let payee = null; + if (transferId) { + payee = payees.find( + p => + p.transfer_acct === entityIdMap.get(t.targetAccountId) + ).id; + } else { + payee = entityIdMap.get(t.payeeId); + } + + return { transfer_id: transferId, payee } } let newTransaction = { @@ -219,8 +232,7 @@ async function importTransactions(data, entityIdMap) { : getCategory(transaction.categoryId), date: transaction.date, notes: transaction.memo || null, - payee, - transfer_id: transferId + ...transferProperties(transaction), }; newTransaction.subtransactions = @@ -228,8 +240,10 @@ async function importTransactions(data, entityIdMap) { transaction.subTransactions.map((t, i) => { return { amount: amountToInteger(t.amount), - category: getCategory(t.categoryId) - }; + category: getCategory(t.categoryId), + notes: t.memo || null, + ...transferProperties(t), + }; }); return newTransaction; @@ -266,12 +280,8 @@ function fillInBudgets(data, categoryBudgets) { async function importBudgets(data, entityIdMap) { let budgets = sortByKey(data.monthlyBudgets, 'month'); - let earliestMonth = monthFromDate(budgets[0].month); - let currentMonth = getCurrentMonth(); await actual.batchBudgetUpdates(async () => { - const carryoverFlags = {}; - for (let budget of budgets) { let filled = fillInBudgets( data, @@ -290,17 +300,8 @@ async function importBudgets(data, entityIdMap) { await actual.setBudgetAmount(month, catId, amount); if (catBudget.overspendingHandling === 'AffectsBuffer') { - // Turn off the carryover flag so it doesn't propagate - // to future months - carryoverFlags[catId] = false; - } else if ( - catBudget.overspendingHandling === 'Confined' || - carryoverFlags[catId] - ) { - // Overspending has switched to carryover, set the - // flag so it propagates to future months - carryoverFlags[catId] = true; - + await actual.setBudgetCarryover(month, catId, false); + } else if (catBudget.overspendingHandling === 'Confined') { await actual.setBudgetCarryover(month, catId, true); } }) -- GitLab