Skip to content

Commit

Permalink
Merge branch 'master' into 10082-remove-pref
Browse files Browse the repository at this point in the history
  • Loading branch information
aherzberg authored Nov 6, 2024
2 parents 2325583 + aa18014 commit 335e0ec
Show file tree
Hide file tree
Showing 23 changed files with 1,398 additions and 23 deletions.
1 change: 1 addition & 0 deletions .rubocop_todo.yml
Original file line number Diff line number Diff line change
Expand Up @@ -299,6 +299,7 @@ Lint/MissingSuper:
- 'lib/search_typeahead/service.rb'
- 'lib/token_validation/v2/client.rb'
- 'lib/va_profile/address_validation/service.rb'
- 'lib/va_profile/v3/address_validation/service.rb'
- 'lib/va_profile/service.rb'
- 'lib/vbs/requests/list_statements.rb'
- 'lib/vic/id_card_attribute_error.rb'
Expand Down
16 changes: 14 additions & 2 deletions app/controllers/v0/profile/address_validation_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

require 'va_profile/models/validation_address'
require 'va_profile/address_validation/service'
require 'va_profile/models/v3/validation_address'
require 'va_profile/v3/address_validation/service'

module V0
module Profile
Expand All @@ -11,7 +13,12 @@ class AddressValidationController < ApplicationController
skip_before_action :authenticate, only: [:create]

def create
address = VAProfile::Models::ValidationAddress.new(address_params)
address = if Flipper.enabled?(:va_v3_contact_information_service, @current_user)
VAProfile::Models::V3::ValidationAddress.new(address_params)
else
VAProfile::Models::ValidationAddress.new(address_params)
end

raise Common::Exceptions::ValidationErrors, address unless address.valid?

Rails.logger.warn('AddressValidationController#create request completed', sso_logging_info)
Expand All @@ -29,6 +36,7 @@ def address_params
:address_pou,
:address_type,
:city,
:country_name,
:country_code_iso3,
:international_postal_code,
:province,
Expand All @@ -39,7 +47,11 @@ def address_params
end

def service
@service ||= VAProfile::AddressValidation::Service.new
@service ||= if Flipper.enabled?(:va_v3_contact_information_service, @current_user)
VAProfile::V3::AddressValidation::Service.new
else
VAProfile::AddressValidation::Service.new
end
end
end
end
Expand Down
1 change: 0 additions & 1 deletion app/swagger/swagger/requests/profile.rb
Original file line number Diff line number Diff line change
Expand Up @@ -401,7 +401,6 @@ class Profile
key :description, 'Response is OK'
schema do
key :type, :object
property(:validation_key, type: :integer)

property(:addresses) do
key :type, :array
Expand Down
2 changes: 2 additions & 0 deletions config/initializers/breakers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
require 'search_typeahead/configuration'
require 'search_click_tracking/configuration'
require 'va_profile/address_validation/configuration'
require 'va_profile/v3/address_validation/configuration'
require 'va_profile/contact_information/configuration'
require 'va_profile/v2/contact_information/configuration'
require 'va_profile/communication/configuration'
Expand Down Expand Up @@ -64,6 +65,7 @@
Preneeds::Configuration.instance.breakers_service,
SM::Configuration.instance.breakers_service,
VAProfile::AddressValidation::Configuration.instance.breakers_service,
VAProfile::V3::AddressValidation::Configuration.instance.breakers_service,
VAProfile::ContactInformation::Configuration.instance.breakers_service,
VAProfile::V2::ContactInformation::Configuration.instance.breakers_service,
VAProfile::Communication::Configuration.instance.breakers_service,
Expand Down
1 change: 1 addition & 0 deletions config/settings.yml
Original file line number Diff line number Diff line change
Expand Up @@ -280,6 +280,7 @@ vet360:
timeout: 30
mock: false
address_validation:
url: "https://sandbox-api.va.gov"
hostname: "sandbox-api.va.gov"
api_key: "<AV_KEY>"
profile_information:
Expand Down
79 changes: 79 additions & 0 deletions lib/va_profile/models/v3/validation_address.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
# frozen_string_literal: true

