[PR #5925] [MERGED] Adds balance variable to rule templates #36408

Closed
opened 2026-04-20 23:16:34 -05:00 by GiteaMirror · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/actualbudget/actual/pull/5925
Author: @StephenBrown2
Created: 10/14/2025
Status: Merged
Merged: 10/18/2025
Merged by: @youngcw

Base: masterHead: add-balance-to-rule-template


📝 Commits (10+)

  • 440f6b1 Adds balance variable to rule templates
  • b1501a5 Add release notes
  • 738a96c indexed sql params not supported
  • 49e4abd [autofix.ci] apply automated fixes
  • 42bc3a7 Skip parent transactions of splits
  • 76443c6 Uses aql for account balance
  • 754b519 Add block scoping to switch cases and ensure correct fallthrough handling in Action type conversions
  • 58a6e8d Corrects transaction rule sorting order
  • 01e75c8 Improves transaction rule balance calculation
  • 356d467 Apply coderabbit lessons learned

📊 Changes

4 files changed (+106 additions, -4 deletions)

View changed files

📝 packages/loot-core/src/server/rules/action.ts (+7 -3)
📝 packages/loot-core/src/server/rules/index.test.ts (+47 -0)
📝 packages/loot-core/src/server/transactions/transaction-rules.ts (+46 -1)
upcoming-release-notes/5925.md (+6 -0)

📄 Description

Enables access to the account balance within rule templates. This allows for more complex rule creation based on the current account balance.

Calculates the account balance up to the transaction being processed, including transactions on the same date with lower sort order.

Handles cases where the balance is undefined gracefully, defaulting to 0 to prevent errors.

Copilot Summary

This pull request adds support for using the account balance in transaction rule actions, enabling more advanced rule logic and calculations. It introduces the balance property to transactions processed by rules, ensures correct calculation of the balance up to each transaction, and updates both the rule execution logic and tests to handle balance-related scenarios robustly.

Balance Calculation and Integration:

  • Added calculation of the account balance up to (but not including) each transaction in prepareTransactionForRules, making it available for rule actions and templates.
  • Extended the transaction type (TransactionForRules) to include the optional balance property for use in rules.
  • Ensured the balance property is removed from transactions after rule processing to avoid side effects.

Rule Engine and Validation Improvements:

  • Updated the Action class to default number fields to 0 when template evaluation results in NaN, preventing database errors when balance is undefined.

Testing and Validation:

  • Added comprehensive tests to verify that balance is accessible in rule templates, supports math operations, and is handled gracefully when undefined, including scenarios for both string and number fields. [1] [2]

Reference

Attempts to address point 1 in the first comment: https://github.com/actualbudget/actual/issues/3606#issuecomment-2400673544

AI disclaimer

This PR contains code that was partially or fully generated by AI and may contain errors. All suggestions for improvement are welcome.


🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.

## 📋 Pull Request Information **Original PR:** https://github.com/actualbudget/actual/pull/5925 **Author:** [@StephenBrown2](https://github.com/StephenBrown2) **Created:** 10/14/2025 **Status:** ✅ Merged **Merged:** 10/18/2025 **Merged by:** [@youngcw](https://github.com/youngcw) **Base:** `master` ← **Head:** `add-balance-to-rule-template` --- ### 📝 Commits (10+) - [`440f6b1`](https://github.com/actualbudget/actual/commit/440f6b15aeb7687526f8282dd7b0e44914645492) Adds balance variable to rule templates - [`b1501a5`](https://github.com/actualbudget/actual/commit/b1501a5056df84bde4e99c59ae2184e484c2cd3f) Add release notes - [`738a96c`](https://github.com/actualbudget/actual/commit/738a96cf25c5be146dcaf10100782e93014cf9f5) indexed sql params not supported - [`49e4abd`](https://github.com/actualbudget/actual/commit/49e4abd43c6eed09fe1c5cd50d976e20801cb55d) [autofix.ci] apply automated fixes - [`42bc3a7`](https://github.com/actualbudget/actual/commit/42bc3a7878a44f8005b92952583eac9574b2daba) Skip parent transactions of splits - [`76443c6`](https://github.com/actualbudget/actual/commit/76443c6eea6c81eac2690c77ea9a16b0c30de5bd) Uses aql for account balance - [`754b519`](https://github.com/actualbudget/actual/commit/754b5199fa400bd6c6d02a68dbac70783b902853) Add block scoping to switch cases and ensure correct fallthrough handling in Action type conversions - [`58a6e8d`](https://github.com/actualbudget/actual/commit/58a6e8d02ae23733988e6f82ccc8d93d684caf58) Corrects transaction rule sorting order - [`01e75c8`](https://github.com/actualbudget/actual/commit/01e75c8a0a7b600940431a58c3b302a13dd69894) Improves transaction rule balance calculation - [`356d467`](https://github.com/actualbudget/actual/commit/356d4678bc5e29e8fda81424bdf42881861e7b05) Apply coderabbit lessons learned ### 📊 Changes **4 files changed** (+106 additions, -4 deletions) <details> <summary>View changed files</summary> 📝 `packages/loot-core/src/server/rules/action.ts` (+7 -3) 📝 `packages/loot-core/src/server/rules/index.test.ts` (+47 -0) 📝 `packages/loot-core/src/server/transactions/transaction-rules.ts` (+46 -1) ➕ `upcoming-release-notes/5925.md` (+6 -0) </details> ### 📄 Description Enables access to the account balance within rule templates. This allows for more complex rule creation based on the current account balance. Calculates the account balance up to the transaction being processed, including transactions on the same date with lower sort order. Handles cases where the balance is undefined gracefully, defaulting to 0 to prevent errors. # Copilot Summary This pull request adds support for using the account balance in transaction rule actions, enabling more advanced rule logic and calculations. It introduces the `balance` property to transactions processed by rules, ensures correct calculation of the balance up to each transaction, and updates both the rule execution logic and tests to handle balance-related scenarios robustly. **Balance Calculation and Integration:** * Added calculation of the account `balance` up to (but not including) each transaction in `prepareTransactionForRules`, making it available for rule actions and templates. * Extended the transaction type (`TransactionForRules`) to include the optional `balance` property for use in rules. * Ensured the `balance` property is removed from transactions after rule processing to avoid side effects. **Rule Engine and Validation Improvements:** * Updated the `Action` class to default number fields to `0` when template evaluation results in `NaN`, preventing database errors when `balance` is undefined. **Testing and Validation:** * Added comprehensive tests to verify that `balance` is accessible in rule templates, supports math operations, and is handled gracefully when undefined, including scenarios for both string and number fields. [[1]](diffhunk://#diff-03472a29ce63ef88a786032d62a9d9448077800db24356db421b1435a30d6488R333-R341) [[2]](diffhunk://#diff-03472a29ce63ef88a786032d62a9d9448077800db24356db421b1435a30d6488R479-R516) ## Reference Attempts to address point 1 in the first comment: https://github.com/actualbudget/actual/issues/3606#issuecomment-2400673544 ### AI disclaimer This PR contains code that was partially or fully generated by AI and may contain errors. All suggestions for improvement are welcome. <!-- Thank you for submitting a pull request! Make sure to follow the instructions to write release notes for your PR — it should only take a minute or two: https://github.com/actualbudget/docs#writing-good-release-notes. Try running yarn generate:release-notes *before* pushing your PR for an interactive experience. --> --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
GiteaMirror added the pull-request label 2026-04-20 23:16:34 -05:00
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: github-starred/actual#36408