Skip to content
Draft
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
26 changes: 24 additions & 2 deletions app/models/project.rb
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,30 @@ module Types
)

def self.users(current_user)
school = School.find_by(id: pluck(:school_id))
SchoolStudent::List.call(school:, token: current_user.token, student_ids: pluck(:user_id).uniq)[:school_students] || []
return [] unless current_user&.token

projects_scope = all
school_user_pairs = projects_scope.pluck(:school_id, :user_id)
user_ids_by_school = school_user_pairs.each_with_object(Hash.new { |h, k| h[k] = [] }) do |(school_id, user_id), memo|
next if user_id.blank?

memo[school_id] << user_id
end

school_lookup = School.where(id: user_ids_by_school.keys.compact).index_by(&:id)

user_ids_by_school.each_with_object([]) do |(school_id, user_ids), users|
school = school_lookup[school_id]
next unless school

response = SchoolStudent::List.call(
school:,
token: current_user.token,
student_ids: user_ids.uniq
)

users.concat(response.fetch(:school_students, []))
end
end

def self.with_users(current_user)
Expand Down
2 changes: 1 addition & 1 deletion app/models/school_class.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ class SchoolClass < ApplicationRecord
has_many :lessons, dependent: :nullify
accepts_nested_attributes_for :teachers

scope :with_teachers, ->(user_id) { joins(:teachers).where(teachers: { id: user_id }) }
scope :with_teachers, ->(user_id) { joins(:teachers).where(class_teachers: { teacher_id: user_id }) }

before_validation :assign_class_code, on: %i[create import]

Expand Down
16 changes: 9 additions & 7 deletions app/services/student_removal_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,23 +14,25 @@ def remove_students
@students.each do |user_id|
result = { user_id: }
begin
# Skip if student has projects
projects = Project.where(user_id: user_id)
result[:skipped] = true if projects.length.positive?
projects_scope = Project.where(user_id:, school_id: @school.id)
result[:skipped] = true if projects_scope.exists?

unless result[:skipped]
ActiveRecord::Base.transaction do
# Remove from classes
class_assignments = ClassStudent.where(student_id: user_id)
class_assignments.destroy_all

# Remove roles
roles = Role.student.where(user_id: user_id)
roles.destroy_all
end

# Remove from profile if requested
ProfileApiClient.delete_school_student(token: @token, school_id: @school.id, student_id: user_id) if @remove_from_profile && @token.present?
if @remove_from_profile && @token.present?
ProfileApiClient.delete_school_student(
token: @token,
school_id: @school.id,
student_id: user_id
)
end
end
rescue StandardError => e
result[:error] = "#{e.class}: #{e.message}"
Expand Down