Chatwoot/lib/integrations/widget/outgoing_message_builder.rb
Pranav Raj Sreepuram 2a34255e0b Initial Commit
Co-authored-by: Subin <subinthattaparambil@gmail.com>
Co-authored-by: Manoj <manojmj92@gmail.com>
Co-authored-by: Nithin <webofnithin@gmail.com>
2019-08-14 15:18:44 +05:30

50 lines
No EOL
918 B
Ruby

class Integrations::Widget::OutgoingMessageBuilder
# params = {
# user_id: 1,
# inbox_id: 1,
# content: "Hello world",
# conversation_id: 2
# }
attr_accessor :options, :message
def initialize(options)
@options = options
end
def perform
ActiveRecord::Base.transaction do
build_conversation
build_message
end
end
private
def inbox
@inbox ||= Inbox.find(options[:inbox_id])
end
def user
@user ||= Contact.find(options[:user_id])
end
def build_conversation
@conversation ||= Conversation.find(options[:conversation_id])
end
def build_message
@message = @conversation.messages.new(message_params)
@message.save!
end
def message_params
{
account_id: @conversation.account_id,
inbox_id: @conversation.inbox_id,
message_type: 1,
content: options[:content],
user_id: user.id
}
end
end