chore: one off SMS campaign APIs (#2589)

This commit is contained in:
Sojan Jose
2021-07-14 12:24:09 +05:30
committed by GitHub
parent cb44eb2964
commit dfddf9cacc
15 changed files with 337 additions and 23 deletions

View File

@@ -28,6 +28,7 @@ class Api::V1::Accounts::CampaignsController < Api::V1::Accounts::BaseController
end
def campaign_params
params.require(:campaign).permit(:title, :description, :message, :enabled, :inbox_id, :sender_id, trigger_rules: {})
params.require(:campaign).permit(:title, :description, :message, :enabled, :inbox_id, :sender_id,
:scheduled_at, audience: [:type, :id], trigger_rules: {})
end
end

View File

@@ -0,0 +1,7 @@
class Campaigns::TriggerOneoffCampaignJob < ApplicationJob
queue_as :low
def perform(campaign)
campaign.trigger!
end
end

View File

@@ -0,0 +1,10 @@
class TriggerScheduledItemsJob < ApplicationJob
queue_as :scheduled_jobs
def perform
# trigger the scheduled campaign jobs
Campaign.where(campaign_type: :one_off, campaign_status: :active).where(scheduled_at: 3.days.ago..Time.current).all.each do |campaign|
Campaigns::TriggerOneoffCampaignJob.perform_later(campaign)
end
end
end

View File

@@ -2,23 +2,30 @@
#
# Table name: campaigns
#
# id :bigint not null, primary key
# description :text
# enabled :boolean default(TRUE)
# message :text not null
# title :string not null
# trigger_rules :jsonb
# created_at :datetime not null
# updated_at :datetime not null
# account_id :bigint not null
# display_id :integer not null
# inbox_id :bigint not null
# sender_id :integer
# id :bigint not null, primary key
# audience :jsonb
# campaign_status :integer default("active"), not null
# campaign_type :integer default("ongoing"), not null
# description :text
# enabled :boolean default(TRUE)
# message :text not null
# scheduled_at :datetime
# title :string not null
# trigger_rules :jsonb
# created_at :datetime not null
# updated_at :datetime not null
# account_id :bigint not null
# display_id :integer not null
# inbox_id :bigint not null
# sender_id :integer
#
# Indexes
#
# index_campaigns_on_account_id (account_id)
# index_campaigns_on_inbox_id (inbox_id)
# index_campaigns_on_account_id (account_id)
# index_campaigns_on_campaign_status (campaign_status)
# index_campaigns_on_campaign_type (campaign_type)
# index_campaigns_on_inbox_id (inbox_id)
# index_campaigns_on_scheduled_at (scheduled_at)
#
# Foreign Keys
#
@@ -30,20 +37,57 @@ class Campaign < ApplicationRecord
validates :inbox_id, presence: true
validates :title, presence: true
validates :message, presence: true
validate :validate_campaign_inbox
validate :prevent_completed_campaign_from_update, on: :update
belongs_to :account
belongs_to :inbox
belongs_to :sender, class_name: 'User', optional: true
enum campaign_type: { ongoing: 0, one_off: 1 }
# TODO : enabled attribute is unneccessary . lets move that to the campaign status with additional statuses like draft, disabled etc.
enum campaign_status: { active: 0, completed: 1 }
has_many :conversations, dependent: :nullify, autosave: true
before_validation :ensure_correct_campaign_attributes
after_commit :set_display_id, unless: :display_id?
def trigger!
return unless one_off?
return if completed?
Twilio::OneoffSmsCampaignService.new(campaign: self).perform if inbox.inbox_type == 'Twilio SMS'
end
private
def set_display_id
reload
end
def validate_campaign_inbox
return unless inbox
errors.add :inbox, 'Unsupported Inbox type' unless ['Website', 'Twilio SMS'].include? inbox.inbox_type
end
# TO-DO we clean up with better validations when campaigns evolve into more inboxes
def ensure_correct_campaign_attributes
return if inbox.blank?
if inbox.inbox_type == 'Twilio SMS'
self.campaign_type = 'one_off'
self.scheduled_at ||= Time.now.utc
else
self.campaign_type = 'ongoing'
self.scheduled_at = nil
end
end
def prevent_completed_campaign_from_update
errors.add :status, 'The campaign is already completed' if !campaign_status_changed? && completed?
end
# creating db triggers
trigger.before(:insert).for_each(:row) do
"NEW.display_id := nextval('camp_dpid_seq_' || NEW.account_id);"

View File

@@ -0,0 +1,40 @@
class Twilio::OneoffSmsCampaignService
pattr_initialize [:campaign!]
def perform
raise "Invalid campaign #{campaign.id}" if campaign.inbox.inbox_type != 'Twilio SMS' || !campaign.one_off?
raise 'Completed Campaign' if campaign.completed?
# marks campaign completed so that other jobs won't pick it up
campaign.completed!
audience_label_ids = campaign.audience.select { |audience| audience['type'] == 'Label' }.pluck('id')
audience_labels = campaign.account.labels.where(id: audience_label_ids).pluck(:title)
process_audience(audience_labels)
end
private
delegate :inbox, to: :campaign
delegate :channel, to: :inbox
def process_audience(audience_labels)
campaign.account.contacts.tagged_with(audience_labels, any: true).each do |contact|
next if contact.phone_number.blank?
send_message(to: contact.phone_number, from: channel.phone_number, content: campaign.message)
end
end
def send_message(to:, from:, content:)
client.messages.create({
body: content,
from: from,
to: to
})
end
def client
::Twilio::REST::Client.new(channel.account_sid, channel.auth_token)
end
end

View File

@@ -9,7 +9,13 @@ json.sender do
json.partial! 'api/v1/models/agent.json.jbuilder', resource: resource.sender if resource.sender.present?
end
json.message resource.message
json.campaign_status resource.campaign_status
json.enabled resource.enabled
json.campaign_type resource.campaign_type
if resource.campaign_type == 'one_off'
json.scheduled_at resource.scheduled_at
json.audience resource.audience
end
json.trigger_rules resource.trigger_rules
json.created_at resource.created_at
json.updated_at resource.updated_at