Skip to content

Commit 03492f7

Browse files
committed
Modify check on owner to fail if owner has any role in any other school.
1 parent 51bdcd0 commit 03492f7

File tree

3 files changed

+28
-5
lines changed

3 files changed

+28
-5
lines changed

app/jobs/school_import_job.rb

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff 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
}

app/models/school_import_error.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff 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',

spec/jobs/school_import_job_spec.rb

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,27 @@
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
[

0 commit comments

Comments
 (0)