fix: Ensure Automation rule executions are account scoped (#5768)

This is one possible fix to tackle with cached automation rules when automation gets called on two different accounts with the same event name.

Fixes: https://github.com/chatwoot/product/issues/605
This commit is contained in:
Tejaswini Chile 2022-11-03 05:54:35 +05:30 committed by GitHub
parent 9c0cce0392
commit 936c2ec7e2
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 58 additions and 5 deletions

View file

@ -8,7 +8,9 @@ class AutomationRuleListener < BaseListener
return unless rule_present?('conversation_updated', account)
@rules.each do |rule|
rules = current_account_rules('conversation_updated', account)
rules.each do |rule|
conditions_match = ::AutomationRules::ConditionsFilterService.new(rule, conversation, { changed_attributes: changed_attributes }).perform
AutomationRules::ActionService.new(rule, account, conversation).perform if conditions_match.present?
end
@ -23,7 +25,9 @@ class AutomationRuleListener < BaseListener
return unless rule_present?('conversation_created', account)
@rules.each do |rule|
rules = current_account_rules('conversation_created', account)
rules.each do |rule|
conditions_match = ::AutomationRules::ConditionsFilterService.new(rule, conversation, { changed_attributes: changed_attributes }).perform
::AutomationRules::ActionService.new(rule, account, conversation).perform if conditions_match.present?
end
@ -38,7 +42,9 @@ class AutomationRuleListener < BaseListener
return unless rule_present?('message_created', account)
@rules.each do |rule|
rules = current_account_rules('message_created', account)
rules.each do |rule|
conditions_match = ::AutomationRules::ConditionsFilterService.new(rule, message.conversation,
{ message: message, changed_attributes: changed_attributes }).perform
::AutomationRules::ActionService.new(rule, account, message.conversation).perform if conditions_match.present?
@ -48,12 +54,15 @@ class AutomationRuleListener < BaseListener
def rule_present?(event_name, account)
return if account.blank?
@rules = AutomationRule.where(
current_account_rules(event_name, account).any?
end
def current_account_rules(event_name, account)
AutomationRule.where(
event_name: event_name,
account_id: account.id,
active: true
)
@rules.any?
end
def performed_by_automation?(event_obj)

View file

@ -644,4 +644,48 @@ describe AutomationRuleListener do
end
end
end
describe '#conversation_created for two accounts' do
let!(:new_account) { create(:account) }
before do
new_inbox = create(:inbox, account: new_account)
new_conversation = create(:conversation, inbox: new_inbox, account: new_account)
new_automation_rule = create(:automation_rule, account: new_account, name: 'Test Automation Rule - 1')
create(:message, account: account, conversation: conversation, message_type: 'incoming')
create(:message, account: new_account, conversation: new_conversation, message_type: 'incoming')
automation_rule.update!(
event_name: 'conversation_created',
conditions: [{ attribute_key: 'status', filter_operator: 'equal_to', values: ['all'], query_operator: nil }.with_indifferent_access],
actions: [{ 'action_name' => 'send_message', 'action_params' => ['Send this message.'] }]
)
new_automation_rule.update!(
event_name: 'conversation_created',
conditions: [{ attribute_key: 'status', filter_operator: 'equal_to', values: ['all'], query_operator: nil }.with_indifferent_access],
actions: [{ 'action_name' => 'send_message', 'action_params' => ['Send this message. - 1'] }]
)
end
it 'triggers automation at the same time' do
new_conversation = new_account.conversations.last
new_automation_rule = new_account.automation_rules.last
event = Events::Base.new('conversation_created', Time.zone.now, { conversation: conversation })
new_event = Events::Base.new('conversation_created', Time.zone.now, { conversation: new_conversation })
listener.conversation_created(event)
listener.conversation_created(new_event)
expect(conversation.messages.count).to eq(2)
expect(new_conversation.messages.count).to eq(2)
expect(conversation.messages.last.content).to eq('Send this message.')
expect(new_conversation.messages.last.content).to eq('Send this message. - 1')
expect(conversation.messages.last.content_attributes).to eq({ 'automation_rule_id' => automation_rule.id })
expect(new_conversation.messages.last.content_attributes).to eq({ 'automation_rule_id' => new_automation_rule.id })
end
end
end