From c20410f3f4dd2b188e2741f046649d5c53cebf5c Mon Sep 17 00:00:00 2001 From: CristianDuta Date: Wed, 8 Mar 2023 14:12:49 +0100 Subject: [PATCH] feat: Ability to update CSAT over Client APIs (#6470) This PR allows updating CSAT over Client APIs. ref: #6328 Co-authored-by: Cristian Duta Co-authored-by: Sojan Jose --- .../api/v1/inboxes/messages_controller.rb | 8 ++++- .../api/v1/inbox/messages_controller_spec.rb | 32 ++++++++++++++++++- 2 files changed, 38 insertions(+), 2 deletions(-) diff --git a/app/controllers/public/api/v1/inboxes/messages_controller.rb b/app/controllers/public/api/v1/inboxes/messages_controller.rb index 925c16d38..4dc780fff 100644 --- a/app/controllers/public/api/v1/inboxes/messages_controller.rb +++ b/app/controllers/public/api/v1/inboxes/messages_controller.rb @@ -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 diff --git a/spec/controllers/public/api/v1/inbox/messages_controller_spec.rb b/spec/controllers/public/api/v1/inbox/messages_controller_spec.rb index 5ad9e91ef..5916b7fb7 100644 --- a/spec/controllers/public/api/v1/inbox/messages_controller_spec.rb +++ b/spec/controllers/public/api/v1/inbox/messages_controller_spec.rb @@ -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