-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move ATS api services in relative namespace
- Loading branch information
1 parent
8bfa5ba
commit 8fb7ad6
Showing
9 changed files
with
120 additions
and
108 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
spec/services/update_vacancy_service_spec.rb → ...rs/ats_api/update_vacancy_service_spec.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters