Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

check if phone number has opted out #241

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions services/comm/app/models/providers/aws.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@ def sms(from, to, body)
Rails.logger.warn("No AWS client configured for tenant.account_id. #{e.inspect}")
end

def phone_number_opted_out?(phone_number)
client.check_if_phone_number_is_opted_out(phone_number: phone_number).is_opted_out
end

private

# TODO: Cleanup this logic. This should probably live in an initializer.
Expand Down
5 changes: 5 additions & 0 deletions services/comm/app/models/providers/twilio.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,11 @@ def sms(from, to, body)
Rails.logger.debug message
end

def phone_number_opted_out?(_phone_number)
# TODO: Implement the Twilio API call to check if phone number is opted out or not
false
end

def call(_message)
# to = whatup.From.gsub('whatsapp:', '')
client.calls.create(from: from, to: to, url: 'http://demo.twilio.com/docs/voice.xml')
Expand Down
12 changes: 11 additions & 1 deletion services/comm/app/operations/message_send.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

class MessageSend < Ros::ActivityBase
step :retrieve_message
failed :message_not_found
failed :message_not_found, Output(:success) => End(:failure)
step :check_if_phone_number_is_opted_in
failed :phone_number_opted_out, Output(:success) => End(:failure)
step :send_message
step :update_message_provider_id

Expand All @@ -14,6 +16,14 @@ def message_not_found(_ctx, errors:, id:, **)
errors.add(:message, "with #{id} not found")
end

def check_if_phone_number_is_opted_in(_ctx, message:, **)
!message.provider.phone_number_opted_out?(message.to)
end

def phone_number_opted_out(_ctx, errors:, message:, **)
errors.add(:phone_number, "#{message.to} is already opted out")
end

def send_message(ctx, message:, **)
ctx[:msg_id] = message.provider.send(message.channel, message.from, message.to, message.body)
end
Expand Down
2 changes: 1 addition & 1 deletion services/comm/spec/operations/message_create_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
end

before do
allow_any_instance_of(Providers::Aws).to receive(:sms).and_return true
allow_any_instance_of(Providers::Aws).to receive(:phone_number_opted_out?).and_return false
end

context 'when all attributes are valid' do
Expand Down
20 changes: 16 additions & 4 deletions services/comm/spec/operations/message_send_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,15 @@
let(:op_result) { described_class.call(op_params) }
let(:target) { stubbed_resource(resource: Ros::Cognito::Pool, attributes: OpenStruct.new) }
let(:message) { create(:message) }
let(:op_params) { { id: message.id } }

context 'when message is sent' do
let(:op_params) { { id: message.id } }
before do
allow_any_instance_of(Providers::Aws).to receive(:sms).and_return true
allow_any_instance_of(Providers::Aws).to receive(:phone_number_opted_out?).and_return false
end

context 'when message is sent' do
before do
allow_any_instance_of(Providers::Aws).to receive(:sms).and_return true
target
op_result
end
Expand All @@ -25,7 +28,6 @@
let(:op_params) { { id: rand(100..500) } }

before do
allow_any_instance_of(Providers::Aws).to receive(:sms).and_return true
target
op_result
end
Expand All @@ -34,4 +36,14 @@
expect(op_result.errors.size).to be_positive
end
end

context 'when phone number is opted out' do
before do
allow_any_instance_of(Providers::Aws).to receive(:phone_number_opted_out?).and_return true
end

it 'throws errors' do
expect(op_result.errors.size).to be_positive
end
end
end
1 change: 1 addition & 0 deletions services/comm/spec/requests/messages_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@

context 'Authenticated user' do
before do
allow_any_instance_of(Providers::Aws).to receive(:phone_number_opted_out?).and_return false
post url, params: post_data, headers: request_headers
end

Expand Down