Skip to content

Commit

Permalink
Deepak/appeals 51312 (#22364)
Browse files Browse the repository at this point in the history
* Move update_document Feature Flag Logic

Move to wrap call to the legacy SOAP request. Previously, the
legacy request could still be used if users called update_document
directly, but this PR closes that loophole.

* Add Subject to Doc Update

* allow VeteranFileUpload to connect with CEAPI

* update response

* update specs

* fix

* add json adapter

* small fix

* Fix lint issues

* lint fix

* rubocop fix

* Updated ruby ce api gem

---------

Co-authored-by: Alexander Smith <alex.smith.99@thunderyard.com>
Co-authored-by: youfoundmanesh <maneshreddy.kommidi@va.gov>
  • Loading branch information
3 people committed Aug 1, 2024
1 parent 5cb618e commit 8dfd0d0
Show file tree
Hide file tree
Showing 6 changed files with 198 additions and 16 deletions.
2 changes: 1 addition & 1 deletion Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ GIT

GIT
remote: https://github.com/department-of-veterans-affairs/ruby_claim_evidence_api.git
revision: b1201d5ba234b798d1ab5845880aa9059f4fd176
revision: cdc4da3ae836dbd8fa2d6b26fbe726c2b587fd36
branch: feature/APPEALS-43121-efolder
specs:
ruby_claim_evidence_api (0.0.1)
Expand Down
69 changes: 57 additions & 12 deletions app/services/external_api/vbms_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -65,17 +65,59 @@ def self.update_document_in_vbms(appeal, uploadable_document)
update_document(appeal, uploadable_document)
end

# rubocop:disable Metrics/MethodLength
def self.upload_document_to_vbms(appeal, uploadable_document)
@vbms_client ||= init_vbms_client
response = initialize_upload(appeal, uploadable_document)
upload_document(appeal.veteran_file_number, response.upload_token, uploadable_document.pdf_location)
if FeatureToggle.enabled?(:use_ce_api)
filename = SecureRandom.uuid + File.basename(uploadable_document.pdf_location)
file_upload_payload = ClaimEvidenceFileUploadPayload.new(
content_name: filename,
content_source: uploadable_document.source,
date_va_received_document: Time.current.strftime("%Y-%m-%d"),
document_type_id: uploadable_document.document_type_id,
subject: uploadable_document.document_type,
new_mail: true
)
response = VeteranFileUploader.upload_veteran_file(
file_path: uploadable_document.pdf_location,
veteran_file_number: appeal.veteran_file_number,
doc_info: file_upload_payload
)
JsonApiResponseAdapter.new.adapt_upload_document(response)

else
@vbms_client ||= init_vbms_client
response = initialize_upload(appeal, uploadable_document)
upload_document(appeal.veteran_file_number, response.upload_token, uploadable_document.pdf_location)
end
end
# rubocop:enable Metrics/MethodLength

# rubocop:disable Metrics/MethodLength
def self.upload_document_to_vbms_veteran(veteran_file_number, uploadable_document)
@vbms_client ||= init_vbms_client
response = initialize_upload_veteran(veteran_file_number, uploadable_document)
upload_document(veteran_file_number, response.upload_token, uploadable_document.pdf_location)
if FeatureToggle.enabled?(:use_ce_api)
filename = SecureRandom.uuid + File.basename(uploadable_document.pdf_location)
file_upload_payload = ClaimEvidenceFileUploadPayload.new(
content_name: filename,
content_source: uploadable_document.source,
date_va_received_document: Time.current.strftime("%Y-%m-%d"),
document_type_id: uploadable_document.document_type_id,
subject: uploadable_document.document_subject.presence || uploadable_document.document_type,
new_mail: true
)

response = VeteranFileUploader.upload_veteran_file(
file_path: uploadable_document.pdf_location,
veteran_file_number: veteran_file_number,
doc_info: file_upload_payload
)
JsonApiResponseAdapter.new.adapt_upload_document(response)
else
@vbms_client ||= init_vbms_client
response = initialize_upload_veteran(veteran_file_number, uploadable_document)
upload_document(veteran_file_number, response.upload_token, uploadable_document.pdf_location)
end
end
# rubocop:enable Metrics/MethodLength

def self.initialize_upload(appeal, uploadable_document)
content_hash = Digest::SHA1.hexdigest(File.read(uploadable_document.pdf_location))
Expand Down Expand Up @@ -111,11 +153,13 @@ def self.initialize_upload_veteran(veteran_file_number, uploadable_document)
end

def self.upload_document(vbms_id, upload_token, filepath)
request = VBMS::Requests::UploadDocument.new(
upload_token: upload_token,
filepath: filepath
)
send_and_log_request(vbms_id, request)
if !FeatureToggle.enabled?(:use_ce_api)
request = VBMS::Requests::UploadDocument.new(
upload_token: upload_token,
filepath: filepath
)
send_and_log_request(vbms_id, request)
end
end

def self.initialize_update(appeal, uploadable_document)
Expand All @@ -142,11 +186,12 @@ def self.update_document(appeal, uploadable_document)

file_uuid = uploadable_document.document_series_reference_id.delete("{}")

VeteranFileUpdater.update_veteran_file(
response = VeteranFileUpdater.update_veteran_file(
veteran_file_number: appeal.veteran_file_number,
file_uuid: file_uuid,
file_update_payload: file_update_payload
)
JsonApiResponseAdapter.new.adapt_update_document(response)
else
@vbms_client ||= init_vbms_client
response = initialize_update(appeal, uploadable_document)
Expand Down
26 changes: 26 additions & 0 deletions app/services/json_api_response_adapter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,14 @@ def adapt_fetch_document_series_for(json_response)
documents
end

def adapt_upload_document(json_response)
document_upload_response(JSON.parse(json_response.body))
end

def adapt_update_document(json_response)
document_update_response(JSON.parse(json_response.body))
end

private

def valid_json_response?(json_response)
Expand Down Expand Up @@ -45,4 +53,22 @@ def fetch_document_series_for_response(file_json)
upload_date: system_data["uploadedDateTime"]
)
end

def document_upload_response(file_json)
OpenStruct.new(
upload_document_response: {
"@new_document_version_ref_id": file_json["currentVersionUuid"],
"@document_series_ref_id": file_json["uuid"]
}
)
end

def document_update_response(file_json)
OpenStruct.new(
update_document_response: {
"@new_document_version_ref_id": file_json["currentVersionUuid"],
"@document_series_ref_id": file_json["uuid"]
}
)
end
end
3 changes: 3 additions & 0 deletions config/initializers/ruby_claim_evidence_api.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,6 @@

VeteranFileUpdater = ExternalApi::VeteranFileUpdater
.new(use_canned_api_responses: ApplicationController.dependencies_faked?, logger: Rails.logger)

VeteranFileUploader = ExternalApi::VeteranFileUploader
.new(use_canned_api_responses: ApplicationController.dependencies_faked?, logger: Rails.logger)
80 changes: 77 additions & 3 deletions spec/services/external_api/vbms_service_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,17 @@

describe ExternalApi::VBMSService do
subject(:described) { described_class }
let(:mock_json_adapter) { instance_double(JsonApiResponseAdapter) }
before do
allow(JsonApiResponseAdapter).to receive(:new).and_return(mock_json_adapter)
end

describe ".fetch_document_series_for" do
let(:mock_json_adapter) { instance_double(JsonApiResponseAdapter) }
let(:mock_vbms_document_series_for_appeal) { instance_double(ExternalApi::VbmsDocumentSeriesForAppeal) }

let!(:appeal) { create(:appeal) }

before do
allow(JsonApiResponseAdapter).to receive(:new).and_return(mock_json_adapter)
allow(ExternalApi::VbmsDocumentSeriesForAppeal).to receive(:new).and_return(mock_vbms_document_series_for_appeal)
end

Expand Down Expand Up @@ -86,7 +88,8 @@
document_type_id: 1,
pdf_location: "/path/to/test/location",
source: "my_source",
document_series_reference_id: "{12345}"
document_series_reference_id: "{12345}",
document_subject: "testing1"
)
end
let(:appeal) { create(:appeal) }
Expand All @@ -106,6 +109,7 @@
file_uuid: "12345",
file_update_payload: mock_file_update_payload
)
expect(mock_json_adapter).to receive(:adapt_update_document)

