-
-
Notifications
You must be signed in to change notification settings - Fork 528
Expand file tree
/
Copy pathvolunteers_controller.rb
More file actions
193 lines (159 loc) · 5.73 KB
/
volunteers_controller.rb
File metadata and controls
193 lines (159 loc) · 5.73 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
class VolunteersController < ApplicationController
include SmsBodyHelper
before_action :set_volunteer, except: %i[index new create datatable stop_impersonating]
after_action :verify_authorized, except: %i[stop_impersonating]
def index
authorize Volunteer
@supervisors = policy_scope(current_organization.supervisors)
end
def show
authorize @volunteer
redirect_to action: :edit
end
def datatable
authorize Volunteer
volunteers = policy_scope current_organization.volunteers
datatable = VolunteerDatatable.new volunteers, params
render json: datatable
end
def new
@volunteer = current_organization.volunteers.new
authorize @volunteer
end
def create
@volunteer = current_organization.volunteers.new(create_volunteer_params)
authorize @volunteer
if @volunteer.save
# invitation error handling
begin
@volunteer.invite!(current_user)
rescue => e
flash[:alert] = "Volunteer invitation failed. Reason: #{e.message}"
end
# call short io api here
invitation_url = Rails.application.routes.url_helpers.accept_user_invitation_url(invitation_token: @volunteer.raw_invitation_token, host: request.base_url)
hash_of_short_urls = {0 => nil, 1 => nil}
if @volunteer.phone_number.present?
hash_of_short_urls = handle_short_url([invitation_url, request.base_url + "/users/edit"])
end
sms_status = deliver_sms_to @volunteer, account_activation_msg("volunteer", hash_of_short_urls)
redirect_to edit_volunteer_path(@volunteer), notice: sms_acct_creation_notice("volunteer", sms_status)
else
render :new, status: :unprocessable_content
end
end
def edit
authorize @volunteer
@supervisors = policy_scope current_organization.supervisors.active
end
def update
authorize @volunteer
if params.dig(:volunteer, :email).present? && params[:volunteer][:email] != @volunteer.email
@volunteer.skip_reconfirmation!
end
if @volunteer.update(update_volunteer_params)
notice = check_unconfirmed_email_notice(@volunteer)
@volunteer.filter_old_emails!(@volunteer.email)
redirect_to edit_volunteer_path(@volunteer), notice: notice
else
render :edit, status: :unprocessable_content
end
end
def activate
authorize @volunteer
if @volunteer.activate
VolunteerMailer.account_setup(@volunteer).deliver
if (params[:redirect_to_path] == "casa_case") && (casa_case = CasaCase.friendly.find(params[:casa_case_id]))
redirect_to edit_casa_case_path(casa_case), notice: "Volunteer was activated. They have been sent an email."
else
redirect_to edit_volunteer_path(@volunteer), notice: "Volunteer was activated. They have been sent an email."
end
else
render :edit, status: :unprocessable_content
end
end
def deactivate
authorize @volunteer
if @volunteer.deactivate
redirect_to edit_volunteer_path(@volunteer), notice: "Volunteer was deactivated."
else
render :edit, status: :unprocessable_content
end
end
def resend_invitation
authorize @volunteer
@volunteer = Volunteer.find(params[:id])
if @volunteer.invitation_accepted_at.nil?
@volunteer.invite!(current_user)
redirect_to edit_volunteer_path(@volunteer), notice: "Invitation sent"
else
redirect_to edit_volunteer_path(@volunteer), notice: "User already accepted invitation"
end
end
def send_reactivation_alert
authorize @volunteer
if @volunteer.save
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
def reminder
authorize @volunteer
with_cc = params[:with_cc].present?
cc_recipients = []
if with_cc
if current_user.casa_admin?
cc_recipients.append(current_user.email)
end
cc_recipients.append(@volunteer.supervisor.email) if @volunteer.supervisor
end
VolunteerMailer.case_contacts_reminder(@volunteer, cc_recipients).deliver
redirect_to edit_volunteer_path(@volunteer), notice: "Reminder sent to volunteer."
end
def impersonate
authorize @volunteer
impersonate_user(@volunteer)
redirect_to root_path
end
def stop_impersonating
stop_impersonating_user
redirect_to root_path
end
private
def set_volunteer
@volunteer = Volunteer.find(params[:id])
end
def generate_devise_password
Devise.friendly_token.first(8)
end
def create_volunteer_params
VolunteerParameters
.new(params)
.with_password(generate_devise_password)
.without_active
end
def update_volunteer_params
VolunteerParameters
.new(params)
.without_active
end
def volunteers_phone_number
authorize @volunteer
@volunteers_phone_number = @volunteer.phone_number
end
def send_sms_to(phone_number, body)
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)
# Error handling for spec test purposes
if twilio_res.error_code.nil?
"SMS has been sent to Volunteer!"
else
"SMS was not sent to Volunteer due to an error."
end
end
end