File tree Expand file tree Collapse file tree 3 files changed +28
-5
lines changed
Expand file tree Collapse file tree 3 files changed +28
-5
lines changed Original file line number Diff line number Diff line change @@ -50,13 +50,14 @@ def import_school(school_data)
5050 return
5151 end
5252
53- # Check if this owner already has a school as creator
54- existing_school = School . find_by ( creator_id : owner [ :id ] )
55- if existing_school
53+ # Check if this owner already has any role in any school
54+ existing_role = Role . find_by ( user_id : owner [ :id ] )
55+ if existing_role
56+ existing_school = existing_role . school
5657 @results [ :failed ] << {
5758 name : school_data [ :name ] ,
58- error_code : SchoolImportError ::CODES [ :owner_already_creator ] ,
59- error : "Owner #{ school_data [ :owner_email ] } is already the creator of school '#{ existing_school . name } '" ,
59+ error_code : SchoolImportError ::CODES [ :owner_has_existing_role ] ,
60+ error : "Owner #{ school_data [ :owner_email ] } already has a role in school '#{ existing_school . name } '" ,
6061 owner_email : school_data [ :owner_email ] ,
6162 existing_school_id : existing_school . id
6263 }
Original file line number Diff line number Diff line change @@ -7,6 +7,7 @@ module SchoolImportError
77 csv_validation_failed : 'CSV_VALIDATION_FAILED' ,
88 owner_not_found : 'OWNER_NOT_FOUND' ,
99 owner_already_creator : 'OWNER_ALREADY_CREATOR' ,
10+ owner_has_existing_role : 'OWNER_HAS_EXISTING_ROLE' ,
1011 duplicate_owner_email : 'DUPLICATE_OWNER_EMAIL' ,
1112 school_validation_failed : 'SCHOOL_VALIDATION_FAILED' ,
1213 job_not_found : 'JOB_NOT_FOUND' ,
Original file line number Diff line number Diff line change 8181 end
8282 end
8383
84+ context 'when owner already has a role in another school' do
85+ let ( :existing_school ) { create ( :school , name : 'Existing School' ) }
86+
87+ before do
88+ Role . owner . create! ( school_id : existing_school . id , user_id : owner1_id )
89+ end
90+
91+ it 'adds failed result for that school' do
92+ results = described_class . new . perform (
93+ schools_data : [ schools_data . first ] ,
94+ user_id : user_id
95+ )
96+
97+ expect ( results [ :successful ] . count ) . to eq ( 0 )
98+ expect ( results [ :failed ] . count ) . to eq ( 1 )
99+ expect ( results [ :failed ] . first [ :error_code ] ) . to eq ( 'OWNER_HAS_EXISTING_ROLE' )
100+ expect ( results [ :failed ] . first [ :error ] ) . to include ( 'already has a role in school' )
101+ expect ( results [ :failed ] . first [ :existing_school_id ] ) . to eq ( existing_school . id )
102+ end
103+ end
104+
84105 context 'when schools_data has string keys (simulating ActiveJob serialization)' do
85106 let ( :schools_data_with_string_keys ) do
86107 [
You can’t perform that action at this time.
0 commit comments