Skip to content

Commit

Permalink
[Automated] Merged master into target k8s
Browse files Browse the repository at this point in the history
  • Loading branch information
va-vsp-bot authored Apr 15, 2024
2 parents 1ee5397 + 9e780a2 commit 83b5a4d
Show file tree
Hide file tree
Showing 15 changed files with 337 additions and 185 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@ class ApplicationController < ::ApplicationController

def handle_exceptions
yield
rescue AskVAApi::V0::InquiriesController::InvalidInquiryError => e
log_and_render_error('invalid_inquiry_error', e, :bad_request)
rescue ErrorHandler::ServiceError, Crm::ErrorHandler::ServiceError => e
log_and_render_error('service_error', e, :unprocessable_entity)
rescue => e
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,14 +59,11 @@ def inquiry_params

def get_inquiry_by_id
inq = retriever.fetch_by_id(id: params[:id])

raise InvalidInquiryError if inq.is_a?(Hash)

@inquiry = Result.new(payload: Inquiries::Serializer.new(inq).serializable_hash, status: :ok)
end

def get_inquiries_by_icn
inquiries = retriever.fetch_by_icn
inquiries = retriever.call
@user_inquiries = Result.new(payload: Inquiries::Serializer.new(inquiries).serializable_hash, status: :ok)
end

Expand All @@ -89,11 +86,11 @@ def mock_service
end

def retriever
@retriever ||= Inquiries::Retriever.new(icn: current_user.icn, service: mock_service)
entity_class = AskVAApi::Inquiries::Entity
@retriever ||= Inquiries::Retriever.new(icn: current_user.icn, user_mock_data: params[:mock], entity_class:)
end

Result = Struct.new(:payload, :status, keyword_init: true)
class InvalidInquiryError < StandardError; end
class InvalidAttachmentError < StandardError; end
end
end
Expand Down
7 changes: 5 additions & 2 deletions modules/ask_va_api/app/lib/ask_va_api/base_retriever.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,11 @@ def initialize(user_mock_data:, entity_class:)
end

def call
data_array = fetch_data
data_array.map { |item| entity_class.new(item) }
if fetch_data.is_a?(Array)
fetch_data.map { |item| entity_class.new(item) }
else
entity_class.new(fetch_data)
end
rescue => e
::ErrorHandler.handle_service_error(e)
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ module AskVAApi
module Correspondences
class Entity
attr_reader :id,
:inquiry_id,
:message_type,
:modified_on,
:status_reason,
Expand All @@ -14,7 +13,6 @@ class Entity

def initialize(info)
@id = info[:Id]
@inquiry_id = info[:InquiryId]
@message_type = info[:MessageType]
@modified_on = info[:ModifiedOn]
@status_reason = info[:StatusReason]
Expand Down
44 changes: 26 additions & 18 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,37 +2,45 @@

module AskVAApi
module Correspondences
ENDPOINT = 'replies'
class CorrespondencesRetrieverError < StandardError; end

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

def initialize(inquiry_id:, service: nil)
def initialize(inquiry_id:, user_mock_data:, entity_class:)
super(user_mock_data:, entity_class:)
@inquiry_id = inquiry_id
@service = service || default_service
end

def call
private

def fetch_data
validate_input(inquiry_id, 'Invalid Inquiry ID')
fetch_data(payload: { InquiryId: inquiry_id }).map do |cor|
Entity.new(cor)
if user_mock_data
data = File.read('modules/ask_va_api/config/locales/get_replies_mock_data.json')

data = JSON.parse(data, symbolize_names: true)[:Data]
filter_data(data)
else
endpoint = "inquiries/#{inquiry_id}/replies"

response = Crm::Service.new(icn: nil).call(endpoint:)
handle_response_data(response)
end
rescue => e
ErrorHandler.handle_service_error(e)
end

private

def default_service
Crm::Service.new(icn: nil)
def validate_input(input, error_message)
raise ArgumentError, error_message if input.blank?
end

def fetch_data(payload: {})
service.call(endpoint: ENDPOINT, payload:)[:Data]
def filter_data(data)
data.select do |cor|
cor[:InquiryId] == inquiry_id
end
end

def validate_input(input, error_message)
raise ArgumentError, error_message if input.blank?
def handle_response_data(response)
response[:Data].presence || raise(CorrespondencesRetrieverError, response[:Message])
end
end
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@ class Serializer < ActiveModel::Serializer
include JSONAPI::Serializer
set_type :correspondence

attributes :inquiry_id,
:message_type,
attributes :message_type,
:modified_on,
:status_reason,
:description,
Expand Down
62 changes: 36 additions & 26 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 @@ -2,51 +2,61 @@

module AskVAApi
module Inquiries
ENDPOINT = 'inquiries'
class InquiriesRetrieverError < StandardError; end

class Retriever
attr_reader :service, :icn
class Retriever < BaseRetriever
attr_reader :icn, :user_mock_data, :entity_class

def initialize(icn:, service: nil)
def initialize(user_mock_data:, entity_class:, icn: nil)
super(user_mock_data:, entity_class:)
@icn = icn
@service = service || default_service
end

