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:
@@ -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)
|
||||
|
||||
Reference in New Issue
Block a user