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

@@ -0,0 +1,31 @@
class Rule::ConditionFilter::TransactionDetails < Rule::ConditionFilter
def type
"text"
end
def prepare(scope)
scope
end
def apply(scope, operator, value)
# Search within the transaction's extra JSONB field
# This allows matching on provider-specific details like SimpleFin payee, description, memo
# Validate operator using parent class method
sanitize_operator(operator)
if operator == "is_null"
# Check if extra field is empty or null
scope.where("transactions.extra IS NULL OR transactions.extra = '{}'::jsonb")
else
# For both "like" and "=" operators, perform contains search
# "like" is case-insensitive (ILIKE), "=" is case-sensitive (LIKE)
# Note: For JSONB fields, both operators use contains semantics rather than exact match
# because searching within structured JSON data makes contains more useful than exact equality
sanitized_value = "%#{ActiveRecord::Base.sanitize_sql_like(value)}%"
sql_operator = operator == "like" ? "ILIKE" : "LIKE"
scope.where("transactions.extra::text #{sql_operator} ?", sanitized_value)
end
end
end

View File

@@ -0,0 +1,14 @@
class Rule::ConditionFilter::TransactionNotes < Rule::ConditionFilter
def type
"text"
end
def prepare(scope)
scope.with_entry
end
def apply(scope, operator, value)
expression = build_sanitized_where_condition("entries.notes", operator, value)
scope.where(expression)
end
end

View File

@@ -8,7 +8,9 @@ class Rule::Registry::TransactionResource < Rule::Registry
Rule::ConditionFilter::TransactionName.new(rule),
Rule::ConditionFilter::TransactionAmount.new(rule),
Rule::ConditionFilter::TransactionMerchant.new(rule),
Rule::ConditionFilter::TransactionCategory.new(rule)
Rule::ConditionFilter::TransactionCategory.new(rule),
Rule::ConditionFilter::TransactionDetails.new(rule),
Rule::ConditionFilter::TransactionNotes.new(rule)
]
end