chore: Automate conversation display_id generation with db triggers (#1412)
Automate conversation display_id generation with db triggers Co-authored-by: Saurabh Mehta <saurabh1.mehta@airtel.com> Co-authored-by: Sojan Jose <sojan@pepalo.com>
This commit is contained in:
@@ -91,4 +91,8 @@ class Account < ApplicationRecord
|
||||
def notify_creation
|
||||
Rails.configuration.dispatcher.dispatch(ACCOUNT_CREATED, Time.zone.now, account: self)
|
||||
end
|
||||
|
||||
trigger.after(:insert).for_each(:row) do
|
||||
"execute format('create sequence IF NOT EXISTS conv_dpid_seq_%s', NEW.id);"
|
||||
end
|
||||
end
|
||||
|
||||
@@ -7,12 +7,12 @@
|
||||
# agent_last_seen_at :datetime
|
||||
# contact_last_seen_at :datetime
|
||||
# identifier :string
|
||||
# last_activity_at :datetime not null
|
||||
# last_activity_at :datetime
|
||||
# locked :boolean default(FALSE)
|
||||
# status :integer default("open"), not null
|
||||
# uuid :uuid not null
|
||||
# created_at :datetime not null
|
||||
# updated_at :datetime not null
|
||||
# created_at :datetime
|
||||
# updated_at :datetime
|
||||
# account_id :integer not null
|
||||
# assignee_id :integer
|
||||
# contact_id :bigint
|
||||
@@ -52,12 +52,13 @@ class Conversation < ApplicationRecord
|
||||
has_many :messages, dependent: :destroy, autosave: true
|
||||
|
||||
before_create :set_bot_conversation
|
||||
before_create :set_display_id, unless: :display_id?
|
||||
|
||||
# wanted to change this to after_update commit. But it ended up creating a loop
|
||||
# reinvestigate in future and identity the implications
|
||||
after_update :notify_status_change, :create_activity
|
||||
after_create_commit :notify_conversation_creation, :queue_conversation_auto_resolution_job
|
||||
after_save :run_round_robin
|
||||
after_commit :set_display_id, unless: :display_id?
|
||||
|
||||
delegate :auto_resolve_duration, to: :account
|
||||
|
||||
@@ -154,10 +155,7 @@ class Conversation < ApplicationRecord
|
||||
end
|
||||
|
||||
def set_display_id
|
||||
self.display_id = loop do
|
||||
next_display_id = account.conversations.maximum('display_id').to_i + 1
|
||||
break next_display_id unless account.conversations.exists?(display_id: next_display_id)
|
||||
end
|
||||
reload
|
||||
end
|
||||
|
||||
def create_activity
|
||||
@@ -289,4 +287,9 @@ class Conversation < ApplicationRecord
|
||||
def mute_period
|
||||
6.hours
|
||||
end
|
||||
|
||||
# creating db triggers
|
||||
trigger.before(:insert).for_each(:row) do
|
||||
"NEW.display_id := nextval('conv_dpid_seq_' || NEW.account_id);"
|
||||
end
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user