fix: Condition based backend validation (#6554)

This commit is contained in:
Tejaswini Chile
2023-02-27 20:22:07 +05:30
committed by GitHub
parent b76fda53a2
commit ce807d3251
2 changed files with 15 additions and 0 deletions

View File

@@ -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

View File

@@ -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