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
20 changes: 10 additions & 10 deletions app/controllers/api/class_members_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,28 +22,28 @@ def index
end

def create
user_ids = [class_member_params[:user_id]]
user_type = class_member_params[:type]
user_id, user_type = class_member_params.require(%i[user_id type])
owners = SchoolOwner::List.call(school: @school).fetch(:school_owners, [])
@school_owner_ids = owners.map(&:id)
if user_type == 'student'
teachers = { school_teachers: [] }
students = SchoolStudent::List.call(school: @school, token: current_user.token, student_ids: user_ids)
teachers = []
students = SchoolStudent::List.call(school: @school, token: current_user.token, student_ids: [user_id])[:school_students]
else
teachers = SchoolTeacher::List.call(school: @school, teacher_ids: user_ids)
students = { school_students: [] }
teachers = SchoolTeacher::List.call(school: @school, teacher_ids: [user_id])[:school_teachers]
students = []
end
result = ClassMember::Create.call(school_class: @school_class, students: students[:school_students], teachers: teachers[:school_teachers])

if result.success?
result = ClassMember::Create.call(school_class: @school_class, students: students, teachers: teachers)

if students.blank? && teachers.blank?
render json: { error: 'No valid school members provided' }, status: :unprocessable_content
elsif result.success?
@class_member = result[:class_members].first
track_class_students_added([@class_member])
render :show, formats: [:json], status: :created
else
render json: result.slice(:error, :errors), status: :unprocessable_content
end
rescue ArgumentError => e
render json: { error: e.message }, status: :unprocessable_content
end

def create_batch
Expand Down
30 changes: 4 additions & 26 deletions app/controllers/api/school_classes_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,21 +37,14 @@ def create
def import
school_class_params = import_school_class_params
school_students_params = import_school_students_params

# Find or create the school class
school_class_result = find_or_create_school_class(school_class_params)

if school_class_result.success?
school_class = school_class_result[:school_class]
@school_class_with_teachers = school_class.with_teachers

# Create students if class exists
school_students_result = create_school_students(school_students_params, school_class)
@school_students = school_students_result[:school_students]
@school_students_errors = school_students_result[:errors]

# Assign students to class
@class_members = assign_students_to_class(school_class, @school_students)
school_students = create_school_students(school_students_params, school_class)
assign_students_to_class(school_class, school_students)

render :import, formats: [:json], status: :created
else
Expand Down Expand Up @@ -146,34 +139,20 @@ def accessible_school_classes
end

def create_school_students(school_students_params, school_class)
return { school_students: [], errors: nil } unless school_class.present? && school_students_params.present?

school_students_result = SchoolStudent::CreateBatchSSO.call(
school: @school,
school_students_params: school_students_params,
current_user:
)

{
school_students: school_students_result[:school_students],
errors: school_students_result[:errors]
}.tap { track_created_school_students(school_students_result[:school_students], class_id: school_class.id) }
school_students_result[:school_students].tap { track_created_school_students(school_students_result[:school_students], class_id: school_class.id) }
end

def assign_students_to_class(school_class, school_students)
return [] unless school_class.present? && school_students.present? && school_students.any?

# Extract the student objects for class member creation
students = school_students.pluck(:student)
class_members_result = ClassMember::Create.call(school_class:, students:, teachers: [])

# Put the errors in a more useful format for the response
class_members_errors = class_members_result[:errors].map do |user_id, error|
{ success: false, student_id: user_id, school_class_id: school_class.id, error: error }
end

track_imported_class_students_added(school_class, class_members_result[:class_members])
class_members_result[:class_members] + class_members_errors
end

def load_and_authorize_school
Expand Down Expand Up @@ -209,8 +188,7 @@ def import_school_class_params
end

def import_school_students_params
school_students_data = params[:school_students]
return [] if school_students_data.blank?
school_students_data = Array(params[:school_students])

school_students_data.filter_map do |student|
next if student.blank?
Expand Down
29 changes: 0 additions & 29 deletions app/views/api/school_classes/import.json.jbuilder
Original file line number Diff line number Diff line change
Expand Up @@ -20,32 +20,3 @@ json.school_class do
json.partial! '/api/school_teachers/school_teacher', teacher:, include_email: false
end
end

# Students section
if @school_students_errors.present?
# This is the validation error case
json.students do
json.errors @school_students_errors
end
else
json.students(@school_students) do |student_item|
json.partial! '/api/school_students/school_student', student: student_item[:student]

