Skip to content

Commit

Permalink
feat: Ability to update CSAT over Client APIs (#6470)
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
3 people committed Mar 8, 2023
1 parent 9bc0f67 commit c20410f
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 2 deletions.
8 changes: 7 additions & 1 deletion app/controllers/public/api/v1/inboxes/messages_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ def create
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
Expand Down Expand Up @@ -43,7 +45,7 @@ def message_finder
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
Expand All @@ -64,4 +66,8 @@ def message_params
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
32 changes: 31 additions & 1 deletion spec/controllers/public/api/v1/inbox/messages_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@
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}",
Expand All @@ -65,5 +65,35 @@
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

0 comments on commit c20410f

Please sign in to comment.