Skip to content

Commit

Permalink
Merge pull request #382 from Crown-Commercial-Service/merge-uat-into-…
Browse files Browse the repository at this point in the history
…preproduction

Merge uat into main
  • Loading branch information
tberey committed Feb 9, 2022
2 parents 69a7c8f + de15e57 commit 06f37a2
Show file tree
Hide file tree
Showing 14 changed files with 256 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ def delete_additional_identifier
delete_organisation
render json: '', status: :ok
rescue StandardError
render json: '', status: :bad_request
render json: '', status: :not_found
end

def validate_params
Expand Down
16 changes: 8 additions & 8 deletions app/controllers/api/v1/testing/access_token_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ def render_results(access_token)
end

def org_creation(org_id)
response = Faraday.post("#{ENV['ACCESS_TOKEN_WRAPPER_URL']}/organisations") do |req|
response = Faraday.post("#{ENV['ACCESS_TOKEN_SECURITY_URL']}/organisation-profiles") do |req|
req.headers['x-api-key'] = request.headers['x-api-key']
req.headers['Content-Type'] = 'application/json'
req.body = org_creation_body(org_id)
Expand All @@ -48,7 +48,7 @@ def org_creation(org_id)
@error_reason = {
StatusCode: response.status,
ResponseBody: response.body,
RequestURL: "POST #{ENV['ACCESS_TOKEN_WRAPPER_URL']}/organisations",
RequestURL: "POST #{ENV['ACCESS_TOKEN_SECURITY_URL']}/organisation-profiles",
Note: 'Org Creation Step'
}
false
Expand Down Expand Up @@ -78,7 +78,7 @@ def org_creation_body(org_id)
end

