Skip to content

Commit

Permalink
Merge branch 'master' into arf.80297/ARP-OGC-number-migrations
Browse files Browse the repository at this point in the history
* master:
  BE | Ask Va Api: Update `Correspondences::Retriever` and `Inquiries::Retriever` (#16510)
  Revert "post database updates (#16500)" (#16507)
  Revert "creates data to seed dev & staging (#16502)" (#16506)
  81092: Refactor Client Config Memoization (#16490)
  • Loading branch information
gabezurita committed Apr 25, 2024
2 parents 2de044a + 15552a8 commit 84e8fea
Show file tree
Hide file tree
Showing 29 changed files with 150 additions and 446 deletions.
3 changes: 2 additions & 1 deletion app/controllers/v0/sign_in_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -369,7 +369,8 @@ def cookie_authentication?(client_id)
end

def client_config(client_id)
@client_config ||= SignIn::ClientConfig.find_by(client_id:)
@client_config ||= {}
@client_config[client_id] ||= SignIn::ClientConfig.find_by(client_id:)
end

def sign_in_logger
Expand Down
25 changes: 14 additions & 11 deletions modules/ask_va_api/app/lib/ask_va_api/correspondences/retriever.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,27 @@

module AskVAApi
module Correspondences
class CorrespondencesRetrieverError < StandardError; end

class Retriever < BaseRetriever
attr_reader :inquiry_id, :entity_class
class Retriever
attr_reader :inquiry_id, :entity_class, :user_mock_data

def initialize(inquiry_id:, user_mock_data:, entity_class:)
super(user_mock_data:, entity_class:)
@user_mock_data = user_mock_data
@entity_class = entity_class
@inquiry_id = inquiry_id
end

def call
case fetch_data
when Array
fetch_data.map { |data| entity_class.new(data) }
else
fetch_data
end
end

private

def fetch_data
validate_input(inquiry_id, 'Invalid Inquiry ID')
if user_mock_data
data = File.read('modules/ask_va_api/config/locales/get_replies_mock_data.json')

Expand All @@ -29,18 +36,14 @@ def fetch_data
end
end

def validate_input(input, error_message)
raise ArgumentError, error_message if input.blank?
end

def filter_data(data)
data.select do |cor|
cor[:InquiryId] == inquiry_id
end
end

def handle_response_data(response)
response[:Data].presence || raise(CorrespondencesRetrieverError, response[:Message])
response[:Data].presence || response
end
end
end
Expand Down
16 changes: 9 additions & 7 deletions modules/ask_va_api/app/lib/ask_va_api/inquiries/retriever.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,18 @@ def fetch_data(id = nil)
end

def fetch_correspondences(inquiry_id:)
Correspondences::Retriever.new(
correspondences = Correspondences::Retriever.new(
inquiry_id:,
user_mock_data:,
entity_class: AskVAApi::Correspondences::Entity
).call

case correspondences
when Hash
[]
else
correspondences
end
end

def read_mock_data(file_name)
Expand All @@ -57,12 +64,7 @@ def filter_data(data, id = nil)
end

def handle_response_data(response)
if response[:Data].nil?
error = JSON.parse(response[:body], symbolize_names: true)
raise InquiriesRetrieverError, error[:Message]
else
response[:Data]
end
response[:Data].presence || raise(InquiriesRetrieverError, response)
end
end
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,6 @@
end

describe '#call' do
context 'when id is blank' do
let(:inquiry_id) { nil }

it 'raises an ArgumentError' do
expect { retriever.call }
.to raise_error(ErrorHandler::ServiceError, 'ArgumentError: Invalid Inquiry ID')
end
end

context 'when Crm raise an error' do
let(:endpoint) { 'inquiries/1/replies' }
let(:response) do
Expand All @@ -42,8 +33,12 @@
allow(service).to receive(:call).and_return(response)
end

it 'raise CorrespondenceRetrieverError' do
expect { retriever.call }.to raise_error(ErrorHandler::ServiceError)
it 'returns the error' do
expect(retriever.call).to eq({ Data: [],
Message: 'Data Validation: No Inquiry Found',
ExceptionOccurred: true,
ExceptionMessage: 'Data Validation: No Inquiry Found',
MessageId: '2d746074-9e5c-4987-a894-e3f834b156b5' })
end
end

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,74 @@
end
end
end

context 'with Correspondences' do
let(:id) { '123' }
let(:response) do
{ Data: [{ Id: '154163f2-8fbb-ed11-9ac4-00155da17a6f',
InquiryNumber: 'A-20230305-306178',
InquiryStatus: 'Reopened',
SubmitterQuestion: 'test',
LastUpdate: '4/1/2024 12:00:00 AM',
InquiryHasAttachments: true,
InquiryHasBeenSplit: true,
VeteranRelationship: 'GIBillBeneficiary',
SchoolFacilityCode: '77a51029-6816-e611-9436-0050568d743d',
InquiryTopic: 'Medical Care Concerns at a VA Medical Facility',
InquiryLevelOfAuthentication: 'Unauthenticated',
AttachmentNames: [{ Id: '367e8d31-6c82-1d3c-81b8-dd2cabed7555',
Name: 'Test.txt' }] }] }
end

context 'when Correspondence::Retriever returns an error' do
before do
allow_any_instance_of(Crm::CrmToken).to receive(:call).and_return('Token')
allow(service).to receive(:call).and_return(response)
allow_any_instance_of(AskVAApi::Correspondences::Retriever).to receive(:call)
.and_return({ Data: [],
Message: 'Data Validation: No Inquiry Found',
ExceptionOccurred: true,
ExceptionMessage: 'Data Validation: No Inquiry Found',
MessageId: '2d746074-9e5c-4987-a894-e3f834b156b5' })
end

it 'returns correspondences as an empty array' do
inquiry = retriever.fetch_by_id(id:)

expect(inquiry.correspondences).to eq([])
end
end

context 'when Correspondence::Retriever returns a success' do
let(:cor_info) do
{
Id: 'f4b12ee3-93bb-ed11-9886-001dd806a6a7',
ModifiedOn: '3/5/2023 8:25:49 PM',
StatusReason: 'Sent',
Description: 'Dear aminul, Thank you for submitting your ' \
'Inquiry with the U.S. Department of Veteran Affairs.',
MessageType: 'Notification',
EnableReply: true,
AttachmentNames: nil
}
end

let(:correspondence) { AskVAApi::Correspondences::Entity.new(cor_info) }

before do
allow_any_instance_of(Crm::CrmToken).to receive(:call).and_return('Token')
allow(service).to receive(:call).and_return(response)
allow_any_instance_of(AskVAApi::Correspondences::Retriever).to receive(:call)
.and_return([correspondence])
end

it 'returns correspondences as an empty array' do
inquiry = retriever.fetch_by_id(id:)

expect(inquiry.correspondences).to eq([correspondence])
end
end
end
end
end
end
6 changes: 5 additions & 1 deletion modules/ask_va_api/spec/requests/v0/inquiries_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,11 @@

it_behaves_like 'common error handling', :unprocessable_entity, 'service_error',
'AskVAApi::Inquiries::InquiriesRetrieverError: ' \
'Data Validation: No Inquiries found by ID A-20240423-30709'
'{:status=>400, :body=>"{\"Data\":null,\"Message\":\"Data Validation: ' \
'No Inquiries found by ID A-20240423-30709\",\"ExceptionOccurred\":' \
'true,\"ExceptionMessage\":\"Data Validation: No Inquiries found by ID ' \
'A-20240423-30709\",\"MessageId\":\"ca5b990a-63fe-407d-a364-46caffce12c1\"}",' \
' :response_headers=>nil, :url=>nil}'
end
end
end
Expand Down
2 changes: 1 addition & 1 deletion modules/ask_va_api/spec/services/crm/crm_token_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ def mock_response(status:, body:)
end
end

context 'with invalid JSON' do
context 'when veis auth service returns a 401 error response' do
let(:resp) { mock_response(body: { error: 'invalid_client' }, status: 401) }
let(:exception) { Common::Exceptions::BackendServiceException.new(nil, {}, resp.status, resp.body) }

Expand Down
46 changes: 1 addition & 45 deletions modules/ask_va_api/spec/services/crm/service_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,29 +5,14 @@
RSpec.describe Crm::Service do
let(:service) { described_class.new(icn: '123') }

# Helper method to create a mock response
def mock_response(status:, body:)
instance_double(Faraday::Response, status:, body: body.to_json)
end

# Shared examples for error handling
shared_examples 'error handling' do |status, message|
let(:body) { 'Sample error message' }

it "returns a formatted message for status #{status}" do
response = mock_response(status:, body:)
expected_error_message = "#{message} to #{endpoint}: \"#{body}\""

expect do
Crm::ErrorHandler.handle(endpoint, response)
end.to raise_error(Crm::ErrorHandler::ServiceError, expected_error_message)
end
end

describe '#call' do
let(:endpoint) { 'inquiries' }

context 'server response' do
context 'when server response successful' do
context 'with valid JSON' do
let(:response) do
mock_response(
Expand Down Expand Up @@ -64,35 +49,6 @@ def mock_response(status:, body:)
end
end

describe 'error message formatting' do
context 'when response is nil' do
it 'returns a message indicating no response was received' do
expect do
Crm::ErrorHandler.handle(endpoint,
nil)
end.to raise_error(Crm::ErrorHandler::ServiceError, "Server Error to #{endpoint}: ")
end
end

context 'with specific response status codes' do
include_examples 'error handling', 400, 'Bad request'
include_examples 'error handling', 401, 'Unauthorized'
include_examples 'error handling', 403, 'Forbidden: You do not have permission to access'
include_examples 'error handling', 404, 'Resource not found'
end

context 'with unspecified response status codes' do
let(:body) { 'General error message' }

it 'returns a generic error message' do
response = mock_response(status: 418, body:)
expect do
Crm::ErrorHandler.handle(endpoint, response)
end.to raise_error(Crm::ErrorHandler::ServiceError, "Service Error to #{endpoint}: \"#{body}\"")
end
end
end

context 'when the server returns an error' do
let(:resp) { mock_response(body: { error: 'server error' }, status: 500) }
let(:exception) { Common::Exceptions::BackendServiceException.new(nil, {}, resp.status, resp.body) }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,7 @@ class Vye::V1::VerificationsController < Vye::V1::ApplicationController
def create
authorize user_info, policy_class: UserInfoPolicy

award = user_info.awards.first
user_profile = user_info.user_profile
Verification.create!(source_ind:, award:, user_profile:)
user_info.verifications.create!(source_ind:)
end

private
Expand Down
12 changes: 1 addition & 11 deletions modules/vye/app/models/vye/address_change.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,17 +23,7 @@ class Vye::AddressChange < ApplicationRecord
presence: true, if: -> { origin == 'backend' }
)

enum origin: {

frontend: 'f',

# This is a special case where the record was created on the frontend
# but will not have been reflected from the backend yet
cached: 'c',

backend: 'b'

}
enum origin: { frontend: 'f', backend: 'b' }

scope :created_today, lambda {
includes(user_info: :user_profile)
Expand Down
1 change: 0 additions & 1 deletion modules/vye/app/models/vye/award.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
module Vye
class Vye::Award < ApplicationRecord
belongs_to :user_info
has_many :verifications, dependent: :nullify

enum cur_award_ind: { current: 'C', future: 'F', past: 'P' }

Expand Down
4 changes: 0 additions & 4 deletions modules/vye/app/models/vye/bdn_clone.rb

This file was deleted.

2 changes: 1 addition & 1 deletion modules/vye/app/models/vye/pending_document.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

module Vye
class Vye::PendingDocument < ApplicationRecord
self.ignored_columns += %i[claim_no_ciphertext encrypted_kms_key ssn_ciphertext ssn_digest]
self.ignored_columns += %i[claim_no_ciphertext ssn_ciphertext ssn_digest]

belongs_to :user_profile

Expand Down
11 changes: 6 additions & 5 deletions modules/vye/app/models/vye/user_info.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,13 @@ class Vye::UserInfo < ApplicationRecord
has_many :address_changes, dependent: :destroy
has_many :awards, dependent: :destroy
has_many :direct_deposit_changes, dependent: :destroy

scope :with_bdn_clone_active, -> { where(bdn_clone_active: true) }
has_many :verifications, dependent: :destroy

enum mr_status: { active: 'A', expired: 'E' }
enum indicator: { chapter1606: 'A', chapter1607: 'E', chapter30: 'B', D: 'D' }

delegate :icn, to: :user_profile, allow_nil: true
delegate :ssn, to: :mpi_profile, allow_nil: true
delegate :pending_documents, to: :user_profile
delegate :verifications, to: :user_profile
delegate :pending_documents, to: :user_profile, allow_nil: true

has_kms_key
has_encrypted(:dob, :file_number, :stub_nm, key: :kms_key, **lockbox_options)
Expand All @@ -46,6 +43,10 @@ def verification_required
verifications.empty?
end

def ssn
mpi_profile&.ssn
end

private

def mpi_profile
Expand Down
Loading

0 comments on commit 84e8fea

Please sign in to comment.