@@ -19,7 +19,8 @@ def create
1919 create_invitation_success ( result )
2020 else
2121 propagate_errors
22- render json : { errors : current_course . errors . full_messages . to_sentence } , status : :bad_request
22+ errors = current_course . errors [ :base ]
23+ render json : { errors : errors } , status : :bad_request
2324 end
2425 end
2526
@@ -59,7 +60,8 @@ def course_user_invitation_params
5960 @course_user_invitation_params ||= begin
6061 params [ :course ] = { invitations_attributes : { } } unless params . key? ( :course )
6162 params . require ( :course ) . permit ( :invitations_file , :registration_key ,
62- invitations_attributes : [ :name , :email , :role , :phantom , :timeline_algorithm ] )
63+ invitations_attributes : [ :name , :email , :role , :phantom ,
64+ :timeline_algorithm , :external_id ] )
6365 end
6466 end
6567
@@ -120,7 +122,7 @@ def invite_by_file?
120122 def invite
121123 invitation_service . invite ( invitation_params )
122124 rescue CSV ::MalformedCSVError => e
123- current_course . errors . add ( :invitations_file , e . message )
125+ current_course . errors . add ( :base , e . message )
124126 false
125127 end
126128
@@ -135,15 +137,7 @@ def invitation_service
135137 #
136138 # @return [void]
137139 def propagate_errors
138- propagate_errors_to_file if invite_by_file?
139- end
140-
141- # Propagates errors from the generated records to the file.
142- #
143- # @return [void]
144- def propagate_errors_to_file
145- errors = aggregate_errors
146- current_course . errors . add ( :invitations_file , errors . to_sentence ) unless errors . empty?
140+ aggregate_errors . each { |msg | current_course . errors . add ( :base , msg ) }
147141 end
148142
149143 # Aggregates errors from all the known sources of failure.
@@ -157,9 +151,23 @@ def aggregate_errors
157151 #
158152 # @return [Array<String>]
159153 def invalid_course_user_errors
160- invalid_course_users . map do |course_user |
161- user = self . class . helpers . display_course_user ( course_user )
162- t ( 'errors.course.user_invitations.duplicate_user' , user : user )
154+ invalid_course_users . flat_map do |course_user |
155+ email = course_user . user &.email || course_user . name
156+ errors = [ ]
157+ if course_user . errors [ :external_id ] . any?
158+ errors << t ( 'errors.course.user_invitations.duplicate_external_id' ,
159+ email : email , external_id : course_user . external_id )
160+ end
161+ if course_user . errors [ :user ] . any? || course_user . errors [ :course ] . any?
162+ errors << t ( 'errors.course.user_invitations.already_enrolled' , email : email )
163+ end
164+ other_errors = course_user . errors . reject { |e | %w[ external_id user course ] . include? ( e . attribute . to_s ) }
165+ if other_errors . any?
166+ message = other_errors . map { |e | course_user . errors . full_message ( e . attribute , e . message ) } .
167+ to_sentence
168+ errors << t ( 'errors.course.user_invitations.other_error' , email : email , message : message )
169+ end
170+ errors
163171 end
164172 end
165173
@@ -174,9 +182,25 @@ def invalid_course_users
174182 #
175183 # @return [Array<String>]
176184 def invalid_invitation_email_errors
177- invalid_invitations . map do |invitation |
178- message = invitation . errors . full_messages . to_sentence
179- t ( 'errors.course.user_invitations.invalid_email' , email : invitation . email , message : message )
185+ invalid_invitations . flat_map do |invitation |
186+ email = invitation . email
187+ errors = [ ]
188+ if invitation . errors [ :external_id ] . any?
189+ errors << t ( 'errors.course.user_invitations.duplicate_external_id' ,
190+ email : email , external_id : invitation . external_id )
191+ end
192+ if invitation . errors [ :email ] . any?
193+ message = invitation . errors [ :email ] . to_sentence
194+ errors << t ( 'errors.course.user_invitations.invalid_email' , email : email , message : message )
195+ end
196+ errors << t ( 'errors.course.user_invitations.duplicate_invitation' , email : email ) if invitation . errors [ :base ] . any?
197+ other_errors = invitation . errors . reject { |e | %w[ external_id email base ] . include? ( e . attribute . to_s ) }
198+ if other_errors . any?
199+ message = other_errors . map { |e | invitation . errors . full_message ( e . attribute , e . message ) } .
200+ to_sentence
201+ errors << t ( 'errors.course.user_invitations.other_error' , email : email , message : message )
202+ end
203+ errors
180204 end
181205 end
182206
@@ -254,7 +278,7 @@ def destroy_invitation_success
254278
255279 def destroy_invitation_failure
256280 respond_to do |format |
257- format . json { render json : { errors : @invitation . errors . full_messages . to_sentence } , status : :bad_request }
281+ format . json { render json : { errors : @invitation . errors . full_messages } , status : :bad_request }
258282 end
259283 end
260284
0 commit comments