diff --git a/app/controllers/api/v1/widget/conversations_controller.rb b/app/controllers/api/v1/widget/conversations_controller.rb index c54298cd9..283b67395 100644 --- a/app/controllers/api/v1/widget/conversations_controller.rb +++ b/app/controllers/api/v1/widget/conversations_controller.rb @@ -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 diff --git a/app/jobs/send_reply_job.rb b/app/jobs/send_reply_job.rb index ce6beb98b..fb6bcddae 100644 --- a/app/jobs/send_reply_job.rb +++ b/app/jobs/send_reply_job.rb @@ -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 diff --git a/app/listeners/csat_survey_listener.rb b/app/listeners/csat_survey_listener.rb index 82cf09497..138696ab7 100644 --- a/app/listeners/csat_survey_listener.rb +++ b/app/listeners/csat_survey_listener.rb @@ -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 diff --git a/spec/controllers/api/v1/widget/conversations_controller_spec.rb b/spec/controllers/api/v1/widget/conversations_controller_spec.rb index 954e5ab7c..ab01c2ca3 100644 --- a/spec/controllers/api/v1/widget/conversations_controller_spec.rb +++ b/spec/controllers/api/v1/widget/conversations_controller_spec.rb @@ -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 diff --git a/spec/jobs/send_reply_job_spec.rb b/spec/jobs/send_reply_job_spec.rb index 3f95164b5..32a35d6d4 100644 --- a/spec/jobs/send_reply_job_spec.rb +++ b/spec/jobs/send_reply_job_spec.rb @@ -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 diff --git a/spec/listeners/csat_survey_listener_spec.rb b/spec/listeners/csat_survey_listener_spec.rb index 0f957dcb4..cc0c44906 100644 --- a/spec/listeners/csat_survey_listener_spec.rb +++ b/spec/listeners/csat_survey_listener_spec.rb @@ -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