Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New: Skip YNAB export for unmapped accounts #646

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

whatuserever
Copy link
Contributor

@whatuserever whatuserever commented Feb 10, 2025

  1. Refactored YNAB exporter to run per account
  2. If account is not mapped, skip it instead of throwing

Closes #644. Please read the issue for more details.

Summary by CodeRabbit

  • New Features
    • Added account-specific transaction export capabilities, allowing users to filter and export transactions by account number.
    • Enhanced transaction processing with integrated vendor configuration and improved error notifications to better inform users when accounts are unmapped.

Copy link

coderabbitai bot commented Feb 10, 2025

Walkthrough

This pull request introduces account-specific export functionality for transactions. In the common types file, a new interface extends export parameters with an account number, and a function type is added to handle these parameters. In the YNAB exporter file, the transaction creation functions are modified to accept an additional configuration parameter, perform account-specific transaction grouping, handle unmapped accounts by emitting progress events, and adjust error reporting. These changes enable the exporter to skip unmapped accounts while processing transactions by account.

Changes

File Summary
packages/main/src/backend/commonTypes.ts Added new interface ExportTransactionsForAccountParams (extending existing parameters with accountNumber) and new type ExportTransactionsForAccountFunction.
packages/main/src/backend/export/outputVendors/ynab/ynab.ts Updated createTransactions function to include an additional parameter outputVendorsConfig and to group transactions by account; added new function createTransactionsForAccount to handle account-specific export logic, including error handling for unmapped accounts and filtering transactions by account number.

Sequence Diagram(s)

sequenceDiagram
    participant Client
    participant Exporter as ExportTransactions
    participant AccountExporter as createTransactionsForAccount
    participant YNABService

    Client->>Exporter: Request export(transactionsToCreate, outputVendorsConfig, startDate)
    Exporter->>Exporter: Check and init categories if needed
    Exporter->>Exporter: Group transactions by account number
    loop For each account
        Exporter->>AccountExporter: Process account-specific transactions
        AccountExporter->>YNABService: Get YnabAccountId by account number
        alt Account is mapped
            YNABService-->>AccountExporter: Return YnabAccountId
            AccountExporter-->>Exporter: Return processed transaction count
        else Account is unmapped
            AccountExporter-->>Exporter: Emit progress event "unmapped account", return 0
        end
    end
    Exporter->>Client: Return aggregated export result
Loading

Assessment against linked issues

Objective Addressed Explanation
Skip unmapped accounts without failing (#644)

Possibly related PRs

Suggested labels

released

Poem

I'm a coding rabbit on the run,
Hopping through updates, having fun.
Accounts now sorted in a clever way,
Unmapped ones skipped without delay.
With carrots and code, my heart sings bright,
Celebrating these changes with pure delight!
🐇💻

✨ Finishing Touches
  • 📝 Generate Docstrings (Beta)

Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR. (Beta)
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 0

🔭 Outside diff range comments (1)
packages/main/src/backend/export/outputVendors/ynab/ynab.ts (1)

237-238: Remove @ts-expect-error by adding proper type guards.

The type error suppression could be replaced with proper null checks to ensure type safety.

-    // @ts-expect-error error TS18049: 'transactionToCreate.amount' is possibly 'null' or 'undefined'
-    Math.abs(transactionToCreate.amount - transactionFromYnab.amount) < 1000 &&
+    typeof transactionToCreate.amount === 'number' &&
+    typeof transactionFromYnab.amount === 'number' &&
+    Math.abs(transactionToCreate.amount - transactionFromYnab.amount) < 1000 &&
🧹 Nitpick comments (2)
packages/main/src/backend/export/outputVendors/ynab/ynab.ts (2)

41-65: Consider moving categories initialization to the init function.

The categories initialization could be moved to the init function to ensure it's ready before any transactions are processed, reducing potential delays during transaction creation.

 export async function init(outputVendorsConfig: Config['outputVendors']) {
   ynabConfig = outputVendorsConfig.ynab;
   initFromToken(outputVendorsConfig[OutputVendorName.YNAB]?.options.accessToken);
+  await initCategories();
 }

 const createTransactions: ExportTransactionsFunction = async (
   { transactionsToCreate, startDate, outputVendorsConfig },
   eventPublisher,
 ) => {
-  if (!categoriesMap.size) {
-    await initCategories();
-  }
   const accountNumbers = _.uniq(transactionsToCreate.map((t) => t.accountNumber));

77-77: Enhance the unmapped account message with more context.

The current message could be more informative by including guidance on how to map the account.

-    await emitProgressEvent(eventPublisher, allTransactions, `Account ${accountNumber} is unmapped. Skipping.`);
+    await emitProgressEvent(eventPublisher, allTransactions, `Account ${accountNumber} is not mapped to any YNAB account. Please add the mapping in the YNAB configuration. Skipping.`);
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 3e7c007 and 005bb0a.

📒 Files selected for processing (2)
  • packages/main/src/backend/commonTypes.ts (1 hunks)
  • packages/main/src/backend/export/outputVendors/ynab/ynab.ts (4 hunks)
🔇 Additional comments (2)
packages/main/src/backend/commonTypes.ts (2)

103-105: LGTM! Well-structured interface for account-specific exports.

The interface correctly extends ExportTransactionsParams and adds the necessary accountNumber property for per-account operations.


107-110: LGTM! Well-defined function type for account-specific exports.

The type definition maintains consistency with existing patterns while adding account-specific functionality.

Comment on lines +74 to +80
try {
getYnabAccountIdByAccountNumberFromTransaction(accountNumber);
} catch (e) {
await emitProgressEvent(eventPublisher, allTransactions, `Account ${accountNumber} is unmapped. Skipping.`);
return {
exportedTransactionsNum: 0,
};
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As mentioned in the linked issue, let me know if you'd prefer to have an option in the config for enabling the skipping logic.

}
const transactionsFromFinancialAccount = transactionsToCreate.map(convertTransactionToYnabFormat);
const transactionsFromFinancialAccount = allTransactions
.filter((v) => v.accountNumber === accountNumber)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I decided to do the filter here instead of in the outer function (createTransactions) for 2 reasons:

  1. The emitProgressEvent function (used below) expects to receive allTransactions. I'm not sure how important that is, so I didn't want to change that.
  2. This way there's no dependency on the caller.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Feature Request: Don't fail YNAB exporter on unmapped accounts
2 participants