Automation: attribute_changed condition treats lowercase AND (as sent by the dashboard) as OR
Describe the bug
An automation rule that combines an attribute changed condition with another condition using AND behaves as OR when the rule was saved from the dashboard.
The dashboard sends query_operator in lowercase ('and' / 'or', see app/javascript/dashboard/constants/automation.js and composables/useEditableAutomation.js), and AutomationRule#validate_single_condition accepts either case because it upcases before checking. The SQL branch is fine with that because SQL keywords are case-insensitive. But the Ruby branch for attribute-changed conditions compares the raw string:
filter['query_operator'] == 'AND' is false for 'and', so the method falls into the else branch and unions the records instead of intersecting them. Every attribute-changed rule created through the UI with AND therefore fires whenever either side matches.
To Reproduce
- Settings → Automations → add a rule on Conversation Updated.
- Condition 1: Company name attribute changed from
DCtoMarvel, joined with AND. - Condition 2: Status equal to
Snoozed. - Action: assign to a team.
- Take an open conversation whose contact company name changes from DC to Marvel.
Actual: the rule fires and the team is assigned although the status condition is not met.
Expected: the rule does not fire; it should only fire when both conditions hold.
The existing specs only exercise uppercase 'AND' / 'OR' (spec/listeners/automation_rule_listener_old_spec.rb), which is why this passes CI. A spec with 'and' reproduces it:
rule.update!(conditions: [
{ values: { from: ['DC'], to: ['Marvel'] }, attribute_key: 'company_name', query_operator: 'and', filter_operator: 'attribute_changed' },
{ values: ['snoozed'], attribute_key: 'status', query_operator: nil, filter_operator: 'equal_to' }
])
conversation.update!(status: :open)
AutomationRules::ConditionsFilterService.new(rule, conversation, changed_attributes: { company_name: %w[DC Marvel] }).perform
# => true on develop, expected falseExpected behavior
attribute_changed_filter_query should treat the query operator case-insensitively, the same way the model validation does, so 'and' intersects and 'or' unions.
I have a one-line fix plus specs ready and will open a PR.
Environment
Self-hosted (also reproducible on current develop, d4b5441)
Cloud Provider
No response
Platform
Docker
Operating system
Linux
Browser and version
Not browser-specific
Docker (if applicable)
chatwoot/chatwoot:v4.18.0
Additional context
The bug only affects attribute-changed conditions; other condition types are combined in SQL where and/or are case-insensitive.
Source: chatwoot/chatwoot