From c711a768c711d1e4f59308506e494b0d5e20b7d4 Mon Sep 17 00:00:00 2001 From: Matthew Rider Date: Mon, 7 Oct 2024 19:23:23 +0200 Subject: [PATCH] Improve job api - Pass in message_id to Template job and look up record. - Derive organazation and contributor from the message record to avoid potential mismatches. --- app/adapters/whats_app_adapter/twilio_outbound.rb | 3 +-- .../whats_app_adapter/twilio_outbound/template.rb | 7 ++++--- spec/adapters/whats_app_adapter/twilio_outbound_spec.rb | 8 ++------ spec/models/message_spec.rb | 4 +--- spec/requests/whats_app/webhook_spec.rb | 3 +-- 5 files changed, 9 insertions(+), 16 deletions(-) diff --git a/app/adapters/whats_app_adapter/twilio_outbound.rb b/app/adapters/whats_app_adapter/twilio_outbound.rb index 72735767d..60bc2fa95 100644 --- a/app/adapters/whats_app_adapter/twilio_outbound.rb +++ b/app/adapters/whats_app_adapter/twilio_outbound.rb @@ -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 diff --git a/app/adapters/whats_app_adapter/twilio_outbound/template.rb b/app/adapters/whats_app_adapter/twilio_outbound/template.rb index 8174daa68..04ad617fb 100644 --- a/app/adapters/whats_app_adapter/twilio_outbound/template.rb +++ b/app/adapters/whats_app_adapter/twilio_outbound/template.rb @@ -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, diff --git a/spec/adapters/whats_app_adapter/twilio_outbound_spec.rb b/spec/adapters/whats_app_adapter/twilio_outbound_spec.rb index 242ffe605..b4ffb55d3 100644 --- a/spec/adapters/whats_app_adapter/twilio_outbound_spec.rb +++ b/spec/adapters/whats_app_adapter/twilio_outbound_spec.rb @@ -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 @@ -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 diff --git a/spec/models/message_spec.rb b/spec/models/message_spec.rb index e1c3e390e..00c6de127 100644 --- a/spec/models/message_spec.rb +++ b/spec/models/message_spec.rb @@ -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 diff --git a/spec/requests/whats_app/webhook_spec.rb b/spec/requests/whats_app/webhook_spec.rb index 6b1597f3a..b568a2320 100644 --- a/spec/requests/whats_app/webhook_spec.rb +++ b/spec/requests/whats_app/webhook_spec.rb @@ -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