feat: Add agent_reply_time_window in API channels (#4857)

This commit is contained in:
Pranav Raj S
2022-06-14 18:05:37 +05:30
committed by GitHub
parent f0db8545cb
commit 1bb0371c1d
10 changed files with 116 additions and 36 deletions

View File

@@ -26,8 +26,22 @@ class Channel::Api < ApplicationRecord
has_secure_token :identifier
has_secure_token :hmac_token
validate :ensure_valid_agent_reply_time_window
def name
'API'
end
def messaging_window_enabled?
additional_attributes.present? && additional_attributes['agent_reply_time_window'].present?
end
private
def ensure_valid_agent_reply_time_window
return if additional_attributes['agent_reply_time_window'].blank?
return if additional_attributes['agent_reply_time_window'].to_i.positive?
errors.add(:agent_reply_time_window, 'agent_reply_time_window must be greater than 0')
end
end

View File

@@ -32,7 +32,7 @@ class Channel::FacebookPage < ApplicationRecord
'Facebook'
end
def has_24_hour_messaging_window?
def messaging_window_enabled?
false
end

View File

@@ -34,7 +34,7 @@ class Channel::TwilioSms < ApplicationRecord
medium == 'sms' ? 'Twilio SMS' : 'Whatsapp'
end
def has_24_hour_messaging_window?
def messaging_window_enabled?
medium == 'whatsapp'
end
end

View File

@@ -57,7 +57,7 @@ class Channel::Whatsapp < ApplicationRecord
{ 'D360-API-KEY' => provider_config['api_key'], 'Content-Type' => 'application/json' }
end
def has_24_hour_messaging_window?
def messaging_window_enabled?
true
end

View File

@@ -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

View File

@@ -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