diff --git a/packages/import-ynab4/importer.js b/packages/import-ynab4/importer.js index c98a17f7242c308666ce5d08ca5767228585aa7a..2e77770d47407b25785e542bee48b6701ab707e1 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); } })