Skip to content

Commit

Permalink
Extract organisation fetcher
Browse files Browse the repository at this point in the history
  • Loading branch information
ddippolito committed Jan 16, 2025
1 parent 6f06685 commit 8130dca
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 30 deletions.
17 changes: 3 additions & 14 deletions app/controllers/publishers/ats_api/v1/create_vacancy_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ module Publishers
module AtsApi
module V1
class CreateVacancyService
extend OrganisationFetcher

InvalidOrganisationError = Class.new(StandardError)

class << self
Expand All @@ -26,20 +28,7 @@ def sanitised_params(params)
raise InvalidOrganisationError, "No valid organisations found" if organisations.blank?

params[:publish_on] ||= Time.zone.today.to_s
params[:working_patterns] ||= []
params[:phases] ||= []

params.except(:schools, :trust_uid).merge(organisations: organisations)
end

def fetch_organisations(school_params)
return [] unless school_params

if school_params[:trust_uid].present?
SchoolGroup.trusts.find_by(uid: school_params[:trust_uid])&.schools&.where(urn: school_params[:school_urns]) || []
else
School.where(urn: school_params[:school_urns])
end
params.except(:schools).merge(organisations: organisations)
end

def conflict_vacancy(vacancy)
Expand Down
17 changes: 17 additions & 0 deletions app/controllers/publishers/ats_api/v1/organisation_fetcher.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
module Publishers
module AtsApi
module V1
module OrganisationFetcher
def fetch_organisations(school_params)
return [] unless school_params

if school_params[:trust_uid].present?
SchoolGroup.trusts.find_by(uid: school_params[:trust_uid]).schools&.where(urn: school_params[:school_urns]) || []
else
School.where(urn: school_params[:school_urns])
end
end
end
end
end
end
14 changes: 3 additions & 11 deletions app/controllers/publishers/ats_api/v1/update_vacancy_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ module Publishers
module AtsApi
module V1
class UpdateVacancyService
extend OrganisationFetcher

class << self
def call(vacancy, params)
if vacancy.update(sanitised_params(params))
Expand All @@ -19,17 +21,7 @@ def sanitised_params(params)
organisations = fetch_organisations(params[:schools])
raise ActiveRecord::RecordNotFound, "No valid organisations found" if organisations.blank?

params.except(:schools, :trust_uid).merge(organisations: organisations)
end

def fetch_organisations(school_params)
return [] unless school_params

if school_params[:trust_uid].present?
SchoolGroup.trusts.find_by(uid: school_params[:trust_uid]).schools.where(urn: school_params[:school_urns])
else
School.where(urn: school_params[:school_urns])
end
params.except(:schools).merge(organisations: organisations)
end

def format_errors(errors)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,11 +63,10 @@
let(:school_urns) { { school_urns: [9999] } }

it "raises ActiveRecord::RecordNotFound" do
expect { create_vacancy_service }
.to raise_error(
Publishers::AtsApi::V1::CreateVacancyService::InvalidOrganisationError,
"No valid organisations found",
)
expect { create_vacancy_service }.to raise_error(
Publishers::AtsApi::V1::CreateVacancyService::InvalidOrganisationError,
"No valid organisations found",
)
end
end

Expand Down

0 comments on commit 8130dca

Please sign in to comment.