Chatwoot/app/models/automation_rule.rb

70 lines
2.1 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
include Rails.application.routes.url_helpers
2022-01-10 07:11:59 +00:00
belongs_to :account
2022-03-30 02:38:58 +00:00
has_many_attached :files
2022-01-10 07:11:59 +00:00
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) }
CONDITIONS_ATTRS = %w[content email country_code status message_type browser_language assignee_id team_id referer city company inbox_id].freeze
ACTIONS_ATTRS = %w[send_message add_label send_email_to_team assign_team assign_agent send_webhook_event mute_conversation send_attachment
change_status resolve_conversation snooze_conversation send_email_transcript].freeze
def file_base_data
files.map do |file|
{
id: file.id,
automation_rule_id: id,
file_type: file.content_type,
account_id: account_id,
file_url: url_for(file),
blob_id: file.blob_id,
filename: file.filename.to_s
}
end
end
2022-01-10 07:11:59 +00:00
private
def json_conditions_format
return if conditions.blank?
2022-01-10 07:11:59 +00:00
attributes = conditions.map { |obj, _| obj['attribute_key'] }
2022-03-30 02:38:58 +00:00
conditions = attributes - CONDITIONS_ATTRS
conditions -= account.custom_attribute_definitions.pluck(:attribute_key)
errors.add(:conditions, "Automation conditions #{conditions.join(',')} not supported.") if conditions.any?
2022-01-10 07:11:59 +00:00
end
def json_actions_format
return if actions.blank?
2022-01-10 07:11:59 +00:00
attributes = actions.map { |obj, _| obj['action_name'] }
2022-03-30 02:38:58 +00:00
actions = attributes - ACTIONS_ATTRS
errors.add(:actions, "Automation actions #{actions.join(',')} not supported.") if actions.any?
2022-01-10 07:11:59 +00:00
end
end