Files
chatwoot/app/models/canned_response.rb
T
Shivam Mishra 991bc5a265 feat: expand workspace cache_keys to canned responses and agents
Adds CannedResponse and AccountUser to the per-account cacheable_models so
their cache keys are exposed via /cache_keys and broadcast over ActionCable
on mutation. The endpoint now delegates to Account#cache_keys instead of
listing the three models inline, so future additions only need to touch the
concern.

AccountUser uses bespoke field-gated callbacks rather than the blanket
AccountCacheRevalidator: active_at is bumped on every page navigation and
would otherwise invalidate the agent cache constantly. User adds a
cross-account callback so name/email/display_name/confirmed_at changes
propagate, since those fields appear in the agent serializer but live on
the User model.
2026-05-21 17:21:17 +05:30

33 lines
1.0 KiB
Ruby

# == Schema Information
#
# Table name: canned_responses
#
# id :integer not null, primary key
# content :text
# short_code :string
# created_at :datetime not null
# updated_at :datetime not null
# account_id :integer not null
#
class CannedResponse < ApplicationRecord
include AccountCacheRevalidator
validates :content, presence: true
validates :short_code, presence: true
validates :account, presence: true
validates :short_code, uniqueness: { scope: :account_id }
belongs_to :account
scope :order_by_search, lambda { |search|
short_code_starts_with = sanitize_sql_array(['WHEN short_code ILIKE ? THEN 1', "#{search}%"])
short_code_like = sanitize_sql_array(['WHEN short_code ILIKE ? THEN 0.5', "%#{search}%"])
content_like = sanitize_sql_array(['WHEN content ILIKE ? THEN 0.2', "%#{search}%"])
order_clause = "CASE #{short_code_starts_with} #{short_code_like} #{content_like} ELSE 0 END"
order(Arel.sql(order_clause) => :desc)
}
end