Skip to content

Commit

Permalink
Merge branch 'main' into fix_flaky_court_dates_edit_test
Browse files Browse the repository at this point in the history
  • Loading branch information
compwron authored Jun 23, 2023
2 parents 1877851 + a4c0fdb commit f5749c8
Show file tree
Hide file tree
Showing 40 changed files with 377 additions and 415 deletions.
1 change: 0 additions & 1 deletion .allow_skipping_tests
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@ controllers/placement_reports_controller.rb
controllers/reports_controller.rb
controllers/supervisor_volunteers_controller.rb
controllers/supervisors_controller.rb
controllers/system_settings_controller.rb
controllers/users/passwords_controller.rb
controllers/users/sessions_controller.rb
controllers/users_controller.rb
Expand Down
5 changes: 3 additions & 2 deletions app/assets/stylesheets/pages/casa_cases.scss
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,12 @@ body.casa_cases {
}

.a2cldr {
width: 175px;
width: 190px;
}

.a2cldr .a2cldr-btn {
width: 175px;
width: 190px;
text-align: center
}

.a2cldr-list {
Expand Down
30 changes: 16 additions & 14 deletions app/controllers/application_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -47,39 +47,41 @@ def handle_short_url(url_list)
# volunteer/supervisor/casa_admin controller uses to send SMS
# returns appropriate flash notice for SMS
def deliver_sms_to(resource, body_msg)
if resource.phone_number.blank?
if resource.phone_number.blank? || !resource.casa_org.twilio_enabled?
return "blank"
end
acc_sid = current_user.casa_org.twilio_account_sid
api_key = current_user.casa_org.twilio_api_key_sid
api_secret = current_user.casa_org.twilio_api_key_secret

body = body_msg
to = resource.phone_number
from = current_user.casa_org.twilio_phone_number

twilio = TwilioService.new(api_key, api_secret, acc_sid)
@twilio = TwilioService.new(current_user.casa_org)
req_params = {
From: from,
Body: body,
To: to
}

begin
twilio_res = twilio.send_sms(req_params)
twilio_res = @twilio.send_sms(req_params)
twilio_res.error_code.nil? ? "sent" : "error"
rescue Twilio::REST::RestError
rescue Twilio::REST::RestError => error
@error = error
"error"
rescue # unverfied error isnt picked up by Twilio::Rest::RestError
# https://www.twilio.com/docs/errors/21608
@error = "Phone number is unverifiied"
"error"
end
end

def sms_acct_creation_notice(resource_name, sms_status)
if sms_status === "blank"
return "New #{resource_name} created successfully."
end
if sms_status === "error"
return "New #{resource_name} created successfully. SMS not sent due to error."
end
if sms_status === "sent"
case sms_status
when "blank"
"New #{resource_name} created successfully."
when "error"
"New #{resource_name} created successfully. SMS not sent. Error: #{@error}."
when "sent"
"New #{resource_name} created successfully. SMS has been sent!"
end
end
Expand Down
4 changes: 3 additions & 1 deletion app/controllers/casa_org_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,12 @@ def casa_org_update_params
:logo,
:court_report_template,
:show_driving_reimbursement,
:additional_expenses_enabled,
:twilio_account_sid,
:twilio_phone_number,
:twilio_api_key_sid,
:twilio_api_key_secret
:twilio_api_key_secret,
:twilio_enabled
)
end

Expand Down
4 changes: 2 additions & 2 deletions app/controllers/case_contacts_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ def update
@current_organization_groups = current_organization.contact_type_groups

if @case_contact.update_cleaning_contact_types(update_case_contact_params)
if additional_expense_params&.any? && FeatureFlagService.is_enabled?(FeatureFlagService::SHOW_ADDITIONAL_EXPENSES_FLAG)
if additional_expense_params&.any? && policy(:case_contact).additional_expenses_allowed?
update_or_create_additional_expense(additional_expense_params, @case_contact)
end
if @case_contact.valid?
Expand Down Expand Up @@ -156,7 +156,7 @@ def save_or_add_error(obj, case_contact)

def create_case_contact_for_every_selected_casa_case(selected_cases)
selected_cases.map do |casa_case|
if FeatureFlagService.is_enabled?(FeatureFlagService::SHOW_ADDITIONAL_EXPENSES_FLAG)
if policy(:case_contact).additional_expenses_allowed?
new_cc = casa_case.case_contacts.new(create_case_contact_params.except(:casa_case_attributes))
update_or_create_additional_expense(additional_expense_params, new_cc)
if new_cc.valid?
Expand Down
19 changes: 0 additions & 19 deletions app/controllers/system_settings_controller.rb

This file was deleted.

2 changes: 1 addition & 1 deletion app/controllers/users/passwords_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ def send_password_reset_sms
@reset_token ||= @resource.generate_password_reset_token

