Skip to content

Commit

Permalink
extract findById into sreusable function
Browse files Browse the repository at this point in the history
to 'simplify' usage
  • Loading branch information
Marethyu1 committed Jan 11, 2024
1 parent 4388291 commit 10e6654
Showing 1 changed file with 23 additions and 24 deletions.
47 changes: 23 additions & 24 deletions packages/loot-core/src/server/importers/ynab5.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,16 +39,13 @@ async function importCategories(
// so it's already handled.

const categories = await actual.getCategories();
const incomeCatId = categories.find(cat =>
equalsIgnoreCase(cat.name, 'Income'),
).id;
const incomeCatId = findIdByName(categories, 'Income');
const ynabIncomeCategories = ['To be Budgeted', 'Inflow: Ready to Assign'];

function checkSpecialCat(cat) {
if (
data.category_groups.find(group =>
equalsIgnoreCase(group.name, 'Internal Master Category'),
).id
cat.category_group_id ===
findIdByName(data.category_groups, 'Internal Master Category')
) {
if (
ynabIncomeCategories.some(ynabIncomeCategory =>
Expand All @@ -61,9 +58,7 @@ async function importCategories(
}
} else if (
cat.category_group_id ===
data.category_groups.find(group =>
equalsIgnoreCase(group.name, 'Credit Card Payments'),
).id
findIdByName(data.category_groups, 'Credit Card Payments')
) {
return 'creditCard';
}
Expand Down Expand Up @@ -138,16 +133,10 @@ async function importTransactions(
) {
const payees = await actual.getPayees();
const categories = await actual.getCategories();
const incomeCatId = categories.find(cat =>
equalsIgnoreCase(cat.name, 'Income'),
).id;
const startingBalanceCatId = categories.find(cat =>
equalsIgnoreCase(cat.name, 'Starting Balances'),
).id; //better way to do it?
const incomeCatId = findIdByName(categories, 'Income');
const startingBalanceCatId = findIdByName(categories, 'Starting Balances'); //better way to do it?

const startingPayeeYNAB = data.payees.find(payee =>
equalsIgnoreCase(payee.name, 'Starting Balance'),
).id;
const startingPayeeYNAB = findIdByName(data.payees, 'Starting Balance');

const transactionsGrouped = groupBy(data.transactions, 'account_id');
const subtransactionsGrouped = groupBy(
Expand Down Expand Up @@ -267,12 +256,14 @@ async function importBudgets(

const budgets = sortByKey(data.months, 'month');

const internalCatIdYnab = data.category_groups.find(group =>
equalsIgnoreCase(group.name, 'Internal Master Category'),
).id;
const creditcardCatIdYnab = data.category_groups.find(group =>
equalsIgnoreCase(group.name, 'Credit Card Payments'),
).id;
const internalCatIdYnab = findIdByName(
data.category_groups,
'Internal Master Category',
);
const creditcardCatIdYnab = findIdByName(
data.category_groups,
'Credit Card Payments',
);

await actual.batchBudgetUpdates(async () => {
for (const budget of budgets) {
Expand Down Expand Up @@ -344,3 +335,11 @@ function equalsIgnoreCase(stringa: string, stringb: string): boolean {
}) === 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;
}

0 comments on commit 10e6654

Please sign in to comment.