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

feat: templating on payee_name #3619

Merged
merged 5 commits into from
Oct 15, 2024

Conversation

UnderKoen
Copy link
Member

@UnderKoen UnderKoen commented Oct 9, 2024

Adds the field payee_name as possiblity for templating.

Makes it possible to change payee_name to whatever template you want.

Currently there is another field in the selector for setting the name but maybe this should not be visible and be auto selected when enabling templating on payee

#3606

@actual-github-bot actual-github-bot bot changed the title feat: templating on payee_name [WIP] feat: templating on payee_name Oct 9, 2024
Copy link

netlify bot commented Oct 9, 2024

Deploy Preview for actualbudget ready!

Name Link
🔨 Latest commit f8e8f73
🔍 Latest deploy log https://app.netlify.com/sites/actualbudget/deploys/6706e606d9dc6300082c239b
😎 Deploy Preview https://deploy-preview-3619.demo.actualbudget.org
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site configuration.

Copy link
Contributor

coderabbitai bot commented Oct 9, 2024

Walkthrough

The pull request introduces several modifications across multiple files, primarily enhancing the EditRuleModal component and its functionalities. In the EditRuleModal.jsx, a new field payee_name is added to the actionFields array, which alters the logic for field selection based on the actionTemplating feature flag and the presence of a splitIndex. This change allows for more dynamic field selection in the ActionEditor. In the rules.ts file, a new conditional check is added to the regex helper function to handle null values, enhancing its robustness. The sync.ts file updates the runRules function calls to be asynchronous, affecting transaction handling. Additionally, the transaction-rules.ts file introduces a new type, TransactionForRules, and updates the runRules function to be asynchronous. The FIELD_INFO object in rules.ts is modified to include the new payee_name field, and the FieldValueTypes type in rule.d.ts is updated accordingly.

Possibly related PRs

Suggested labels

::sparkles: Merged

Suggested reviewers

  • youngcw
  • MikesGlitch
  • 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

github-actions bot commented Oct 9, 2024

Bundle Stats — desktop-client

Hey there, this message comes from a GitHub action that helps you and reviewers to understand how these changes affect the size of this project's bundle.

As this PR is updated, I'll keep you updated on how the bundle size is impacted.

Total

Files count Total bundle size % Changed
9 5.31 MB → 5.31 MB (+272 B) +0.00%
Changeset
File Δ Size
home/runner/work/actual/actual/packages/loot-core/src/shared/rules.ts 📈 +92 B (+1.27%) 7.08 kB → 7.17 kB
src/components/rules/Value.tsx 📈 +27 B (+0.56%) 4.74 kB → 4.77 kB
src/components/modals/EditRuleModal.jsx 📈 +153 B (+0.38%) 39.04 kB → 39.19 kB
View detailed bundle breakdown

Added

No assets were added

Removed

No assets were removed

Bigger

Asset File Size % Changed
static/js/index.js 3.33 MB → 3.33 MB (+272 B) +0.01%

Smaller

No assets were smaller

Unchanged

Asset File Size % Changed
static/js/indexeddb-main-thread-worker-e59fee74.js 13.5 kB 0%
static/js/resize-observer.js 18.37 kB 0%
static/js/usePreviewTransactions.js 1.64 kB 0%
static/js/AppliedFilters.js 20.96 kB 0%
static/js/BackgroundImage.js 122.29 kB 0%
static/js/narrow.js 82.55 kB 0%
static/js/wide.js 224.88 kB 0%
static/js/ReportRouter.js 1.51 MB 0%

Copy link
Contributor

github-actions bot commented Oct 9, 2024

Bundle Stats — loot-core

Hey there, this message comes from a GitHub action that helps you and reviewers to understand how these changes affect the size of this project's bundle.

As this PR is updated, I'll keep you updated on how the bundle size is impacted.

Total

Files count Total bundle size % Changed
1 1.26 MB → 1.27 MB (+464 B) +0.03%
Changeset
File Δ Size
packages/loot-core/src/server/accounts/transaction-rules.ts 📈 +1.07 kB (+3.85%) 27.9 kB → 28.97 kB
packages/loot-core/src/shared/rules.ts 📈 +110 B (+1.14%) 9.46 kB → 9.57 kB
packages/loot-core/src/server/accounts/rules.ts 📈 +76 B (+0.22%) 33.11 kB → 33.19 kB
packages/loot-core/src/server/accounts/sync.ts 📈 +12 B (+0.05%) 24.15 kB → 24.16 kB
View detailed bundle breakdown

Added

No assets were added

Removed

No assets were removed

Bigger

Asset File Size % Changed
kcab.worker.js 1.26 MB → 1.27 MB (+464 B) +0.03%