def identity_provider(org_id)
response = Faraday.get("#{ENV['ACCESS_TOKEN_WRAPPER_URL']}/organisations/#{org_id}/identity-providers",
response = Faraday.get("#{ENV['ACCESS_TOKEN_SECURITY_URL']}/organisations/#{org_id}/identity-providers",
nil,
{ 'x-api-key' => request.headers['x-api-key'] })

Expand All @@ -87,14 +87,14 @@ def identity_provider(org_id)
@error_reason = {
StatusCode: response.status,
ResponseBody: response.body,
RequestURL: "GET #{ENV['ACCESS_TOKEN_WRAPPER_URL']}/organisations/#{org_id}/identity-providers",
RequestURL: "GET #{ENV['ACCESS_TOKEN_SECURITY_URL']}/organisations/#{org_id}/identity-providers",
Note: 'Identity Provider Step'
}
false
end

def role_id(org_id)
response = Faraday.get("#{ENV['ACCESS_TOKEN_WRAPPER_URL']}/organisations/#{org_id}/roles",
response = Faraday.get("#{ENV['ACCESS_TOKEN_SECURITY_URL']}/organisations/#{org_id}/roles",
nil,
{ 'x-api-key' => request.headers['x-api-key'] })

Expand All @@ -103,14 +103,14 @@ def role_id(org_id)
@error_reason = {
StatusCode: response.status,
ResponseBody: response.body,
RequestURL: "GET #{ENV['ACCESS_TOKEN_WRAPPER_URL']}/organisations/#{org_id}/roles",
RequestURL: "GET #{ENV['ACCESS_TOKEN_SECURITY_URL']}/organisations/#{org_id}/roles",
Note: 'Role ID Step'
}
false
end

def create_user(email, org_id, provider, role)
response = Faraday.post("#{ENV['ACCESS_TOKEN_WRAPPER_URL']}/users") do |req|
response = Faraday.post("#{ENV['ACCESS_TOKEN_SECURITY_URL']}/user-profiles") do |req|
req.body = create_user_body(email, org_id, provider, role)
req.headers['Content-Type'] = 'application/json'
req.headers['x-api-key'] = request.headers['x-api-key']
Expand All @@ -121,7 +121,7 @@ def create_user(email, org_id, provider, role)
@error_reason = {
StatusCode: response.status,
ResponseBody: response.body,
RequestURL: "POST #{ENV['ACCESS_TOKEN_WRAPPER_URL']}/users",
RequestURL: "POST #{ENV['ACCESS_TOKEN_SECURITY_URL']}/user-profiles",
Note: 'User Creation Step'
}
false
Expand Down
3 changes: 2 additions & 1 deletion app/services/common/additional_identifier.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ class AdditionalIdentifier
SCHEME_SCOTISH_CHARITY = 'GB-SC'.freeze
SCHEME_COMPANIES_HOUSE = 'GB-COH'.freeze
SCHEME_DANDB = 'US-DUN'.freeze
SCHEME_NHS = 'GB-NHS'.freeze
SCHEME_CCS = 'GB-CCS'.freeze

# The below three constants are required to match the correct uri provided in the identifiers uri property.
Expand All @@ -20,7 +21,7 @@ class AdditionalIdentifier
GB_SC_SCHEME_URI_SITE = 'Office of Scottish Charity Regulator'.freeze

def schemes
[SCHEME_ENG_WALES_CHARITY, SCHEME_NORTHEN_IRELAND_CHARITY, SCHEME_SCOTISH_CHARITY, SCHEME_COMPANIES_HOUSE, SCHEME_DANDB]
[SCHEME_ENG_WALES_CHARITY, SCHEME_NORTHEN_IRELAND_CHARITY, SCHEME_SCOTISH_CHARITY, SCHEME_COMPANIES_HOUSE, SCHEME_DANDB, SCHEME_NHS]
end

def dandb_codes
Expand Down
2 changes: 1 addition & 1 deletion app/services/common/api_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ def self.return_all_organisation_schemes(ccs_org_id)

def self.faraday_new(options)
Faraday.new(options) do |builder|
builder.use Faraday::HttpCache, store: Rails.cache, logger: Rails.logger, shared_cache: false
builder.use Faraday::HttpCache, store: Rails.cache, logger: Rails.logger, shared_cache: false, serializer: Marshal
builder.use Faraday::OverrideCacheControl, cache_control: 'public, max-age=86400'
builder.adapter Faraday.default_adapter
end
Expand Down
2 changes: 1 addition & 1 deletion app/services/common/salesforce_search_ids.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ def self.account_id_types_salesforce
end

def self.account_id_types_all
[Common::SalesforceSearchIds::SFURN, Common::SalesforceSearchIds::SFID, Common::AdditionalIdentifier::SCHEME_COMPANIES_HOUSE, Common::AdditionalIdentifier::SCHEME_DANDB, Common::AdditionalIdentifier::SCHEME_ENG_WALES_CHARITY, Common::AdditionalIdentifier::SCHEME_NORTHEN_IRELAND_CHARITY, Common::AdditionalIdentifier::SCHEME_SCOTISH_CHARITY]
[Common::SalesforceSearchIds::SFURN, Common::SalesforceSearchIds::SFID, Common::AdditionalIdentifier::SCHEME_COMPANIES_HOUSE, Common::AdditionalIdentifier::SCHEME_DANDB, Common::AdditionalIdentifier::SCHEME_ENG_WALES_CHARITY, Common::AdditionalIdentifier::SCHEME_NORTHEN_IRELAND_CHARITY, Common::AdditionalIdentifier::SCHEME_SCOTISH_CHARITY, Common::AdditionalIdentifier::SCHEME_NHS]
end
end
end
2 changes: 2 additions & 0 deletions app/services/mocking_service/api_stub.rb
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ def url
when Common::AdditionalIdentifier::SCHEME_DANDB
duns_token
@api_url = "#{ENV['DNB_API_ENDPOINT']}/v1/data/duns/#{@params[:id]}?productId=cmptcs&versionId=v1"
when Common::AdditionalIdentifier::SCHEME_NHS
@api_url = "https://directory.spineservices.nhs.uk/ORD/2-0-0/organisations/#{@params[:id]}"
end
end

Expand Down
52 changes: 52 additions & 0 deletions app/services/nhs/address.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
module Nhs
class Address
def initialize(result)
super()
@result = result
end

def build_response
{
streetAddress: street_address,
locality: locality,
region: region,
postalCode: postal_code,
countryName: country_name
}
end

def street_address
"#{exists_or_null(@result['Organisation']['GeoLoc']['Location']['AddrLn1'])}#{street_address_two}#{street_address_three}"
end

def street_address_two
", #{exists_or_null(@result['Organisation']['GeoLoc']['Location']['AddrLn2'])}" if exists_or_null(@result['Organisation']['GeoLoc']['Location']['AddrLn2']).present?
end

def street_address_three
", #{exists_or_null(@result['Organisation']['GeoLoc']['Location']['AddrLn3'])}" if exists_or_null(@result['Organisation']['GeoLoc']['Location']['AddrLn3']).present?
end

def locality
exists_or_null(@result['Organisation']['GeoLoc']['Location']['Town'])
end

def region
exists_or_null(@result['Organisation']['GeoLoc']['Location']['County'])
end

def postal_code
exists_or_null(@result['Organisation']['GeoLoc']['Location']['PostCode'])
end

def country_name
exists_or_null(@result['Organisation']['GeoLoc']['Location']['Country'])
end

private

def exists_or_null(api_param)
api_param.present? ? api_param : ''
end
end
end
45 changes: 45 additions & 0 deletions app/services/nhs/contact.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
module Nhs
class Contact
def initialize(result)
super()
@result = result
end

def build_response
{
name: '',
email: '',
telephone: exists_or_null(telephone).to_s,
faxNumber: exists_or_null(fax).to_s,
uri: exists_or_null(uri).to_s
}
end

def telephone
get_contact_value(@result['Organisation']['Contacts']['Contact'], 'tel') if @result['Organisation']['Contacts'].present?
end

def fax
get_contact_value(@result['Organisation']['Contacts']['Contact'], 'fax') if @result['Organisation']['Contacts'].present?
end

def uri
get_contact_value(@result['Organisation']['Contacts']['Contact'], 'http') if @result['Organisation']['Contacts'].present?
end

private

def get_contact_value(contacts, type)
return '' if contacts.blank?

contacts.each do |contact|
return contact['value'] if contact['type'] == type.to_s
end
''
end

def exists_or_null(api_param)
api_param.present? ? api_param : ''
end
end
end
31 changes: 31 additions & 0 deletions app/services/nhs/indentifier.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
module Nhs
class Indentifier
def initialize(result)
super()
@result = result
end

def build_response
{
scheme: 'GB-NHS',
id: organisation_code,
legalName: legal_name,
uri: "https://directory.spineservices.nhs.uk/ORD/2-0-0/organisations/#{organisation_code}"
}
end

def organisation_code
exists_or_null(@result['Organisation']['OrgId']['extension'])
end

def legal_name
exists_or_null(@result['Organisation']['Name'])
end

private

def exists_or_null(api_param)
api_param.present? ? api_param : ''
end
end
end
52 changes: 52 additions & 0 deletions app/services/nhs/search.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
module Nhs
class Search
def initialize(organisation_code)
super()
@organisation_code = organisation_code
@company_number = nil
@error = nil
@result = []
@additional_indentifers_list = []
end

def fetch_results
conn = Common::ApiHelper.faraday_new(url: 'https://directory.spineservices.nhs.uk')
resp = conn.get("/ORD/2-0-0/organisations/#{@organisation_code}")
logging(resp)
@result = ActiveSupport::JSON.decode(resp.body) if resp.status == 200

if resp.status == 200 && @result.key?('Organisation') && @result['Organisation']['Status'] == 'Active'
build_response
else
false
end
end

def build_response
{
name: name,
identifier: Nhs::Indentifier.new(@result).build_response,
additionalIdentifiers: [],
address: Nhs::Address.new(@result).build_response,
contactPoint: Nhs::Contact.new(@result).build_response
}
end

def name
exists_or_null(@result['Organisation']['Name'])
end

private

def exists_or_null(api_param)
api_param.present? ? api_param : ''
rescue StandardError => e
ApiLogging::Logger.info(e)
end

def logging(resp)
ApiLogging::Logger.api_status_error('NHS API| method:fetch_results', resp)
# ApiLogging::Logger.info(resp.headers['X-RateLimit-Remain'])
end
end
end
7 changes: 7 additions & 0 deletions app/services/search_api.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ def call
@result = get_charity(@organisation_id, @scheme_id)
when Common::AdditionalIdentifier::SCHEME_DANDB
@result = get_duns(@organisation_id)
when Common::AdditionalIdentifier::SCHEME_NHS
@result = get_nhs(@organisation_id)
end

@result if @result.present?
Expand All @@ -45,6 +47,11 @@ def get_duns(dnd_number)
results
end

def get_nhs(organisation_code)
nhs = Nhs::Search.new(organisation_code)
nhs.fetch_results
end

def get_addtional_identfiers(identfiers)
return if @address_lookup

Expand Down
1 change: 1 addition & 0 deletions db/seeds.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
{scheme_name: 'Charity Commission for England and Wales', scheme_register_code: 'GB-CHC', scheme_uri: 'https://findthatcharity.uk', scheme_country_code: 'GB', scheme_identifier: 'Registered Charity Number'},
{scheme_name: 'Office of The Scottish Charity Regulator (OSCR)', scheme_register_code: 'GB-SC', scheme_uri: 'https://findthatcharity.uk', scheme_country_code: 'GB', scheme_identifier: 'Registered Charity Number'},
{scheme_name: 'The Charity Commission for Northern Ireland', scheme_register_code: 'GB-NIC', scheme_uri: 'https://findthatcharity.uk', scheme_country_code: 'GB', scheme_identifier: 'Registered Charity Number'},
{scheme_name: 'National Health Service Organsations Registry', scheme_register_code: 'GB-NHS', scheme_uri: 'https://www.crowncommercial.gov.uk', scheme_country_code: 'GB', scheme_identifier: 'NHS Registered Number'},
{scheme_name: 'The Crown Commercial Service', scheme_register_code: 'GB-CCS', scheme_uri: 'https://www.crowncommercial.gov.uk', scheme_country_code: 'GB', scheme_identifier: 'Crown Commercial Service Internal Number'}

].each do |rec|
Expand Down
5 changes: 5 additions & 0 deletions spec/controllers/api/v1/organisations_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,11 @@
expect(response.status).to eq(200)
end

it 'search nhs' do
get :search_organisation, params: { scheme: 'GB-NHS', id: 'A23' }
expect(response.status).to eq(200)
end

it 'search invalid params' do
get :search_organisation, params: { scheme: 'INVALID', id: '12345' }
expect(response.status).to eq(404)
Expand Down
48 changes: 48 additions & 0 deletions spec/stub_response/api_stubs/GB-NHS-A23.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
{
"Organisation": {
"Name": "Testing NHS Regsitry",
"Date": [
{
"Type": "Operational",
"Start": "1999-11-11",
"End": "2011-12-12"
}
],
"OrgId": {
"root": "4.14.444.4.414484.4.4.4.4.4.14.44",
"assigningAuthorityName": "HDIDI",
"extension": "A23"
},
"Status": "Active",
"LastChangeDate": "2020-10-10",
"orgRecordClass": "RCX",
"refOnly": true,
"GeoLoc": {
"Location": {
"AddrLn1": "ADDRESS LINE ONE",
"AddrLn2": "ADDRESS LINE TWO",
"Town": "TOWN",
"County": "COUNTY",
"PostCode": "AB1 2CD",
"Country": "UNITED KINGDOM"
}
},
"Roles": {
"Role": [
{
"id": "RO999",
"uniqueRoleId": 37777,
"primaryRole": true,
"Date": [
{
"Type": "Operational",
"Start": "1999-11-11",
"End": "2011-12-12"
}
],
"Status": "Active"
}
]
}
}
}

0 comments on commit 06f37a2

Please sign in to comment.