Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions db/migrate/20261112152938_add_not_null_to_role_user_id.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class AddNotNullToRoleUserId < ActiveRecord::Migration[8.1]
def change
change_column_null(:roles, :user_id, false)
end
end
4 changes: 2 additions & 2 deletions db/schema.rb

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 10 additions & 1 deletion lib/concepts/school_student/create_batch_sso.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,10 @@ def create_batch_sso(school, students, current_user)
SafeguardingFlagService.create_for_school_roles(user: current_user, school:)
responses = ProfileApiClient.create_school_students_sso(token:, students:, school_id: school.id)

create_student_roles(school, responses)
successful_responses, failed_responses = responses.partition { |response| response[:success] }

report_failures(failed_responses)
create_student_roles(school, successful_responses)
format_student_responses(responses)
rescue ProfileApiClient::Student422Error => e
handle_validations(e.errors)
Expand All @@ -50,6 +53,12 @@ def normalize_student_params(students)
end
end

def report_failures(responses)
responses.each do |response|
Sentry.capture_message('Student creation failed in Profile', extra: response[:error])
end
end

def create_student_roles(school, responses)
user_ids = responses.pluck(:id)
existing_user_ids = Role.student.where(school_id: school.id, user_id: user_ids).pluck(:user_id)
Expand Down
39 changes: 39 additions & 0 deletions spec/concepts/school_student/create_batch_sso_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,45 @@
end
end

context 'when an error occurs creating an individual student in profile' do
before do
responses = [
{ id: SecureRandom.uuid, name: 'Success 1', success: true },
{
id: nil,
error: {
path: 'student',
errorCode: 'ERROR_CREATE_STUDENT_FAILED',
message: 'here is an error message',
location: 'body'
},
success: false
},
{ id: SecureRandom.uuid, name: 'Success 2', success: true }
]
allow(ProfileApiClient).to receive(:create_school_students_sso).and_return(responses)
allow(Sentry).to receive(:capture_message)
end

it 'only creates the successful students' do
initial_student_role_count = Role.student.count

described_class.call(school:, school_students_params:, current_user:)

expect(Role.student.count).to eq(initial_student_role_count + 2)
end

it 'reports unexpected errors to sentry' do
described_class.call(school:, school_students_params:, current_user:)
expect(Sentry).to have_received(:capture_message).with('Student creation failed in Profile', extra: {
path: 'student',
errorCode: 'ERROR_CREATE_STUDENT_FAILED',
message: 'here is an error message',
location: 'body'
})
end
end

context 'when a standard error occurs' do
before do
allow(ProfileApiClient).to receive(:create_school_students_sso)
Expand Down
Loading