-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathteachers_controller.rb
More file actions
382 lines (327 loc) · 12.2 KB
/
teachers_controller.rb
File metadata and controls
382 lines (327 loc) · 12.2 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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
# frozen_string_literal: true
# TODO: These belong in a separate file.
require "smarter_csv"
require "csv_process"
require "activerecord-import"
class TeachersController < ApplicationController
include SchoolParams
include CsvProcess
before_action :load_pages, only: [:new, :create, :edit, :update]
before_action :load_teacher, except: [:new, :index, :create, :import, :search, :sync_all_mailbluster]
before_action :sanitize_params, only: [:new, :create, :edit, :update]
before_action :require_login, except: [:new, :create]
before_action :require_admin, only: [:validate, :deny, :destroy, :index, :show, :search, :sync_mailbluster, :sync_all_mailbluster]
before_action :require_edit_permission, only: [:edit, :update, :resend_welcome_email]
rescue_from ActiveRecord::RecordNotUnique, with: :deny_access
# Pages which are publicly visible.
layout "page_with_sidebar", only: [:new, :edit, :update]
def index
respond_to do |format|
format.html {
@all_teachers = Teacher.eager_load(:email_addresses, :school).where(admin: false)
@admins = Teacher.eager_load(:email_addresses, :school).where(admin: true)
}
format.csv { send_data Teacher.csv_export, filename: "teachers-#{Date.today}.csv" }
end
end
def show
@all_teachers_except_current = Teacher.preload(:email_addresses, :school).where.not(id: @teacher.id)
@school = @teacher.school
@status = is_admin? ? "Admin" : "Teacher"
end
def new
ordered_schools
@teacher = Teacher.new
@teacher.school = School.new
@school = @teacher.school # maybe delegate this
@readonly = false
if omniauth_data.present?
email_data = { email: omniauth_data.delete("email"), primary: true }
@teacher.assign_attributes(omniauth_data)
@teacher.email_addresses.build(email_data)
end
end
# TODO: This needs to be re-written.
# If you are logged in and not an admin, this should fail.
def create
if existing_teacher
return
end
load_school
if @school.new_record?
return unless params[:school]
@school = School.new(school_params)
unless @school.save
flash[:alert] = "An error occurred: #{@school.errors.full_messages.join(', ')}"
render "new" && return
end
end
@teacher = Teacher.new(teacher_params)
@teacher.email_addresses.build(email: params[:email][:primary], primary: true)
@teacher.try_append_ip(request.remote_ip)
@teacher.session_count += 1
@teacher.school = @school
if @teacher.save
@teacher.not_reviewed!
flash[:success] = "Thanks for signing up for BJC, #{@teacher.first_name}! You'll hear from us shortly. Your email address is: #{@teacher.primary_email}."
TeacherMailer.form_submission(@teacher).deliver_now
TeacherMailer.teacher_form_submission(@teacher).deliver_now
redirect_to root_path
else
flash.now[:alert] = "An error occurred: #{@teacher.errors.full_messages.join(', ')}"
render "new"
end
end
def edit
ordered_schools
@school = @teacher.school
@status = is_admin? ? "Admin" : "Teacher"
@readonly = !is_admin?
end
def remove_file
file_attachment = @teacher.files.find(params[:file_id])
file_attachment.purge
flash[:notice] = "File was successfully removed"
redirect_back fallback_location: teacher_path(@teacher)
end
def upload_file
@teacher.files.attach(params[:file])
redirect_back fallback_location: teacher_path(@teacher), notice: "File was successfully uploaded"
end
def update
load_school
ordered_schools
primary_email = params.dig(:email, :primary)
@teacher.assign_attributes(teacher_params)
update_primary_email(primary_email)
if teacher_params[:school_id].present?
@teacher.school = @school
else
@school.update(school_params) if school_params
unless @school.save
flash.now[:alert] = "An error occurred: #{@school.errors.full_messages.join(', ')}"
render "edit" && return
end
@teacher.school = @school
end
valid_school = update_school_through_teacher
if !valid_school
return
end
attach_new_files_if_any
send_email_if_application_status_changed_and_email_resend_enabled
sync_to_mailbluster_if_status_changed
if fail_to_update
return
end
if !@teacher.validated? && !current_user.admin?
TeacherMailer.form_submission(@teacher).deliver_now
TeacherMailer.teacher_form_submission(@teacher).deliver_now
end
if is_admin?
redirect_to edit_teacher_path(params[:id]), notice: "Saved #{@teacher.full_name}"
else
@teacher.try_append_ip(request.remote_ip)
redirect_to edit_teacher_path(params[:id]), notice: "Successfully updated your information"
end
end
def send_email_if_application_status_changed_and_email_resend_enabled
if @teacher.application_status_changed? && params[:skip_email] == "No"
case @teacher.application_status
when "validated"
TeacherMailer.welcome_email(@teacher).deliver_now
when "denied"
TeacherMailer.deny_email(@teacher, params[:request_reason]).deliver_now
when "info_needed"
TeacherMailer.request_info_email(@teacher, params[:request_reason]).deliver_now
end
end
end
def sync_to_mailbluster_if_status_changed
return unless MailblusterService.configured?
return unless @teacher.application_status_changed?
if @teacher.validated?
MailblusterService.create_or_update_lead(@teacher)
elsif @teacher.application_status_was == "validated"
# If teacher was validated but status changed away, update MailBluster
# to mark them as unsubscribed
MailblusterService.create_or_update_lead(@teacher)
end
end
def request_info
@teacher.info_needed!
if !params[:skip_email].present?
TeacherMailer.request_info_email(@teacher, params[:request_reason]).deliver_now
end
redirect_to root_path
end
def validate
@teacher.validated!
TeacherMailer.welcome_email(@teacher).deliver_now
MailblusterService.create_or_update_lead(@teacher) if MailblusterService.configured?
redirect_to root_path
end
def deny
@teacher.denied!
if !params[:skip_email].present?
TeacherMailer.deny_email(@teacher, params[:denial_reason]).deliver_now
end
redirect_to root_path
end
def destroy
if MailblusterService.configured? && @teacher.primary_email.present?
MailblusterService.delete_lead(@teacher.primary_email)
end
@teacher.destroy!
flash[:info] = "Deleted #{@teacher.full_name} successfully."
redirect_to teachers_path
end
def resend_welcome_email
if @teacher.validated? || @is_admin
flash[:success] = "Welcome email resent successfully!"
TeacherMailer.welcome_email(@teacher).deliver_now
else
flash[:alert] = "Error resending welcome email. Please ensure that your account has been validated by an administrator."
end
redirect_back(fallback_location: dashboard_path)
end
def import
csv_file = params[:file]
teacher_hash_array = SmarterCSV.process(csv_file)
csv_import_summary_hash = process_record(teacher_hash_array)
add_flash_message(csv_import_summary_hash)
redirect_to teachers_path
end
def sync_mailbluster
unless MailblusterService.configured?
redirect_to teacher_path(@teacher), alert: "MailBluster API key is not configured."
return
end
result = MailblusterService.sync_teacher(@teacher)
if result[:success]
redirect_to teacher_path(@teacher), notice: "Successfully synced #{@teacher.full_name} to MailBluster."
else
redirect_to teacher_path(@teacher), alert: "Failed to sync #{@teacher.full_name} to MailBluster. #{result[:error]}"
end
end
def sync_all_mailbluster
unless MailblusterService.configured?
redirect_to teachers_path, alert: "MailBluster API key is not configured."
return
end
results = MailblusterService.sync_all_teachers
flash[:notice] = "MailBluster sync complete: #{results[:synced]} synced, #{results[:failed]} failed, #{results[:skipped]} skipped."
if results[:errors].any?
flash[:alert] = "Errors: #{results[:errors].first(5).join('; ')}"
end
redirect_to teachers_path
end
private
def load_teacher
@teachers = Teacher.all
@teacher ||= Teacher.find(params[:id])
end
def deny_access
redirect_to new_teacher_path, alert: "Email address or Snap username already in use. Please use a different email or Snap username."
end
def existing_teacher
# Find by email, but allow updating other info.
@teacher = EmailAddress.find_by(email: params.dig(:email, :primary))&.teacher
if @teacher && defined?(current_user.id) && (current_user.id == @teacher.id)
params[:id] = current_user.id
update
return true
elsif @teacher
redirect_to login_path, notice: "You already have signed up with '#{@teacher.email}'. Please log in."
return true
end
false
end
def update_school_through_teacher
if !teacher_params[:school_id].present?
@school.update(school_params) if school_params
unless @school.save
flash[:alert] = "An error occurred: #{@school.errors.full_messages.join(', ')}"
render "new"
return false
end
end
@teacher.school = @school
true
end
def fail_to_update
if @teacher.denied? && !is_admin?
redirect_to root_path, alert: "Failed to update your information. You have already been denied. If you have questions, please email contact@bjc.berkeley.edu."
return true
elsif (@teacher.email_changed_flag || @teacher.snap_changed?) && !is_admin?
flash.now[:alert] = "Failed to update your information. If you want to change your email or Snap! username, please email contact@bjc.berkeley.edu."
render "edit"
return true
elsif !@teacher.save
flash.now[:alert] = "Failed to update data. #{@teacher.errors.full_messages.to_sentence}"
render "edit"
return true
end
false
end
def load_school
if teacher_params[:school_id].present?
@school ||= School.find(teacher_params[:school_id])
end
@school ||= School.find_or_create_by(**unique_school_params)
end
def attach_new_files_if_any
if params.dig(:teacher, :more_files).present?
params[:teacher][:more_files].each do |file|
@teacher.files.attach(file)
end
end
end
def teacher_params
teacher_attributes = [:first_name, :last_name, :school, :status, :snap,
:more_info, :verification_notes, :personal_website, :education_level, :school_id, languages: [], files: [],
more_files: []]
admin_attributes = [:application_status, :request_reason, :skip_email]
teacher_attributes.push(*admin_attributes) if is_admin?
params.require(:teacher).permit(*teacher_attributes)
end
def omniauth_data
@omniauth_data ||= session[:auth_data]&.slice("first_name", "last_name", "email")
end
def ordered_schools
if params[:id].present?
load_teacher
@ordered_schools ||= [@teacher.school] +
School.all.order(:name).reject { |s| s.id == @teacher.school_id }
else
@ordered_schools ||= School.all.order(:name)
end
end
def sanitize_params
teacher = params[:teacher]
if teacher && teacher[:status]
teacher[:status] = teacher[:status].to_i
end
if teacher && teacher[:education_level]
teacher[:education_level] = teacher[:education_level].to_i
end
school = params[:school]
if school && school[:grade_level]
school[:grade_level] = school[:grade_level].to_i
end
if school && school[:school_type]
school[:school_type] = school[:school_type].to_i
end
end
def load_pages
@pages ||= Page.where(viewer_permissions: Page.viewable_pages(current_user))
end
def update_primary_email(primary_email)
return unless primary_email.present?
# First, ensure the current primary email is marked as not primary if it's not the same as the new one
@teacher.email_addresses.find_by(primary: true)&.update(primary: false)
primary_email_record = @teacher.email_addresses.find_or_initialize_by(email: primary_email)
primary_email_record.primary = true
primary_email_record.save if primary_email_record.changed?
end
end