Skip to content

Commit

Permalink
Skip unmapped YNAB accounts
Browse files Browse the repository at this point in the history
  • Loading branch information
whatuserever committed Feb 9, 2025
1 parent 3e7c007 commit 005bb0a
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 11 deletions.
9 changes: 9 additions & 0 deletions packages/main/src/backend/commonTypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,15 @@ export type ExportTransactionsFunction = (
eventPublisher: EventPublisher,
) => Promise<ExportTransactionsResult>;

export interface ExportTransactionsForAccountParams extends ExportTransactionsParams {
accountNumber: string;
}

export type ExportTransactionsForAccountFunction = (
exportTransactionsParams: ExportTransactionsForAccountParams,
eventPublisher: EventPublisher,
) => Promise<ExportTransactionsResult>;

export interface OutputVendor {
name: OutputVendorName;
init?: (outputVendorsConfig: Config['outputVendors']) => Promise<void>;
Expand Down
55 changes: 44 additions & 11 deletions packages/main/src/backend/export/outputVendors/ynab/ynab.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import {
OutputVendorName,
type Config,
type EnrichedTransaction,
type ExportTransactionsForAccountFunction,
type ExportTransactionsFunction,
type OutputVendor,
type YnabAccountDetails,
Expand Down Expand Up @@ -37,14 +38,50 @@ async function initFromToken(accessToken?: string) {
}
}

const createTransactions: ExportTransactionsFunction = async ({ transactionsToCreate, startDate }, eventPublisher) => {
const createTransactions: ExportTransactionsFunction = async (
{ transactionsToCreate, startDate, outputVendorsConfig },
eventPublisher,
) => {
if (!categoriesMap.size) {
await initCategories();
}
const accountNumbers = _.uniq(transactionsToCreate.map((t) => t.accountNumber));
const promises = accountNumbers.map((accountNumber) =>
createTransactionsForAccount(
{
accountNumber,
transactionsToCreate,
startDate,
outputVendorsConfig,
},
eventPublisher,
),
);
const results = await Promise.all(promises);
const exportedTransactionsNums = results.map((v) => v.exportedTransactionsNum);
return {
exportedTransactionsNum: _.sum(exportedTransactionsNums),
};
};

const createTransactionsForAccount: ExportTransactionsForAccountFunction = async (
{ accountNumber, transactionsToCreate: allTransactions, startDate },
eventPublisher,
) => {
if (!ynabConfig) {
throw new Error('Must call init before using ynab functions');
}
if (!categoriesMap.size) {
await initCategories();
try {
getYnabAccountIdByAccountNumberFromTransaction(accountNumber);
} catch (e) {
await emitProgressEvent(eventPublisher, allTransactions, `Account ${accountNumber} is unmapped. Skipping.`);
return {
exportedTransactionsNum: 0,
};
}
const transactionsFromFinancialAccount = transactionsToCreate.map(convertTransactionToYnabFormat);
const transactionsFromFinancialAccount = allTransactions
.filter((v) => v.accountNumber === accountNumber)
.map(convertTransactionToYnabFormat);
let transactionsThatDontExistInYnab = await filterOnlyTransactionsThatDontExistInYnabAlready(
startDate,
transactionsFromFinancialAccount,
Expand All @@ -54,19 +91,15 @@ const createTransactions: ExportTransactionsFunction = async ({ transactionsToCr
moment(transaction.date, YNAB_DATE_FORMAT).isBefore(NOW),
);
if (!transactionsThatDontExistInYnab.length) {
await emitProgressEvent(
eventPublisher,
transactionsToCreate,
'All transactions already exist in ynab. Doing nothing.',
);
await emitProgressEvent(eventPublisher, allTransactions, 'All transactions already exist in ynab. Doing nothing.');
return {
exportedTransactionsNum: 0,
};
}

await emitProgressEvent(
eventPublisher,
transactionsToCreate,
allTransactions,
`Creating ${transactionsThatDontExistInYnab.length} transactions in ynab`,
);
try {
Expand All @@ -83,7 +116,7 @@ const createTransactions: ExportTransactionsFunction = async ({ transactionsToCr
message: (e as Error).message,
error: e as Error,
exporterName: ynabOutputVendor.name,
allTransactions: transactionsToCreate,
allTransactions: allTransactions,
}),
);
console.error('Failed to create transactions in ynab', e);
Expand Down

0 comments on commit 005bb0a

Please sign in to comment.