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

Only pull single account data from SimpleFIN if syncing one account #483

Merged
merged 7 commits into from
Oct 30, 2024

Conversation

psybers
Copy link
Contributor

@psybers psybers commented Oct 22, 2024

This utilizes the new support for account= in the SimpleFIN API so that when requesting transactions you can specify which account(s). When Actual syncs a single account, this uses that parameter to limit the data sent back.

See: simplefin/simplefin.github.com#26 for details on the new API being used.

@actual-github-bot actual-github-bot bot changed the title Only pull single account data from SimpleFIN if syncing one account [WIP] Only pull single account data from SimpleFIN if syncing one account Oct 22, 2024
@psybers psybers changed the title [WIP] Only pull single account data from SimpleFIN if syncing one account Only pull single account data from SimpleFIN if syncing one account Oct 22, 2024
Copy link
Contributor

coderabbitai bot commented Oct 22, 2024

Walkthrough

The changes in the pull request primarily focus on the src/app-simplefin/app-simplefin.js file, specifically modifying the handling of API requests for accounts and transactions. The function signatures for both getAccounts and getTransactions have been updated to include an additional parameter, accounts, which allows for filtering based on specific account IDs.

In the /accounts endpoint, the updated getAccounts function now includes the accounts parameter while maintaining the existing logic for the accessKey. For the /transactions endpoint, the getTransactions function has been modified to ensure that the accountId and startDate parameters are of the same type and checks their lengths if they are arrays.

Error handling has been improved across both endpoints, with specific error messages logged for mismatched array lengths in the /transactions endpoint and centralized handling for invalid tokens. Additionally, utility functions have been adjusted to incorporate the new accounts parameter, enhancing the construction of query strings for API requests.

Possibly related PRs

  • Do not request transactions when listing accounts #482: This PR modifies the getAccounts function in the same file, enhancing error handling and introducing a new parameter that affects how accounts are retrieved, which is directly related to the changes made in the main PR regarding the handling of account requests.

Suggested labels

sparkles: Merged

Suggested reviewers

  • matt-fidd

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 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
Contributor

@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: 1

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

📥 Commits

Files that changed from the base of the PR and between c309816 and 7e4b273.

