feat: API to allow agents to delete a message (#1353)

This commit is contained in:
Pranav Raj S 2020-12-28 23:36:50 +05:30 committed by GitHub
parent 08f1e0eb32
commit 811657c9e7
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 58 additions and 2 deletions

View file

@ -11,9 +11,24 @@ class Api::V1::Accounts::Conversations::MessagesController < Api::V1::Accounts::
render_could_not_create_error(e.message)
end
def destroy
ActiveRecord::Base.transaction do
message.update!(content: I18n.t('conversations.messages.deleted'), deleted: true)
message.attachments.destroy_all
end
end
private
def message
@message ||= @conversation.messages.find(permitted_params[:id])
end
def message_finder
@message_finder ||= MessageFinder.new(@conversation, params)
end
def permitted_params
params.permit(:id)
end
end

View file

@ -55,7 +55,7 @@ class Message < ApplicationRecord
# [:submitted_email, :items, :submitted_values] : Used for bot message types
# [:email] : Used by conversation_continuity incoming email messages
# [:in_reply_to] : Used to reply to a particular tweet in threads
store :content_attributes, accessors: [:submitted_email, :items, :submitted_values, :email, :in_reply_to], coder: JSON
store :content_attributes, accessors: [:submitted_email, :items, :submitted_values, :email, :in_reply_to, :deleted], coder: JSON
store :external_source_ids, accessors: [:slack], coder: JSON, prefix: :external_source_id

View file

@ -0,0 +1 @@
json.partial! 'api/v1/models/message', message: @message

View file

@ -46,6 +46,8 @@ en:
period: Reporting period %{since} to %{until}
conversations:
messages:
deleted: This message was deleted
activity:
status:
resolved: "Conversation was marked resolved by %{user_name}"

View file

@ -49,7 +49,7 @@ Rails.application.routes.draw do
get 'meta', on: :collection
get 'search', on: :collection
scope module: :conversations do
resources :messages, only: [:index, :create]
resources :messages, only: [:index, :create, :destroy]
resources :assignments, only: [:create]
resources :labels, only: [:create, :index]
end

View file

@ -120,4 +120,42 @@ RSpec.describe 'Conversation Messages API', type: :request do
end
end
end
describe 'DELETE /api/v1/accounts/{account.id}/conversations/:conversation_id/messages/:id' do
let(:message) { create(:message, account: account) }
let(:conversation) { message.conversation }
context 'when it is an unauthenticated user' do
it 'returns unauthorized' do
delete "/api/v1/accounts/#{account.id}/conversations/#{conversation.display_id}/messages/#{message.id}"
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 'deletes the message' do
delete "/api/v1/accounts/#{account.id}/conversations/#{conversation.display_id}/messages/#{message.id}",
headers: agent.create_new_auth_token,
as: :json
expect(response).to have_http_status(:success)
expect(message.reload.content).to eq 'This message was deleted'
expect(message.reload.deleted).to eq true
end
end
context 'when the message id is invalid' do
let(:agent) { create(:user, account: account, role: :agent) }
it 'returns not found error' do
delete "/api/v1/accounts/#{account.id}/conversations/#{conversation.display_id}/messages/99999",
headers: agent.create_new_auth_token,
as: :json
expect(response).to have_http_status(:not_found)
end
end
end
end