Skip to content
Merged
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
28 changes: 24 additions & 4 deletions app/services/state_file/messaging_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ def send_message(require_verification: true)
if intake.unsubscribed_from_email?
DatadogApi.increment("mailgun.state_file_notification_emails.not_sent_because_unsubscribed")
end
# send_sms if @do_sms # TODO: Eventually send SMS when fixed

send_sms(require_verification:) if @do_sms

message_tracker.record(sent_messages.last.created_at) if sent_messages.any? # will this be recorded correctly with what we have on line 40

Expand All @@ -52,6 +53,21 @@ def send_email(require_verification: true)
sent_messages << sent_message if sent_message.present?
end
end

def send_sms(require_verification: true)
phone_number_verified = intake.phone_number_verified_at.present? || matching_intakes_has_phone_number_verified_at?(intake)
return if intake.phone_number.nil?
return if require_verification && !phone_number_verified

if @message_instance.sms_body.present?
twilio = TwilioService.new(:statefile)

twilio.send_text_message(
to: intake.phone_number,
body: @message_instance.sms_body(locale: @locale, **sms_args)
)
end
end

def matching_intakes_has_email_verified_at?(intake)
return if intake.email_address.nil? || intake.hashed_ssn.nil?
Expand All @@ -62,10 +78,14 @@ def matching_intakes_has_email_verified_at?(intake)
matching_intakes.present?
end

def matching_intakes_has_phone_number_verified_at?(intake)
return if intake.phone_number.nil? || intake.hashed_ssn.nil?

# def send_sms
# return unless Flipper.enabled?(:sms_notifications)
# end
intake_class = StateFile::StateInformationService.intake_class(intake.state_code)
matching_intakes = intake_class.where(phone_number: intake.phone_number, hashed_ssn: intake.hashed_ssn)
.where.not(phone_number_verified_at: nil)
matching_intakes.present?
end

def base_args
first_name = intake.primary_first_name ? intake.primary_first_name.split(/ |\_/).map(&:capitalize).join(" ") : ""
Expand Down
61 changes: 61 additions & 0 deletions scripts/send_message.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
#!/usr/bin/env ruby

require_relative "../config/environment"

# Quick and dirty script to run notifications from the command line to smoke test/copy-check them
class SendMessage < Thor
all_message_classes = StateFile::AutomatedMessage.constants.select { |c| StateFile::AutomatedMessage.const_get(c).is_a? Class }

all_message_classes.each do |klass|
method_name = klass.to_s.underscore

desc method_name, "Sends the #{method_name} notification. Messages are created in a transaction due to intakes being required"
method_option :sms_number, aliases: '-s', desc: 'Number to send an sms to. Requires valid twilio credentials', type: :string
method_option :email_address, aliases: '-e', desc: 'Email address to send an email to. Requires the rails application to be configured to emit emails.', type: :string
method_option :body_args, aliases: '-b', desc: 'Body arguments. Pass multiple at once', type: :hash, default: {}

define_method(method_name) do
ActiveRecord::Base.transaction do
say "Creating disposable intake to test notification", :cyan

intake = StateFileAzIntake.create(
primary_first_name: "Test",
primary_last_name: "Testerson",
email_address: options.fetch(:email_address, nil),
email_address_verified_at: 1.minute.ago,
phone_number: options.fetch(:sms_number, nil),
phone_number_verified_at: 1.minute.ago,
message_tracker: {},
hashed_ssn: "nonsense"
)

# Horrible hack, but this enables us to avoid having to stub a submission
message = Class.new StateFile::AutomatedMessage.const_get(klass) do
def self.after_transition_notification?
false
end
end

body_args = {
intake_id: intake.id
}.merge(options[:body_args].symbolize_keys)

say "Sending message #{klass}", :cyan
say "Using email #{options[:email_address]}", :cyan if options[:email_address].present?
say "Using sms number #{options[:sms_number]}", :cyan if options[:sms_number].present?
say "Using body arguments #{body_args}", :cyan if options[:body_args].present?
StateFile::MessagingService.new(
message:,
intake:,
email: !!options[:email_address],
sms: !!options[:sms_number],
body_args: body_args
).send_message

raise ActiveRecord::Rollback
end
end
end
end

SendMessage.start
30 changes: 28 additions & 2 deletions spec/services/state_file/messaging_service_spec.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,17 @@
require "rails_helper"

describe StateFile::MessagingService do
let(:intake) { create :state_file_az_intake, primary_first_name: "Mona", email_address: "[email protected]", email_address_verified_at: 1.minute.ago, message_tracker: {}, hashed_ssn: "boopbedoop" }
include MockTwilio

let(:intake) do
create(
:state_file_az_intake,
primary_first_name: "Mona", phone_number: "+15555555555",
phone_number_verified_at: 1.minute.ago, email_address: "[email protected]",
email_address_verified_at: 1.minute.ago, message_tracker: {}, hashed_ssn: "boopbedoop"
)
end

let(:efile_submission) { create :efile_submission, :for_state, data_source: intake }
let(:message) { StateFile::AutomatedMessage::Welcome }
let(:body_args) { {intake_id: intake.id} }
Expand Down Expand Up @@ -48,6 +58,22 @@
end
end

context "when has a phone number" do
it "should send a message if the number is verified" do
expect {
messaging_service.send_message
}.to change { FakeTwilioClient.messages.count }
end

it "should not send a message if the number is unverified" do
intake.update(phone_number_verified_at: nil)

expect {
messaging_service.send_message
}.not_to change { FakeTwilioClient.messages.count }
end
end

context "when message is an after_transition_notification" do
let(:message) { StateFile::AutomatedMessage::Rejected }
let!(:messaging_service) { described_class.new(message: message, intake: intake, submission: efile_submission, body_args: {return_status_link: "link.com"}) }
Expand All @@ -74,4 +100,4 @@
expect(intake.message_tracker).to include "messages.state_file.finish_return"
end
end
end
end
Loading