feat: Contactable Inboxes API (#2101)
- Add endpoint which lists inboxes through which a contact can be contacted - Conversation creation API auto-creates contact_inbox for specific channels [ Twilio, email, api] - Ability to send the initial message payload along with the conversation creation - Fixes #1678 ( issue saving additional attributes for conversation )
This commit is contained in:
@@ -189,6 +189,34 @@ RSpec.describe 'Contacts API', type: :request do
|
||||
end
|
||||
end
|
||||
|
||||
describe 'GET /api/v1/accounts/{account.id}/contacts/:id/contactable_inboxes' do
|
||||
let!(:contact) { create(:contact, account: account) }
|
||||
|
||||
context 'when it is an unauthenticated user' do
|
||||
it 'returns unauthorized' do
|
||||
get "/api/v1/accounts/#{account.id}/contacts/#{contact.id}/contactable_inboxes"
|
||||
|
||||
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) }
|
||||
|
||||
it 'shows the contact' do
|
||||
inbox_service = double
|
||||
allow(Contacts::ContactableInboxesService).to receive(:new).and_return(inbox_service)
|
||||
allow(inbox_service).to receive(:get).and_return({})
|
||||
expect(inbox_service).to receive(:get).and_return({})
|
||||
get "/api/v1/accounts/#{account.id}/contacts/#{contact.id}/contactable_inboxes",
|
||||
headers: admin.create_new_auth_token,
|
||||
as: :json
|
||||
|
||||
expect(response).to have_http_status(:success)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
describe 'POST /api/v1/accounts/{account.id}/contacts' do
|
||||
let(:custom_attributes) { { test: 'test', test1: 'test1' } }
|
||||
let(:valid_params) { { contact: { name: 'test', custom_attributes: custom_attributes } } }
|
||||
|
||||
@@ -151,14 +151,43 @@ RSpec.describe 'Conversations API', type: :request do
|
||||
|
||||
it 'creates a new conversation' do
|
||||
allow(Rails.configuration.dispatcher).to receive(:dispatch)
|
||||
additional_attributes = { test: 'test' }
|
||||
post "/api/v1/accounts/#{account.id}/conversations",
|
||||
headers: agent.create_new_auth_token,
|
||||
params: { source_id: contact_inbox.source_id },
|
||||
params: { source_id: contact_inbox.source_id, additional_attributes: additional_attributes },
|
||||
as: :json
|
||||
|
||||
expect(response).to have_http_status(:success)
|
||||
response_data = JSON.parse(response.body, symbolize_names: true)
|
||||
expect(response_data[:additional_attributes]).to eq(additional_attributes)
|
||||
end
|
||||
|
||||
it 'creates a new conversation with message when message is passed' do
|
||||
allow(Rails.configuration.dispatcher).to receive(:dispatch)
|
||||
post "/api/v1/accounts/#{account.id}/conversations",
|
||||
headers: agent.create_new_auth_token,
|
||||
params: { source_id: contact_inbox.source_id, message: { content: 'hi' } },
|
||||
as: :json
|
||||
|
||||
expect(response).to have_http_status(:success)
|
||||
response_data = JSON.parse(response.body, symbolize_names: true)
|
||||
expect(response_data[:additional_attributes]).to eq({})
|
||||
expect(account.conversations.find_by(display_id: response_data[:id]).messages.first.content).to eq 'hi'
|
||||
end
|
||||
|
||||
it 'calls contact inbox builder if contact_id and inbox_id is present' do
|
||||
builder = double
|
||||
contact = create(:contact, account: account)
|
||||
inbox = create(:inbox, account: account)
|
||||
allow(Rails.configuration.dispatcher).to receive(:dispatch)
|
||||
allow(ContactInboxBuilder).to receive(:new).and_return(builder)
|
||||
allow(builder).to receive(:perform)
|
||||
expect(builder).to receive(:perform)
|
||||
|
||||
post "/api/v1/accounts/#{account.id}/conversations",
|
||||
headers: agent.create_new_auth_token,
|
||||
params: { contact_id: contact.id, inbox_id: inbox.id },
|
||||
as: :json
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user