-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy patheducation_controller.rb
More file actions
248 lines (214 loc) · 8.55 KB
/
Copy patheducation_controller.rb
File metadata and controls
248 lines (214 loc) · 8.55 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
# Query params used for navigation context:
#
# from_edit — "User entered from the hub's Edit button." Set only by the
# hub edit link. Threads through the flow so the review page
# shows "Save changes" and hides the back button.
#
# from_review — "User clicked Edit on the review page to fix one thing."
# Set by edit links on the review page. Tells controllers to
# redirect back to review instead of advancing forward.
class Activities::EducationController < Activities::BaseController
# Keep the user on the loading page (the #show action) at least this long.
ARTIFICIAL_DELAY = 7.seconds
INDICATOR_COUNT = 3
before_action :redirect_if_nsc_disabled, only: %i[verify show sync error]
before_action :set_education_activity, only: %i[show edit update destroy review save_review]
before_action :set_back_url, only: %i[edit review]
after_action :track_info_viewed_event, only: %i[new edit]
after_action :track_review_viewed_event, only: :review
def verify
@identity = current_identity!
end
def create
if params[:education_activity]
create_fully_self_attested_activity
elsif nsc_disabled?
redirect_to new_activities_flow_education_path
else
create_validated_activity
end
end
def show
return redirect_to edit_activities_flow_education_path(id: @education_activity.id) if @education_activity.pre_populated_draft?
@polling_url = activities_flow_education_sync_path(education_id: @education_activity.id)
set_completed_indicators
if @education_activity.sync_failed? || @education_activity.sync_no_enrollments?
redirect_to activities_flow_education_error_path
elsif @education_activity.sync_succeeded? && !testing_synchronization_page?
redirect_to education_sync_success_path
else
# sync is still in progress — render the polling page
end
end
def update
if @education_activity.fully_self_attested?
if @education_activity.update(fully_self_attested_education_params)
track_event(TrackEvent::EducationInfoSubmitted, education_activity_id: @education_activity.id)
if params[:from_review].present?
redirect_to review_activities_flow_education_path(id: @education_activity, from_edit: params[:from_edit].presence)
else
redirect_to edit_activities_flow_education_month_path(education_id: @education_activity, id: 0, from_edit: params[:from_edit].presence)
end
else
track_event(
TrackEvent::EducationInfoValidationFailed,
education_activity_id: @education_activity.id,
error_fields: @education_activity.errors.attribute_names.map(&:to_s)
)
render :edit_fully_self_attested, status: :unprocessable_content
end
elsif @education_activity.update(education_params)
if @education_activity.partially_self_attested? && @education_activity.has_less_than_half_time_terms?
redirect_to edit_activities_flow_education_term_credit_hour_path(
education_id: @education_activity, id: 0
)
else
@education_activity.publish!
redirect_to after_activity_path
end
else
redirect_to :edit, flash: { alert: t("activities.education.errors.unexpected") }
end
end
def edit
render :edit_fully_self_attested if @education_activity.fully_self_attested?
end
def destroy
@education_activity.destroy
redirect_to activities_flow_root_path
end
def sync
@education_activity = @flow.education_activities.find(params[:education_id])
set_completed_indicators
if @education_activity.sync_unknown?
render turbo_stream: turbo_stream.replace(:synchronization, partial: "status")
elsif @education_activity.sync_failed? || @education_activity.sync_no_enrollments?
render turbo_stream: turbo_stream.action(:redirect, activities_flow_education_error_path)
elsif @wait_time < ARTIFICIAL_DELAY && !testing_synchronization_page?
render turbo_stream: turbo_stream.replace(:synchronization, partial: "status")
else
render turbo_stream: turbo_stream.action(:redirect, education_sync_success_path)
end
end
def new
@education_activity = @flow.education_activities.new
end
def review
end
def save_review
@education_activity.update(review_params)
@education_activity.publish!
# Fires for every data source; see track_review_viewed_event.
track_event(TrackEvent::EducationReviewSubmitted, education_activity_id: @education_activity.id)
if @education_activity.fully_self_attested?
redirect_to activities_flow_root_path
else
redirect_to after_activity_path
end
end
def error
end
private
def set_education_activity
@education_activity = @flow.education_activities.find(params[:id])
end
def set_back_url
case action_name
when "edit"
if params[:from_review].present?
@back_url = review_activities_flow_education_path(
id: @education_activity,
from_edit: params[:from_edit].presence
)
end
when "review"
unless params[:from_edit].present?
@back_url = new_activities_flow_education_document_upload_path(
education_id: @education_activity
)
end
end
end
def review_params
params.require(:education_activity).permit(:additional_comments)
end
def education_params
params
.require(:education_activity)
.permit(
:id,
:additional_comments,
:credit_hours
)
end
def fully_self_attested_education_params
params.require(:education_activity).permit(
:school_name, :street_address, :street_address_line_2,
:city, :state, :zip_code,
:contact_name, :contact_email, :contact_phone_number
)
end
def set_completed_indicators
if @education_activity.sync_unknown?
@completed_indicators = INDICATOR_COUNT.times.map { |i| false }
else
@wait_time = Time.now - @education_activity.created_at
@completed_indicators = INDICATOR_COUNT.times.map { |i| @wait_time > ((ARTIFICIAL_DELAY - 1) / INDICATOR_COUNT * (i + 1)) }
end
end
# In order to test the behavior of the /synchronization page in E2E tests, we
# want to disable the artificial delay and ensure the page always shows even
# if the synchronization actually happens immediately.
def testing_synchronization_page?
Rails.env.test?
end
def create_fully_self_attested_activity
@education_activity = @flow.education_activities.new(fully_self_attested_education_params.merge(draft: true))
@education_activity.data_source = :fully_self_attested
if @education_activity.save
track_event(TrackEvent::EducationInfoSubmitted, education_activity_id: @education_activity.id)
redirect_to edit_activities_flow_education_month_path(education_id: @education_activity, id: 0)
else
track_event(
TrackEvent::EducationInfoValidationFailed,
education_activity_id: @education_activity&.id,
error_fields: @education_activity.errors.attribute_names.map(&:to_s)
)
render :new, status: :unprocessable_content
end
end
def create_validated_activity
@education_activity = @flow.education_activities.create(draft: true)
NscSynchronizationJob.perform_later(@education_activity.id)
redirect_to activities_flow_education_path(id: @education_activity.id)
end
def education_sync_success_path
if @education_activity.partially_self_attested? || @education_activity.validated?
edit_activities_flow_education_path(id: @education_activity.id)
else
after_activity_path
end
end
def summer_logic_applied?
terms = @education_activity.nsc_enrollment_terms
@education_activity.activity_flow.reporting_months.any? do |month_start|
EducationSummerCarryoverService.applies?(terms, month_start)
end
end
helper_method :summer_logic_applied?
def track_info_viewed_event
return unless response.successful?
if action_name == "edit" && !@education_activity&.fully_self_attested?
track_event(TrackEvent::EducationEnrollmentReviewViewed, education_activity_id: @education_activity&.id)
else
track_event(TrackEvent::EducationInfoViewed, education_activity_id: @education_activity&.id)
end
end
# Fires for every data source (validated, partially_self_attested, fully_self_attested)
# since the review page and review step are shared across all of them, not just the
# self-attested paths.
def track_review_viewed_event
return unless response.successful?
track_event(TrackEvent::EducationReviewViewed, education_activity_id: @education_activity.id)
end
end