Skip to content

Commit 0a63a2d

Browse files
authored
322: Nicer handling for no jobs (#466)
## Status - Related to RaspberryPiFoundation/digital-editor-issues#322 ## What's changed? - Improve handling where no jobs are found ## Steps to perform after deploying to production N/A
1 parent 9aa96ed commit 0a63a2d

File tree

4 files changed

+14
-5
lines changed

4 files changed

+14
-5
lines changed

app/controllers/api/school_students_controller.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,8 @@ def create_batch
3737
)
3838

3939
if result.success?
40-
head :accepted
40+
@job_id = result[:job_id]
41+
render :create_batch, formats: [:json], status: :accepted
4142
else
4243
render json: { error: result[:error] }, status: :unprocessable_entity
4344
end

app/controllers/api/user_jobs_controller.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ class UserJobsController < ApiController
66

77
def index
88
user_jobs = UserJob.where(user_id: current_user.id).includes(:good_job)
9-
jobs = user_jobs.map { |user_job| job_attributes(user_job.good_job) }
9+
jobs = user_jobs&.map { |user_job| job_attributes(user_job&.good_job) }
1010
if jobs.any?
1111
render json: { jobs: }, status: :ok
1212
else
@@ -16,8 +16,8 @@ def index
1616

1717
def show
1818
user_job = UserJob.find_by(good_job_id: params[:id], user_id: current_user.id)
19-
job = job_attributes(user_job.good_job)
20-
if job
19+
job = job_attributes(user_job&.good_job) unless user_job.nil?
20+
if job.present?
2121
render json: { job: }, status: :ok
2222
else
2323
render json: { error: 'Job not found' }, status: :not_found
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# frozen_string_literal: true
2+
3+
json.job_id @job_id

lib/concepts/school_student/create_batch.rb

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,11 @@ def call(school:, school_students_params:, token:, user_id:)
3030
private
3131

3232
def create_batch(school, students, token, user_id)
33+
# Ensure that nil values are empty strings, else Profile will swallow validations
34+
students = students.map do |student|
35+
student.transform_values { |value| value.nil? ? '' : value }
36+
end
37+
3338
validate(school:, students:, token:)
3439

3540
job = CreateStudentsJob.attempt_perform_later(school_id: school.id, students:, token:, user_id:)
@@ -45,7 +50,7 @@ def validate(school:, students:, token:)
4550

4651
def decrypt_students(students)
4752
students.deep_dup.each do |student|
48-
student[:password] = DecryptionHelpers.decrypt_password(student[:password])
53+
student[:password] = DecryptionHelpers.decrypt_password(student[:password]) if student[:password].present?
4954
end
5055
end
5156

0 commit comments

Comments
 (0)