Skip to content

Commit

Permalink
Move ATS api services in relative namespace
Browse files Browse the repository at this point in the history
  • Loading branch information
ddippolito committed Jan 17, 2025
1 parent 8bfa5ba commit 8fb7ad6
Show file tree
Hide file tree
Showing 9 changed files with 120 additions and 108 deletions.
6 changes: 3 additions & 3 deletions app/controllers/publishers/ats_api/v1/vacancies_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ class Publishers::AtsApi::V1::VacanciesController < Api::ApplicationController
rescue_from StandardError, with: :render_server_error
rescue_from ActiveRecord::RecordNotFound, with: :render_not_found
rescue_from ActionController::ParameterMissing, with: :render_bad_request
rescue_from CreateVacancyService::InvalidOrganisationError, with: :render_bad_request
rescue_from Publishers::AtsApi::CreateVacancyService::InvalidOrganisationError, with: :render_bad_request

def index
@pagy, @vacancies = pagy(vacancies.where(publisher_ats_api_client: client), items: 100)
Expand All @@ -20,14 +20,14 @@ def show
end

def create
result = CreateVacancyService.call(permitted_vacancy_params)
result = Publishers::AtsApi::CreateVacancyService.call(permitted_vacancy_params)

render result.slice(:json, :status)
end

def update
vacancy = Vacancy.find(params[:id])
result = UpdateVacancyService.call(vacancy, permitted_vacancy_params)
result = Publishers::AtsApi::UpdateVacancyService.call(vacancy, permitted_vacancy_params)

if result[:success]
@vacancy = vacancy
Expand Down
63 changes: 0 additions & 63 deletions app/services/create_vacancy_service.rb

This file was deleted.

11 changes: 0 additions & 11 deletions app/services/organisation_fetcher.rb

This file was deleted.

67 changes: 67 additions & 0 deletions app/services/publishers/ats_api/create_vacancy_service.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
module Publishers
module AtsApi
class CreateVacancyService
extend OrganisationFetcher

InvalidOrganisationError = Class.new(StandardError)

class << self
def call(params)
vacancy = Vacancy.new(sanitised_params(params))

if (conflict = conflict_vacancy(vacancy))
return conflict_response(conflict)
end

if vacancy.save
success_response(vacancy)
else
validation_error_response(vacancy)
end
end

private

def sanitised_params(params)
organisations = fetch_organisations(params[:schools])
raise InvalidOrganisationError, "No valid organisations found" if organisations.blank?

params[:publish_on] ||= Time.zone.today.to_s
params.except(:schools).merge(organisations: organisations)
end

def conflict_vacancy(vacancy)
Vacancy.find_by(
publisher_ats_api_client_id: vacancy.publisher_ats_api_client_id,
external_reference: vacancy.external_reference,
)
end

def conflict_response(conflict_vacancy)
{
status: :conflict,
json: {
error: "A vacancy with the provided external reference already exists",
link: Rails.application.routes.url_helpers.vacancy_url(conflict_vacancy),
},
}
end

def success_response(vacancy)
{ status: :created, json: { id: vacancy.id } }
end

def validation_error_response(vacancy)
{
status: :unprocessable_entity,
json: {
errors: vacancy.errors.messages.flat_map do |attr, messages|
messages.map { |message| "#{attr}: #{message}" }
end,
},
}
end
end
end
end
end
15 changes: 15 additions & 0 deletions app/services/publishers/ats_api/organisation_fetcher.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
module Publishers
module AtsApi
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
32 changes: 32 additions & 0 deletions app/services/publishers/ats_api/update_vacancy_service.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
module Publishers
module AtsApi
class UpdateVacancyService
extend OrganisationFetcher

class << self
def call(vacancy, params)
if vacancy.update(sanitised_params(params))
{ success: true }
else
{ success: false, errors: format_errors(vacancy.errors.messages) }
end
end

private

attr_reader :vacancy, :params

def sanitised_params(params)
organisations = fetch_organisations(params[:schools])
raise ActiveRecord::RecordNotFound, "No valid organisations found" if organisations.blank?

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

def format_errors(errors)
errors.flat_map { |attr, messages| messages.map { |msg| "#{attr}: #{msg}" } }
end
end
end
end
end
28 changes: 0 additions & 28 deletions app/services/update_vacancy_service.rb

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
require "rails_helper"

RSpec.describe CreateVacancyService do
RSpec.describe Publishers::AtsApi::CreateVacancyService do
subject(:create_vacancy_service) { described_class.call(params) }

let(:school) { create(:school) }
Expand Down Expand Up @@ -72,7 +72,7 @@

it "raises ActiveRecord::RecordNotFound" do
expect { create_vacancy_service }.to raise_error(
CreateVacancyService::InvalidOrganisationError,
Publishers::AtsApi::CreateVacancyService::InvalidOrganisationError,
"No valid organisations found",
)
end
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
require "rails_helper"

RSpec.describe UpdateVacancyService do
RSpec.describe Publishers::AtsApi::UpdateVacancyService do
subject(:udpate_vacancy_service) { described_class.call(vacancy, params) }

let(:vacancy) { create(:vacancy, :external, organisations: [school]) }
Expand Down

0 comments on commit 8fb7ad6

Please sign in to comment.