Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Check renewal for relevant convictions #193

Merged
merged 13 commits into from
Jun 25, 2018
Merged
1 change: 1 addition & 0 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
# Domains
WCRS_RENEWALS_PUBLIC_APP_DOMAIN="http://localhost:3002"
WCRS_FRONTEND_PUBLIC_APP_DOMAIN="http://localhost:3000"
WCRS_SERVICES_PUBLIC_APP_DOMAIN="http://localhost:8003"

# Application settings
WCRS_REGISTRATION_EXPIRES_AFTER=3
Expand Down
6 changes: 5 additions & 1 deletion app/controllers/declaration_forms_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ def new
end

def create
super(DeclarationForm, "declaration_form")
return unless super(DeclarationForm, "declaration_form")

entity_matching_service = EntityMatchingService.new(@transient_registration)
entity_matching_service.check_business_for_matches
entity_matching_service.check_people_for_matches
end
end
4 changes: 3 additions & 1 deletion app/controllers/forms_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ def new(form_class, form)

# Expects a form class name (eg BusinessTypeForm) and a snake_case name for the form (eg business_type_form)
def create(form_class, form)
return unless set_up_form(form_class, form, params[form][:reg_identifier])
return false unless set_up_form(form_class, form, params[form][:reg_identifier])

# Submit the form by getting the instance variable we just set
submit_form(instance_variable_get("@#{form}"), params[form])
Expand Down Expand Up @@ -48,8 +48,10 @@ def submit_form(form, params)
if form.submit(params)
@transient_registration.next!
format.html { redirect_to_correct_form }
true
else
format.html { render :new }
false
end
end
end
Expand Down
18 changes: 17 additions & 1 deletion app/models/concerns/can_have_registration_attributes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,23 @@ def relevant_people
end

def conviction_check_required?
declared_convictions == true
return true if declared_convictions == true
business_has_matching_or_unknown_conviction? || key_person_has_matching_or_unknown_conviction?
end

def business_has_matching_or_unknown_conviction?
return true unless convictionSearchResult.present?
return false if convictionSearchResult.match_result == "NO"
true
end

def key_person_has_matching_or_unknown_conviction?
return true unless keyPeople.present?

conviction_search_results = keyPeople.map(&:convictionSearchResult)
match_results = conviction_search_results.map(&:match_result)

match_results.include?("YES") || match_results.include?("UNKNOWN")
end

def update_last_modified
Expand Down
15 changes: 14 additions & 1 deletion app/models/conviction_search_result.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,20 @@ class ConvictionSearchResult
field :reference, type: String
field :matchedName, as: :matched_name, type: String
field :searchedAt, as: :searched_at, type: DateTime
field :confirmed, type: Boolean
field :confirmed, type: String
field :confirmedAt, as: :confirmed_at, type: DateTime
field :confirmedBy, as: :confirmed_by, type: String

def self.new_from_entity_matching_service(data)
result = ConvictionSearchResult.new

result.match_result = data["match_result"]
result.matching_system = data["matching_system"]
result.reference = data["reference"]
result.matched_name = data["matched_name"]
result.searched_at = data["searched_at"]
result.confirmed = data["confirmed"]

result
end
end
79 changes: 79 additions & 0 deletions app/services/entity_matching_service.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
class EntityMatchingService
def initialize(transient_registration)
@transient_registration = transient_registration
end

def check_business_for_matches
data = query_service(company_url)
store_match_result(@transient_registration, data)
end

def check_people_for_matches
@transient_registration.keyPeople.each do |person|
url = person_url(person)
data = query_service(url)
store_match_result(person, data)
end
end

private

def query_service(url)
Rails.logger.debug "Sending request to Entity Matching service"

begin
response = RestClient::Request.execute(method: :get, url: url)

begin
JSON.parse(response)
rescue JSON::ParserError => e
Airbrake.notify(e)
Rails.logger.error "Entity Matching JSON error: " + e.to_s
unknown_result_data
end
rescue RestClient::ExceptionWithResponse => e
Airbrake.notify(e)
Rails.logger.error "Entity Matching response error: " + e.to_s
unknown_result_data
rescue Errno::ECONNREFUSED => e
Airbrake.notify(e)
Rails.logger.error "Entity Matching connection error: " + e.to_s
unknown_result_data
rescue SocketError => e
Airbrake.notify(e)
Rails.logger.error "Entity Matching socket error: " + e.to_s
unknown_result_data
end
end

def store_match_result(entity, data)
entity.convictionSearchResult = ConvictionSearchResult.new_from_entity_matching_service(data)
entity.save!
end

def unknown_result_data
{
"match_result" => "UNKNOWN",
"matching_system" => "ERROR",
"searched_at" => Time.now.to_i,
"confirmed" => "no"
}
end

# URLs

def base_url
"#{Rails.configuration.wcrs_services_url}/match/"
end

def company_url
"#{base_url}company?name=#{@transient_registration.company_name}&number=#{@transient_registration.company_no}"
end

def person_url(person)
first_name = person.first_name
last_name = person.last_name
date_of_birth = person.date_of_birth.to_s(:entity_matching)
"#{base_url}person?firstname=#{first_name}&lastname=#{last_name}&dateofbirth=#{date_of_birth}"
end
end
1 change: 1 addition & 0 deletions config/application.rb
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ class Application < Rails::Application
# Paths
config.wcrs_renewals_url = ENV["WCRS_RENEWALS_PUBLIC_APP_DOMAIN"] || "http://localhost:3002"
config.wcrs_frontend_url = ENV["WCRS_FRONTEND_PUBLIC_APP_DOMAIN"] || "http://localhost:3000"
config.wcrs_services_url = ENV["WCRS_SERVICES_PUBLIC_APP_DOMAIN"] || "http://localhost:8003"
config.os_places_service_url = ENV["WCRS_RENEWALS_OS_PLACES_SERVICE_DOMAIN"] || "http://localhost:8005"

# Fees
Expand Down
3 changes: 2 additions & 1 deletion config/initializers/date_formats.rb
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
Date::DATE_FORMATS[:default] = '%e %B %Y'
Date::DATE_FORMATS[:default] = "%e %B %Y"
Time::DATE_FORMATS[:entity_matching] = "%d-%m-%Y"
37 changes: 37 additions & 0 deletions spec/cassettes/entity_matching_business_has_matches.yml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

36 changes: 36 additions & 0 deletions spec/cassettes/entity_matching_business_no_matches.yml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

37 changes: 37 additions & 0 deletions spec/cassettes/entity_matching_person_has_matches.yml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

69 changes: 69 additions & 0 deletions spec/cassettes/entity_matching_person_no_matches.yml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 11 additions & 0 deletions spec/factories/conviction_search_result.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,15 @@
FactoryBot.define do
factory :convictionSearchResult do
trait :match_result_yes do
match_result "YES"
end

trait :match_result_no do
match_result "NO"
end

trait :match_result_unknown do
match_result "UNKNOWN"
end
end
end