From 68a1ac660cabb902808b66d1f0ff7be816045ea9 Mon Sep 17 00:00:00 2001 From: 7riumph Date: Sun, 3 Jul 2022 15:00:54 -0600 Subject: [PATCH 01/12] Added SMS delivery method for followups --- app/notifications/delivery_methods/sms.rb | 12 ++++++++++++ app/notifications/followup_notification.rb | 11 ++++++++++- 2 files changed, 22 insertions(+), 1 deletion(-) create mode 100644 app/notifications/delivery_methods/sms.rb diff --git a/app/notifications/delivery_methods/sms.rb b/app/notifications/delivery_methods/sms.rb new file mode 100644 index 0000000000..aa21f8fd2f --- /dev/null +++ b/app/notifications/delivery_methods/sms.rb @@ -0,0 +1,12 @@ +class DeliveryMethods::Sms < Noticed::DeliveryMethods::Base + def deliver + # Logic for sending the notification + end + + # You may override this method to validate options for the delivery method + # Invalid options should raise a ValidationError + # + # def self.validate!(options) + # raise ValidationError, "required_option missing" unless options[:required_option] + # end +end diff --git a/app/notifications/followup_notification.rb b/app/notifications/followup_notification.rb index d2219c4c50..2d30d0befd 100644 --- a/app/notifications/followup_notification.rb +++ b/app/notifications/followup_notification.rb @@ -7,9 +7,10 @@ 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 :slack # deliver_by :custom, class: "MyDeliveryMethod" + deliver_by :sms, class: "DeliveryMethods:Sms", if: sms_notifications? # Reminder: Validate that when 'receive_sms_notifications' == true 'phone_number' is not nil (UserValidator) # Add required params # @@ -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, From 7e68bdd56a333cf3b14fec8a0c8719a3a3daa3a1 Mon Sep 17 00:00:00 2001 From: 7riumph Date: Sun, 3 Jul 2022 15:44:15 -0600 Subject: [PATCH 02/12] Instantiated Twilio API --- app/notifications/delivery_methods/sms.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/notifications/delivery_methods/sms.rb b/app/notifications/delivery_methods/sms.rb index aa21f8fd2f..bb3991dc2b 100644 --- a/app/notifications/delivery_methods/sms.rb +++ b/app/notifications/delivery_methods/sms.rb @@ -1,6 +1,6 @@ class DeliveryMethods::Sms < Noticed::DeliveryMethods::Base def deliver - # Logic for sending the notification + twilio = TwilioService.new(recipient.casa_org.twilio_api_key_sid, recipient.casa_org.twilio_api_key_secret, recipient.casa_org.twilio_account_sid) end # You may override this method to validate options for the delivery method From bc92a82af7847817764bd0d440b3ea581669c104 Mon Sep 17 00:00:00 2001 From: 7riumph Date: Sun, 3 Jul 2022 23:26:13 -0600 Subject: [PATCH 03/12] Added '.send_sms' method and its parameters --- app/notifications/delivery_methods/sms.rb | 2 ++ 1 file changed, 2 insertions(+) diff --git a/app/notifications/delivery_methods/sms.rb b/app/notifications/delivery_methods/sms.rb index bb3991dc2b..4a75663c42 100644 --- a/app/notifications/delivery_methods/sms.rb +++ b/app/notifications/delivery_methods/sms.rb @@ -1,6 +1,8 @@ class DeliveryMethods::Sms < Noticed::DeliveryMethods::Base def deliver twilio = TwilioService.new(recipient.casa_org.twilio_api_key_sid, recipient.casa_org.twilio_api_key_secret, recipient.casa_org.twilio_account_sid) + req_params = {From: recipient.casa_org.twilio_phone_number, Body: "-\n \n[supervisor/admin name] has flagged a Case Contact that needs follow up. Click to see more: [link]", To: recipient.phone_number} + twilio_res = twilio.send_sms(req_params) end # You may override this method to validate options for the delivery method From 63fe3d586e6278ce048f3b9c4fe531a315d6805c Mon Sep 17 00:00:00 2001 From: 7riumph Date: Sun, 3 Jul 2022 23:48:10 -0600 Subject: [PATCH 04/12] Fetch 'display_name' and format followup edit page url --- app/notifications/delivery_methods/sms.rb | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/app/notifications/delivery_methods/sms.rb b/app/notifications/delivery_methods/sms.rb index 4a75663c42..18b37b2acd 100644 --- a/app/notifications/delivery_methods/sms.rb +++ b/app/notifications/delivery_methods/sms.rb @@ -1,10 +1,24 @@ class DeliveryMethods::Sms < Noticed::DeliveryMethods::Base def deliver twilio = TwilioService.new(recipient.casa_org.twilio_api_key_sid, recipient.casa_org.twilio_api_key_secret, recipient.casa_org.twilio_account_sid) - req_params = {From: recipient.casa_org.twilio_phone_number, Body: "-\n \n[supervisor/admin name] has flagged a Case Contact that needs follow up. Click to see more: [link]", To: recipient.phone_number} + req_params = {From: recipient.casa_org.twilio_phone_number, Body: "-\n \n#{supervisor_or_admin_display_name} has flagged a Case Contact that needs follow up. Click to see more: #{url}", To: recipient.phone_number} twilio_res = twilio.send_sms(req_params) end + def url + Rails.application.credentials[:BASE_URL] + "/case_contacts/" + case_contact_id.to_s + "/edit" + end + + private + + def supervisor_or_admin_display_name + params[:created_by][:display_name] + end + + def case_contact_id + params[:followup][:case_contact_id] + end + # You may override this method to validate options for the delivery method # Invalid options should raise a ValidationError # From 188619f2368ea586d94d82d230159d899451a3f1 Mon Sep 17 00:00:00 2001 From: 7riumph Date: Mon, 4 Jul 2022 00:14:08 -0600 Subject: [PATCH 05/12] 'create_short_url()' and grab the 'short_url' from JSON response (Short.io API) --- app/notifications/delivery_methods/sms.rb | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/app/notifications/delivery_methods/sms.rb b/app/notifications/delivery_methods/sms.rb index 18b37b2acd..0ea12f521d 100644 --- a/app/notifications/delivery_methods/sms.rb +++ b/app/notifications/delivery_methods/sms.rb @@ -1,7 +1,12 @@ class DeliveryMethods::Sms < Noticed::DeliveryMethods::Base def deliver twilio = TwilioService.new(recipient.casa_org.twilio_api_key_sid, recipient.casa_org.twilio_api_key_secret, recipient.casa_org.twilio_account_sid) - req_params = {From: recipient.casa_org.twilio_phone_number, Body: "-\n \n#{supervisor_or_admin_display_name} has flagged a Case Contact that needs follow up. Click to see more: #{url}", To: recipient.phone_number} + + short_io_main = ShortUrlService.new + short_io_main.create_short_url(url) + shortened_url = short_io_main.short_url + + req_params = {From: recipient.casa_org.twilio_phone_number, Body: "-\n \n#{supervisor_or_admin_display_name} has flagged a Case Contact that needs follow up. Click to see more: #{shortened_url}", To: recipient.phone_number} twilio_res = twilio.send_sms(req_params) end From b2336f3537a4b4a0f2b229a4fb6b79915d8c0525 Mon Sep 17 00:00:00 2001 From: 7riumph Date: Tue, 5 Jul 2022 12:42:34 -0600 Subject: [PATCH 06/12] Refactor with 'sender' logic and fixes --- app/notifications/delivery_methods/sms.rb | 30 ++++++++-------------- app/notifications/followup_notification.rb | 4 +-- 2 files changed, 13 insertions(+), 21 deletions(-) diff --git a/app/notifications/delivery_methods/sms.rb b/app/notifications/delivery_methods/sms.rb index 0ea12f521d..bf477b8b4e 100644 --- a/app/notifications/delivery_methods/sms.rb +++ b/app/notifications/delivery_methods/sms.rb @@ -1,33 +1,25 @@ class DeliveryMethods::Sms < Noticed::DeliveryMethods::Base def deliver - twilio = TwilioService.new(recipient.casa_org.twilio_api_key_sid, recipient.casa_org.twilio_api_key_secret, recipient.casa_org.twilio_account_sid) - - short_io_main = ShortUrlService.new - short_io_main.create_short_url(url) - shortened_url = short_io_main.short_url - - req_params = {From: recipient.casa_org.twilio_phone_number, Body: "-\n \n#{supervisor_or_admin_display_name} has flagged a Case Contact that needs follow up. Click to see more: #{shortened_url}", To: recipient.phone_number} - twilio_res = twilio.send_sms(req_params) + 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: "-\n \n#{sender.display_name} has flagged a Case Contact that needs follow up. Click to see more: #{shortened_url}", To: recipient.phone_number}) + end end - def url - Rails.application.credentials[:BASE_URL] + "/case_contacts/" + case_contact_id.to_s + "/edit" + 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 supervisor_or_admin_display_name - params[:created_by][:display_name] + def sender + User.find(params[:followup][:creator_id]) end def case_contact_id params[:followup][:case_contact_id] end - - # You may override this method to validate options for the delivery method - # Invalid options should raise a ValidationError - # - # def self.validate!(options) - # raise ValidationError, "required_option missing" unless options[:required_option] - # end end diff --git a/app/notifications/followup_notification.rb b/app/notifications/followup_notification.rb index 2d30d0befd..cfc24abe9d 100644 --- a/app/notifications/followup_notification.rb +++ b/app/notifications/followup_notification.rb @@ -7,10 +7,10 @@ class FollowupNotification < BaseNotification # Add your delivery methods # deliver_by :database - # deliver_by :email, mailer: "UserMailer", if: email_notifications? + # deliver_by :email, mailer: "UserMailer", if: :email_notifications? # deliver_by :slack # deliver_by :custom, class: "MyDeliveryMethod" - deliver_by :sms, class: "DeliveryMethods:Sms", if: sms_notifications? # Reminder: Validate that when 'receive_sms_notifications' == true 'phone_number' is not nil (UserValidator) + deliver_by :sms, class: "DeliveryMethods::Sms", if: :sms_notifications? # Reminder: Validate that when 'receive_sms_notifications' == true 'phone_number' is not nil (UserValidator) # Add required params # From 5668da61f2ca82ce6a6e55ead462ebfd4cb50378 Mon Sep 17 00:00:00 2001 From: 7riumph Date: Tue, 5 Jul 2022 14:21:57 -0600 Subject: [PATCH 07/12] Addded msg contents to 'SmsBodyHelper' --- app/helpers/sms_body_helper.rb | 3 +++ app/notifications/delivery_methods/sms.rb | 3 ++- 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/app/helpers/sms_body_helper.rb b/app/helpers/sms_body_helper.rb index 48c0f94e81..85a5475e25 100644 --- a/app/helpers/sms_body_helper.rb +++ b/app/helpers/sms_body_helper.rb @@ -25,4 +25,7 @@ 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 diff --git a/app/notifications/delivery_methods/sms.rb b/app/notifications/delivery_methods/sms.rb index bf477b8b4e..bff8ad8b98 100644 --- a/app/notifications/delivery_methods/sms.rb +++ b/app/notifications/delivery_methods/sms.rb @@ -1,11 +1,12 @@ 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: "-\n \n#{sender.display_name} has flagged a Case Contact that needs follow up. Click to see more: #{shortened_url}", To: recipient.phone_number}) + 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 From c7e7eae3f877d308f00ab3bacf9b327710f18b5e Mon Sep 17 00:00:00 2001 From: 7riumph Date: Tue, 5 Jul 2022 14:22:28 -0600 Subject: [PATCH 08/12] lint --- app/helpers/sms_body_helper.rb | 1 + app/notifications/delivery_methods/sms.rb | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/app/helpers/sms_body_helper.rb b/app/helpers/sms_body_helper.rb index 85a5475e25..deab1b86d7 100644 --- a/app/helpers/sms_body_helper.rb +++ b/app/helpers/sms_body_helper.rb @@ -25,6 +25,7 @@ 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 diff --git a/app/notifications/delivery_methods/sms.rb b/app/notifications/delivery_methods/sms.rb index bff8ad8b98..7ed412b89b 100644 --- a/app/notifications/delivery_methods/sms.rb +++ b/app/notifications/delivery_methods/sms.rb @@ -1,5 +1,5 @@ class DeliveryMethods::Sms < Noticed::DeliveryMethods::Base -include SmsBodyHelper + include SmsBodyHelper def deliver if sender.casa_admin? || sender.supervisor? short_io_api = ShortUrlService.new From 25f82de02681ff385ad1ea78895ea52220fd0170 Mon Sep 17 00:00:00 2001 From: 7riumph Date: Tue, 5 Jul 2022 16:52:19 -0600 Subject: [PATCH 09/12] Ensured users add a valid phone number before SMS preference is selected (also tests) --- app/validators/user_validator.rb | 7 +++++++ spec/system/users/edit_spec.rb | 24 ++++++++++++++++++++++++ 2 files changed, 31 insertions(+) diff --git a/app/validators/user_validator.rb b/app/validators/user_validator.rb index 2cae803aa1..b7a4a9a835 100644 --- a/app/validators/user_validator.rb +++ b/app/validators/user_validator.rb @@ -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 @@ -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 diff --git a/spec/system/users/edit_spec.rb b/spec/system/users/edit_spec.rb index aa3a314fe0..6fa7940563 100644 --- a/spec/system/users/edit_spec.rb +++ b/spec/system/users/edit_spec.rb @@ -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) @@ -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 @@ -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 From 58e8b79554aa8967c14e53d7ffd5be8ad74a9ed4 Mon Sep 17 00:00:00 2001 From: 7riumph Date: Tue, 5 Jul 2022 18:02:32 -0600 Subject: [PATCH 10/12] Commented out delivery method --- app/notifications/followup_notification.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/notifications/followup_notification.rb b/app/notifications/followup_notification.rb index cfc24abe9d..5af5bdd58e 100644 --- a/app/notifications/followup_notification.rb +++ b/app/notifications/followup_notification.rb @@ -8,9 +8,9 @@ class FollowupNotification < BaseNotification # deliver_by :database # deliver_by :email, mailer: "UserMailer", if: :email_notifications? + # deliver_by :sms, class: "DeliveryMethods::Sms", if: :sms_notifications? # deliver_by :slack # deliver_by :custom, class: "MyDeliveryMethod" - deliver_by :sms, class: "DeliveryMethods::Sms", if: :sms_notifications? # Reminder: Validate that when 'receive_sms_notifications' == true 'phone_number' is not nil (UserValidator) # Add required params # From aedabd77fbb01fe0ddf693166eeeb539f4f45f6c Mon Sep 17 00:00:00 2001 From: 7riumph Date: Tue, 5 Jul 2022 18:35:58 -0600 Subject: [PATCH 11/12] Possibly remove old tests to reflect update (volunteers now need to have a phone number if sms preference is picked) --- .../tasks/case_contact_types_reminder_spec.rb | 9 --------- spec/lib/tasks/no_contact_made_reminder_spec.rb | 16 ---------------- 2 files changed, 25 deletions(-) diff --git a/spec/lib/tasks/case_contact_types_reminder_spec.rb b/spec/lib/tasks/case_contact_types_reminder_spec.rb index c6f3fd2125..9762fc606b 100644 --- a/spec/lib/tasks/case_contact_types_reminder_spec.rb +++ b/spec/lib/tasks/case_contact_types_reminder_spec.rb @@ -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 diff --git a/spec/lib/tasks/no_contact_made_reminder_spec.rb b/spec/lib/tasks/no_contact_made_reminder_spec.rb index d3eb03f2ce..a3b72d5595 100644 --- a/spec/lib/tasks/no_contact_made_reminder_spec.rb +++ b/spec/lib/tasks/no_contact_made_reminder_spec.rb @@ -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 From c6c4ae03bad45e7be9557d5b72bde186bde04223 Mon Sep 17 00:00:00 2001 From: 7riumph Date: Tue, 5 Jul 2022 18:51:51 -0600 Subject: [PATCH 12/12] Cannot test gem --- .allow_skipping_tests | 1 + 1 file changed, 1 insertion(+) diff --git a/.allow_skipping_tests b/.allow_skipping_tests index 4818a8c1d7..78e0ca33fe 100644 --- a/.allow_skipping_tests +++ b/.allow_skipping_tests @@ -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 policies/case_court_order_policy.rb policies/learning_hour_policy.rb presenters/base_presenter.rb