create_short_url
twilio_service = TwilioService.new(@resource.casa_org.twilio_api_key_sid, @resource.casa_org.twilio_api_key_secret, @resource.casa_org.twilio_account_sid)
twilio_service = TwilioService.new(@resource.casa_org)
sms_params = {
From: @resource.casa_org.twilio_phone_number,
Body: password_reset_msg(@resource.display_name, @short_io_service.short_url),
Expand Down
12 changes: 7 additions & 5 deletions app/controllers/volunteers_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -99,10 +99,12 @@ def resend_invitation
def send_reactivation_alert
authorize @volunteer
if @volunteer.save
send_sms_to(volunteers_phone_number, "Hello #{@volunteer.display_name}, \n \n Your CASA/Prince George’s County volunteer console account has been reactivated. You can login using the credentials you were already using. \n \n If you have any questions, please contact your most recent Case Supervisor for assistance. \n \n CASA/Prince George’s County")
redirect_to edit_volunteer_path(@volunteer), notice: "Volunteer reactivation alert sent"
else
redirect_to edit_volunteer_path(@volunteer), alert: "Volunteer reactivation alert failed"
begin
send_sms_to(volunteers_phone_number, "Hello #{@volunteer.display_name}, \n \n Your CASA/Prince George’s County volunteer console account has been reactivated. You can login using the credentials you were already using. \n \n If you have any questions, please contact your most recent Case Supervisor for assistance. \n \n CASA/Prince George’s County")
redirect_to edit_volunteer_path(@volunteer), notice: "Volunteer reactivation alert sent"
rescue
redirect_to edit_volunteer_path(@volunteer), notice: "Volunteer reactivation alert not sent. Twilio is disabled for #{@volunteer.casa_org.name}."
end
end
end

Expand Down Expand Up @@ -162,7 +164,7 @@ def volunteers_phone_number
end

def send_sms_to(phone_number, body)
twilio = TwilioService.new(current_user.casa_org.twilio_api_key_sid, current_user.casa_org.twilio_api_key_secret, current_user.casa_org.twilio_account_sid)
twilio = TwilioService.new(current_user.casa_org)
req_params = {From: current_user.casa_org.twilio_phone_number, Body: body, To: phone_number}
twilio_res = twilio.send_sms(req_params)

Expand Down
32 changes: 17 additions & 15 deletions app/models/casa_org.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ class CasaOrg < ApplicationRecord

validates :name, presence: true, uniqueness: true
validates_with CasaOrgValidator
validate :validate_twilio_credentials, if: -> { twilio_account_sid.present? || twilio_api_key_sid.present? || twilio_api_key_secret.present? }, on: :update
validate :validate_twilio_credentials, if: -> { twilio_enabled || twilio_account_sid.present? || twilio_api_key_sid.present? || twilio_api_key_secret.present? }, on: :update

has_many :users, dependent: :destroy
has_many :casa_cases, dependent: :destroy
Expand Down Expand Up @@ -117,20 +117,22 @@ def normalize_phone_number
#
# Table name: casa_orgs
#
# id :bigint not null, primary key
# address :string
# display_name :string
# footer_links :string default([]), is an Array
# name :string not null
# show_driving_reimbursement :boolean default(TRUE)
# show_fund_request :boolean default(FALSE)
# slug :string
# twilio_account_sid :string
# twilio_api_key_secret :string
# twilio_api_key_sid :string
# twilio_phone_number :string
# created_at :datetime not null
# updated_at :datetime not null
# id :bigint not null, primary key
# additional_expenses_enabled :boolean default(FALSE)
# address :string
# display_name :string
# footer_links :string default([]), is an Array
# name :string not null
# show_driving_reimbursement :boolean default(TRUE)
# show_fund_request :boolean default(FALSE)
# slug :string
# twilio_account_sid :string
# twilio_api_key_secret :string
# twilio_api_key_sid :string
# twilio_enabled :boolean default(FALSE)
# twilio_phone_number :string
# created_at :datetime not null
# updated_at :datetime not null
#
# Indexes
#
Expand Down
2 changes: 1 addition & 1 deletion app/notifications/delivery_methods/sms.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ def deliver
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 = TwilioService.new(sender.casa_org)
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
Expand Down
1 change: 0 additions & 1 deletion app/policies/application_policy.rb
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,5 @@ def current_organization
end

alias_method :modify_organization?, :is_admin?
alias_method :edit_system_settings?, :is_admin?
alias_method :see_import_page?, :is_admin?
end
5 changes: 5 additions & 0 deletions app/policies/case_contact_policy.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@ def is_creator_or_supervisor_or_casa_admin?
is_creator? || admin_or_supervisor?
end

def additional_expenses_allowed?
FeatureFlagService.is_enabled?(FeatureFlagService::SHOW_ADDITIONAL_EXPENSES_FLAG) &&
current_organization.additional_expenses_enabled
end

alias_method :index?, :admin_or_supervisor_or_volunteer?
alias_method :new?, :admin_or_supervisor_or_volunteer?
alias_method :show?, :is_creator_or_casa_admin?
Expand Down
4 changes: 2 additions & 2 deletions app/services/sms_reminder_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ module SmsReminderService

BASE_URL = Rails.application.credentials[:BASE_URL]
def send_reminder(user, message)
return if !user[:receive_sms_notifications] || user[:phone_number].blank?
return if !user[:receive_sms_notifications] || user[:phone_number].blank? || !user.casa_org.twilio_enabled?

user_casa_org = user.casa_org
twilio_service = TwilioService.new(user_casa_org.twilio_api_key_sid, user_casa_org.twilio_api_key_secret, user_casa_org.twilio_account_sid)
twilio_service = TwilioService.new(user_casa_org)
sms_params = {
From: user_casa_org.twilio_phone_number,
Body: message,
Expand Down
32 changes: 23 additions & 9 deletions app/services/twilio_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,32 +2,46 @@
require "twilio-ruby"

class TwilioService
attr_writer :api_key, :api_secret, :acc_sid
attr_writer :api_key, :api_secret, :acc_sid, :casa_org

def initialize(api_key, api_secret, acc_sid)
@api_key = api_key
@api_secret = api_secret
@acc_sid = acc_sid
@client = Twilio::REST::Client.new(api_key, api_secret, acc_sid)
def initialize(casa_org)
@api_key = casa_org.twilio_api_key_sid
@api_secret = casa_org.twilio_api_key_secret
@acc_sid = casa_org.twilio_account_sid
@enabled = casa_org.twilio_enabled
end

def client # lazy create client only if twilio enabled
@client = Twilio::REST::Client.new(@api_key, @api_secret, @acc_sid)
end

def enabled?
@enabled
end

# this method takes in a hash
# required keys are: From:, To:, Body:
# to send a short url, set URL: key in hash
def send_sms(params)
if !enabled?
return nil
end

from = params[:From]
body = params.key?(:URL) ? params[:Body] + params[:URL] : params[:Body]
to = params[:To]
# returns a twilio API message object
# refer to docs: https://www.twilio.com/docs/sms/api/message-resource#message-properties
begin
@client.messages.create(
client
client.messages.create(
from: from,
body: body,
to: to
)
rescue => e
Rails.logger.error("send SMS failed: #{e}")
rescue => error
Rails.logger.error("send SMS failed: #{error}")
error
end
end
end
4 changes: 4 additions & 0 deletions app/views/casa_org/edit.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,10 @@
<%= form.check_box :show_driving_reimbursement, class: 'form-check-input' %>
<%= form.label :show_driving_reimbursement, "Show driving reimbursement", class: 'form-check-label mb-2' %>
</div>
<div class="form-check checkbox-style mb-20">
<%= form.check_box :additional_expenses_enabled, class: 'form-check-input' %>
<%= form.label :additional_expenses_enabled, "Volunteers can add Other Expenses", class: 'form-check-label mb-2' %>
</div>
<div class="actions mb-10">
<%= button_tag(
type: "submit",
Expand Down
2 changes: 1 addition & 1 deletion app/views/case_contacts/_form.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ readonly: true %>
</div>
<% end %>
<% if FeatureFlagService.is_enabled?(FeatureFlagService::SHOW_ADDITIONAL_EXPENSES_FLAG) %>
<% if Pundit.policy(current_user, case_contact).additional_expenses_allowed? %>
<div class="other-expenses">
<h5 class="mb-3"><%= form.label :additional_expense, "d. Other Expenses" %></h5>
<ol>
Expand Down
9 changes: 0 additions & 9 deletions app/views/layouts/_sidebar.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -142,15 +142,6 @@
</li>
<% end %>
<% if policy(:application).edit_system_settings? %>
<li>
<%= link_to settings_path, class: "#{active_class(settings_path)}" do %>
<i class="lni lni-cog mr-10"></i>
System Settings
<% end %>
</li>
<% end %>
<% if policy(:application).see_mileage_rate? %>
<li>
<%= link_to mileage_rates_path, class: "#{active_class(mileage_rates_path)}" do %>
Expand Down
33 changes: 0 additions & 33 deletions app/views/system_settings/index.html.erb

This file was deleted.

1 change: 0 additions & 1 deletion config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,6 @@
get :download_failed
end
end
resources :settings, only: %i[index create], controller: "system_settings"
resources :case_contact_reports, only: %i[index]
resources :mileage_reports, only: %i[index]
resources :mileage_rates, only: %i[index new create edit update]
Expand Down
5 changes: 5 additions & 0 deletions db/migrate/20230615155223_add_twilio_enabled_to_casa_orgs.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class AddTwilioEnabledToCasaOrgs < ActiveRecord::Migration[7.0]
def change
add_column :casa_orgs, :twilio_enabled, :boolean, default: false
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class AddAdditionalExpensesToCasaOrgs < ActiveRecord::Migration[7.0]
def change
add_column :casa_orgs, :additional_expenses_enabled, :boolean, default: false
end
end
Loading

0 comments on commit f5749c8

Please sign in to comment.