Skip to content

Commit 1876049

Browse files
authored
Fix-missing-id-handling (#603)
## Status Ready for review ## What's changed? - Ensures import_id is set on import, and prevent the edge case of a nil import_id being passed and a random record being import - Improve readability of list_student method - Add the SSO student to the seeds ## Steps to perform after deploying to production N/A
1 parent 2a90ded commit 1876049

File tree

7 files changed

+10
-16
lines changed

7 files changed

+10
-16
lines changed

app/models/school_class.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,9 @@ class SchoolClass < ApplicationRecord
1919
enum :import_origin, { google_classroom: 0 }, validate: { allow_nil: true }
2020

2121
validates :import_origin, presence: true, on: :import
22+
validates :import_id, presence: true, on: :import
2223

2324
validates :import_id, uniqueness: { scope: %i[school_id import_origin] }, if: -> { import_id.present? }
24-
validates :import_id, presence: true, if: -> { import_origin.present? }
2525

2626
has_paper_trail(
2727
meta: {

lib/concepts/school_member/list.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ def call(school:, token:)
3737

3838
def fetch_students(school:, token:)
3939
student_roles = Role.student.where(school:)
40+
4041
students_response = student_roles.any? ? SchoolStudent::List.call(school:, token:).fetch(:school_students, []) : []
4142

4243
students_response.map do |student|

lib/concepts/school_student/list.rb

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,9 @@ def call(school:, token:, student_ids: nil)
1717

1818
def list_students(school, token, student_ids)
1919
student_ids ||= Role.student.where(school:).map(&:user_id)
20-
ProfileApiClient.list_school_students(token:, school_id: school.id, student_ids:).map do |student|
21-
User.new(student.to_h.slice(:id, :username, :name, :email).merge(
22-
sso_providers: student.ssoProviders
23-
))
20+
students = ProfileApiClient.list_school_students(token:, school_id: school.id, student_ids:)
21+
students.map do |student|
22+
User.new(student.to_h.slice(:id, :username, :name, :email).merge(sso_providers: student.ssoProviders))
2423
end
2524
end
2625
end

lib/tasks/for_education.rake

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ namespace :for_education do
2121
teacher_id = ENV.fetch('SEEDING_TEACHER_ID', TEST_USERS[:john_doe])
2222

2323
# Hard coded as the student's school needs to match
24-
student_ids = [TEST_USERS[:jane_smith], TEST_USERS[:john_smith]]
24+
student_ids = [TEST_USERS[:jane_smith], TEST_USERS[:john_smith], TEST_USERS[:emily_ssouser]]
2525
school_id = TEST_SCHOOL
2626

2727
# Remove the roles first

lib/tasks/seeds_helper.rb

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@ module SeedsHelper
55
jane_doe: '583ba872-b16e-46e1-9f7d-df89d267550d', # jane.doe@example.com
66
john_doe: 'bbb9b8fd-f357-4238-983d-6f87b99bdbb2', # john.doe@example.com
77
jane_smith: 'e52de409-9210-4e94-b08c-dd11439e07d9', # student
8-
john_smith: '0d488bec-b10d-46d3-b6f3-4cddf5d90c71' # student
8+
john_smith: '0d488bec-b10d-46d3-b6f3-4cddf5d90c71', # student
9+
emily_ssouser: '88e0aed6-8f20-4e40-98f9-610a0ab1cfcc' # sso student
910
}.freeze
1011

1112
# Match the school in profile...
@@ -64,7 +65,7 @@ def assign_a_teacher(user_id, school)
6465
end
6566

6667
def assign_students(school_class, school)
67-
[TEST_USERS[:jane_smith], TEST_USERS[:john_smith]].map do |student_id|
68+
[TEST_USERS[:jane_smith], TEST_USERS[:john_smith], TEST_USERS[:emily_ssouser]].map do |student_id|
6869
Rails.logger.info 'Assigning student role...'
6970
Role.student.find_or_create_by!(user_id: student_id, school:)
7071

lib/tasks/test_seeds.rake

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ namespace :test_seeds do
1414
teacher_id = ENV.fetch('SEEDING_TEACHER_ID', TEST_USERS[:john_doe])
1515

1616
# Hard coded as the student's school needs to match
17-
student_ids = [TEST_USERS[:jane_smith], TEST_USERS[:john_smith]]
17+
student_ids = [TEST_USERS[:jane_smith], TEST_USERS[:john_smith], TEST_USERS[:emily_ssouser]]
1818
school_id = TEST_SCHOOL
1919

2020
# Remove the roles first

spec/models/school_class_spec.rb

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -112,13 +112,6 @@
112112
expect(school_class.errors[:import_origin]).to include('is not included in the list')
113113
end
114114

115-
it 'requires import_id when import_origin is set' do
116-
school_class.import_origin = :google_classroom
117-
school_class.import_id = nil
118-
expect(school_class).not_to be_valid
119-
expect(school_class.errors[:import_id]).to include("can't be blank")
120-
end
121-
122115
it 'allows import_id to be nil when import_origin is nil' do
123116
school_class.import_origin = nil
124117
school_class.import_id = nil

0 commit comments

Comments
 (0)