described.update_document_in_vbms(appeal, fake_document)
end
Expand All @@ -125,4 +129,74 @@
end
end
end

describe ExternalApi::VBMSService do
describe ".upload_document_to_vbms" do
let(:fake_document) do
instance_double(
"UploadDocumentToVbms",
pdf_location: "/path/to/test/location",
source: "my_source",
document_type_id: 1,
document_type: "test",
subject: "testing1",
new_mail: true
)
end
let(:appeal) { create(:appeal) }

context "with use_ce_api feature toggle enabled" do
before { FeatureToggle.enable!(:use_ce_api) }
after { FeatureToggle.disable!(:use_ce_api) }

let(:mock_file_upload_payload) { instance_double("ClaimEvidenceFileUploadPayload") }

it "calls the CE API" do
allow(SecureRandom).to receive(:uuid).and_return("12345")
# rubocop:disable Rails/TimeZone
allow(Time).to receive(:current).and_return(Time.parse("2024-07-26"))
# rubocop:enable Rails/TimeZone
filename = "12345location"

expect(ClaimEvidenceFileUploadPayload).to receive(:new).with(
content_name: filename,
content_source: fake_document.source,
date_va_received_document: "2024-07-26",
document_type_id: fake_document.document_type_id,
subject: fake_document.document_type,
new_mail: true
).and_return(mock_file_upload_payload)

