Skip to content
Snippets Groups Projects
Unverified Commit 54559478 authored by Rian McGuire's avatar Rian McGuire Committed by GitHub
Browse files

Fix some YNAB4 importer bugs (#88)

parent daf677e8
No related branches found
No related tags found
No related merge requests found
...@@ -80,8 +80,10 @@ function getCurrentMonth() { ...@@ -80,8 +80,10 @@ function getCurrentMonth() {
// Importer // Importer
async function importAccounts(data, entityIdMap) { async function importAccounts(data, entityIdMap) {
const accounts = sortByKey(data.accounts, 'sortableIndex');
return Promise.all( return Promise.all(
data.accounts.map(async account => { accounts.map(async account => {
if (!account.isTombstone) { if (!account.isTombstone) {
const id = await actual.createAccount({ const id = await actual.createAccount({
type: mapAccountType(account.accountType), type: mapAccountType(account.accountType),
...@@ -182,6 +184,12 @@ async function importTransactions(data, entityIdMap) { ...@@ -182,6 +184,12 @@ async function importTransactions(data, entityIdMap) {
// reliably resolve transfers // reliably resolve transfers
for (let transaction of data.transactions) { for (let transaction of data.transactions) {
entityIdMap.set(transaction.entityId, uuid.v4()); entityIdMap.set(transaction.entityId, uuid.v4());
if (transaction.subTransactions) {
for (let subTransaction of transaction.subTransactions) {
entityIdMap.set(subTransaction.entityId, uuid.v4());
}
}
} }
let sortOrder = 1; let sortOrder = 1;
...@@ -198,17 +206,22 @@ async function importTransactions(data, entityIdMap) { ...@@ -198,17 +206,22 @@ async function importTransactions(data, entityIdMap) {
} }
let id = entityIdMap.get(transaction.entityId); let id = entityIdMap.get(transaction.entityId);
let transferId =
entityIdMap.get(transaction.transferTransactionId) || null; function transferProperties(t) {
let transferId =
let payee = null; entityIdMap.get(t.transferTransactionId) || null;
if (transferId) {
payee = payees.find( let payee = null;
p => if (transferId) {
p.transfer_acct === entityIdMap.get(transaction.targetAccountId) payee = payees.find(
).id; p =>
} else { p.transfer_acct === entityIdMap.get(t.targetAccountId)
payee = entityIdMap.get(transaction.payeeId); ).id;
} else {
payee = entityIdMap.get(t.payeeId);
}
return { transfer_id: transferId, payee }
} }
let newTransaction = { let newTransaction = {
...@@ -219,8 +232,7 @@ async function importTransactions(data, entityIdMap) { ...@@ -219,8 +232,7 @@ async function importTransactions(data, entityIdMap) {
: getCategory(transaction.categoryId), : getCategory(transaction.categoryId),
date: transaction.date, date: transaction.date,
notes: transaction.memo || null, notes: transaction.memo || null,
payee, ...transferProperties(transaction),
transfer_id: transferId
}; };
newTransaction.subtransactions = newTransaction.subtransactions =
...@@ -228,8 +240,10 @@ async function importTransactions(data, entityIdMap) { ...@@ -228,8 +240,10 @@ async function importTransactions(data, entityIdMap) {
transaction.subTransactions.map((t, i) => { transaction.subTransactions.map((t, i) => {
return { return {
amount: amountToInteger(t.amount), amount: amountToInteger(t.amount),
category: getCategory(t.categoryId) category: getCategory(t.categoryId),
}; notes: t.memo || null,
...transferProperties(t),
};
}); });
return newTransaction; return newTransaction;
...@@ -266,12 +280,8 @@ function fillInBudgets(data, categoryBudgets) { ...@@ -266,12 +280,8 @@ function fillInBudgets(data, categoryBudgets) {
async function importBudgets(data, entityIdMap) { async function importBudgets(data, entityIdMap) {
let budgets = sortByKey(data.monthlyBudgets, 'month'); let budgets = sortByKey(data.monthlyBudgets, 'month');
let earliestMonth = monthFromDate(budgets[0].month);
let currentMonth = getCurrentMonth();
await actual.batchBudgetUpdates(async () => { await actual.batchBudgetUpdates(async () => {
const carryoverFlags = {};
for (let budget of budgets) { for (let budget of budgets) {
let filled = fillInBudgets( let filled = fillInBudgets(
data, data,
...@@ -290,17 +300,8 @@ async function importBudgets(data, entityIdMap) { ...@@ -290,17 +300,8 @@ async function importBudgets(data, entityIdMap) {
await actual.setBudgetAmount(month, catId, amount); await actual.setBudgetAmount(month, catId, amount);
if (catBudget.overspendingHandling === 'AffectsBuffer') { if (catBudget.overspendingHandling === 'AffectsBuffer') {
// Turn off the carryover flag so it doesn't propagate await actual.setBudgetCarryover(month, catId, false);
// to future months } else if (catBudget.overspendingHandling === 'Confined') {
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, true); await actual.setBudgetCarryover(month, catId, true);
} }
}) })
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment