Skip to content

Commit

Permalink
Changes to API service to get individual providers based on code
Browse files Browse the repository at this point in the history
  • Loading branch information
Jamie committed Dec 22, 2023
1 parent 9ab506f commit abdfae3
Show file tree
Hide file tree
Showing 36 changed files with 240 additions and 152 deletions.
1 change: 1 addition & 0 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ SIGN_IN_METHOD=persona
PLACEMENTS_HOST=placements.localhost
CLAIMS_HOST=claims.localhost
GIAS_CSV_BASE_URL=https://ea-edubase-api-prod.azurewebsites.net/edubase/downloads/public
PUBLISH_BASE_URL=https://www.publish-teacher-training-courses.service.gov.uk
28 changes: 11 additions & 17 deletions adr/00003-testing-practices.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,13 @@ Use the following test types for our classes, modules, and functionalities.
```ruby
RSpec.describe User, type: :model do
subject { build(:user) }

describe ".class_method" do
it "does something" do
expect(User.class_method).to do_something
end
end

describe "#instance_method" do
it "does something" do
expect(subject.instance_method).to do_something
Expand All @@ -41,7 +41,7 @@ Use the following test types for our classes, modules, and functionalities.
expect(User.class_method).to do_something
end
end

describe "#instance_method" do
it "does something" do
expect(subject.instance_method).to do_something
Expand All @@ -59,11 +59,11 @@ Use the following test types for our classes, modules, and functionalities.
given_i_am_on_the_landing_page
i_can_see_something
end

def given_i_am_on_the_landing_page
visit "/"
end

def i_can_see_something
expect(page).to have_content("Something")
end
Expand All @@ -81,33 +81,27 @@ Use the following test types for our classes, modules, and functionalities.
# Base happy path first
it "returns a list of users" do
get :index

expected_json = [
{
id: 1,
first_name: "John",
last_name: "Doe"
}
]


expected_json = [{ id: 1, first_name: "John", last_name: "Doe" }]

expect(response.body).to eq(expected_json)
end

# Happy path variants next
context "when given a 'name' query parameter" do
it "returns a list of users filtered by name" do
# Assertion
end
end

# Error paths
context "without authentication" do
it "returns a 401 error" do
# Assertion
end
end
end

context "POST /users" do
it "returns a list of users" do
get :index
Expand Down
Original file line number Diff line number Diff line change
@@ -1,33 +1,37 @@
class Placements::Support::ProviderSuggestionsController < Placements::Support::ApplicationController
def index
accredited_providers = AccreditedProviderApi.call
filtered_providers = filter_providers(accredited_providers, params[:query])
filtered_providers = filter_providers(accredited_providers, query_params)
providers =
filtered_providers.map { |provider| formatted_provider(provider) }
render json: providers
end

private

def query_params
params.require(:query)
end

def filter_providers(providers, query)
return providers if query.blank?

downcase_query = query.downcase
providers.select do |provider|
[
provider["attributes"]["name"],
provider["attributes"]["postcode"],
provider["attributes"]["urn"],
provider["attributes"]["ukprn"]
provider.dig("attributes", "name"),
provider.dig("attributes", "postcode"),
provider.dig("attributes", "urn"),
provider.dig("attributes", "ukprn"),
].any? { |attribute| attribute&.downcase&.include?(downcase_query) }
end
end

def formatted_provider(provider)
{
id: provider["id"],
name: provider["attributes"]["name"],
code: provider["attributes"]["code"]
id: provider.fetch("id"),
name: provider.dig("attributes", "name"),
code: provider.dig("attributes", "code"),
}
end
end
15 changes: 5 additions & 10 deletions app/controllers/placements/support/providers_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ def new
end

def create
@provider = Provider.find_or_initialize_by(provider_params)
@provider = Provider.new(provider_params)
if @provider.save
redirect_to placements_support_organisations_path
else
Expand All @@ -14,15 +14,10 @@ def create

def check
@provider = Provider.new(provider_code: params[:accredited_provider_id])
if @provider.valid?
accredited_providers = AccreditedProviderApi.call
@selected_provider =
accredited_providers.find do |provider|
provider["attributes"]["code"] == params[:accredited_provider_id]
end
else
render :new
end
@provider_details = AccreditedProviderApi.call(@provider.provider_code)
return if @provider.valid? && @provider_details.present?

render :new
end

private
Expand Down
4 changes: 2 additions & 2 deletions app/helpers/routes_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@ module RoutesHelper
def root_path
{
claims: claims_root_path,
placements: placements_root_path
placements: placements_root_path,
}.fetch current_service
end

def support_root_path
{
claims: root_path, # TODO: claims support path in another PR
placements: placements_support_root_path
placements: placements_support_root_path,
}.fetch current_service
end
end
4 changes: 2 additions & 2 deletions app/models/dfe_sign_in_user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ def self.begin_session!(session, omniauth_payload)
"email" => omniauth_payload["info"]["email"],
# "dfe_sign_in_uid" => omniauth_payload["uid"],
"first_name" => omniauth_payload["info"]["first_name"],
"last_name" => omniauth_payload["info"]["last_name"]
"last_name" => omniauth_payload["info"]["last_name"],
# "last_active_at" => Time.zone.now,
# "id_token" => omniauth_payload["credentials"]["id_token"],
# "provider" => omniauth_payload["provider"],
Expand All @@ -39,7 +39,7 @@ def self.load_from_session(session)
last_name: dfe_sign_in_session["last_name"],
# id_token: dfe_sign_in_session["id_token"],
# provider: dfe_sign_in_session["provider"],
service: session["service"]
service: session["service"],
)
end

Expand Down
2 changes: 1 addition & 1 deletion app/models/provider.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,6 @@ class Provider < ApplicationRecord
validates :provider_code,
uniqueness: {
case_sensitive: false,
message: "Provider already exists!"
message: "Provider already exists!",
}
end
39 changes: 33 additions & 6 deletions app/services/accredited_provider_api.rb
Original file line number Diff line number Diff line change
@@ -1,20 +1,47 @@
class AccreditedProviderApi
include ServicePattern

def initialize(code = nil)
@code = code
end

attr_reader :code

def call
code.present? ? provider_list(code) : provider_details
end

private

def provider_list(code)
Rails
.cache
.fetch("accredited_provider_api", expires_in: 24.hours) do
response = HTTParty.get(provider_url)
.fetch("accredited_provider_details_#{code}", expires_in: 24.hours) do
response = HTTParty.get(provider_details_url(code))
response = JSON.parse(response.to_s)
response["data"]
end
end

private
def provider_details
Rails
.cache
.fetch("all_accredited_providers", expires_in: 24.hours) do
response = HTTParty.get(all_providers_url)
response = JSON.parse(response.to_s)
response["data"]
end
end

def all_providers_url
"#{ENV["PUBLISH_BASE_URL"]}/api/public/v1/recruitment_cycles/#{next_year}/providers?filter[is_accredited_body]=true"
end

def provider_details_url(code)
"#{ENV["PUBLISH_BASE_URL"]}/api/public/v1/recruitment_cycles/#{next_year}/providers/#{code}"
end

def provider_url
year = Time.current.next_year.year
"https://www.publish-teacher-training-courses.service.gov.uk/api/public/v1/recruitment_cycles/#{year}/providers?filter[is_accredited_body]=true"
def next_year
Time.current.next_year.year
end
end
2 changes: 1 addition & 1 deletion app/services/gias_csv_importer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ def call
address2: school["Locality"].presence,
address3: school["Address3"].presence,
website: school["SchoolWebsite"].presence,
telephone: school["TelephoneNum"].presence
telephone: school["TelephoneNum"].presence,
}
end

Expand Down
26 changes: 13 additions & 13 deletions app/views/placements/support/providers/check.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<div class="govuk-grid-row">
<div class="govuk-grid-column-two-thirds">
<%= form_with(model: @provider, url: placements_support_providers_path, method: "post", data: {turbo: false}) do |f| %>
<%= f.hidden_field :provider_code, value: @selected_provider["attributes"]["code"] %>
<%= f.hidden_field :provider_code, value: @provider_details.dig("attributes", "code") %>

<label class="govuk-label govuk-label--l">
<span class="govuk-caption-l"><%= t(".add_organisation") %></span>
Expand All @@ -18,7 +18,7 @@
<%= t(".organisation_name") %>
</dt>
<dd class="govuk-summary-list__value">
<%= @selected_provider["attributes"]["name"] %>
<%= @provider_details.dig("attributes", "name") %>
</dd>

<dd class="govuk-summary-list__actions">
Expand All @@ -30,7 +30,7 @@
<%= t(".ukprn") %>
</dt>
<dd class="govuk-summary-list__value">
<%= @selected_provider["attributes"]["ukprn"] %>
<%= @provider_details.dig("attributes", "ukprn") %>
</dd>
</div>

Expand All @@ -39,7 +39,7 @@
<%= t(".accredited_provider_id") %>
</dt>
<dd class="govuk-summary-list__value">
<%= @selected_provider["attributes"]["accredited_provider_id"] %>
<%= @provider_details.dig("attributes", "accredited_provider_id") %>
</dd>
</div>
</dl>
Expand All @@ -51,7 +51,7 @@
<%= t(".email") %>
</dt>
<dd class="govuk-summary-list__value">
<%= @selected_provider["attributes"]["email"] %>
<%= @provider_details.dig("attributes", "email") %>
</dd>
</div>

Expand All @@ -60,7 +60,7 @@
<%= t(".telephone") %>
</dt>
<dd class="govuk-summary-list__value">
<%= @selected_provider["attributes"]["telephone"] %>
<%= @provider_details.dig("attributes", "telephone") %>
</dd>
</div>

Expand All @@ -69,7 +69,7 @@
<%= t(".website") %>
</dt>
<dd class="govuk-summary-list__value">
<%= @selected_provider["attributes"]["website"] %>
<%= @provider_details.dig("attributes", "website") %>
</dd>
</div>

Expand All @@ -79,17 +79,17 @@
</dt>
<dd class="govuk-summary-list__value">
<p class="govuk-body">
<%= @selected_provider["attributes"]["street_address_1"] %>
<%= @provider_details.dig("attributes", "street_address_1") %>
<br>
<%= @selected_provider["attributes"]["street_address_2"] %>
<%= @provider_details.dig("attributes", "street_address_2") %>
<br>
<%= @selected_provider["attributes"]["street_address_3"] %>
<%= @provider_details.dig("attributes", "street_address_3") %>
<br>
<%= @selected_provider["attributes"]["city"] %>
<%= @provider_details.dig("attributes", "city") %>
<br>
<%= @selected_provider["attributes"]["county"] %>
<%= @provider_details.dig("attributes", "county") %>
<br>
<%= @selected_provider["attributes"]["postcode"] %>
<%= @provider_details.dig("attributes", "postcode") %>
<br>
</p>
</dd>
Expand Down
2 changes: 1 addition & 1 deletion bin/bundle
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ m =
require_error = activation_error_handling { require "bundler/version" }
if require_error.nil? &&
Gem::Requirement.new(bundler_requirement).satisfied_by?(
Gem::Version.new(Bundler::VERSION)
Gem::Version.new(Bundler::VERSION),
)
return
end
Expand Down
2 changes: 1 addition & 1 deletion config/application.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ class Application < Rails::Application
config.autoload_lib(ignore: %w[])

config.assets.paths << Rails.root.join(
"node_modules/govuk-frontend/dist/govuk/assets"
"node_modules/govuk-frontend/dist/govuk/assets",
)

config.autoload_paths += %W[#{config.root}/app/assets/components]
Expand Down
4 changes: 2 additions & 2 deletions config/environments/development.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@

config.cache_store = :memory_store
config.public_file_server.headers = {
"Cache-Control" => "public, max-age=#{2.days.to_i}"
"Cache-Control" => "public, max-age=#{2.days.to_i}",
}
else
config.action_controller.perform_caching = false
Expand All @@ -35,7 +35,7 @@

config.action_mailer.delivery_method = :notify
config.action_mailer.notify_settings = {
api_key: ENV.fetch("GOVUK_NOTIFY_API_KEY")
api_key: ENV.fetch("GOVUK_NOTIFY_API_KEY"),
}

# Store uploaded files on the local file system (see config/storage.yml for options).
Expand Down
2 changes: 1 addition & 1 deletion config/environments/test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
# Configure public file server for tests with Cache-Control for performance.
config.public_file_server.enabled = true
config.public_file_server.headers = {
"Cache-Control" => "public, max-age=#{1.hour.to_i}"
"Cache-Control" => "public, max-age=#{1.hour.to_i}",
}

# Show full error reports and disable caching.
Expand Down
Loading

0 comments on commit abdfae3

Please sign in to comment.