From 7662fdce4791af91307e5b223285ab2d5e90b0a5 Mon Sep 17 00:00:00 2001 From: Muhsin Keloth Date: Wed, 28 Jul 2021 14:59:13 +0530 Subject: [PATCH] feat: CSAT response public APIs (#2670) --- .../public/api/v1/csat_survey_controller.rb | 32 ++++++++++++ .../api/v1/csat_survey/show.json.jbuilder | 1 + .../api/v1/csat_survey/update.json.jbuilder | 1 + .../api/v1/models/_csat_survey.json.jbuilder | 5 ++ config/routes.rb | 1 + .../api/v1/csat_survey_controller.spec.rb | 50 +++++++++++++++++++ 6 files changed, 90 insertions(+) create mode 100644 app/controllers/public/api/v1/csat_survey_controller.rb create mode 100644 app/views/public/api/v1/csat_survey/show.json.jbuilder create mode 100644 app/views/public/api/v1/csat_survey/update.json.jbuilder create mode 100644 app/views/public/api/v1/models/_csat_survey.json.jbuilder create mode 100644 spec/controllers/public/api/v1/csat_survey_controller.spec.rb diff --git a/app/controllers/public/api/v1/csat_survey_controller.rb b/app/controllers/public/api/v1/csat_survey_controller.rb new file mode 100644 index 000000000..b839bee77 --- /dev/null +++ b/app/controllers/public/api/v1/csat_survey_controller.rb @@ -0,0 +1,32 @@ +class Public::Api::V1::CsatSurveyController < PublicController + before_action :set_conversation + before_action :set_message + + def show; 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[:message]) + end + + private + + def set_conversation + return if params[:id].blank? + + @conversation = Conversation.find_by!(uuid: params[:id]) + end + + def set_message + @message = @conversation.messages.find_by!(content_type: 'input_csat') + end + + def message_update_params + params.permit(message: [{ submitted_values: [:name, :title, :value, { csat_survey_response: [:feedback_message, :rating] }] }]) + end + + def check_csat_locked + (Time.zone.now.to_date - @message.created_at.to_date).to_i > 14 + end +end diff --git a/app/views/public/api/v1/csat_survey/show.json.jbuilder b/app/views/public/api/v1/csat_survey/show.json.jbuilder new file mode 100644 index 000000000..1ab9e564d --- /dev/null +++ b/app/views/public/api/v1/csat_survey/show.json.jbuilder @@ -0,0 +1 @@ +json.partial! 'public/api/v1/models/csat_survey.json.jbuilder', resource: @message diff --git a/app/views/public/api/v1/csat_survey/update.json.jbuilder b/app/views/public/api/v1/csat_survey/update.json.jbuilder new file mode 100644 index 000000000..1ab9e564d --- /dev/null +++ b/app/views/public/api/v1/csat_survey/update.json.jbuilder @@ -0,0 +1 @@ +json.partial! 'public/api/v1/models/csat_survey.json.jbuilder', resource: @message diff --git a/app/views/public/api/v1/models/_csat_survey.json.jbuilder b/app/views/public/api/v1/models/_csat_survey.json.jbuilder new file mode 100644 index 000000000..f11dd61a6 --- /dev/null +++ b/app/views/public/api/v1/models/_csat_survey.json.jbuilder @@ -0,0 +1,5 @@ +json.id resource.id +json.csat_survey_response resource.csat_survey_response +json.inbox_avatar_url resource.inbox.avatar_url +json.conversation_id resource.conversation_id +json.created_at resource.created_at diff --git a/config/routes.rb b/config/routes.rb index 103063b13..e118ca0bb 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -220,6 +220,7 @@ Rails.application.routes.draw do end end end + resources :csat_survey, only: [:show, :update] end end end diff --git a/spec/controllers/public/api/v1/csat_survey_controller.spec.rb b/spec/controllers/public/api/v1/csat_survey_controller.spec.rb new file mode 100644 index 000000000..1cc411118 --- /dev/null +++ b/spec/controllers/public/api/v1/csat_survey_controller.spec.rb @@ -0,0 +1,50 @@ +require 'rails_helper' + +RSpec.describe 'Public Survey Responses API', type: :request do + describe 'GET public/api/v1/csat_survey/{uuid}' do + it 'return the csat response for that conversation' do + conversation = create(:conversation) + create(:message, conversation: conversation, content_type: 'input_csat') + get "/public/api/v1/csat_survey/#{conversation.uuid}" + data = JSON.parse(response.body) + expect(response).to have_http_status(:success) + expect(data['conversation_id']).to eq conversation.id + end + + it 'returns not found error for the open conversation' do + conversation = create(:conversation) + create(:message, conversation: conversation, content_type: 'text') + get "/public/api/v1/csat_survey/#{conversation.uuid}" + expect(response).to have_http_status(:not_found) + end + end + + describe 'PUT public/api/v1/csat_survey/{uuid}' do + params = { message: { submitted_values: { csat_survey_response: { rating: 4, feedback_message: 'amazing experience' } } } } + it 'update csat survey response for the conversation' do + conversation = create(:conversation) + message = create(:message, 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/csat_survey/#{conversation.uuid}", + params: params, + as: :json + expect(response).to have_http_status(:success) + data = JSON.parse(response.body) + expect(data['conversation_id']).to eq conversation.id + expect(data['csat_survey_response']['conversation_id']).to eq conversation.id + expect(data['csat_survey_response']['feedback_message']).to eq 'amazing experience' + expect(data['csat_survey_response']['rating']).to eq 4 + end + + it 'returns update error if CSAT message sent more than 14 days' do + conversation = create(:conversation) + message = create(:message, conversation: conversation, content_type: 'input_csat', created_at: 15.days.ago) + create(:csat_survey_response, conversation: conversation, message: message, rating: 4, feedback_message: 'amazing experience') + patch "/public/api/v1/csat_survey/#{conversation.uuid}", + params: params, + as: :json + expect(response).to have_http_status(:unprocessable_entity) + end + end +end