require_relative 'base_address'
require 'common/hash_helpers'

module VAProfile
module Models
module V3
# Model for addresses sent and received from the VA profile address validation API
# AddressValidationV is used for ProfileServiceV3 and ContactInformationV2
class ValidationAddress < V3::BaseAddress
# Convert a ValidationAddress into a hash that can be sent to the address validation
# API
# @return [Hash] hash that is formatted for POSTing to address validation API
def address_validation_req
Common::HashHelpers.deep_remove_blanks(
address: attributes.slice(
:address_line1,
:address_line2,
:address_line3
).deep_transform_keys { |k| k.to_s.camelize(:lower) }.merge(
intPostalCode: @international_postal_code,
cityName: @city,
zipCode5: @zip_code,
zipCode4: @zip_code_suffix,
country: { countryCodeISO2: @country_code_iso2, countryCodeISO3: @country_code_iso3,
countryName: @country_name, countryCodeFIPS: @country_code_fips },
state: { stateCode: @state_code, stateName: @state_name },
province: { provinceName: @province_name, provinceCode: @province_code },
addressPOU: @address_pou
)
)
end

# @return [VAProfile::Models::V3::ValidationAddress] validation address model created from
# address validation API response
def self.build_from_address_suggestion(address_suggestion_hash)
address_type = address_suggestion_hash['address_type'].upcase
attributes = {
address_line1: address_suggestion_hash['address_line1'],
address_line2: address_suggestion_hash['address_line2'],
address_line3: address_suggestion_hash['address_line3'],
address_type:,
city: address_suggestion_hash['city_name'],
country_name: address_suggestion_hash.dig('country', 'country_name'),
country_code_iso3: address_suggestion_hash.dig('country', 'country_code_iso3')
}.merge(regional_attributes(address_type, address_suggestion_hash))

new(attributes)
end

def self.regional_attributes(address_type, address_hash)
if address_type == INTERNATIONAL
{
province: address_hash['province']['province_name'],
international_postal_code: address_hash['int_postal_code']
}
else
{
state_code: address_hash.dig('state', 'state_code'),
county_code: address_hash.dig('county', 'county_code'),
county_name: address_hash.dig('county', 'county_name'),
zip_code: address_hash['zip_code5'],
zip_code_suffix: address_hash['zip_code4']
}
end
end

def self.build_address_metadata(address_suggestion_hash)
{
confidence_score: address_suggestion_hash['confidence'],
address_type: address_suggestion_hash['address_type'],
delivery_point_validation: address_suggestion_hash['delivery_point_validation']
}
end
end
end
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# frozen_string_literal: true

require 'va_profile/models/v3/validation_address'
require_relative 'service'

module VAProfile
module V3
module AddressValidation
# Wrapper for response from VA profile address validation API.
# Contains address suggestions and validation key used to ignore suggested addresses
# and use original address.
class AddressSuggestionsResponse
def initialize(candidate_res)
validation_key = candidate_res['override_validation_key']
@response = {
addresses: candidate_res['candidate_addresses'].map do |address_suggestion_hash|
{
address: VAProfile::Models::V3::ValidationAddress.build_from_address_suggestion(
address_suggestion_hash
).to_h.compact,
address_meta_data: VAProfile::Models::V3::ValidationAddress.build_address_metadata(
address_suggestion_hash
).to_h
}
end,
validation_key:
}
end

def to_json(*_args)
@response.to_json
end
end
end
end
end
23 changes: 23 additions & 0 deletions lib/va_profile/v3/address_validation/configuration.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# frozen_string_literal: true

require 'va_profile/configuration'

module VAProfile
module V3
module AddressValidation
class Configuration < VAProfile::Configuration
def base_path
"#{VAProfile::Configuration::SETTINGS.address_validation.url}/services/address-validation/v3/"
end

def base_request_headers
super.merge('apiKey' => VAProfile::Configuration::SETTINGS.address_validation.api_key)
end

