Skip to content

Commit

Permalink
Handle requests to receive previous requests
Browse files Browse the repository at this point in the history
  • Loading branch information
mattwr18 committed Oct 7, 2024
1 parent 8e1e3cd commit 793dd03
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ def initialize_message(whats_app_message)
trigger(REQUEST_FOR_MORE_INFO, sender) if request_for_more_info?(text)
trigger(UNSUBSCRIBE_CONTRIBUTOR, sender) if unsubscribe_text?(text)
trigger(RESUBSCRIBE_CONTRIBUTOR, sender) if resubscribe_text?(text)
trigger(REQUEST_TO_RECEIVE_MESSAGE, sender) if request_to_receive_message?(sender, text)
trigger(REQUEST_TO_RECEIVE_MESSAGE, sender, message) if request_to_receive_message?(sender, text)

message = Message.new(text: text, sender: sender)
message.raw_data.attach(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ def perform(organization_id:, components:)
handle_request_for_more_info(contributor, organization)
end

adapter.on(WhatsAppAdapter::ThreeSixtyDialogInbound::REQUEST_TO_RECEIVE_MESSAGE) do |contributor|
handle_request_to_receive_message(contributor)
adapter.on(WhatsAppAdapter::ThreeSixtyDialogInbound::REQUEST_TO_RECEIVE_MESSAGE) do |contributor, whats_app_message|
handle_request_to_receive_message(contributor, whats_app_message)
end

adapter.on(WhatsAppAdapter::ThreeSixtyDialogInbound::UNSUPPORTED_CONTENT) do |contributor|
Expand All @@ -43,10 +43,11 @@ def handle_unknown_contributor(whats_app_phone_number)
ErrorNotifier.report(exception)
end

def handle_request_to_receive_message(contributor)
def handle_request_to_receive_message(contributor, whats_app_message)
contributor.update!(whats_app_message_template_responded_at: Time.current, whats_app_message_template_sent_at: nil)

WhatsAppAdapter::ThreeSixtyDialogOutbound.send!(contributor.received_messages.first)
external_id = whats_app_message.dig(:context, :id)
message = Message.find_by(external_id: external_id) if external_id
WhatsAppAdapter::ThreeSixtyDialogOutbound.send!(message || contributor.received_messages.first)
end

def handle_request_for_more_info(contributor, organization)
Expand Down
34 changes: 34 additions & 0 deletions spec/adapters/whats_app_adapter/three_sixty_dialog_inbound_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -477,5 +477,39 @@
end
end
end

describe 'REQUEST_TO_RECEIVE_MESSAGE' do
let(:request_to_receive_message_callback) { spy('request_to_receive_message_callback') }

before do
adapter.on(WhatsAppAdapter::ThreeSixtyDialogInbound::REQUEST_TO_RECEIVE_MESSAGE) do |contributor, message|
request_to_receive_message_callback.call(contributor, message)
end
end

subject do
adapter.consume(organization, whats_app_message)
request_to_receive_message_callback
end

before do
create(:message, external_id: 'some_external_id')
whats_app_message[:messages].first[:context] = { id: 'some_external_id' }
end

describe 'with no WhatsApp template sent' do
it 'does not trigger the callback' do
expect(subject).not_to have_received(:call)
end
end

describe 'with a WhatsApp template sent' do
before { contributor.update!(whats_app_message_template_sent_at: 1.hour.ago) }

it 'triggerd the callback' do
expect(subject).to have_received(:call)
end
end
end
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -58,11 +58,7 @@
}
}
end
let(:latest_message) { contributor.received_messages.first.text }

before do
create(:message, :outbound, request: request, recipient: contributor)
end
let!(:latest_message) { create(:message, :outbound, request: request, recipient: contributor) }

context 'no message template sent' do
it 'creates a messsage' do
Expand All @@ -72,20 +68,44 @@

context 'responding to template' do
before { contributor.update(whats_app_message_template_sent_at: Time.current) }
let(:text) { latest_message }
let(:text) { latest_message.text }

context 'request to receive latest message' do
it 'enqueues a job to send the latest received message' do
expect do
subject.call
end.to have_enqueued_job(WhatsAppAdapter::ThreeSixtyDialogOutbound::Text).on_queue('default').with(text_payload)
end

it 'marks that contributor has responded to template message' do
expect { subject.call }.to change {
contributor.reload.whats_app_message_template_responded_at
}.from(nil).to(kind_of(ActiveSupport::TimeWithZone))
end

describe 'with no external id' do
it 'enqueues a job to send the latest received message' do
expect do
subject.call
end.to have_enqueued_job(WhatsAppAdapter::ThreeSixtyDialogOutbound::Text).on_queue('default').with(
text_payload.merge({ message_id: latest_message.id })
)
end
end

describe 'with an external id' do
let(:previous_message) do
create(:message, :outbound, request: request, recipient: contributor, created_at: 2.days.ago, external_id: 'some_external_id')
end
let(:text) { previous_message.text }

before do
previous_message
components[:messages].first[:context] = { id: 'some_external_id' }
end

it 'sends out the message with that external id' do
expect do
subject.call
end.to have_enqueued_job(WhatsAppAdapter::ThreeSixtyDialogOutbound::Text).on_queue('default').with(
text_payload.merge({ message_id: previous_message.id })
)
end
end
end
end

Expand Down

0 comments on commit 793dd03

Please sign in to comment.