chore: Enable updating conversation additional attributes (#1611)
- Enable updating additional attributes via conversation API - Handle the case when additional_attributes is nil Fixes: #1357
This commit is contained in:
@@ -86,7 +86,8 @@ class Api::V1::Accounts::ConversationsController < Api::V1::Accounts::BaseContro
|
|||||||
account_id: Current.account.id,
|
account_id: Current.account.id,
|
||||||
inbox_id: @contact_inbox.inbox_id,
|
inbox_id: @contact_inbox.inbox_id,
|
||||||
contact_id: @contact_inbox.contact_id,
|
contact_id: @contact_inbox.contact_id,
|
||||||
contact_inbox_id: @contact_inbox.id
|
contact_inbox_id: @contact_inbox.id,
|
||||||
|
additional_attributes: params[:additional_attributes]
|
||||||
}
|
}
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|||||||
@@ -54,6 +54,8 @@ class Api::V1::Widget::MessagesController < Api::V1::Widget::BaseController
|
|||||||
end
|
end
|
||||||
|
|
||||||
def conversation_params
|
def conversation_params
|
||||||
|
# FIXME: typo referrer in additional attributes
|
||||||
|
# will probably require a migration.
|
||||||
{
|
{
|
||||||
account_id: inbox.account_id,
|
account_id: inbox.account_id,
|
||||||
inbox_id: inbox.id,
|
inbox_id: inbox.id,
|
||||||
|
|||||||
@@ -36,6 +36,7 @@ class Conversation < ApplicationRecord
|
|||||||
|
|
||||||
validates :account_id, presence: true
|
validates :account_id, presence: true
|
||||||
validates :inbox_id, presence: true
|
validates :inbox_id, presence: true
|
||||||
|
before_validation :validate_additional_attributes
|
||||||
|
|
||||||
enum status: { open: 0, resolved: 1, bot: 2 }
|
enum status: { open: 0, resolved: 1, bot: 2 }
|
||||||
|
|
||||||
@@ -136,6 +137,10 @@ class Conversation < ApplicationRecord
|
|||||||
|
|
||||||
private
|
private
|
||||||
|
|
||||||
|
def validate_additional_attributes
|
||||||
|
self.additional_attributes = {} unless additional_attributes.is_a?(Hash)
|
||||||
|
end
|
||||||
|
|
||||||
def set_bot_conversation
|
def set_bot_conversation
|
||||||
self.status = :bot if inbox.agent_bot_inbox&.active?
|
self.status = :bot if inbox.agent_bot_inbox&.active?
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -117,6 +117,38 @@ RSpec.describe 'Conversations API', type: :request do
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
describe 'POST /api/v1/accounts/{account.id}/conversations' do
|
||||||
|
let(:contact) { create(:contact, account: account) }
|
||||||
|
let(:inbox) { create(:inbox, account: account) }
|
||||||
|
let(:contact_inbox) { create(:contact_inbox, contact: contact, inbox: inbox) }
|
||||||
|
|
||||||
|
context 'when it is an unauthenticated user' do
|
||||||
|
it 'returns unauthorized' do
|
||||||
|
post "/api/v1/accounts/#{account.id}/conversations",
|
||||||
|
params: { source_id: contact_inbox.source_id },
|
||||||
|
as: :json
|
||||||
|
|
||||||
|
expect(response).to have_http_status(:unauthorized)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
context 'when it is an authenticated user' do
|
||||||
|
let(:agent) { create(:user, account: account, role: :agent) }
|
||||||
|
|
||||||
|
it 'creates a new conversation' 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 },
|
||||||
|
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({})
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
describe 'POST /api/v1/accounts/{account.id}/conversations/:id/toggle_status' do
|
describe 'POST /api/v1/accounts/{account.id}/conversations/:id/toggle_status' do
|
||||||
let(:conversation) { create(:conversation, account: account) }
|
let(:conversation) { create(:conversation, account: account) }
|
||||||
|
|
||||||
|
|||||||
@@ -100,6 +100,9 @@ post:
|
|||||||
source_id:
|
source_id:
|
||||||
type: string
|
type: string
|
||||||
description: Contact Source Id
|
description: Contact Source Id
|
||||||
|
additional_attributes:
|
||||||
|
type: object
|
||||||
|
description: Lets you specify attributes like browser information
|
||||||
|
|
||||||
responses:
|
responses:
|
||||||
200:
|
200:
|
||||||
|
|||||||
@@ -377,6 +377,10 @@
|
|||||||
"source_id": {
|
"source_id": {
|
||||||
"type": "string",
|
"type": "string",
|
||||||
"description": "Contact Source Id"
|
"description": "Contact Source Id"
|
||||||
|
},
|
||||||
|
"additional_attributes": {
|
||||||
|
"type": "object",
|
||||||
|
"description": "Lets you specify attributes like browser information"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user