From 8db5a528efd82551fbe2053cb77cf18691ff75d5 Mon Sep 17 00:00:00 2001 From: Sojan Jose Date: Mon, 1 Aug 2022 15:32:34 +0200 Subject: [PATCH] chore: Roundrobin should consider auto-offline false config (#5160) Previously round-robin conversation assignment logic wasn't considering the case where the Agent who had auto-offline config set as false. fixes: #4780 --- lib/online_status_tracker.rb | 13 ++++++++++++- spec/lib/online_status_tracker_spec.rb | 24 ++++++++++++++++++++++++ 2 files changed, 36 insertions(+), 1 deletion(-) create mode 100644 spec/lib/online_status_tracker_spec.rb diff --git a/lib/online_status_tracker.rb b/lib/online_status_tracker.rb index ec5f65812..2dcc102a8 100644 --- a/lib/online_status_tracker.rb +++ b/lib/online_status_tracker.rb @@ -48,10 +48,21 @@ module OnlineStatusTracker end def self.get_available_users(account_id) - user_ids = ::Redis::Alfred.zrangebyscore(presence_key(account_id, 'User'), (Time.zone.now - PRESENCE_DURATION).to_i, Time.now.to_i) + user_ids = get_available_user_ids(account_id) + return {} if user_ids.blank? user_availabilities = ::Redis::Alfred.hmget(status_key(account_id), user_ids) user_ids.map.with_index { |id, index| [id, (user_availabilities[index] || 'online')] }.to_h end + + def self.get_available_user_ids(account_id) + account = Account.find(account_id) + # TODO: to migrate this to zrange as its is being deprecated + # https://redis.io/commands/zrangebyscore/ + user_ids = ::Redis::Alfred.zrangebyscore(presence_key(account_id, 'User'), (Time.zone.now - PRESENCE_DURATION).to_i, Time.now.to_i) + # since we are dealing with redis items as string, casting to string + user_ids += account.account_users.where(auto_offline: false)&.map(&:user_id)&.map(&:to_s) + user_ids.uniq + end end diff --git a/spec/lib/online_status_tracker_spec.rb b/spec/lib/online_status_tracker_spec.rb new file mode 100644 index 000000000..cb7770d42 --- /dev/null +++ b/spec/lib/online_status_tracker_spec.rb @@ -0,0 +1,24 @@ +require 'rails_helper' + +describe OnlineStatusTracker do + let!(:account) { create(:account) } + let!(:user1) { create(:user, account: account) } + let!(:user2) { create(:user, account: account) } + let!(:user3) { create(:user, account: account) } + + context 'when get_available_users' do + before do + described_class.update_presence(account.id, 'User', user1.id) + end + + it 'returns only the online user ids with presence' do + expect(described_class.get_available_users(account.id).keys).to contain_exactly(user1.id.to_s) + expect(described_class.get_available_users(account.id).values).not_to include(user3.id) + end + + it 'returns agents who have auto offline configured false' do + user2.account_users.first.update(auto_offline: false) + expect(described_class.get_available_users(account.id).keys).to contain_exactly(user1.id.to_s, user2.id.to_s) + end + end +end