Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: Add an API to find the contacts using contact inbox sourceId (#…
- Loading branch information
1 parent
ffc2d98
commit 12a64f1
Showing
4 changed files
with
98 additions
and
0 deletions.
There are no files selected for viewing
21 changes: 21 additions & 0 deletions
21
app/controllers/api/v1/accounts/contact_inboxes_controller.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
class Api::V1::Accounts::ContactInboxesController < Api::V1::Accounts::BaseController | ||
before_action :ensure_inbox | ||
|
||
def filter | ||
contact_inbox = @inbox.contact_inboxes.where(inbox_id: permitted_params[:inbox_id], source_id: permitted_params[:source_id]) | ||
return head :not_found if contact_inbox.empty? | ||
|
||
@contact = contact_inbox.first.contact | ||
end | ||
|
||
private | ||
|
||
def ensure_inbox | ||
@inbox = Current.account.inboxes.find(permitted_params[:inbox_id]) | ||
authorize @inbox, :show? | ||
end | ||
|
||
def permitted_params | ||
params.permit(:inbox_id, :source_id) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
json.partial! 'api/v1/models/contact', formats: [:json], resource: @contact, with_contact_inboxes: true |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
71 changes: 71 additions & 0 deletions
71
spec/controllers/api/v1/accounts/contact_inboxes_controller_spec.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
require 'rails_helper' | ||
|
||
RSpec.describe 'Contact Inboxes API', type: :request do | ||
let(:account) { create(:account) } | ||
|
||
let(:inbox) { create(:inbox, account: account) } | ||
let(:contact) { create(:contact, account: account) } | ||
let!(:contact_inbox) { create(:contact_inbox, contact: contact, inbox: inbox) } | ||
|
||
describe 'POST /api/v1/accounts/{account.id}/contact_inboxes/filter' do | ||
let(:admin) { create(:user, account: account, role: :administrator) } | ||
|
||
context 'when it is an unauthenticated user' do | ||
it 'returns unauthorized' do | ||
post "/api/v1/accounts/#{account.id}/contact_inboxes/filter" | ||
|
||
expect(response).to have_http_status(:unauthorized) | ||
end | ||
end | ||
|
||
context 'when it is an authenticated admin user' do | ||
it 'returns not found if the params are invalid' do | ||
post "/api/v1/accounts/#{account.id}/contact_inboxes/filter", | ||
headers: admin.create_new_auth_token, | ||
params: { inbox_id: inbox.id, source_id: 'random_source_id' }, | ||
as: :json | ||
|
||
expect(response).to have_http_status(:not_found) | ||
end | ||
|
||
it 'returns the contact if the params are valid' do | ||
post "/api/v1/accounts/#{account.id}/contact_inboxes/filter", | ||
headers: admin.create_new_auth_token, | ||
params: { inbox_id: inbox.id, source_id: contact_inbox.source_id }, | ||
as: :json | ||
|
||
expect(response).to have_http_status(:success) | ||
response_body = response.parsed_body | ||
expect(response_body['id']).to eq(contact.id) | ||
expect(response_body['contact_inboxes'].first['source_id']).to eq(contact_inbox.source_id) | ||
end | ||
end | ||
|
||
context 'when it is an authenticated agent user' do | ||
let(:agent_with_inbox_access) { create(:user, account: account, role: :agent) } | ||
let(:agent_without_inbox_access) { create(:user, account: account, role: :agent) } | ||
|
||
before do | ||
create(:inbox_member, user: agent_with_inbox_access, inbox: inbox) | ||
end | ||
|
||
it 'returns unauthorized if agent does not have inbox access' do | ||
post "/api/v1/accounts/#{account.id}/contact_inboxes/filter", | ||
headers: agent_without_inbox_access.create_new_auth_token, | ||
params: { inbox_id: inbox.id, source_id: contact_inbox.source_id }, | ||
as: :json | ||
|
||
expect(response).to have_http_status(:unauthorized) | ||
end | ||
|
||
it 'returns success if agent have inbox access' do | ||
post "/api/v1/accounts/#{account.id}/contact_inboxes/filter", | ||
headers: agent_with_inbox_access.create_new_auth_token, | ||
params: { inbox_id: inbox.id, source_id: contact_inbox.source_id }, | ||
as: :json | ||
|
||
expect(response).to have_http_status(:success) | ||
end | ||
end | ||
end | ||
end |