fix(inboxes): block disabled inbox side effects
This commit is contained in:
@@ -1,7 +1,9 @@
|
||||
class Api::V1::Widget::ConversationsController < Api::V1::Widget::BaseController
|
||||
include Events::Types
|
||||
|
||||
DISABLED_INBOX_ACTIONS = [:create, :toggle_typing, :toggle_status, :update_last_seen, :set_custom_attributes, :destroy_custom_attributes].freeze
|
||||
DISABLED_INBOX_ACTIONS = [
|
||||
:create, :toggle_typing, :toggle_status, :update_last_seen, :set_custom_attributes, :destroy_custom_attributes, :transcript
|
||||
].freeze
|
||||
|
||||
before_action :render_not_found_if_empty, only: [:toggle_typing, :toggle_status, :set_custom_attributes, :destroy_custom_attributes]
|
||||
before_action :ensure_inbox_active, only: DISABLED_INBOX_ACTIONS
|
||||
|
||||
@@ -1,6 +1,8 @@
|
||||
class SendReplyJob < ApplicationJob
|
||||
queue_as :high
|
||||
|
||||
INBOX_DISABLED_ERROR = 'This inbox is currently disabled'.freeze
|
||||
|
||||
CHANNEL_SERVICES = {
|
||||
'Channel::TwitterProfile' => ::Twitter::SendOnTwitterService,
|
||||
'Channel::TwilioSms' => ::Twilio::SendOnTwilioService,
|
||||
@@ -17,7 +19,7 @@ class SendReplyJob < ApplicationJob
|
||||
|
||||
def perform(message_id)
|
||||
message = Message.find(message_id)
|
||||
return unless message.inbox.active?
|
||||
return mark_message_failed(message) unless message.inbox.active?
|
||||
|
||||
channel_name = message.conversation.inbox.channel.class.to_s
|
||||
|
||||
@@ -31,6 +33,10 @@ class SendReplyJob < ApplicationJob
|
||||
|
||||
private
|
||||
|
||||
def mark_message_failed(message)
|
||||
Messages::StatusUpdateService.new(message, 'failed', INBOX_DISABLED_ERROR).perform
|
||||
end
|
||||
|
||||
def send_on_facebook_page(message)
|
||||
if message.conversation.additional_attributes['type'] == 'instagram_direct_message'
|
||||
::Instagram::Messenger::SendOnInstagramService.new(message: message).perform
|
||||
|
||||
@@ -3,6 +3,7 @@ class CsatSurveyListener < BaseListener
|
||||
conversation = extract_conversation_and_account(event)[0]
|
||||
|
||||
return unless conversation.resolved?
|
||||
return unless conversation.inbox.active?
|
||||
|
||||
CsatSurveyService.new(conversation: conversation).perform
|
||||
end
|
||||
|
||||
@@ -274,6 +274,19 @@ RSpec.describe '/api/v1/widget/conversations/toggle_typing', type: :request do
|
||||
|
||||
describe 'POST /api/v1/widget/conversations/transcript' do
|
||||
context 'with a conversation' do
|
||||
it 'does not send transcript email when the inbox is disabled' do
|
||||
web_widget.inbox.update!(active: false)
|
||||
contact.update!(email: 'test@test.com')
|
||||
expect(ConversationReplyMailer).not_to receive(:with)
|
||||
|
||||
post '/api/v1/widget/conversations/transcript',
|
||||
headers: { 'X-Auth-Token' => token },
|
||||
params: { website_token: web_widget.website_token },
|
||||
as: :json
|
||||
|
||||
expect(response).to have_http_status(:forbidden)
|
||||
end
|
||||
|
||||
it 'sends transcript email' do
|
||||
contact.update(email: 'test@test.com')
|
||||
mailer = double
|
||||
|
||||
@@ -122,5 +122,18 @@ RSpec.describe SendReplyJob do
|
||||
message = create(:message, conversation: create(:conversation, inbox: tiktok_channel.inbox))
|
||||
expect_mapped_service_to_perform(message, 'Tiktok::SendOnTiktokService')
|
||||
end
|
||||
|
||||
it 'marks the message as failed when the inbox is disabled before the queued send runs' do
|
||||
twilio_channel = create(:channel_twilio_sms)
|
||||
message = create(:message, conversation: create(:conversation, inbox: twilio_channel.inbox))
|
||||
twilio_channel.inbox.update!(active: false)
|
||||
|
||||
expect(Twilio::SendOnTwilioService).not_to receive(:new)
|
||||
|
||||
described_class.perform_now(message.id)
|
||||
|
||||
expect(message.reload).to be_failed
|
||||
expect(message.external_error).to eq('This inbox is currently disabled')
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@@ -52,6 +52,14 @@ describe CsatSurveyListener do
|
||||
|
||||
listener.conversation_status_changed(event)
|
||||
end
|
||||
|
||||
it 'does not trigger CSAT survey service when the inbox is disabled' do
|
||||
resolved_conversation.inbox.update!(active: false)
|
||||
event = Events::Base.new(event_name, Time.zone.now, conversation: resolved_conversation)
|
||||
|
||||
expect(CsatSurveyService).not_to receive(:new)
|
||||
listener.conversation_status_changed(event)
|
||||
end
|
||||
end
|
||||
|
||||
context 'when conversation is not resolved' do
|
||||
|
||||
Reference in New Issue
Block a user