Smaller

No assets were smaller

Unchanged

No assets were unchanged

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: 6

🧹 Outside diff range and nitpick comments (3)
packages/desktop-client/src/components/rules/Value.tsx (1)

89-89: LGTM! Consider grouping similar cases for improved readability.

The addition of the payee_name case is consistent with the handling of similar fields like notes and imported_payee. This change aligns well with the PR objective of introducing templating capabilities for the payee_name field.

For improved code readability, consider grouping similar cases together. You could refactor the switch statement like this:

switch (field) {
  case 'notes':
  case 'imported_payee':
  case 'payee_name':
    return value;
  // ... other cases
}

This grouping makes it easier to see at a glance which fields are treated similarly.

packages/loot-core/src/shared/rules.ts (1)

64-64: Overall impact: payee_name field support added successfully

The changes successfully implement the templating capability for the payee_name field as described in the PR objectives. The implementation is consistent with the existing code structure and patterns.

Consider the following follow-up actions:

  1. Ensure that other parts of the codebase (e.g., UI components, API endpoints) are updated to utilize this new field.
  2. Update relevant documentation to reflect the addition of the payee_name field.
  3. Add or update unit tests to cover the new payee_name field in rule processing.

Also applies to: 116-117

packages/loot-core/src/server/accounts/sync.ts (1)

Ensure all runRules calls are awaited in test files