expect(VeteranFileUploader).to receive(:upload_veteran_file).with(
file_path: fake_document.pdf_location,
veteran_file_number: appeal.veteran_file_number,
doc_info: mock_file_upload_payload
)
expect(mock_json_adapter).to receive(:adapt_upload_document)
described_class.upload_document_to_vbms(appeal, fake_document)
end
end

context "with use_ce_api feature toggle disabled" do
before { FeatureToggle.disable!(:use_ce_api) }

let(:mock_vbms_client) { instance_double("VBMS::Client") }
let(:mock_initialize_upload_response) { double(upload_token: "document-token") }

it "calls the VBMS client" do
allow(described_class).to receive(:init_vbms_client).and_return(mock_vbms_client)
allow(described_class).to receive(:initialize_upload)
.with(appeal, fake_document).and_return(mock_initialize_upload_response)
allow(described_class).to receive(:upload_document)
.with(appeal.veteran_file_number, "document-token", fake_document.pdf_location)

described_class.upload_document_to_vbms(appeal, fake_document)

expect(described_class).to have_received(:initialize_upload).with(appeal, fake_document)
expect(described_class).to have_received(:upload_document)
.with(appeal.veteran_file_number, "document-token", fake_document.pdf_location)
end
end
end
end
end
34 changes: 34 additions & 0 deletions spec/services/json_api_response_adapter_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -51,4 +51,38 @@
expect(parsed[1].mime_type).to eq "application/pdf"
end
end

describe "adapt_upload_document" do
it "correctly parses an API response" do
data_hash = {
"currentVersionUuid": "7D6AFD8C-3BF7-4224-93AE-E1F07AC43C71",
"uuid": "03223945-468B-4E8A-B79B-82FA73C2D2D9"
}.to_json

expect(api_response).to receive(:body).and_return(data_hash)

parsed = described.adapt_upload_document(api_response)

expect(parsed[:upload_document_response][:@new_document_version_ref_id])
.to eq "7D6AFD8C-3BF7-4224-93AE-E1F07AC43C71"
expect(parsed[:upload_document_response][:@document_series_ref_id]).to eq "03223945-468B-4E8A-B79B-82FA73C2D2D9"
end
end

describe "adapt_update_document" do
it "correctly parses an API response" do
data_hash = {
"currentVersionUuid": "7D6AFD8C-3BF7-4224-93AE-E1F07AC43C71",
"uuid": "03223945-468B-4E8A-B79B-82FA73C2D2D9"
}.to_json

expect(api_response).to receive(:body).and_return(data_hash)

parsed = described.adapt_update_document(api_response)

expect(parsed[:update_document_response][:@new_document_version_ref_id])
.to eq "7D6AFD8C-3BF7-4224-93AE-E1F07AC43C71"
expect(parsed[:update_document_response][:@document_series_ref_id]).to eq "03223945-468B-4E8A-B79B-82FA73C2D2D9"
end
end
end

0 comments on commit 8dfd0d0

Please sign in to comment.