Skip to content

Commit

Permalink
Rename from callout_participations to alerts
Browse files Browse the repository at this point in the history
  • Loading branch information
samnang committed Feb 13, 2025
1 parent c3ee5da commit dadf29a
Show file tree
Hide file tree
Showing 33 changed files with 191 additions and 189 deletions.
19 changes: 9 additions & 10 deletions app/jobs/retry_phone_call_job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,28 +3,27 @@ class RetryPhoneCallJob < ApplicationJob
IN_PROGRESS_CALL_STATUSES = %i[created queued remotely_queued in_progress].freeze

def perform(phone_call)
callout_participation = phone_call.callout_participation
alert = phone_call.alert

return if callout_participation.answered?
return if max_calls_reached?(callout_participation)
return if in_progress_calls?(callout_participation)
return if alert.answered?
return if max_calls_reached?(alert)
return if in_progress_calls?(alert)

PhoneCall.create!(
account: phone_call.account,
callout_participation:,
alert:,
beneficiary: phone_call.beneficiary,
broadcast: phone_call.broadcast
)
end

private

def max_calls_reached?(callout_participation)
callout_participation.phone_calls.where(status: RETRY_CALL_STATUSES).count >=
callout_participation.account.max_phone_calls_for_callout_participation
def max_calls_reached?(alert)
alert.phone_calls.where(status: RETRY_CALL_STATUSES).count >= alert.account.max_phone_calls_for_alert
end

def in_progress_calls?(callout_participation)
callout_participation.phone_calls.where(status: IN_PROGRESS_CALL_STATUSES).any?
def in_progress_calls?(alert)
alert.phone_calls.where(status: IN_PROGRESS_CALL_STATUSES).any?
end
end
4 changes: 2 additions & 2 deletions app/models/account.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ class Account < ApplicationRecord
class_name: "BatchOperation::Base",
dependent: :restrict_with_error

has_many :callout_participations, through: :broadcasts
has_many :alerts, through: :broadcasts

has_many :phone_calls

Expand Down Expand Up @@ -97,7 +97,7 @@ def from_phone_number
settings.fetch("from_phone_number")
end

def max_phone_calls_for_callout_participation
def max_phone_calls_for_alert
settings.fetch("max_phone_calls_for_callout_participation").to_i
end

Expand Down
5 changes: 2 additions & 3 deletions app/models/callout_participation.rb → app/models/alert.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
class CalloutParticipation < ApplicationRecord
class Alert < ApplicationRecord
include MetadataHelpers
include HasCallFlowLogic

Expand All @@ -14,8 +14,7 @@ class CalloutParticipation < ApplicationRecord
optional: true,
class_name: "BatchOperation::CalloutPopulation"

has_many :phone_calls,
dependent: :restrict_with_error
has_many :phone_calls, dependent: :restrict_with_error

has_many :remote_phone_call_events, through: :phone_calls

Expand Down
28 changes: 14 additions & 14 deletions app/models/batch_operation/callout_population.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ class CalloutPopulation < Base

belongs_to :broadcast

has_many :callout_participations, dependent: :restrict_with_error
has_many :beneficiaries, through: :callout_participations
has_many :alerts, foreign_key: :callout_population_id, dependent: :restrict_with_error
has_many :beneficiaries, through: :alerts

store_accessor :parameters,
:contact_filter_params,
Expand All @@ -20,7 +20,7 @@ class CalloutPopulation < Base

def run!
transaction do
create_callout_participations
create_alerts
create_phone_calls
end
end
Expand All @@ -41,11 +41,11 @@ def beneficiaries_scope
Filter::Resource::Beneficiary.new(
{ association_chain: account.beneficiaries },
contact_filter_params.with_indifferent_access
).resources.where.not(id: CalloutParticipation.select(:beneficiary_id).where(broadcast:))
).resources.where.not(id: Alert.select(:beneficiary_id).where(broadcast:))
end

def create_callout_participations
callout_participations = beneficiaries_scope.find_each.map do |beneficiary|
def create_alerts
alerts = beneficiaries_scope.find_each.map do |beneficiary|
{
beneficiary_id: beneficiary.id,
phone_number: beneficiary.phone_number,
Expand All @@ -54,27 +54,27 @@ def create_callout_participations
call_flow_logic: broadcast.call_flow_logic
}
end
CalloutParticipation.upsert_all(callout_participations) if callout_participations.any?
Alert.upsert_all(alerts) if alerts.any?
end

