2020-01-09 07:36:40 +00:00
|
|
|
class MessageTemplates::HookExecutionService
|
|
|
|
pattr_initialize [:message!]
|
|
|
|
|
|
|
|
def perform
|
2020-03-05 20:13:12 +00:00
|
|
|
return if inbox.agent_bot_inbox&.active?
|
2021-05-10 07:31:00 +00:00
|
|
|
return if conversation.campaign.present?
|
2020-03-05 20:13:12 +00:00
|
|
|
|
2021-06-23 13:59:27 +00:00
|
|
|
trigger_templates
|
2020-01-09 07:36:40 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
delegate :inbox, :conversation, to: :message
|
|
|
|
delegate :contact, to: :conversation
|
|
|
|
|
2021-06-23 13:59:27 +00:00
|
|
|
def trigger_templates
|
2021-09-17 16:47:11 +00:00
|
|
|
::MessageTemplates::Template::OutOfOffice.new(conversation: conversation).perform if should_send_out_of_office_message?
|
2021-06-23 13:59:27 +00:00
|
|
|
::MessageTemplates::Template::Greeting.new(conversation: conversation).perform if should_send_greeting?
|
|
|
|
::MessageTemplates::Template::EmailCollect.new(conversation: conversation).perform if inbox.enable_email_collect && should_send_email_collect?
|
|
|
|
::MessageTemplates::Template::CsatSurvey.new(conversation: conversation).perform if should_send_csat_survey?
|
|
|
|
end
|
|
|
|
|
2020-10-31 18:44:33 +00:00
|
|
|
def should_send_out_of_office_message?
|
2021-09-17 16:47:11 +00:00
|
|
|
# should not send if its a tweet message
|
|
|
|
return false if conversation.tweet?
|
2021-09-21 04:46:14 +00:00
|
|
|
# should not send for outbound messages
|
|
|
|
return false unless message.incoming?
|
2021-09-17 16:47:11 +00:00
|
|
|
|
2020-10-31 18:44:33 +00:00
|
|
|
inbox.out_of_office? && conversation.messages.today.template.empty? && inbox.out_of_office_message.present?
|
|
|
|
end
|
|
|
|
|
2020-01-09 07:36:40 +00:00
|
|
|
def first_message_from_contact?
|
|
|
|
conversation.messages.outgoing.count.zero? && conversation.messages.template.count.zero?
|
|
|
|
end
|
|
|
|
|
2020-06-09 18:24:35 +00:00
|
|
|
def should_send_greeting?
|
2021-09-08 09:37:24 +00:00
|
|
|
# should not send if its a tweet message
|
|
|
|
return false if conversation.tweet?
|
|
|
|
|
2020-10-31 18:44:33 +00:00
|
|
|
first_message_from_contact? && inbox.greeting_enabled? && inbox.greeting_message.present?
|
2020-06-09 18:24:35 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def email_collect_was_sent?
|
|
|
|
conversation.messages.where(content_type: 'input_email').present?
|
|
|
|
end
|
|
|
|
|
2021-03-01 05:28:49 +00:00
|
|
|
# TODO: we should be able to reduce this logic once we have a toggle for email collect messages
|
2020-01-09 07:36:40 +00:00
|
|
|
def should_send_email_collect?
|
2021-03-01 05:28:49 +00:00
|
|
|
!contact_has_email? && inbox.web_widget? && !inbox.channel.pre_chat_form_enabled? && !email_collect_was_sent?
|
2020-04-03 07:34:58 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def contact_has_email?
|
|
|
|
contact.email
|
2020-01-09 07:36:40 +00:00
|
|
|
end
|
2021-06-23 13:59:27 +00:00
|
|
|
|
2021-08-23 16:30:47 +00:00
|
|
|
def csat_enabled_conversation?
|
|
|
|
return false unless conversation.resolved?
|
|
|
|
# should not sent since the link will be public
|
|
|
|
return false if conversation.tweet?
|
|
|
|
return false unless inbox.csat_survey_enabled?
|
2021-06-23 13:59:27 +00:00
|
|
|
|
|
|
|
true
|
|
|
|
end
|
|
|
|
|
|
|
|
def should_send_csat_survey?
|
2021-08-23 16:30:47 +00:00
|
|
|
return unless csat_enabled_conversation?
|
|
|
|
|
2021-06-23 13:59:27 +00:00
|
|
|
# only send CSAT once in a conversation
|
|
|
|
return if conversation.messages.where(content_type: :input_csat).present?
|
|
|
|
|
|
|
|
true
|
|
|
|
end
|
2020-01-09 07:36:40 +00:00
|
|
|
end
|