Chatwoot/app/models/automation_rule.rb

48 lines
1.3 KiB
Ruby
Raw Normal View History

2022-01-10 07:11:59 +00:00
# == Schema Information
#
# Table name: automation_rules
#
# id :bigint not null, primary key
# actions :jsonb not null
2022-01-13 05:51:06 +00:00
# active :boolean default(TRUE), not null
2022-01-10 07:11:59 +00:00
# conditions :jsonb not null
# description :text
# event_name :string not null
# name :string not null
# created_at :datetime not null
# updated_at :datetime not null
# account_id :bigint not null
#
# Indexes
#
# index_automation_rules_on_account_id (account_id)
#
class AutomationRule < ApplicationRecord
belongs_to :account
validate :json_conditions_format
validate :json_actions_format
2022-03-21 07:42:27 +00:00
validates :account_id, presence: true
2022-01-10 07:11:59 +00:00
2022-01-13 05:51:06 +00:00
scope :active, -> { where(active: true) }
2022-01-10 07:11:59 +00:00
CONDITIONS_ATTRS = %w[country_code status browser_language assignee_id team_id referer].freeze
ACTIONS_ATTRS = %w[send_message add_label send_email_to_team assign_team assign_best_agents].freeze
private
def json_conditions_format
return if conditions.nil?
attributes = conditions.map { |obj, _| obj['attribute_key'] }
(attributes - CONDITIONS_ATTRS).blank?
end
def json_actions_format
return if actions.nil?
attributes = actions.map { |obj, _| obj['attribute_key'] }
(attributes - ACTIONS_ATTRS).blank?
end
end