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

@@ -182,4 +182,153 @@ class Rule::ConditionTest < ActiveSupport::TestCase
assert_equal 3, filtered.count
assert filtered.all? { |t| t.merchant_id.nil? }
end
test "applies transaction_details condition with like operator" do
scope = @rule_scope
# Create a transaction with extra details (simulating PayPal with underlying merchant)
paypal_entry = create_transaction(
date: Date.current,
account: @account,
amount: 75,
name: "PayPal"
)
paypal_entry.transaction.update!(
extra: {
"simplefin" => {
"payee" => "Amazon via PayPal",
"description" => "Purchase from Amazon",
"memo" => "Order #12345"
}
}
)
condition = Rule::Condition.new(
rule: @transaction_rule,
condition_type: "transaction_details",
operator: "like",
value: "Amazon"
)
scope = condition.prepare(scope)
filtered = condition.apply(scope)
assert_equal 1, filtered.count
assert_equal paypal_entry.transaction.id, filtered.first.id
end
test "applies transaction_details condition with equal operator case-sensitive" do
scope = @rule_scope
# Create transaction with specific details
transaction_entry = create_transaction(
date: Date.current,
account: @account,
amount: 100,
name: "PayPal"
)
transaction_entry.transaction.update!(
extra: {
"simplefin" => {
"payee" => "Netflix"
}
}
)
# Test case-sensitive match (should match)
condition = Rule::Condition.new(
rule: @transaction_rule,
condition_type: "transaction_details",
operator: "=",
value: "Netflix"
)
scope = condition.prepare(scope)
filtered = condition.apply(scope)
assert_equal 1, filtered.count
# Test case-sensitive match (should NOT match due to case difference)
condition_lowercase = Rule::Condition.new(
rule: @transaction_rule,
condition_type: "transaction_details",
operator: "=",
value: "netflix"
)
scope = condition_lowercase.prepare(scope)
filtered = condition_lowercase.apply(scope)
assert_equal 0, filtered.count
end
test "applies transaction_details condition with is_null operator" do
scope = @rule_scope
# Create transaction with extra details
transaction_with_details = create_transaction(
date: Date.current,
account: @account,
amount: 50,
name: "Transaction with details"
)
transaction_with_details.transaction.update!(
extra: { "simplefin" => { "payee" => "Test Merchant" } }
)
condition = Rule::Condition.new(
rule: @transaction_rule,
condition_type: "transaction_details",
operator: "is_null",
value: nil
)
scope = condition.prepare(scope)
filtered = condition.apply(scope)
# Should return all original transactions (which have no extra details) but not the new one
assert_equal 5, filtered.count
assert_not filtered.map(&:id).include?(transaction_with_details.transaction.id)
end
test "applies transaction_notes condition" do
scope = @rule_scope
# Add notes to one transaction
transaction_entry = @account.entries.first
transaction_entry.update!(notes: "Important: This is a business expense")
condition = Rule::Condition.new(
rule: @transaction_rule,
condition_type: "transaction_notes",
operator: "like",
value: "business expense"
)
scope = condition.prepare(scope)
filtered = condition.apply(scope)
assert_equal 1, filtered.count
assert_equal transaction_entry.transaction.id, filtered.first.id
end
test "applies transaction_notes condition with is_null operator" do
scope = @rule_scope
# Add notes to one transaction
transaction_entry = @account.entries.first
transaction_entry.update!(notes: "Some notes")
condition = Rule::Condition.new(
rule: @transaction_rule,
condition_type: "transaction_notes",
operator: "is_null",
value: nil
)
scope = condition.prepare(scope)
filtered = condition.apply(scope)
# Should return all transactions without notes
assert_equal 4, filtered.count
assert_not filtered.map(&:id).include?(transaction_entry.transaction.id)
end
end