Chatwoot/app/services/message_templates/hook_execution_service.rb
Sojan Jose 0e721653e5
feat: Business hour Inbox APIs (#1821)
* feat: Business hour Inbox APIs
2021-02-23 12:11:15 +05:30

41 lines
1.3 KiB
Ruby

class MessageTemplates::HookExecutionService
pattr_initialize [:message!]
def perform
return if inbox.agent_bot_inbox&.active?
::MessageTemplates::Template::OutOfOffice.new(conversation: conversation).perform if should_send_out_of_office_message?
# TODO: let's see whether this is needed and remove this and related logic if not
#::MessageTemplates::Template::Greeting.new(conversation: conversation).perform if should_send_greeting?
::MessageTemplates::Template::EmailCollect.new(conversation: conversation).perform if should_send_email_collect?
end
private
delegate :inbox, :conversation, to: :message
delegate :contact, to: :conversation
def should_send_out_of_office_message?
inbox.out_of_office? && conversation.messages.today.template.empty? && inbox.out_of_office_message.present?
end
def first_message_from_contact?
conversation.messages.outgoing.count.zero? && conversation.messages.template.count.zero?
end
def should_send_greeting?
first_message_from_contact? && inbox.greeting_enabled? && inbox.greeting_message.present?
end
def email_collect_was_sent?
conversation.messages.where(content_type: 'input_email').present?
end
def should_send_email_collect?
!contact_has_email? && inbox.web_widget? && !email_collect_was_sent?
end
def contact_has_email?
contact.email
end
end