From 8130dca9e24b0063ead04eb2820fd0ca311ec500 Mon Sep 17 00:00:00 2001 From: Davide Dippolito Date: Thu, 16 Jan 2025 11:54:06 +0000 Subject: [PATCH] Extract organisation fetcher --- .../ats_api/v1/create_vacancy_service.rb | 17 +++-------------- .../ats_api/v1/organisation_fetcher.rb | 17 +++++++++++++++++ .../ats_api/v1/update_vacancy_service.rb | 14 +++----------- .../ats_api/v1/create_vacancy_service_spec.rb | 9 ++++----- 4 files changed, 27 insertions(+), 30 deletions(-) create mode 100644 app/controllers/publishers/ats_api/v1/organisation_fetcher.rb diff --git a/app/controllers/publishers/ats_api/v1/create_vacancy_service.rb b/app/controllers/publishers/ats_api/v1/create_vacancy_service.rb index cdabb7ad54..c103561176 100644 --- a/app/controllers/publishers/ats_api/v1/create_vacancy_service.rb +++ b/app/controllers/publishers/ats_api/v1/create_vacancy_service.rb @@ -2,6 +2,8 @@ module Publishers module AtsApi module V1 class CreateVacancyService + extend OrganisationFetcher + InvalidOrganisationError = Class.new(StandardError) class << self @@ -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) diff --git a/app/controllers/publishers/ats_api/v1/organisation_fetcher.rb b/app/controllers/publishers/ats_api/v1/organisation_fetcher.rb new file mode 100644 index 0000000000..21394d6775 --- /dev/null +++ b/app/controllers/publishers/ats_api/v1/organisation_fetcher.rb @@ -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 diff --git a/app/controllers/publishers/ats_api/v1/update_vacancy_service.rb b/app/controllers/publishers/ats_api/v1/update_vacancy_service.rb index 3c4ee9860f..f4ab28a658 100644 --- a/app/controllers/publishers/ats_api/v1/update_vacancy_service.rb +++ b/app/controllers/publishers/ats_api/v1/update_vacancy_service.rb @@ -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)) @@ -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) diff --git a/spec/requests/publishers/ats_api/v1/create_vacancy_service_spec.rb b/spec/requests/publishers/ats_api/v1/create_vacancy_service_spec.rb index c9108fe5e3..a53f5e66d6 100644 --- a/spec/requests/publishers/ats_api/v1/create_vacancy_service_spec.rb +++ b/spec/requests/publishers/ats_api/v1/create_vacancy_service_spec.rb @@ -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