Skip to content
Snippets Groups Projects
Unverified Commit 540c4109 authored by Stefan Hall's avatar Stefan Hall Committed by GitHub
Browse files

fixed:issue->#1968 Import from nYNAB fails with unknown error (#2191)


* Allow case insensitive ynab5 import for special 'starting balance' payee

* set upcoming release number to related github issue

* extract string comparison into separate function

and reuse when checking starting balance/s on ynab4 import

* make all category group checks case insensitive

when importing from ynab5 to make the check strategy consistent when importing from ynab5

* extract findById into sreusable function

to 'simplify' usage

* Add null check

Co-authored-by: default avatarJoel Jeremy Marquez <joeljeremy.marquez@gmail.com>

---------

Co-authored-by: default avatarJoel Jeremy Marquez <joeljeremy.marquez@gmail.com>
parent e2053448
No related branches found
No related tags found
No related merge requests found
...@@ -39,25 +39,26 @@ async function importCategories( ...@@ -39,25 +39,26 @@ async function importCategories(
// so it's already handled. // so it's already handled.
const categories = await actual.getCategories(); const categories = await actual.getCategories();
const incomeCatId = categories.find(cat => cat.name === 'Income').id; const incomeCatId = findIdByName(categories, 'Income');
const ynabIncomeCategories = ['To be Budgeted', 'Inflow: Ready to Assign']; const ynabIncomeCategories = ['To be Budgeted', 'Inflow: Ready to Assign'];
function checkSpecialCat(cat) { function checkSpecialCat(cat) {
if ( if (
cat.category_group_id === cat.category_group_id ===
data.category_groups.find( findIdByName(data.category_groups, 'Internal Master Category')
group => group.name === 'Internal Master Category',
).id
) { ) {
if (ynabIncomeCategories.includes(cat.name)) { if (
ynabIncomeCategories.some(ynabIncomeCategory =>
equalsIgnoreCase(cat.name, ynabIncomeCategory),
)
) {
return 'income'; return 'income';
} else { } else {
return 'internal'; return 'internal';
} }
} else if ( } else if (
cat.category_group_id === cat.category_group_id ===
data.category_groups.find(group => group.name === 'Credit Card Payments') findIdByName(data.category_groups, 'Credit Card Payments')
.id
) { ) {
return 'creditCard'; return 'creditCard';
} }
...@@ -70,8 +71,8 @@ async function importCategories( ...@@ -70,8 +71,8 @@ async function importCategories(
let groupId; let groupId;
// Ignores internal category and credit cards // Ignores internal category and credit cards
if ( if (
group.name !== 'Internal Master Category' && !equalsIgnoreCase(group.name, 'Internal Master Category') &&
group.name !== 'Credit Card Payments' !equalsIgnoreCase(group.name, 'Credit Card Payments')
) { ) {
groupId = await actual.createCategoryGroup({ groupId = await actual.createCategoryGroup({
name: group.name, name: group.name,
...@@ -132,13 +133,10 @@ async function importTransactions( ...@@ -132,13 +133,10 @@ async function importTransactions(
) { ) {
const payees = await actual.getPayees(); const payees = await actual.getPayees();
const categories = await actual.getCategories(); const categories = await actual.getCategories();
const incomeCatId = categories.find(cat => cat.name === 'Income').id; const incomeCatId = findIdByName(categories, 'Income');
const startingBalanceCatId = categories.find( const startingBalanceCatId = findIdByName(categories, 'Starting Balances'); //better way to do it?
cat => cat.name === 'Starting Balances',
).id; //better way to do it? const startingPayeeYNAB = findIdByName(data.payees, 'Starting Balance');
const startingPayeeYNAB = data.payees.find(
payee => payee.name === 'Starting Balance',
).id;
const transactionsGrouped = groupBy(data.transactions, 'account_id'); const transactionsGrouped = groupBy(data.transactions, 'account_id');
const subtransactionsGrouped = groupBy( const subtransactionsGrouped = groupBy(
...@@ -258,12 +256,14 @@ async function importBudgets( ...@@ -258,12 +256,14 @@ async function importBudgets(
const budgets = sortByKey(data.months, 'month'); const budgets = sortByKey(data.months, 'month');
const internalCatIdYnab = data.category_groups.find( const internalCatIdYnab = findIdByName(
group => group.name === 'Internal Master Category', data.category_groups,
).id; 'Internal Master Category',
const creditcardCatIdYnab = data.category_groups.find( );
group => group.name === 'Credit Card Payments', const creditcardCatIdYnab = findIdByName(
).id; data.category_groups,
'Credit Card Payments',
);
await actual.batchBudgetUpdates(async () => { await actual.batchBudgetUpdates(async () => {
for (const budget of budgets) { for (const budget of budgets) {
...@@ -327,3 +327,19 @@ export function parseFile(buffer: Buffer): YNAB5.Budget { ...@@ -327,3 +327,19 @@ export function parseFile(buffer: Buffer): YNAB5.Budget {
export function getBudgetName(_filepath: string, data: YNAB5.Budget) { export function getBudgetName(_filepath: string, data: YNAB5.Budget) {
return data.budget_name || data.name; return data.budget_name || data.name;
} }
function equalsIgnoreCase(stringa: string, stringb: string): boolean {
return (
stringa.localeCompare(stringb, undefined, {
sensitivity: 'base',
}) === 0
);
}
function findByNameIgnoreCase(categories: YNAB5.CategoryGroup[], name: string) {
return categories.find(cat => equalsIgnoreCase(cat.name, name));
}
function findIdByName(categories: YNAB5.CategoryGroup[], name: string) {
return findByNameIgnoreCase(categories, name)?.id;
}
---
category: Bugfix
authors: [Marethyu1]
---
Allow case insensitive ynab5 import for special 'starting balance' payee
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