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

Feature/1886 Volunteer receives SMS to notify of follow up generated by admin or supervisor #3738

Merged
merged 12 commits into from
Jul 8, 2022
1 change: 1 addition & 0 deletions .allow_skipping_tests
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ notifications/base_notification.rb
notifications/followup_notification.rb
notifications/followup_resolved_notification.rb
notifications/youth_birthday_notification.rb
notifications/delivery_methods/sms.rb
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ideally let's not add to the skipped tests file

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For sure. I'm beginning to figure out how this could be tested, which will hopefully lay the groundwork for testing the other notifications in .allow_skipping_tests

policies/case_court_order_policy.rb
policies/learning_hour_policy.rb
presenters/base_presenter.rb
Expand Down
4 changes: 4 additions & 0 deletions app/helpers/sms_body_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,8 @@ def court_report_due_msg(report_due_date, short_link)
def no_contact_made_msg(contact_type, short_link)
"It's been two weeks since you've tried reaching '#{contact_type}'. Try again! #{short_link}"
end

def case_contact_flagged_msg(display_name, short_link)
"-\n \n#{display_name} has flagged a Case Contact that needs follow up. Click to see more: #{short_link}"
end
end
26 changes: 26 additions & 0 deletions app/notifications/delivery_methods/sms.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
class DeliveryMethods::Sms < Noticed::DeliveryMethods::Base
include SmsBodyHelper
def deliver
if sender.casa_admin? || sender.supervisor?
short_io_api = ShortUrlService.new
short_io_api.create_short_url(case_contact_url)
shortened_url = short_io_api.short_url
twilio_api = TwilioService.new(sender.casa_org.twilio_api_key_sid, sender.casa_org.twilio_api_key_secret, sender.casa_org.twilio_account_sid)
twilio_api.send_sms({From: sender.casa_org.twilio_phone_number, Body: case_contact_flagged_msg(sender.display_name, shortened_url), To: recipient.phone_number})
end
end

def case_contact_url
Rails.application.credentials[:BASE_URL] + "/case_contacts/" + case_contact_id.to_s + "/edit?notification_id=" + record.id.to_s
end

private

def sender
User.find(params[:followup][:creator_id])
end

def case_contact_id
params[:followup][:case_contact_id]
end
end
11 changes: 10 additions & 1 deletion app/notifications/followup_notification.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ class FollowupNotification < BaseNotification
# Add your delivery methods
#
deliver_by :database
# deliver_by :email, mailer: "UserMailer"
# deliver_by :email, mailer: "UserMailer", if: :email_notifications?
# deliver_by :sms, class: "DeliveryMethods::Sms", if: :sms_notifications?
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is this supposed to be commented out?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, the email one was commented out when I started this. https://github.com/rubyforgood/casa/pull/1612/files is the email and database pr

Copy link
Collaborator Author

@7riumph 7riumph Jul 6, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also the "noticed gem" was first added here #1611

# deliver_by :slack
# deliver_by :custom, class: "MyDeliveryMethod"

Expand All @@ -29,6 +30,14 @@ def url

private

def sms_notifications?
recipient.receive_sms_notifications == true
end

def email_notifications?
recipient.receive_email_notifications == true
end

def message_with_note(note)
[
message_heading,
Expand Down
7 changes: 7 additions & 0 deletions app/validators/user_validator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ def validate(record)
valid_phone_number_contents(record.phone_number, record)
validate_presence(:display_name, record)
at_least_one_communication_preference_selected(record)
valid_phone_number_if_receive_sms_notifications(record)
end

private
Expand All @@ -29,4 +30,10 @@ def validate_presence(attribute, record)
def at_least_one_communication_preference_selected(record)
record.errors.add(:base, " At least one communication preference must be selected.") unless record.receive_email_notifications || record.receive_sms_notifications
end

def valid_phone_number_if_receive_sms_notifications(record)
if record.receive_sms_notifications && record.phone_number.blank?
record.errors.add(:base, " Must add a valid phone number to receive SMS notifications.")
end
end
end
9 changes: 0 additions & 9 deletions spec/lib/tasks/case_contact_types_reminder_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -85,13 +85,4 @@
expect(responses[0][:messages][2].body).to match CaseContactTypesReminder::THIRD_MESSAGE + "https://42ni.short.gy/jzTwdF"
end
end

context "volunteer with uncontacted contact types, sms notifications on, no reminder in last quarter, no phone number" do
it "should not send sms reminder" do
UserReminderTime.destroy_all
Volunteer.update_all(phone_number: nil)
responses = CaseContactTypesReminder.new.send!
expect(responses.count).to match 0
end
end
end
16 changes: 0 additions & 16 deletions spec/lib/tasks/no_contact_made_reminder_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -125,20 +125,4 @@
expect(responses.count).to eq 0
end
end

context "volunteer with no phone number" do
let(:volunteer) {
create(
:volunteer,
casa_org_id: casa_org.id,
phone_number: nil,
receive_sms_notifications: true
)
}

it "should send not sms reminder" do
responses = NoContactMadeReminder.new.send!
expect(responses.count).to eq 0
end
end
end
24 changes: 24 additions & 0 deletions spec/system/users/edit_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,14 @@
expect(page).to have_text("At least one communication preference must be selected.")
end

it "displays Volunteer error message if SMS communication preference is selected without adding a valid phone number" do
uncheck "user_receive_email_notifications"
check "user_receive_sms_notifications"
click_on "Save Preferences"
expect(page).to have_content "1 error prohibited this Volunteer from being saved:"
expect(page).to have_text("Must add a valid phone number to receive SMS notifications.")
end

it "displays notification events selection as enabled if sms notification preference is selected", js: true do
check "user_receive_sms_notifications"
expect(page).to have_field("toggle-sms-notification-event", type: "checkbox", disabled: false)
Expand Down Expand Up @@ -150,6 +158,14 @@
expect(page).to have_content "1 error prohibited this Supervisor from being saved:"
expect(page).to have_text("At least one communication preference must be selected.")
end

it "displays Supervisor error message if SMS communication preference is selected without adding a valid phone number" do
uncheck "user_receive_email_notifications"
check "user_receive_sms_notifications"
click_on "Save Preferences"
expect(page).to have_content "1 error prohibited this Supervisor from being saved:"
expect(page).to have_text("Must add a valid phone number to receive SMS notifications.")
end
end

context "when admin" do
Expand Down Expand Up @@ -230,5 +246,13 @@
expect(page).to have_content "1 error prohibited this Casa admin from being saved:"
expect(page).to have_text("At least one communication preference must be selected.")
end

it "displays admin error message if SMS communication preference is selected without adding a valid phone number" do
uncheck "user_receive_email_notifications"
check "user_receive_sms_notifications"
click_on "Save Preferences"
expect(page).to have_content "1 error prohibited this Casa admin from being saved:"
expect(page).to have_text("Must add a valid phone number to receive SMS notifications.")
end
end
end