feat: Ability to update CSAT over Client APIs (#6470)

This PR allows updating CSAT over Client APIs.

ref: #6328

Co-authored-by: Cristian Duta <Cristian.Duta@ti8m.ch>
Co-authored-by: Sojan Jose <sojan@pepalo.com>
This commit is contained in:
CristianDuta
2023-03-08 14:12:49 +01:00
committed by GitHub
parent 9bc0f67f54
commit c20410f3f4
2 changed files with 38 additions and 2 deletions

View File

@@ -12,6 +12,8 @@ class Public::Api::V1::Inboxes::MessagesController < Public::Api::V1::InboxesCon
end
def update
render json: { error: 'You cannot update the CSAT survey after 14 days' }, status: :unprocessable_entity and return if check_csat_locked
@message.update!(message_update_params)
rescue StandardError => e
render json: { error: @contact.errors, message: e.message }.to_json, status: :internal_server_error
@@ -43,7 +45,7 @@ class Public::Api::V1::Inboxes::MessagesController < Public::Api::V1::InboxesCon
end
def message_update_params
params.permit(submitted_values: [:name, :title, :value])
params.permit(submitted_values: [:name, :title, :value, { csat_survey_response: [:feedback_message, :rating] }])
end
def permitted_params
@@ -64,4 +66,8 @@ class Public::Api::V1::Inboxes::MessagesController < Public::Api::V1::InboxesCon
message_type: :incoming
}
end
def check_csat_locked
(Time.zone.now.to_date - @message.created_at.to_date).to_i > 14 and @message.content_type == 'input_csat'
end
end

View File

@@ -55,7 +55,7 @@ RSpec.describe 'Public Inbox Contact Conversation Messages API', type: :request
end
describe 'PATCH /public/api/v1/inboxes/{identifier}/contact/{source_id}/conversations/{conversation_id}/messages/{id}' do
it 'creates a message in the conversation' do
it 'updates a message in the conversation' do
message = create(:message, account: conversation.account, inbox: conversation.inbox, conversation: conversation)
patch "/public/api/v1/inboxes/#{api_channel.identifier}/contacts/#{contact_inbox.source_id}/conversations/" \
"#{conversation.display_id}/messages/#{message.id}",
@@ -65,5 +65,35 @@ RSpec.describe 'Public Inbox Contact Conversation Messages API', type: :request
data = JSON.parse(response.body)
expect(data['content_attributes']['submitted_values'].first['title']).to eq 'test'
end
it 'updates CSAT survey response for the conversation' do
message = create(:message, account: conversation.account, inbox: conversation.inbox, conversation: conversation, content_type: 'input_csat')
# since csat survey is created in async job, we are mocking the creation.
create(:csat_survey_response, conversation: conversation, message: message, rating: 4, feedback_message: 'amazing experience')
patch "/public/api/v1/inboxes/#{api_channel.identifier}/contacts/#{contact_inbox.source_id}/conversations/" \
"#{conversation.display_id}/messages/#{message.id}",
params: { submitted_values: { csat_survey_response: { rating: 4, feedback_message: 'amazing experience' } } },
as: :json
expect(response).to have_http_status(:success)
data = JSON.parse(response.body)
expect(data['content_attributes']['submitted_values']['csat_survey_response']['feedback_message']).to eq 'amazing experience'
expect(data['content_attributes']['submitted_values']['csat_survey_response']['rating']).to eq 4
end
it 'returns update error if CSAT message sent more than 14 days' do
message = create(:message, account: conversation.account, inbox: conversation.inbox, conversation: conversation, content_type: 'input_csat',
created_at: 15.days.ago)
# since csat survey is created in async job, we are mocking the creation.
create(:csat_survey_response, conversation: conversation, message: message, rating: 4, feedback_message: 'amazing experience')
patch "/public/api/v1/inboxes/#{api_channel.identifier}/contacts/#{contact_inbox.source_id}/conversations/" \
"#{conversation.display_id}/messages/#{message.id}",
params: { submitted_values: { csat_survey_response: { rating: 4, feedback_message: 'amazing experience' } } },
as: :json
expect(response).to have_http_status(:unprocessable_entity)
end
end
end