Chore: Refactor round robin logic (#1015)

Co-authored-by: Pranav Raj S <pranav@thoughtwoot.com>
This commit is contained in:
Sojan Jose
2020-07-08 00:14:07 +05:30
committed by GitHub
parent 49db9c5d8a
commit 0fc0dc1683
18 changed files with 197 additions and 74 deletions

View File

@@ -188,7 +188,7 @@ class Conversation < ApplicationRecord
return unless conversation_status_changed_to_open?
return unless should_round_robin?
inbox.next_available_agent.then { |new_assignee| update_assignee(new_assignee) }
::RoundRobin::AssignmentService.new(conversation: self).perform
end
def create_status_change_message(user_name)

View File

@@ -64,11 +64,6 @@ class Inbox < ApplicationRecord
channel.class.name.to_s == 'Channel::WebWidget'
end
def next_available_agent
user_id = Redis::Alfred.rpoplpush(round_robin_key, round_robin_key)
account.users.find_by(id: user_id)
end
def webhook_data
{
id: id,
@@ -79,10 +74,6 @@ class Inbox < ApplicationRecord
private
def delete_round_robin_agents
Redis::Alfred.delete(round_robin_key)
end
def round_robin_key
format(Constants::RedisKeys::ROUND_ROBIN_AGENTS, inbox_id: id)
::RoundRobin::ManageService.new(inbox: self).clear_queue
end
end

View File

@@ -26,14 +26,10 @@ class InboxMember < ApplicationRecord
private
def add_agent_to_round_robin
Redis::Alfred.lpush(round_robin_key, user_id)
::RoundRobin::ManageService.new(inbox: inbox).add_agent_to_queue(user_id)
end
def remove_agent_from_round_robin
Redis::Alfred.lrem(round_robin_key, user_id)
end
def round_robin_key
format(Constants::RedisKeys::ROUND_ROBIN_AGENTS, inbox_id: inbox_id)
::RoundRobin::ManageService.new(inbox: inbox).remove_agent_from_queue(user_id)
end
end

View File

@@ -153,7 +153,6 @@ class Message < ApplicationRecord
end
def notify_via_mail
conversation_mail_key = Redis::Alfred::CONVERSATION_MAILER_KEY % conversation.id
if Redis::Alfred.get(conversation_mail_key).nil? && conversation.contact.email? && outgoing?
# set a redis key for the conversation so that we don't need to send email for every
# new message that comes in and we dont enque the delayed sidekiq job for every message
@@ -165,6 +164,10 @@ class Message < ApplicationRecord
end
end
def conversation_mail_key
format(::Redis::Alfred::CONVERSATION_MAILER_KEY, conversation_id: conversation.id)
end
def validate_attachments_limit(_attachment)
errors.add(attachments: 'exceeded maximum allowed') if attachments.size >= NUMBER_OF_PERMITTED_ATTACHMENTS
end