Skip to content

Commit

Permalink
Fix download and tests
Browse files Browse the repository at this point in the history
  • Loading branch information
samnang committed Mar 2, 2025
1 parent ef840bc commit 1646249
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 22 deletions.
8 changes: 7 additions & 1 deletion app/models/broadcast.rb
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,8 @@ def audio_file_blob_changed?
event :start do
transitions(
from: [ :pending, :queued, :errored ],
to: :running
to: :running,
before_transaction: -> { self.error_message = nil }
)
end

Expand Down Expand Up @@ -140,6 +141,11 @@ def updatable?
status == "pending"
end

def mark_as_errored!(message)
self.error_message = message
self.error!
end

private

def set_call_flow_logic
Expand Down
40 changes: 20 additions & 20 deletions app/workflows/populate_alerts.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
class PopulateAlerts < ApplicationWorkflow
attr_reader :broadcast

class BroadcastStartedError < StandardError; end

delegate :account, :beneficiary_filter, to: :broadcast, private: true

def initialize(broadcast)
Expand All @@ -9,32 +11,35 @@ def initialize(broadcast)

def call
ApplicationRecord.transaction do
create_alerts
return unless broadcast.audio_file.attached?
download_audio_file unless broadcast.audio_file.attached?

if broadcast.alerts.any?
create_delivery_attempts
create_alerts
create_delivery_attempts

broadcast.error_message = nil
broadcast.start!
else
mark_as_errored!("No beneficiaries match the filters")
end
broadcast.error_message = nil
broadcast.start!
end
rescue BroadcastStartedError => e
broadcast.mark_as_errored!(e.message)
end

private

def download_audio_file
audio_file = URI.open(broadcast.audio_url)
# FIXME: update file name
broadcast.audio_file.attach(avatar.attach(io: audio_file, filename: "audio.mp3"))
rescue OpenURI::HTTPError
mark_as_errored!("Unable to download audio file")
uri = URI.parse(broadcast.audio_url)
broadcast.audio_file.attach(
io: URI.open(uri),
filename: File.basename(uri)
)
rescue OpenURI::HTTPError, URI::InvalidURIError
raise BroadcastStartedError, "Unable to download audio file"
end

def create_alerts
alerts = beneficiaries_scope.find_each.map do |beneficiary|
beneficiaries = beneficiaries_scope
raise BroadcastStartedError, "No beneficiaries match the filters" if beneficiaries.none?

alerts = beneficiaries.find_each.map do |beneficiary|
{
broadcast_id: broadcast.id,
beneficiary_id: beneficiary.id,
Expand Down Expand Up @@ -70,9 +75,4 @@ def beneficiaries_scope
BeneficiaryFilter.new(input_params: beneficiary_filter).output
).apply
end

def mark_as_errored!(message)
broadcast.error_message = message
broadcast.error!
end
end
3 changes: 3 additions & 0 deletions spec/requests/open_ews_api/v1/broadcasts_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,9 @@
}
)

stub_request(:get, "https://www.example.com/sample.mp3").to_return(status: 200)
allow(AudioFileProcessorJob).to receive(:perform_later).with(broadcast)

set_authorization_header_for(account)
perform_enqueued_jobs do
do_request(
Expand Down
21 changes: 20 additions & 1 deletion spec/workflows/populate_alerts_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,11 @@
other_female_beneficiary = create(:beneficiary, account:, gender: "F")
create(:beneficiary_address, beneficiary: other_female_beneficiary, iso_region_code: "KH-11")

stub_request(:get, "https://example.com/cowbell.mp3").to_return(status: 200)

broadcast = create(
:broadcast,
audio_url: "https://example.com/cowbell.mp3",
status: :pending,
account:,
error_message: "existing error message",
Expand Down Expand Up @@ -38,12 +41,28 @@
)
end

it "marks errored when the audio file can't be downloaded" do
broadcast = create(
:broadcast,
status: :queued,
audio_url: "https://example.com/not-found.mp3",
)

stub_request(:get, "https://example.com/not-found.mp3").to_return(status: 404)

PopulateAlerts.new(broadcast).call

expect(broadcast.status).to eq("errored")
expect(broadcast.error_message).to eq("Unable to download audio file")
end

it "marks errored when there are no beneficiaries that match the filters" do
account = create(:account)
_male_beneficiary = create(:beneficiary, account:, gender: "M")

broadcast = create(
:broadcast,
audio_file: file_fixture("test.mp3"),
status: :queued,
beneficiary_filter: {
gender: { eq: "F" }
Expand All @@ -52,7 +71,7 @@

PopulateAlerts.new(broadcast).call

expect(broadcast.error_message).to eq("No beneficiaries match the filters")
expect(broadcast.status).to eq("errored")
expect(broadcast.error_message).to eq("No beneficiaries match the filters")
end
end

0 comments on commit 1646249

Please sign in to comment.