Skip to content

Commit

Permalink
Improve job api
Browse files Browse the repository at this point in the history
- Pass in message_id to Template job and look up record.
- Derive organazation and contributor from the message record to avoid
  potential mismatches.
  • Loading branch information
mattwr18 committed Oct 7, 2024
1 parent 4514132 commit c711a76
Show file tree
Hide file tree
Showing 5 changed files with 9 additions and 16 deletions.
3 changes: 1 addition & 2 deletions app/adapters/whats_app_adapter/twilio_outbound.rb
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,7 @@ def send_resubscribe_error_message!(contributor, organization)
def send_message_template!(recipient, message)
recipient.update(whats_app_message_template_sent_at: Time.current)
content_sid = message.organization.twilio_content_sids["new_request_#{time_of_day}#{rand(1..3)}"]
WhatsAppAdapter::TwilioOutbound::Template.perform_later(organization_id: message.organization.id, contributor_id: recipient.id,
content_sid: content_sid, message: message)
WhatsAppAdapter::TwilioOutbound::Template.perform_later(content_sid: content_sid, message_id: message.id)
end

private
Expand Down
7 changes: 4 additions & 3 deletions app/adapters/whats_app_adapter/twilio_outbound/template.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,10 @@ class TwilioOutbound
class Template < ApplicationJob
queue_as :default

def perform(organization_id:, contributor_id:, content_sid:, message:)
organization = Organization.find(organization_id)
contributor = organization.contributors.find(contributor_id)
def perform(content_sid:, message_id:)
message = Message.find(message_id)
organization = message.organization
contributor = message.recipient

response = organization.twilio_instance.messages.create(
content_sid: content_sid,
Expand Down
8 changes: 2 additions & 6 deletions spec/adapters/whats_app_adapter/twilio_outbound_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,8 @@
describe 'contributor has not sent a message within 24 hours' do
it 'enqueues the Text job with WhatsApp template' do
expect { subject.call }.to(have_enqueued_job(WhatsAppAdapter::TwilioOutbound::Template).on_queue('default').with do |params|
expect(params[:organization_id]).to eq(message.organization.id)
expect(params[:contributor_id]).to eq(contributor.id)
expect(params[:content_sid]).to be_kind_of(String)
expect(params[:message]).to eq(message)
expect(params[:message_id]).to eq(message.id)
end)
end
end
Expand Down Expand Up @@ -96,10 +94,8 @@
context 'contributor has not sent a message within 24 hours' do
it 'enqueues the Text job with WhatsApp template' do
expect { subject.call }.to(have_enqueued_job(WhatsAppAdapter::TwilioOutbound::Template).on_queue('default').with do |params|
expect(params[:organization_id]).to eq(message.organization.id)
expect(params[:contributor_id]).to eq(contributor.id)
expect(params[:content_sid]).to be_kind_of(String)
expect(params[:message]).to eq(message)
expect(params[:message_id]).to eq(message.id)
end)
end
end
Expand Down
4 changes: 1 addition & 3 deletions spec/models/message_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -97,10 +97,8 @@

it 'enqueues a job to send the message' do
expect { subject }.to(have_enqueued_job(WhatsAppAdapter::TwilioOutbound::Template).on_queue('default').with do |params|
expect(params[:organization_id]).to eq(message.organization_id)
expect(params[:contributor_id]).to eq(message.contributor.id)
expect(params[:content_sid]).to be_kind_of(String)
expect(params[:message]).to eq(message)
expect(params[:message_id]).to eq(message.id)
end)
end
end
Expand Down
3 changes: 1 addition & 2 deletions spec/requests/whats_app/webhook_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -375,9 +375,8 @@

it 'enqueues the Text job with WhatsApp template' do
expect { subject.call }.to(have_enqueued_job(WhatsAppAdapter::TwilioOutbound::Template).on_queue('default').with do |params|
expect(params[:contributor_id]).to eq(contributor.id)
expect(params[:content_sid]).to be_kind_of(String)
expect(params[:message]).to eq(message)
expect(params[:message_id]).to eq(message.id)
end)
end

Expand Down

0 comments on commit c711a76

Please sign in to comment.