def create_phone_calls
phone_calls = callout_participations.includes(:phone_calls).find_each.map do |callout_participation|
next if callout_participation.phone_calls.any?
phone_calls = alerts.includes(:phone_calls).find_each.map do |alert|
next if alert.phone_calls.any?

{
account_id: broadcast.account_id,
broadcast_id:,
beneficiary_id: callout_participation.beneficiary_id,
call_flow_logic: callout_participation.call_flow_logic,
callout_participation_id: callout_participation.id,
phone_number: callout_participation.phone_number,
beneficiary_id: alert.beneficiary_id,
call_flow_logic: alert.call_flow_logic,
alert_id: alert.id,
phone_number: alert.phone_number,
status: :created
}
end

if phone_calls.any?
PhoneCall.upsert_all(phone_calls)
CalloutParticipation.where(id: phone_calls.pluck(:callout_participation_id)).update_all(phone_calls_count: 1)
Alert.where(id: phone_calls.pluck(:alert_id)).update_all(phone_calls_count: 1)
end
end

Expand Down
6 changes: 2 additions & 4 deletions app/models/broadcast.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,8 @@ def audio_file_blob_changed?
belongs_to :account
belongs_to :created_by, class_name: "User", optional: true

has_many :callout_participations, dependent: :restrict_with_error
has_many :broadcast_beneficiaries, class_name: "CalloutParticipation", dependent: :restrict_with_error
has_many :beneficiaries, through: :broadcast_beneficiaries
has_many :alerts, dependent: :restrict_with_error
has_many :beneficiaries, through: :alerts

has_many :batch_operations,
class_name: "BatchOperation::Base",
Expand All @@ -50,7 +49,6 @@ def audio_file_blob_changed?

has_many :phone_calls
has_many :remote_phone_call_events, through: :phone_calls
has_many :beneficiaries, through: :callout_participations

has_one_attached :audio_file

Expand Down
12 changes: 6 additions & 6 deletions app/models/broadcast_summary.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@ def initialize(broadcast)
self.broadcast = broadcast
end

def participations
callout_participations.count
def alerts_count
alerts.count
end

def participations_still_to_be_called
callout_participations.still_trying(broadcast.account.max_phone_calls_for_callout_participation).count
def alerts_still_to_be_called
alerts.still_trying(broadcast.account.max_phone_calls_for_alert).count
end

def completed_calls
Expand All @@ -39,8 +39,8 @@ def errored_calls

private

def callout_participations
broadcast.callout_participations
def alerts
broadcast.alerts
end

def phone_calls
Expand Down
10 changes: 5 additions & 5 deletions app/models/call_flow_logic/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,9 @@ def run!

def retry_call
return unless phone_call.status.in?(RETRY_CALL_STATUSES + ALWAYS_RETRY_CALL_STATUSES)
return if callout_participation.blank?
return if phone_call.status.in?(RETRY_CALL_STATUSES) && callout_participation.phone_calls_count >= phone_call.account.max_phone_calls_for_callout_participation
return if callout_participation.phone_calls_count >= MAX_RETRIES
return if alert.blank?
return if phone_call.status.in?(RETRY_CALL_STATUSES) && alert.phone_calls_count >= phone_call.account.max_phone_calls_for_alert
return if alert.phone_calls_count >= MAX_RETRIES

RetryPhoneCallJob.set(wait: 15.minutes).perform_later(phone_call)
end
Expand All @@ -49,8 +49,8 @@ def phone_call
event.phone_call
end

def callout_participation
phone_call.callout_participation
def alert
phone_call.alert
end
end
end
Expand Down
2 changes: 2 additions & 0 deletions app/models/filter/resource/phone_call.rb
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ def filter_params
:callout_id,
:broadcast_id,
:callout_participation_id,
:alert_id,
:beneficiary_id,
:status,
:call_flow_logic,
Expand All @@ -58,6 +59,7 @@ def filter_params
)
result[:beneficiary_id] = result.delete(:contact_id) if result.key?(:contact_id)
result[:broadcast_id] = result.delete(:callout_id) if result.key?(:callout_id)
result[:alert_id] = result.delete(:callout_participation_id) if result.key?(:callout_participation_id)
result
end
end
Expand Down
20 changes: 10 additions & 10 deletions app/models/phone_call.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class PhoneCall < ApplicationRecord