The shell script results indicate that several instances of runRules in transaction-rules.test.ts are not using the await keyword. To maintain consistency and proper asynchronous handling across the codebase, please update these calls as follows:

  • File: packages/loot-core/src/server/accounts/transaction-rules.test.ts
    • Replace const transaction = runRules({ with const transaction = await runRules({
    • Replace let transaction = runRules({ with let transaction = await runRules({
    • Replace transaction = runRules({ with transaction = await runRules({
    • Ensure all other instances of runRules in this file are preceded by await.
🔗 Analysis chain

Line range hint 1-824: Summary of changes and follow-up suggestion

The modifications in this file consistently update the runRules function to be asynchronous, aligning with the PR objectives for introducing templating capabilities for the payee_name field. While the changes are approved, there are a few improvements suggested:

  1. Update the matchTransactions function signature to be explicitly async.
  2. Add error handling for the asynchronous runRules calls in both matchTransactions and addTransactions functions.

To ensure the changes are comprehensive and consistent, it's recommended to:

This will help identify any other instances of runRules that might need to be updated to maintain consistency across the codebase.

🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Search for other occurrences of runRules that might need to be updated
rg "runRules\(" --type ts

Length of output: 1252

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

📥 Commits

Files that changed from the base of the PR and between f786bdc and a2a20f0.

⛔ Files ignored due to path filters (1)
  • upcoming-release-notes/3619.md is excluded by !**/*.md
📒 Files selected for processing (7)
  • packages/desktop-client/src/components/modals/EditRuleModal.jsx (3 hunks)
  • packages/desktop-client/src/components/rules/Value.tsx (1 hunks)
  • packages/loot-core/src/server/accounts/rules.ts (1 hunks)
  • packages/loot-core/src/server/accounts/sync.ts (2 hunks)
  • packages/loot-core/src/server/accounts/transaction-rules.ts (5 hunks)
  • packages/loot-core/src/shared/rules.ts (2 hunks)
  • packages/loot-core/src/types/models/rule.d.ts (1 hunks)
🧰 Additional context used
🪛 Biome
packages/loot-core/src/server/accounts/transaction-rules.ts

[error] 804-804: Avoid the delete operator which can impact performance.

Unsafe fix: Use an undefined assignment instead.

(lint/performance/noDelete)

🔇 Additional comments (7)
packages/loot-core/src/shared/rules.ts (2)

64-64: LGTM: Addition of payee_name to FIELD_INFO

The addition of payee_name as a string type in the FIELD_INFO object is consistent with the existing structure and aligns with the PR objectives. This change enables the use of payee_name in rule processing and validation.


116-117: LGTM: Update to mapField function

The addition of the payee_name case in the mapField function is consistent with the PR objectives. The returned string 'payee (name)' provides a clear and user-friendly representation of the field.

packages/loot-core/src/server/accounts/rules.ts (1)

47-50: LGTM! Improved null handling in regex helper.

This change enhances the robustness of the regex helper function by adding a null check for the value parameter. It prevents potential runtime errors when the function is called with null values, returning null in such cases. This is a good defensive programming practice.

packages/loot-core/src/server/accounts/transaction-rules.ts (1)

Line range hint 277-291: Ensure all callers of runRules handle its new async nature

The runRules function has been updated to be asynchronous. Verify that all functions calling runRules have been updated to handle the Promise it now returns, to prevent unhandled Promise rejections or unexpected behavior.

Run the following script to find all usages of runRules and check if they handle the async function properly:

packages/desktop-client/src/components/modals/EditRuleModal.jsx (3)

350-350: 'payee_name' field successfully added to actionFields

The inclusion of 'payee_name' in actionFields expands the action options available to users, allowing them to modify the payee name through templating as intended.


376-381: Verify the filtering logic for fields including 'payee_name'

Please ensure that the filtering logic in the fields variable correctly includes the 'payee_name' field when the actionTemplating feature flag is enabled or when the current field is 'payee_name'. This verification is crucial to confirm that users have access to the payee_name field as expected when templating is active.


394-394: FieldSelect now utilizes dynamic fields

The FieldSelect component has been updated to use the dynamic fields variable, enhancing flexibility by displaying the appropriate fields based on the feature flag and current context.

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)
packages/loot-core/src/server/accounts/transaction-rules.test.ts (3)

Line range hint 129-134: LGTM! Consider using async/await syntax consistently.

The addition of await to runRules is correct and necessary if the function has been changed to return a Promise. This ensures that the test waits for the rules to be applied before making assertions.

For consistency, consider updating the entire test case to use async/await syntax:

-  test('insert a rule into the database', async () => {
+  test('insert a rule into the database', async () => {
     await loadRules();
     await insertRule({
       stage: 'pre',
       conditionsOp: 'and',
       conditions: [],
       actions: [],
     });
     expect((await db.all('SELECT * FROM rules')).length).toBe(1);
     // Make sure it was projected
     expect(getRules().length).toBe(1);

     // ... (other test code)

-    const transaction = await runRules({
+    const transaction = await runRules({
       date: '2019-05-10',
       notes: '',
       category: null,
     });
     expect(transaction.date).toBe('2019-05-10');
     expect(transaction.notes).toBe('Sarah');
     expect(transaction.category).toBe('food');
   });

Line range hint 204-209: LGTM! Consistent use of await for runRules in delete rule test.

The addition of await to both runRules calls in this test case is correct and consistent with the previous changes. It ensures that the test waits for the rules to be applied before and after deleting a rule, allowing for accurate assertions on the transaction properties.

For consistency with the rest of the file, consider updating the entire test case to use const instead of let for the transaction variable:

-    let transaction = await runRules({
+    const transaction = await runRules({
       payee: 'Kroger',
       notes: '',
       category: null,
     });
     expect(transaction.payee).toBe('Kroger');
     expect(transaction.category).toBe('food');

     await deleteRule(id);
     expect(getRules().length).toBe(0);
-    transaction = await runRules({
+    const updatedTransaction = await runRules({
       payee: 'Kroger',
       notes: '',
       category: null,
     });
-    expect(transaction.payee).toBe('Kroger');
-    expect(transaction.category).toBe(null);
+    expect(updatedTransaction.payee).toBe('Kroger');
+    expect(updatedTransaction.category).toBe(null);

Also applies to: 214-219


Line range hint 1-1024: Overall, excellent updates to handle asynchronous operations.

The changes made to this test file are consistent and improve the reliability of the tests by properly handling the asynchronous nature of the runRules function. All instances of runRules are now correctly awaited, ensuring that the tests accurately reflect the behavior of the transaction rules system.

A few minor suggestions for improvement have been made in previous comments, but these are not critical and are mainly for consistency and readability.

Consider adding a test case that specifically checks the asynchronous behavior of runRules, such as testing concurrent rule applications or checking the order of rule execution in an asynchronous context. This would further validate the robustness of the transaction rules system in real-world scenarios.

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

📥 Commits

Files that changed from the base of the PR and between a2a20f0 and 1684d02.

📒 Files selected for processing (1)
  • packages/loot-core/src/server/accounts/transaction-rules.test.ts (9 hunks)
🧰 Additional context used
🔇 Additional comments (3)
packages/loot-core/src/server/accounts/transaction-rules.test.ts (3)

Line range hint 152-157: LGTM! Correct usage of await for asynchronous operation.

The addition of await to runRules is correct and consistent with the asynchronous nature of the function. This ensures that the test waits for the rules to be applied before making assertions on the transaction properties.


Line range hint 168-173: LGTM! Consistent use of await for runRules.

The addition of await to this runRules call is correct and consistent with the previous changes in this test case. It ensures that the test waits for the updated rules to be applied before making assertions on the transaction properties.


Line range hint 182-187: LGTM! Consistent use of await for runRules after condition change.

The addition of await to this runRules call is correct and consistent with the previous changes in this test case. It ensures that the test waits for the rules with updated conditions to be applied before making assertions on the transaction properties.

@UnderKoen UnderKoen changed the title [WIP] feat: templating on payee_name feat: templating on payee_name Oct 9, 2024
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 (1)
packages/loot-core/src/server/accounts/transaction-rules.ts (1)

791-810: LGTM! Transaction finalization function added with a minor optimization opportunity.

The finalizeTransactionForRules function ensures proper handling of payee information, creating or retrieving payee IDs as needed. This maintains data integrity and consistency in the database.

A minor optimization could be made:

Consider replacing the delete operator with an assignment to undefined:

-    delete trans.payee_name;
+    trans.payee_name = undefined;

This change addresses the static analysis hint and may provide a slight performance benefit, although the impact is likely minimal in this context.

🧰 Tools
🪛 Biome

[error] 806-806: Avoid the delete operator which can impact performance.

Unsafe fix: Use an undefined assignment instead.

(lint/performance/noDelete)

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

📥 Commits

Files that changed from the base of the PR and between 1684d02 and f8e8f73.

📒 Files selected for processing (1)
  • packages/loot-core/src/server/accounts/transaction-rules.ts (5 hunks)
🧰 Additional context used
📓 Learnings (1)
packages/loot-core/src/server/accounts/transaction-rules.ts (2)
Learnt from: UnderKoen
PR: actualbudget/actual#3619
File: packages/loot-core/src/server/accounts/transaction-rules.ts:551-0
Timestamp: 2024-10-09T20:17:46.493Z
Learning: When finalizing transactions that involve inserting or retrieving payees, avoid using `Promise.all` as it may result in duplicate payees due to concurrent operations. Sequential processing ensures payees are correctly handled without duplication.
Learnt from: UnderKoen
PR: actualbudget/actual#3619
File: packages/loot-core/src/server/accounts/transaction-rules.ts:795-801
Timestamp: 2024-10-09T20:30:39.127Z
Learning: In the `finalizeTransactionForRules` function within `packages/loot-core/src/server/accounts/transaction-rules.ts`, potential race conditions when inserting payees are handled in the method that calls this function, so additional safeguards within `finalizeTransactionForRules` are unnecessary.
🪛 Biome
packages/loot-core/src/server/accounts/transaction-rules.ts

[error] 806-806: Avoid the delete operator which can impact performance.

Unsafe fix: Use an undefined assignment instead.

(lint/performance/noDelete)

🔇 Additional comments (4)
packages/loot-core/src/server/accounts/transaction-rules.ts (4)

Line range hint 277-291: LGTM! New transaction preparation workflow implemented.

The runRules function has been updated to include a new workflow for handling transactions:

  1. Transactions are prepared using prepareTransactionForRules.
  2. Rules are applied as before.
  3. Transactions are finalized using finalizeTransactionForRules.

This change ensures consistent handling of transaction data, particularly for payee information, throughout the rule application process.


543-556: LGTM! Efficient transaction preparation with safe finalization.

The applyActions function has been updated to use Promise.all for preparing transactions, which is an efficient approach. The finalization of transactions is done sequentially in a for loop, which aligns with the previous learning about avoiding duplicate payees when using Promise.all for operations involving payee insertions or retrievals.

This implementation strikes a good balance between efficiency and data integrity.


773-775: LGTM! New type for rule processing introduced.

The TransactionForRules type extends TransactionEntity with an optional payee_name property. This addition allows for better type safety and clarity when dealing with transactions during the rule processing phase, where both payee ID and name might be needed.


777-789: LGTM! Transaction preparation function added.

The prepareTransactionForRules function enriches the transaction data by fetching the payee name when a payee ID is present. This allows for more flexible rule conditions based on payee names and provides a clear separation of concerns in the rule processing workflow.

@youngcw
Copy link
Member

youngcw commented Oct 10, 2024

what the difference between payee and payee_name? Is payee just the ID?

@UnderKoen
Copy link
Member Author

payee is just the id

@youngcw
Copy link
Member

youngcw commented Oct 10, 2024

payee is just the id

In that case its probably not useful to be able to set just payee, and only let people set payee_name

@UnderKoen
Copy link
Member Author

Expect that payee is currently used in the frontend to keep the name consistent even after merges

@youngcw youngcw added this to the v24.11.0 milestone Oct 15, 2024
Copy link
Member

@youngcw youngcw left a comment

Choose a reason for hiding this comment

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

Looks good

@youngcw youngcw merged commit 3d9e90f into actualbudget:master Oct 15, 2024
20 checks passed
@UnderKoen UnderKoen deleted the feat/payee_templating branch October 22, 2024 07:31
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