# Add the metadata
json.success student_item[:success]
json.error student_item[:error]
json.created student_item[:created]
end
end

# Class members section
json.class_members(@class_members) do |class_member|
if class_member.is_a?(Hash) && class_member.key?(:success) && !class_member[:success]
# Add errors to the response
json.merge! class_member
else
json.partial! '/api/class_members/class_member', class_member: class_member
json.success true
json.error nil
end
end
9 changes: 2 additions & 7 deletions lib/concepts/class_member/operations/create.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,9 @@ def call(school_class:, students: [], teachers: [])
response = OperationResponse.new
response[:class_members] = []
response[:errors] = {}
raise ArgumentError, 'No valid school members provided' if students.blank? && teachers.blank?

create_class_teachers(school_class:, teachers:, response:)
create_class_students(school_class:, students:, response:)
response
rescue StandardError => e
Sentry.capture_exception(e)
response[:error] = "Error creating class members: #{e.message}"
create_class_teachers(school_class:, teachers: Array(teachers), response:)
create_class_students(school_class:, students: Array(students), response:)
Comment thread
macroscopeapp[bot] marked this conversation as resolved.
Resolved
response
end

Expand Down
1 change: 1 addition & 0 deletions lib/profile_api_client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,7 @@ def create_school_students(token:, students:, school_id:, preflight: false)
end

def create_school_students_sso(token:, students:, school_id:)
return [] if students.blank?
return nil if token.blank?

students = Array(students)
Expand Down
15 changes: 0 additions & 15 deletions spec/concepts/class_member/create_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -75,21 +75,6 @@
it 'does not create a class teacher' do
expect { described_class.call(school_class:, students:, teachers:) }.not_to change(ClassTeacher, :count)
end

it 'returns a failed operation response' do
response = described_class.call(school_class:, students:, teachers:)
expect(response.failure?).to be(true)
end

it 'returns the error message in the operation response' do
response = described_class.call(school_class:, students:, teachers:)
expect(response[:error]).to include('No valid school members provided')
end

it 'sent the exception to Sentry' do
described_class.call(school_class:, students:, teachers:)
expect(Sentry).to have_received(:capture_exception).with(kind_of(StandardError))
end
end

context 'with a student from a different school' do
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -200,15 +200,9 @@
stub_user_info_api_for_unknown_users(user_id: unknown_user_id)
end

it 'responds 422 Unprocessable Entity' do
it 'skips over unknown users' do
post("/api/schools/#{school.id}/classes/#{school_class.id}/members/batch", headers:, params: invalid_params)
expect(response).to have_http_status(:unprocessable_content)
end

it 'returns the error message in the operation response' do
post("/api/schools/#{school.id}/classes/#{school_class.id}/members/batch", headers:, params: invalid_params)
data = JSON.parse(response.body, symbolize_names: true)
expect(data[:error]).to include('No valid school members provided')
expect(response).to have_http_status(:ok)
end
end

Expand Down
21 changes: 14 additions & 7 deletions spec/features/class_member/creating_a_class_member_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,7 @@
end

context 'with invalid params' do
let(:invalid_params) { { class_member: { user_id: SecureRandom.uuid } } }
let(:invalid_params) { { class_member: { user_id: nil, type: nil } } }

before do
stub_user_info_api_for_unknown_users(user_id: invalid_params[:class_member][:user_id])
Expand All @@ -229,16 +229,23 @@
expect(response).to have_http_status(:bad_request)
end

it 'responds 422 Unprocessable Entity when params are invalid' do
it 'responds 400 Bad Request when params are invalid' do
post("/api/schools/#{school.id}/classes/#{school_class.id}/members", headers:, params: invalid_params)
expect(response).to have_http_status(:unprocessable_content)
expect(response).to have_http_status(:bad_request)
end
end

it 'returns the error message in the operation response' do
post("/api/schools/#{school.id}/classes/#{school_class.id}/members", headers:, params: invalid_params)
data = JSON.parse(response.body, symbolize_names: true)
context 'when the user has no role in the school' do
let(:user_without_role) { create(:user) }
let(:params_for_user_without_role) { { class_member: { user_id: user_without_role.id, type: 'student' } } }

expect(data[:error]).to include('No valid school members provided')
before do
stub_profile_api_list_school_students(school:, student_attributes: [])
end

it 'responds 422 Unprocessable Content' do
post("/api/schools/#{school.id}/classes/#{school_class.id}/members", headers:, params: params_for_user_without_role)
expect(response).to have_http_status(:unprocessable_content)
end
end

Expand Down
Loading
Loading