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 8, 2024
1 parent 274b404 commit efcac3e
Showing 1 changed file with 22 additions and 26 deletions.
48 changes: 22 additions & 26 deletions packages/loot-core/src/server/importers/ynab5.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,17 +39,11 @@ 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
) {
if (findIdByName(data.category_groups, 'Internal Master Category')) {
if (
ynabIncomeCategories.some(ynabIncomeCategory =>
equalsIgnoreCase(cat.name, ynabIncomeCategory),
Expand All @@ -61,9 +55,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 +130,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 +253,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 +332,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 efcac3e

Please sign in to comment.