Skip to content

Commit

Permalink
YNAB5 importer - use a map to speed up importing transactions with tr…
Browse files Browse the repository at this point in the history
…ansfers

does this by generating a Map once and uses this when processing transactions.

Subsequently updated Payee type for a bit for type safety
  • Loading branch information
Marethyu1 committed Nov 4, 2023
1 parent 9f32cce commit cd00145
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 5 deletions.
1 change: 1 addition & 0 deletions packages/loot-core/src/server/importers/ynab5-types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ export namespace YNAB5 {
id: string;
name: string;
deleted: boolean;
transfer_acct: string | null
}

interface CategoryGroup {
Expand Down
12 changes: 7 additions & 5 deletions packages/loot-core/src/server/importers/ynab5.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import * as monthUtils from '../../shared/months';
import { sortByKey, groupBy } from '../../shared/util';

import { YNAB5 } from './ynab5-types';
import Payee = YNAB5.Payee;

function amountFromYnab(amount: number) {
// ynabs multiplies amount by 1000 and actual by 100
Expand Down Expand Up @@ -143,6 +144,10 @@ async function importTransactions(
let transactionsGrouped = groupBy(data.transactions, 'account_id');
let subtransactionsGrouped = groupBy(data.subtransactions, 'transaction_id');

const payeesByTransferAcct = payees.filter((payee: Payee) => payee?.transfer_acct)
.map((payee: Payee) => [payee.transfer_acct, payee])
const payeeTransferAcctHashMap = new Map<string, Payee>(payeesByTransferAcct);

// Go ahead and generate ids for all of the transactions so we can
// reliably resolve transfers
for (let transaction of data.transactions) {
Expand Down Expand Up @@ -180,11 +185,8 @@ async function importTransactions(
? subtransactions.map(subtrans => {
let payee = null;
if (subtrans.transfer_account_id) {
payee = payees.find(
p =>
p.transfer_acct ===
entityIdMap.get(subtrans.transfer_account_id),
).id;
const mappedTransferAccountId = entityIdMap.get(subtrans.transfer_account_id);
payee = payeeTransferAcctHashMap.get(mappedTransferAccountId)?.id;
}

return {
Expand Down

0 comments on commit cd00145

Please sign in to comment.