fix: Condition based backend validation (#6554)
This commit is contained in:
@@ -25,6 +25,7 @@ class AutomationRule < ApplicationRecord
|
||||
|
||||
validate :json_conditions_format
|
||||
validate :json_actions_format
|
||||
validate :query_operator_presence
|
||||
validates :account_id, presence: true
|
||||
|
||||
scope :active, -> { where(active: true) }
|
||||
@@ -67,4 +68,11 @@ class AutomationRule < ApplicationRecord
|
||||
|
||||
errors.add(:actions, "Automation actions #{actions.join(',')} not supported.") if actions.any?
|
||||
end
|
||||
|
||||
def query_operator_presence
|
||||
return if conditions.blank?
|
||||
|
||||
operators = conditions.select { |obj, _| obj['query_operator'].nil? }
|
||||
errors.add(:conditions, 'Automation conditions should have query operator.') if operators.length > 1
|
||||
end
|
||||
end
|
||||
|
||||
@@ -48,5 +48,12 @@ RSpec.describe AutomationRule, type: :model do
|
||||
rule = FactoryBot.build(:automation_rule, params)
|
||||
expect(rule.valid?).to be true
|
||||
end
|
||||
|
||||
it 'returns invalid record' do
|
||||
params[:conditions][0].delete('query_operator')
|
||||
rule = FactoryBot.build(:automation_rule, params)
|
||||
expect(rule.valid?).to be false
|
||||
expect(rule.errors.messages[:conditions]).to eq(['Automation conditions should have query operator.'])
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user