def fetch_by_id(id:)
validate_input(id, 'Invalid ID')
reply = Correspondences::Retriever.new(inquiry_id: id, service:).call
data = fetch_data(payload: { id: })
return {} if data.blank?
inq = fetch_data(id)
reply = fetch_correspondences(inquiry_id: id)

Entity.new(data, reply)
entity_class.new(inq.first, reply)
rescue => e
ErrorHandler.handle_service_error(e)
::ErrorHandler.handle_service_error(e)
end

def fetch_by_icn
validate_input(icn, 'Invalid ICN')
data = fetch_data(payload: { icn: })
if data.empty?
data
private

def fetch_data(id = nil)
if user_mock_data
data = read_mock_data('get_inquiries_mock_data.json')
filter_data(data, id)
else
data.map { |inq| Entity.new(inq) }
endpoint = 'inquiries'
payload = id ? { icn: } : { id: }

response = Crm::Service.new(icn:).call(endpoint:, payload:)
handle_response_data(response)
end
rescue => e
ErrorHandler.handle_service_error(e)
end

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

def default_service
Crm::Service.new(icn:)
def read_mock_data(file_name)
data = File.read("modules/ask_va_api/config/locales/#{file_name}")
JSON.parse(data, symbolize_names: true)[:Data]
end

def fetch_data(payload: {})
service.call(endpoint: ENDPOINT, payload:)[:Data]
def filter_data(data, id = nil)
data.select do |inq|
id ? inq[:InquiryNumber] == id : inq[:Icn] == icn
end
end

def validate_input(input, error_message)
raise ArgumentError, error_message if input.blank?
def handle_response_data(response)
response[:Data].presence || raise(InquiriesRetrieverError, response[:Message])
end
end
end
Expand Down
22 changes: 11 additions & 11 deletions modules/ask_va_api/config/locales/get_replies_mock_data.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"Data": [
{
"InquiryId": "1",
"InquiryId": "A-1",
"Id": "1",
"ModifiedOn": "1/2/23",
"StatusReason": "Completed/Sent",
Expand All @@ -16,7 +16,7 @@
]
},
{
"InquiryId": "2",
"InquiryId": "A-2",
"Id": "2",
"ModifiedOn": "1/21/23",
"StatusReason": "Completed/Sent",
Expand All @@ -31,7 +31,7 @@
]
},
{
"InquiryId": "2",
"InquiryId": "A-2",
"Id": "3",
"ModifiedOn": "1/22/23",
"StatusReason": "Completed/Sent",
Expand All @@ -41,7 +41,7 @@
"AttachmentNames": []
},
{
"InquiryId": "2",
"InquiryId": "A-2",
"Id": "4",
"ModifiedOn": "1/23/23",
"StatusReason": "Completed/Sent",
Expand All @@ -51,7 +51,7 @@
"AttachmentNames": []
},
{
"InquiryId": "9",
"InquiryId": "A-9",
"Id": "5",
"ModifiedOn": "8/23/23",
"StatusReason": "Completed/Sent",
Expand All @@ -61,7 +61,7 @@
"AttachmentNames": []
},
{
"InquiryId": "9",
"InquiryId": "A-9",
"Id": "6",
"ModifiedOn": "8/24/23",
"StatusReason": "Completed/Sent",
Expand All @@ -71,7 +71,7 @@
"AttachmentNames": []
},
{
"InquiryId": "9",
"InquiryId": "A-9",
"Id": "7",
"ModifiedOn": "8/24/23",
"StatusReason": "Completed/Sent",
Expand All @@ -81,7 +81,7 @@
"AttachmentNames": []
},
{
"InquiryId": "9",
"InquiryId": "A-9",
"Id": "7",
"ModifiedOn": "8/25/23",
"StatusReason": "Completed/Sent",
Expand All @@ -91,7 +91,7 @@
"AttachmentNames": []
},
{
"InquiryId": "9",
"InquiryId": "A-9",
"Id": "8",
"ModifiedOn": "8/26/23",
"StatusReason": "Completed/Sent",
Expand All @@ -101,7 +101,7 @@
"AttachmentNames": []
},
{
"InquiryId": "9",
"InquiryId": "A-9",
"Id": "8",
"ModifiedOn": "8/26/23",
"StatusReason": "Completed/Sent",
Expand All @@ -111,7 +111,7 @@
"AttachmentNames": []
},
{
"InquiryId": "9",
"InquiryId": "A-9",
"Id": "9",
"ModifiedOn": "8/28/23",
"StatusReason": "Completed/Sent",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@

let(:info) do
{
InquiryId: '1',
Id: '1',
ModifiedOn: '1/2/23',
StatusReason: 'Completed/Sent',
Expand All @@ -26,7 +25,6 @@

it 'creates an correspondence' do
expect(correspondence).to have_attributes(
inquiry_id: info[:InquiryId],
id: info[:Id],
modified_on: info[:ModifiedOn],
status_reason: info[:StatusReason],
Expand Down
Loading

0 comments on commit 83b5a4d

Please sign in to comment.