def service_name
'VAProfile/V3/AddressValidation'
end
end
end
end
end
65 changes: 65 additions & 0 deletions lib/va_profile/v3/address_validation/service.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
# frozen_string_literal: true

require 'common/client/concerns/monitoring'
require 'common/exceptions'
require_relative 'configuration'
require_relative 'address_suggestions_response'
require 'va_profile/service'
require 'va_profile/stats'

module VAProfile
module V3
module AddressValidation
# Wrapper for the VA profile address validation/suggestions API
class Service < VAProfile::Service
include Common::Client::Concerns::Monitoring

STATSD_KEY_PREFIX = "#{VAProfile::Service::STATSD_KEY_PREFIX}.address_validation".freeze
configuration VAProfile::V3::AddressValidation::Configuration

def initialize; end

# Get address suggestions and override key from the VA profile API
# @return [VAProfile::AddressValidation::AddressSuggestionsResponse] response wrapper around address
# suggestions data
def address_suggestions(address)
with_monitoring do
candidate_res = candidate(address)

AddressSuggestionsResponse.new(candidate_res)
end
end

# @return [Hash] raw data from VA profile address validation API including
# address suggestions, validation key, and address errors
def candidate(address)
begin
res = perform(
:post,
'candidate',
address.address_validation_req.to_json
)
rescue => e
handle_error(e)
end

res.body
end

private

def handle_error(error)
raise error unless error.is_a?(Common::Client::Errors::ClientError)

save_error_details(error)
raise_invalid_body(error, self.class) unless error.body.is_a?(Hash)

raise Common::Exceptions::BackendServiceException.new(
'VET360_AV_ERROR',
detail: error.body
)
end
end
end
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

require 'va_profile/models/validation_address'
require 'va_profile/address_validation/service'
require 'va_profile/models/v3/validation_address'
require 'va_profile/v3/address_validation/service'

module AskVAApi
module V0
Expand All @@ -11,7 +13,12 @@ class AddressValidationController < ApplicationController
service_tag 'profile'

def create
address = VAProfile::Models::ValidationAddress.new(address_params)
validation_model = if Flipper.enabled?(:va_v3_contact_information_service, @current_user)
VAProfile::Models::V3::ValidationAddress
else
VAProfile::Models::ValidationAddress
end
address = validation_model.new(address_params)
raise Common::Exceptions::ValidationErrors, address unless address.valid?

Rails.logger.warn('AddressValidationController#create request completed', sso_logging_info)
Expand Down Expand Up @@ -39,7 +46,11 @@ def address_params
end

def service
@service ||= VAProfile::AddressValidation::Service.new
@service ||= if Flipper.enabled?(:va_v3_contact_information_service, @current_user)
VAProfile::V3::AddressValidation::Service.new
else
VAProfile::AddressValidation::Service.new
end
end
end
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

require 'va_profile/address_validation/service'
require_relative '../concerns/sso_logging'
require 'va_profile/v3/address_validation/service'

module Mobile
module V0
Expand Down
2 changes: 1 addition & 1 deletion modules/va_forms/app/sidekiq/va_forms/form_builder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ def gather_form_attributes(form_data)
form_details_url:
form_data['entityPublished'] ? "#{VAForms::Form::FORM_BASE_URL}#{form_data.dig('entityUrl', 'path')}" : '',
form_tool_intro: form_data['fieldVaFormToolIntro'],
form_tool_url: form_data.dig('fieldVaFormToolUrl', 'uri'),
form_tool_url: form_data['entityPublished'] ? form_data.dig('fieldVaFormToolUrl', 'uri') : '',
deleted_at: form_data.dig('fieldVaFormDeletedDate', 'value'),
related_forms: form_data['fieldVaFormRelatedForms'].map { |f| f.dig('entity', 'fieldVaFormNumber') },
benefit_categories: parse_benefit_categories(form_data),
Expand Down
Loading

0 comments on commit 335e0ec

Please sign in to comment.