feat: Add API to get the active contacts (#1313)
This commit is contained in:
parent
11725de09a
commit
88b2469dc8
6 changed files with 57 additions and 2 deletions
|
@ -8,6 +8,12 @@ class Api::V1::Accounts::ContactsController < Api::V1::Accounts::BaseController
|
|||
@contacts = Current.account.contacts
|
||||
end
|
||||
|
||||
# returns online contacts
|
||||
def active
|
||||
@contacts = Current.account.contacts.where(id: ::OnlineStatusTracker
|
||||
.get_available_contact_ids(Current.account.id))
|
||||
end
|
||||
|
||||
def show; end
|
||||
|
||||
def create
|
||||
|
|
|
@ -3,6 +3,10 @@ class ContactPolicy < ApplicationPolicy
|
|||
true
|
||||
end
|
||||
|
||||
def active?
|
||||
true
|
||||
end
|
||||
|
||||
def search?
|
||||
true
|
||||
end
|
||||
|
|
5
app/views/api/v1/accounts/contacts/active.json.jbuilder
Normal file
5
app/views/api/v1/accounts/contacts/active.json.jbuilder
Normal file
|
@ -0,0 +1,5 @@
|
|||
json.payload do
|
||||
json.array! @contacts do |contact|
|
||||
json.partial! 'api/v1/models/contact.json.jbuilder', resource: contact, with_contact_inboxes: true
|
||||
end
|
||||
end
|
|
@ -64,6 +64,7 @@ Rails.application.routes.draw do
|
|||
|
||||
resources :contacts, only: [:index, :show, :update, :create] do
|
||||
collection do
|
||||
get :active
|
||||
get :search
|
||||
end
|
||||
scope module: :contacts do
|
||||
|
|
|
@ -37,9 +37,13 @@ module OnlineStatusTracker
|
|||
format(::Redis::Alfred::ONLINE_STATUS, account_id: account_id)
|
||||
end
|
||||
|
||||
def self.get_available_contact_ids(account_id)
|
||||
::Redis::Alfred.zrangebyscore(presence_key(account_id, 'Contact'), (Time.zone.now - PRESENCE_DURATION).to_i, Time.now.to_i)
|
||||
end
|
||||
|
||||
def self.get_available_contacts(account_id)
|
||||
contact_ids = ::Redis::Alfred.zrangebyscore(presence_key(account_id, 'Contact'), (Time.zone.now - PRESENCE_DURATION).to_i, Time.now.to_i)
|
||||
contact_ids.index_with { |_id| 'online' }
|
||||
# returns {id1: 'online', id2: 'online'}
|
||||
get_available_contact_ids(account_id).index_with { |_id| 'online' }
|
||||
end
|
||||
|
||||
def self.get_available_users(account_id)
|
||||
|
|
|
@ -30,6 +30,41 @@ RSpec.describe 'Contacts API', type: :request do
|
|||
end
|
||||
end
|
||||
|
||||
describe 'GET /api/v1/accounts/{account.id}/contacts/active' do
|
||||
context 'when it is an unauthenticated user' do
|
||||
it 'returns unauthorized' do
|
||||
get "/api/v1/accounts/#{account.id}/contacts/active"
|
||||
|
||||
expect(response).to have_http_status(:unauthorized)
|
||||
end
|
||||
end
|
||||
|
||||
context 'when it is an authenticated user' do
|
||||
let(:admin) { create(:user, account: account, role: :administrator) }
|
||||
let!(:contact) { create(:contact, account: account) }
|
||||
|
||||
it 'returns no contacts if no are online' do
|
||||
get "/api/v1/accounts/#{account.id}/contacts/active",
|
||||
headers: admin.create_new_auth_token,
|
||||
as: :json
|
||||
|
||||
expect(response).to have_http_status(:success)
|
||||
expect(response.body).not_to include(contact.email)
|
||||
end
|
||||
|
||||
it 'returns all contacts who are online' do
|
||||
allow(::OnlineStatusTracker).to receive(:get_available_contact_ids).and_return([contact.id])
|
||||
|
||||
get "/api/v1/accounts/#{account.id}/contacts/active",
|
||||
headers: admin.create_new_auth_token,
|
||||
as: :json
|
||||
|
||||
expect(response).to have_http_status(:success)
|
||||
expect(response.body).to include(contact.email)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
describe 'GET /api/v1/accounts/{account.id}/contacts/search' do
|
||||
context 'when it is an unauthenticated user' do
|
||||
it 'returns unauthorized' do
|
||||
|
|
Loading…
Reference in a new issue