Add transaction details and notes filters to rules engine (#439)

* Initial plan

* Add transaction details and notes filters to rules engine

Co-authored-by: jjmata <187772+jjmata@users.noreply.github.com>

* Refine transaction details filter to use ILIKE for both operators

Co-authored-by: jjmata <187772+jjmata@users.noreply.github.com>

* Add type methods and fix operator semantics for transaction filters

Co-authored-by: jjmata <187772+jjmata@users.noreply.github.com>

* Refactor to use parent class sanitize_operator and add clear documentation

Co-authored-by: jjmata <187772+jjmata@users.noreply.github.com>

* Linter noise

---------

Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com>
Co-authored-by: jjmata <187772+jjmata@users.noreply.github.com>
Co-authored-by: Juan José Mata <juanjo.mata@gmail.com>
This commit is contained in:
Copilot
2025-12-11 00:55:55 +01:00
committed by GitHub
parent e8e727d76e
commit ba835c74ee
5 changed files with 285 additions and 1 deletions

View File

@@ -113,4 +113,92 @@ class RuleTest < ActiveSupport::TestCase
assert_not rule.valid?
assert_equal [ "Compound conditions cannot be nested" ], rule.errors.full_messages
end
test "rule matching on transaction details" do
# Create PayPal transaction with underlying merchant in details
paypal_entry = create_transaction(
date: Date.current,
account: @account,
name: "PayPal",
amount: 50
)
paypal_entry.transaction.update!(
extra: {
"simplefin" => {
"payee" => "Whole Foods via PayPal",
"description" => "Grocery shopping"
}
}
)
# Create another PayPal transaction with different underlying merchant
paypal_entry2 = create_transaction(
date: Date.current,
account: @account,
name: "PayPal",
amount: 100
)
paypal_entry2.transaction.update!(
extra: {
"simplefin" => {
"payee" => "Amazon via PayPal"
}
}
)
# Rule to categorize PayPal transactions containing "Whole Foods" in details
rule = Rule.create!(
family: @family,
resource_type: "transaction",
effective_date: 1.day.ago.to_date,
conditions: [ Rule::Condition.new(condition_type: "transaction_details", operator: "like", value: "Whole Foods") ],
actions: [ Rule::Action.new(action_type: "set_transaction_category", value: @groceries_category.id) ]
)
rule.apply
paypal_entry.reload
paypal_entry2.reload
assert_equal @groceries_category, paypal_entry.transaction.category, "PayPal transaction with 'Whole Foods' in details should be categorized"
assert_nil paypal_entry2.transaction.category, "PayPal transaction without 'Whole Foods' in details should not be categorized"
end
test "rule matching on transaction notes" do
# Create transaction with notes
transaction_entry = create_transaction(
date: Date.current,
account: @account,
name: "Expense",
amount: 50
)
transaction_entry.update!(notes: "Business lunch with client")
# Create another transaction without relevant notes
transaction_entry2 = create_transaction(
date: Date.current,
account: @account,
name: "Expense",
amount: 100
)
transaction_entry2.update!(notes: "Personal expense")
# Rule to categorize transactions with "business" in notes
business_category = @family.categories.create!(name: "Business")
rule = Rule.create!(
family: @family,
resource_type: "transaction",
effective_date: 1.day.ago.to_date,
conditions: [ Rule::Condition.new(condition_type: "transaction_notes", operator: "like", value: "business") ],
actions: [ Rule::Action.new(action_type: "set_transaction_category", value: business_category.id) ]
)
rule.apply
transaction_entry.reload
transaction_entry2.reload
assert_equal business_category, transaction_entry.transaction.category, "Transaction with 'business' in notes should be categorized"
assert_nil transaction_entry2.transaction.category, "Transaction without 'business' in notes should not be categorized"
end
end