⛔ Files ignored due to path filters (1)
  • upcoming-release-notes/483.md is excluded by !**/*.md
📒 Files selected for processing (1)
  • src/app-simplefin/app-simplefin.js (5 hunks)
🧰 Additional context used
🔇 Additional comments (1)
src/app-simplefin/app-simplefin.js (1)

98-102: Usage of getTransactions updated correctly.

The function getTransactions is called with the updated parameters, correctly passing the accountId as an array. The changes integrate well with the new function signature.

src/app-simplefin/app-simplefin.js Outdated Show resolved Hide resolved
Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com>
Copy link
Contributor

@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 and nitpick comments (3)
src/app-simplefin/app-simplefin.js (3)

304-309: Consider adding JSDoc documentation

The function signature has changed significantly. Adding JSDoc would help document the new parameter and its usage.

+/**
+ * Fetches transactions for specified accounts within a date range
+ * @param {string} accessKey - The SimpleFIN access key
+ * @param {string[]} accounts - Array of account IDs to fetch
+ * @param {Date} [startDate] - Start date for transaction range
+ * @param {Date} [endDate] - End date for transaction range
+ * @returns {Promise<Object>} Transaction data for specified accounts
+ */
async function getTransactions(accessKey, accounts, startDate, endDate) {

Line range hint 320-345: Add input validation and JSDoc documentation

The function would benefit from proper documentation and input validation.

+/**
+ * Fetches account data from SimpleFIN API
+ * @param {string} accessKey - The SimpleFIN access key
+ * @param {string[]|null} accounts - Optional array of account IDs to fetch
+ * @param {Date} [startDate] - Start date for transaction range
+ * @param {Date} [endDate] - End date for transaction range
+ * @returns {Promise<Object>} Account and transaction data
+ * @throws {Error} When access key is invalid or API request fails
+ */
async function getAccounts(accessKey, accounts, startDate, endDate) {
+ if (!accessKey) {
+   throw new Error('Access key is required');
+ }
+ if (accounts && !Array.isArray(accounts)) {
+   throw new Error('accounts parameter must be an array or null');
+ }

Line range hint 304-345: Implementation aligns well with PR objectives

The changes effectively implement the SimpleFIN API's new account filtering capability while maintaining backward compatibility. The implementation:

  1. Properly handles both single and multiple account scenarios
  2. Maintains existing error handling and date range functionality
  3. Uses URL encoding for safety
  4. Integrates seamlessly with the existing architecture

Consider adding integration tests to verify the behavior with the SimpleFIN API.

Would you like help creating integration tests for these changes?

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

📥 Commits

Files that changed from the base of the PR and between 7e4b273 and b95be31.

📒 Files selected for processing (1)
  • src/app-simplefin/app-simplefin.js (5 hunks)
🧰 Additional context used
🔇 Additional comments (2)
src/app-simplefin/app-simplefin.js (2)

55-55: LGTM: Appropriate use of null accounts parameter

The null parameter correctly maintains the existing behavior of fetching all accounts while supporting the new filtering capability.


98-102: Verify error handling for empty accountId

The code assumes accountId is always provided. Consider adding validation to handle cases where accountId is null, undefined, or an empty array.

Copy link
Contributor

@matt-fidd matt-fidd left a comment

Choose a reason for hiding this comment

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

Again, looks reasonable and happy to merge if you, or anyone else who can, have tested

@psybers
Copy link
Contributor Author

psybers commented Oct 23, 2024

I have tested this PR and it works (with my data at least).

@matt-fidd
Copy link
Contributor

Sorry if I merged these in the wrong order, looks like there's a conflict now

@psybers
Copy link
Contributor Author

psybers commented Oct 23, 2024

@matt-fidd No I knew they would conflict, either way you merged them. Conflict is resolved now.

Copy link
Contributor

@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: 2

🧹 Outside diff range and nitpick comments (1)
src/app-simplefin/app-simplefin.js (1)

51-51: Consider using explicit parameter names for clarity.

The getAccounts call with multiple null parameters makes the code harder to understand. Consider using object destructuring for named parameters.

-const accounts = await getAccounts(accessKey, null, null, null, true);
+const accounts = await getAccounts(accessKey, {
+  accounts: null,
+  startDate: null,
+  endDate: null,
+  noTransactions: true
+});
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

📥 Commits

Files that changed from the base of the PR and between b95be31 and 913eb67.

📒 Files selected for processing (1)
  • src/app-simplefin/app-simplefin.js (5 hunks)
🧰 Additional context used
🔇 Additional comments (1)
src/app-simplefin/app-simplefin.js (1)

345-349: Consider URL length limitations when adding multiple account parameters.

While the code correctly handles account parameters, adding too many accounts could potentially exceed URL length limits. Consider:

  1. Adding a reasonable limit to the number of accounts
  2. Using POST with a request body for large numbers of accounts if the API supports it

Comment on lines +300 to +305
async function getTransactions(accessKey, accounts, startDate, endDate) {
const now = new Date();
startDate = startDate || new Date(now.getFullYear(), now.getMonth(), 1);
endDate = endDate || new Date(now.getFullYear(), now.getMonth() + 1, 1);
console.log(`${getDate(startDate)} - ${getDate(endDate)}`);
return await getAccounts(accessKey, startDate, endDate);
return await getAccounts(accessKey, accounts, startDate, endDate);
Copy link
Contributor

Choose a reason for hiding this comment

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

⚠️ Potential issue

Add input validation for accounts parameter.

The function should validate that the accounts parameter is either null or an array of non-empty strings to prevent potential API errors.

 async function getTransactions(accessKey, accounts, startDate, endDate) {
+  if (accounts !== null && (!Array.isArray(accounts) || accounts.some(id => !id || typeof id !== 'string'))) {
+    throw new Error('accounts parameter must be null or an array of non-empty strings');
+  }
   const now = new Date();
   startDate = startDate || new Date(now.getFullYear(), now.getMonth(), 1);
   endDate = endDate || new Date(now.getFullYear(), now.getMonth() + 1, 1);
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
async function getTransactions(accessKey, accounts, startDate, endDate) {
const now = new Date();
startDate = startDate || new Date(now.getFullYear(), now.getMonth(), 1);
endDate = endDate || new Date(now.getFullYear(), now.getMonth() + 1, 1);
console.log(`${getDate(startDate)} - ${getDate(endDate)}`);
return await getAccounts(accessKey, startDate, endDate);
return await getAccounts(accessKey, accounts, startDate, endDate);
async function getTransactions(accessKey, accounts, startDate, endDate) {
if (accounts !== null && (!Array.isArray(accounts) || accounts.some(id => !id || typeof id !== 'string'))) {
throw new Error('accounts parameter must be null or an array of non-empty strings');
}
const now = new Date();
startDate = startDate || new Date(now.getFullYear(), now.getMonth(), 1);
endDate = endDate || new Date(now.getFullYear(), now.getMonth() + 1, 1);
console.log(`${getDate(startDate)} - ${getDate(endDate)}`);
return await getAccounts(accessKey, accounts, startDate, endDate);

src/app-simplefin/app-simplefin.js Show resolved Hide resolved
Copy link
Contributor

@matt-fidd matt-fidd left a comment

Choose a reason for hiding this comment

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

LGTM

@matt-fidd matt-fidd merged commit b5f8aa4 into actualbudget:master Oct 30, 2024
7 of 8 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants