- Refactor filter service for better readability and maintenance - Add validations for the following: - If an invalid attribute is passed, a custom exception InvalidAttribute will be thrown. - If an invalid operator is passed, a custom exception InvalidOperator will be thrown. - If an invalid value (currently checking only null check), a custom exception InvalidValue will be thrown. Fixes: https://linear.app/chatwoot/issue/CW-2702/activerecordstatementinvalid-pginvalidtextrepresentation-error-invalid Fixes: https://linear.app/chatwoot/issue/CW-2703/activerecordstatementinvalid-pginvaliddatetimeformat-error-invalid Fixes: https://linear.app/chatwoot/issue/CW-2700/activerecordstatementinvalid-pgsyntaxerror-error-syntax-error-at-or Co-authored-by: Sojan <sojan@pepalo.com>
57 lines
1.5 KiB
Ruby
57 lines
1.5 KiB
Ruby
class AutomationRules::ConditionValidationService
|
|
ATTRIBUTE_MODEL = 'conversation_attribute'.freeze
|
|
|
|
def initialize(rule)
|
|
@rule = rule
|
|
@account = rule.account
|
|
|
|
file = File.read('./lib/filters/filter_keys.yml')
|
|
@filters = YAML.safe_load(file)
|
|
|
|
@conversation_filters = @filters['conversations']
|
|
@contact_filters = @filters['contacts']
|
|
@message_filters = @filters['messages']
|
|
end
|
|
|
|
def perform
|
|
@rule.conditions.each do |condition|
|
|
return false unless valid_condition?(condition)
|
|
end
|
|
|
|
true
|
|
end
|
|
|
|
private
|
|
|
|
def valid_condition?(condition)
|
|
key = condition['attribute_key']
|
|
|
|
conversation_filter = @conversation_filters[key]
|
|
contact_filter = @contact_filters[key]
|
|
message_filter = @message_filters[key]
|
|
|
|
if conversation_filter || contact_filter || message_filter
|
|
operation_valid?(condition, conversation_filter || contact_filter || message_filter)
|
|
else
|
|
custom_attribute_present?(key, condition['custom_attribute_type'])
|
|
end
|
|
end
|
|
|
|
def operation_valid?(condition, filter)
|
|
filter_operator = condition['filter_operator']
|
|
|
|
# attribute changed is a special case
|
|
return true if filter_operator == 'attribute_changed'
|
|
|
|
filter['filter_operators'].include?(filter_operator)
|
|
end
|
|
|
|
def custom_attribute_present?(attribute_key, attribute_model)
|
|
attribute_model = attribute_model.presence || self.class::ATTRIBUTE_MODEL
|
|
|
|
@account.custom_attribute_definitions.where(
|
|
attribute_model: attribute_model
|
|
).find_by(attribute_key: attribute_key).present?
|
|
end
|
|
end
|