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?
|
|
|
|
|
2020-06-09 18:24:35 +00:00
|
|
|
::MessageTemplates::Template::Greeting.new(conversation: conversation).perform if should_send_greeting?
|
|
|
|
|
2020-01-09 07:36:40 +00:00
|
|
|
::MessageTemplates::Template::EmailCollect.new(conversation: conversation).perform if should_send_email_collect?
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
delegate :inbox, :conversation, to: :message
|
|
|
|
delegate :contact, to: :conversation
|
|
|
|
|
|
|
|
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?
|
|
|
|
first_message_from_contact? && conversation.inbox.greeting_enabled?
|
|
|
|
end
|
|
|
|
|
|
|
|
def email_collect_was_sent?
|
|
|
|
conversation.messages.where(content_type: 'input_email').present?
|
|
|
|
end
|
|
|
|
|
2020-01-09 07:36:40 +00:00
|
|
|
def should_send_email_collect?
|
2020-06-09 18:24:35 +00:00
|
|
|
!contact_has_email? && conversation.inbox.web_widget? && !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
|
|
|
|
end
|