attribute :phone_number, :phone_number

belongs_to :callout_participation, optional: true, counter_cache: true
belongs_to :alert, optional: true, counter_cache: true
belongs_to :beneficiary, validate: true
belongs_to :account
belongs_to :broadcast, optional: true
Expand All @@ -29,12 +29,12 @@ class PhoneCall < ApplicationRecord
include HasCallFlowLogic
include AASM

delegate :call_flow_logic, to: :callout_participation, prefix: true, allow_nil: true
delegate :call_flow_logic, to: :alert, prefix: true, allow_nil: true
delegate :call_flow_logic, to: :beneficiary, prefix: true, allow_nil: true

delegate :beneficiary,
:phone_number,
to: :callout_participation,
to: :alert,
prefix: true,
allow_nil: true

Expand Down Expand Up @@ -107,7 +107,7 @@ class PhoneCall < ApplicationRecord

transitions from: %i[created remotely_queued in_progress expired],
to: :completed,
after: :mark_callout_participation_answered!,
after: :mark_alert_answered!,
if: :remote_status_completed?
end
end
Expand All @@ -130,7 +130,7 @@ def direction
def set_call_flow_logic
return if call_flow_logic.present?

self.call_flow_logic = callout_participation_call_flow_logic || beneficiary_call_flow_logic
self.call_flow_logic = alert_call_flow_logic || beneficiary_call_flow_logic
end

def remote_call_expired?
Expand Down Expand Up @@ -165,8 +165,8 @@ def remote_status_in_progress?
end

def set_defaults
self.beneficiary ||= callout_participation_beneficiary
self.phone_number ||= callout_participation_phone_number
self.beneficiary ||= alert_beneficiary
self.phone_number ||= alert_phone_number
self.account ||= beneficiary&.account
set_call_flow_logic
end
Expand All @@ -178,9 +178,9 @@ def validate_destroy
throw(:abort)
end

def mark_callout_participation_answered!
return true if callout_participation.blank?
def mark_alert_answered!
return true if alert.blank?

callout_participation.update!(answered: true)
alert.update!(answered: true)
end
end
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
class PopulateBroadcastBeneficiaries < ApplicationWorkflow
class PopulateAlerts < ApplicationWorkflow
attr_reader :broadcast

delegate :account, :beneficiary_filter, to: :broadcast, private: true
Expand All @@ -9,7 +9,7 @@ def initialize(broadcast)

def call
ApplicationRecord.transaction do
create_broadcast_beneficiaries
create_alerts
create_phone_calls

broadcast.start!
Expand All @@ -18,8 +18,8 @@ def call

private

def create_broadcast_beneficiaries
broadcast_beneficiaries = beneficiaries_scope.find_each.map do |beneficiary|
def create_alerts
alerts = beneficiaries_scope.find_each.map do |beneficiary|
{
broadcast_id: broadcast.id,
beneficiary_id: beneficiary.id,
Expand All @@ -29,18 +29,18 @@ def create_broadcast_beneficiaries
}
end

CalloutParticipation.upsert_all(broadcast_beneficiaries) if broadcast_beneficiaries.any?
Alert.upsert_all(alerts) if alerts.any?
end

def create_phone_calls
phone_calls = broadcast.broadcast_beneficiaries.find_each.map do |broadcast_beneficiary|
phone_calls = broadcast.alerts.find_each.map do |alert|
{
account_id: account.id,
broadcast_id: broadcast.id,
beneficiary_id: broadcast_beneficiary.beneficiary_id,
call_flow_logic: broadcast_beneficiary.call_flow_logic,
callout_participation_id: broadcast_beneficiary.id,
phone_number: broadcast_beneficiary.phone_number,
beneficiary_id: alert.beneficiary_id,
call_flow_logic: alert.call_flow_logic,
alert_id: alert.id,
phone_number: alert.phone_number,
status: :created
}
end
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
class RenameCalloutParticipationsToAlerts < ActiveRecord::Migration[8.0]
def change
rename_table :callout_participations, :alerts
rename_column :phone_calls, :callout_participation_id, :alert_id
end
end
Loading

0 comments on commit dadf29a

Please sign in to comment.