diff --git a/app/javascript/dashboard/components/widgets/WootWriter/ReplyBottomPanel.vue b/app/javascript/dashboard/components/widgets/WootWriter/ReplyBottomPanel.vue index e7a0b5995..636601dcb 100644 --- a/app/javascript/dashboard/components/widgets/WootWriter/ReplyBottomPanel.vue +++ b/app/javascript/dashboard/components/widgets/WootWriter/ReplyBottomPanel.vue @@ -80,7 +80,7 @@ @click="toggleMessageSignature" />
- - provider_config['api_key'], 'Content-Type' => 'application/json' } end - def has_24_hour_messaging_window? + def messaging_window_enabled? true end diff --git a/app/models/concerns/channelable.rb b/app/models/concerns/channelable.rb index e60066d5f..e54d5f3a6 100644 --- a/app/models/concerns/channelable.rb +++ b/app/models/concerns/channelable.rb @@ -6,7 +6,7 @@ module Channelable has_one :inbox, as: :channel, dependent: :destroy_async end - def has_24_hour_messaging_window? + def messaging_window_enabled? false end end diff --git a/app/models/conversation.rb b/app/models/conversation.rb index cd45adf7b..4791ba924 100644 --- a/app/models/conversation.rb +++ b/app/models/conversation.rb @@ -93,23 +93,24 @@ class Conversation < ApplicationRecord delegate :auto_resolve_duration, to: :account def can_reply? + channel = inbox&.channel + return can_reply_on_instagram? if additional_attributes['type'] == 'instagram_direct_message' - return true unless inbox&.channel&.has_24_hour_messaging_window? + return true unless channel&.messaging_window_enabled? - return false if last_incoming_message.nil? - - last_message_less_than_24_hrs? + messaging_window = inbox.api? ? channel.additional_attributes['agent_reply_time_window'].to_i : 24 + last_message_in_messaging_window?(messaging_window) end def last_incoming_message messages&.incoming&.last end - def last_message_less_than_24_hrs? + def last_message_in_messaging_window?(time) return false if last_incoming_message.nil? - Time.current < last_incoming_message.created_at + 24.hours + Time.current < last_incoming_message.created_at + time.hours end def can_reply_on_instagram? @@ -120,7 +121,7 @@ class Conversation < ApplicationRecord if global_config['ENABLE_MESSENGER_CHANNEL_HUMAN_AGENT'] Time.current < last_incoming_message.created_at + 7.days else - last_message_less_than_24_hrs? + last_message_in_messaging_window?(24) end end diff --git a/spec/models/channel/twilio_sms_spec.rb b/spec/models/channel/twilio_sms_spec.rb index 8aab34a7a..493c644ac 100644 --- a/spec/models/channel/twilio_sms_spec.rb +++ b/spec/models/channel/twilio_sms_spec.rb @@ -7,7 +7,7 @@ RSpec.describe Channel::TwilioSms do let!(:whatsapp_channel) { create(:channel_twilio_sms, medium: :whatsapp) } it 'returns true' do - expect(whatsapp_channel.has_24_hour_messaging_window?).to eq true + expect(whatsapp_channel.messaging_window_enabled?).to eq true expect(whatsapp_channel.name).to eq 'Whatsapp' expect(whatsapp_channel.medium).to eq 'whatsapp' end @@ -17,7 +17,7 @@ RSpec.describe Channel::TwilioSms do let!(:sms_channel) { create(:channel_twilio_sms, medium: :sms) } it 'returns false' do - expect(sms_channel.has_24_hour_messaging_window?).to eq false + expect(sms_channel.messaging_window_enabled?).to eq false expect(sms_channel.name).to eq 'Twilio SMS' expect(sms_channel.medium).to eq 'sms' end diff --git a/spec/models/conversation_spec.rb b/spec/models/conversation_spec.rb index e83ee5195..1b1e84ea5 100644 --- a/spec/models/conversation_spec.rb +++ b/spec/models/conversation_spec.rb @@ -545,6 +545,54 @@ RSpec.describe Conversation, type: :model do end end end + + describe 'on API channels' do + let!(:api_channel) { create(:channel_api, additional_attributes: {}) } + let!(:api_channel_with_limit) { create(:channel_api, additional_attributes: { agent_reply_time_window: '12' }) } + + context 'when agent_reply_time_window is not configured' do + it 'return true irrespective of the last message time' do + conversation = create(:conversation, inbox: api_channel.inbox) + create( + :message, + account: conversation.account, + inbox: api_channel.inbox, + conversation: conversation, + created_at: Time.now - 13.hours + ) + + expect(api_channel.additional_attributes['agent_reply_time_window']).to eq nil + expect(conversation.can_reply?).to eq true + end + end + + context 'when agent_reply_time_window is configured' do + it 'return false if it is outside of agent_reply_time_window' do + conversation = create(:conversation, inbox: api_channel_with_limit.inbox) + create( + :message, + account: conversation.account, + inbox: api_channel_with_limit.inbox, + conversation: conversation, + created_at: Time.now - 13.hours + ) + + expect(api_channel_with_limit.additional_attributes['agent_reply_time_window']).to eq '12' + expect(conversation.can_reply?).to eq false + end + + it 'return true if it is inside of agent_reply_time_window' do + conversation = create(:conversation, inbox: api_channel_with_limit.inbox) + create( + :message, + account: conversation.account, + inbox: api_channel_with_limit.inbox, + conversation: conversation + ) + expect(conversation.can_reply?).to eq true + end + end + end end describe '#delete conversation' do