2021-05-03 14:53:09 +00:00
|
|
|
class Campaigns::CampaignConversationBuilder
|
2022-04-20 05:19:52 +00:00
|
|
|
pattr_initialize [:contact_inbox_id!, :campaign_display_id!, :conversation_additional_attributes, :custom_attributes]
|
2021-05-03 14:53:09 +00:00
|
|
|
|
|
|
|
def perform
|
|
|
|
@contact_inbox = ContactInbox.find(@contact_inbox_id)
|
|
|
|
@campaign = @contact_inbox.inbox.campaigns.find_by!(display_id: campaign_display_id)
|
|
|
|
|
|
|
|
ActiveRecord::Base.transaction do
|
2021-05-10 07:31:00 +00:00
|
|
|
@contact_inbox.lock!
|
|
|
|
|
|
|
|
# We won't send campaigns if a conversation is already present
|
|
|
|
return if @contact_inbox.reload.conversations.present?
|
|
|
|
|
2021-05-03 14:53:09 +00:00
|
|
|
@conversation = ::Conversation.create!(conversation_params)
|
|
|
|
Messages::MessageBuilder.new(@campaign.sender, @conversation, message_params).perform
|
|
|
|
end
|
|
|
|
@conversation
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def message_params
|
|
|
|
ActionController::Parameters.new({
|
2022-05-09 08:34:52 +00:00
|
|
|
content: @campaign.message,
|
|
|
|
campaign_id: @campaign.id
|
2021-05-03 14:53:09 +00:00
|
|
|
})
|
|
|
|
end
|
|
|
|
|
|
|
|
def conversation_params
|
|
|
|
{
|
|
|
|
account_id: @campaign.account_id,
|
|
|
|
inbox_id: @contact_inbox.inbox_id,
|
|
|
|
contact_id: @contact_inbox.contact_id,
|
|
|
|
contact_inbox_id: @contact_inbox.id,
|
|
|
|
campaign_id: @campaign.id,
|
2022-04-20 05:19:52 +00:00
|
|
|
additional_attributes: conversation_additional_attributes,
|
|
|
|
custom_attributes: custom_attributes || {}
|
2021-05-03 14:53:09 +00:00
|
|
|
}
|
|
|
|
end
|
|
|
|
end
|