Created
April 6, 2026 12:59
-
-
Save ckozus/27133ec4ac4b5306648a97bc252173aa to your computer and use it in GitHub Desktop.
DUAL-18976: Console scripts for creating unclaimed students and registrations (QA)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # ============================================================ | |
| # Create registrations for unclaimed students. | |
| # Picks a random course section from each student's college, | |
| # restricted to active terms only. | |
| # | |
| # Usage: copy/paste into `heroku run rails console -a <review-app>` | |
| # ============================================================ | |
| MARKER_MIDDLE_NAME = 'Unclaimed' | |
| # Pick the 'uber' user, or fall back to any site admin | |
| current_user = User.find_by(login: 'uber') || | |
| User.where("role_mask ILIKE ?", '%site_admin%').first | |
| raise "No uber/admin user found - registrations need a current_user" unless current_user | |
| puts "Using current_user: #{current_user.login} (##{current_user.id})" | |
| unclaimed_students = Student.where(middle_name: MARKER_MIDDLE_NAME, claim_state: 'unclaimed') | |
| puts "Found #{unclaimed_students.count} unclaimed students" | |
| # ---- Cleanup: remove any existing StudentDeCourse records for these students | |
| cleanup_count = StudentDeCourse.where(student_id: unclaimed_students.pluck(:id)).count | |
| if cleanup_count > 0 | |
| puts "Cleaning up #{cleanup_count} existing StudentDeCourse records..." | |
| StudentDeCourse.where(student_id: unclaimed_students.pluck(:id)).destroy_all | |
| puts " Cleanup done." | |
| end | |
| results = { success: 0, no_sections: 0, failed: 0 } | |
| unclaimed_students.find_each do |student| | |
| csas = student.college_student_applications.where(owner_type: 'College') | |
| csas.each do |csa| | |
| college = csa.owner | |
| active_term_ids = college.terms.active.pluck(:id) | |
| if active_term_ids.empty? | |
| puts " SKIP student ##{student.id} @ #{college.inst_name}: no active terms" | |
| results[:no_sections] += 1 | |
| next | |
| end | |
| course_section = college.course_sections | |
| .where(term_id: active_term_ids) | |
| .order('RANDOM()') | |
| .first | |
| unless course_section | |
| puts " SKIP student ##{student.id} @ #{college.inst_name}: no course sections in active terms" | |
| results[:no_sections] += 1 | |
| next | |
| end | |
| result = Registrations::CreateStudentDeCourseRegistrationService.call( | |
| student: student, | |
| course_section: course_section, | |
| completed_registration: false, | |
| current_user: current_user | |
| ) | |
| if result.success? | |
| puts " OK: student ##{student.id} -> #{college.inst_name} / section ##{course_section.id} / term #{course_section.term.name}" | |
| results[:success] += 1 | |
| else | |
| puts " FAILED: student ##{student.id} -> #{college.inst_name} - #{result.message}" | |
| results[:failed] += 1 | |
| end | |
| end | |
| end | |
| puts "\n=== Results: #{results[:success]} registered, #{results[:no_sections]} skipped (no sections), #{results[:failed]} failed ===" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # ============================================================ | |
| # Create unclaimed students + CSAs for each active college. | |
| # Picks a random high school from each college's college_hs_linkages | |
| # (only schools linked to the college, where registration is enabled). | |
| # | |
| # Usage: copy/paste into `heroku run rails console -a <review-app>` | |
| # ============================================================ | |
| STUDENTS_PER_COLLEGE = 2 | |
| MARKER_MIDDLE_NAME = 'Unclaimed' | |
| colleges = College.with_branded_host | |
| puts "Creating unclaimed students across #{colleges.count} active colleges..." | |
| created_students = [] | |
| colleges.find_each do |college| | |
| # Only high schools linked to this college AND registration-enabled | |
| linked_hs_ids = college.college_hs_linkages | |
| .where(no_student_course_registration: [nil, false]) | |
| .joins(:high_school) | |
| .where(high_schools: { archive: false, placeholder: [nil, false] }) | |
| .pluck(:high_school_id) | |
| if linked_hs_ids.empty? | |
| puts " SKIP: #{college.inst_name} - no registration-enabled high school linkages" | |
| next | |
| end | |
| STUDENTS_PER_COLLEGE.times do | |
| high_school_id = linked_hs_ids.sample | |
| student = Student.new( | |
| first_name: Faker::Name.first_name, | |
| middle_name: MARKER_MIDDLE_NAME, | |
| last_name: Faker::Name.last_name, | |
| birth_date: Faker::Date.birthday(min_age: 15, max_age: 18), | |
| high_school_id: high_school_id, | |
| claim_state: 'unclaimed', | |
| user: nil | |
| ) | |
| unless student.save | |
| puts " FAILED: #{college.inst_name} - #{student.errors.full_messages.join(', ')}" | |
| next | |
| end | |
| student_number = ('A'..'Z').to_a.sample + rand(10_000_000..99_999_999).to_s[0..6] | |
| csa = CollegeStudentApplication.new( | |
| student: student, | |
| owner: college, | |
| student_number: student_number | |
| ) | |
| if csa.save | |
| created_students << student | |
| puts " OK: #{college.inst_name} - student ##{student.id} / CSA ##{csa.id} / hs ##{high_school_id} / student_number #{student_number}" | |
| else | |
| puts " CSA FAILED: #{college.inst_name} student ##{student.id} - #{csa.errors.full_messages.join(', ')}" | |
| end | |
| end | |
| end | |
| puts "\n=== Created #{created_students.count} unclaimed students ===" | |
| puts "Find them via: Student.where(middle_name: '#{MARKER_MIDDLE_NAME}')" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment