Skip to content

Commit

Permalink
Fetch a CARMA access token only once per user submission (#4527)
Browse files Browse the repository at this point in the history
* Fetch a CARMA access token only once per user submission

* remove interal (private) carma client in processable models
  • Loading branch information
kevinmirc authored Jul 14, 2020
1 parent 27fb32c commit a9fd7be
Show file tree
Hide file tree
Showing 9 changed files with 65 additions and 63 deletions.
14 changes: 11 additions & 3 deletions app/services/form1010cg/service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ def process_claim!

assert_veteran_status

carma_submission = CARMA::Models::Submission.from_claim(claim, build_metadata).submit!
carma_submission = CARMA::Models::Submission.from_claim(claim, build_metadata).submit!(carma_client)

@submission = Form1010cg::Submission.new(
carma_case_id: carma_submission.carma_case_id,
Expand Down Expand Up @@ -75,7 +75,7 @@ def submit_attachment # rubocop:disable Metrics/MethodLength

carma_attachments.add(CARMA::Models::Attachment::DOCUMENT_TYPES['10-10CG'], file_path)

carma_attachments.submit!
carma_attachments.submit!(carma_client)
submission.attachments = carma_attachments.to_hash
rescue
# The end-user doesn't know an attachment is being sent with the submission at all. The PDF we're
Expand Down Expand Up @@ -164,7 +164,7 @@ def is_veteran(form_subject) # rubocop:disable Naming/PredicateName
icn = icn_for(form_subject)
return @cache[:veteran_statuses][form_subject] = false if icn == NOT_FOUND

response = EMIS::VeteranStatusService.new.get_veteran_status(icn: icn)
response = emis_service.get_veteran_status(icn: icn)

is_veteran = response&.items&.first&.title38_status_code == 'V1'

Expand All @@ -177,6 +177,14 @@ def mvi_service
@mvi_service ||= MVI::Service.new
end

def carma_client
@carma_client ||= CARMA::Client::Client.new
end

def emis_service
@emis_service ||= EMIS::VeteranStatusService.new
end

# MVI::Service requires a valid UserIdentity to run a search, but only reads the user's attributes.
# This method will build a valid UserIdentity, so MVI::Service can pluck the name, ssn, dob, and gender.
#
Expand Down
25 changes: 13 additions & 12 deletions lib/carma/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,23 +28,24 @@ submission.metadata = {
}
}
submission.submit!
submission.submit!(CARMA::Client::Client.new)
```

### Submission with Attachments
```
claim = SavedClaim::CaregiversAssistanceClaim.new
submission = CARMA::Models::Submission.from_claim(
claim,
{
veteran: {
icn: '1234',
is_veteran: true
carma_client = CARMA::Client::Client.new
claim = SavedClaim::CaregiversAssistanceClaim.new
submission = CARMA::Models::Submission.from_claim(
claim,
{
veteran: {
icn: '1234',
is_veteran: true
}
}
}
)
)
submission.submit!
submission.submit!(carma_client)
attachments = CARMA::Models::Attachments.new(
submission.carma_case_id,
Expand All @@ -55,5 +56,5 @@ attachments = CARMA::Models::Attachments.new(
attachments.add('10-10CG', 'tmp/pdfs/10-10CG-claim-guid.pdf')
attachments.add('POA', 'tmp/pdfs/POA-claim-guid.pdf')
attachments.submit!
attachments.submit!(carma_client)
```
8 changes: 3 additions & 5 deletions lib/carma/client/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@
module CARMA
module Client
class Client < Salesforce::Service
include Singleton

configuration CARMA::Client::Configuration

STATSD_KEY_PREFIX = 'api.carma'
Expand All @@ -18,7 +16,7 @@ class Client < Salesforce::Service

def create_submission(payload)
with_monitoring do
client.post(
restforce.post(
'/services/apexrest/carma/v1/1010-cg-submissions',
payload,
'Content-Type': 'application/json',
Expand All @@ -42,7 +40,7 @@ def create_submission_stub(_payload)

def upload_attachments(payload)
with_monitoring do
client.post(
restforce.post(
'/services/data/v47.0/composite/tree/ContentVersion',
payload,
'Content-Type': 'application/json'
Expand All @@ -65,7 +63,7 @@ def upload_attachments_stub(_payload)

private

def client
def restforce
@client ||= get_client
end
end
Expand Down
8 changes: 1 addition & 7 deletions lib/carma/models/attachments.rb
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ def to_request_payload
}
end

def submit!
def submit!(client)
return response if response

@response = if Flipper.enabled?(:stub_carma_responses)
Expand Down Expand Up @@ -71,12 +71,6 @@ def to_hash
data: all.map(&:to_hash)
}
end

private

def client
CARMA::Client::Client.instance
end
end
end
end
8 changes: 1 addition & 7 deletions lib/carma/models/submission.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ def initialize(args = {})
self.metadata = args[:metadata] || {}
end

def submit!
def submit!(client)
raise 'This submission has already been submitted to CARMA' if submitted?

response = if Flipper.enabled?(:stub_carma_responses)
Expand All @@ -64,12 +64,6 @@ def submitted?
def metadata=(metadata_hash)
@metadata = Metadata.new(metadata_hash)
end

private

def client
CARMA::Client::Client.instance
end
end
end
end
16 changes: 8 additions & 8 deletions spec/lib/carma/client/client_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
restforce_client = double
response_double = double

expect(described_class.instance).to receive(:client).and_return(restforce_client)
expect(subject).to receive(:get_client).and_return(restforce_client)
expect(restforce_client).to receive(:post).with(
'/services/apexrest/carma/v1/1010-cg-submissions',
payload,
Expand All @@ -31,7 +31,7 @@

expect(response_double).to receive(:body).and_return(:response_token)

response = described_class.instance.create_submission(payload)
response = subject.create_submission(payload)
expect(response).to eq(:response_token)
end
end
Expand All @@ -40,8 +40,8 @@
timestamp = DateTime.parse('2020-03-09T06:48:59-04:00')

it 'returns a hard coded response', run_at: timestamp.iso8601 do
expect(described_class.instance).not_to receive(:client)
response = described_class.instance.create_submission_stub(nil)
expect(subject).not_to receive(:get_client)
response = subject.create_submission_stub(nil)

expect(response['message']).to eq('Application Received')
expect(response['data']).to be_present
Expand All @@ -57,7 +57,7 @@
restforce_client = double
response_double = double

expect(described_class.instance).to receive(:client).and_return(restforce_client)
expect(subject).to receive(:get_client).and_return(restforce_client)

expect(restforce_client).to receive(:post).with(
'/services/data/v47.0/composite/tree/ContentVersion',
Expand All @@ -69,15 +69,15 @@

expect(response_double).to receive(:body).and_return(:response_token)

response = described_class.instance.upload_attachments(payload)
response = subject.upload_attachments(payload)
expect(response).to eq(:response_token)
end
end

describe '#upload_attachments_stub' do
it 'returns a hard coded response' do
expect(described_class.instance).not_to receive(:client)
response = described_class.instance.upload_attachments_stub(nil)
expect(subject).not_to receive(:get_client)
response = subject.upload_attachments_stub(nil)

expect(response).to eq(
{
Expand Down
28 changes: 16 additions & 12 deletions spec/lib/carma/models/attachments_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -118,14 +118,15 @@
]
}

expect(CARMA::Client::Client.instance).not_to receive(:upload_attachments)
expect(CARMA::Client::Client.instance).to receive(:upload_attachments_stub).with(
carma_client = double
expect(carma_client).not_to receive(:upload_attachments)
expect(carma_client).to receive(:upload_attachments_stub).with(
expected_request_payload
).and_return(
expected_response
)

subject.submit!
subject.submit!(carma_client)

expect(subject.response).to eq(expected_response)
expect(subject.has_errors).to eq(false)
Expand Down Expand Up @@ -169,14 +170,15 @@
]
}

expect(CARMA::Client::Client.instance).not_to receive(:upload_attachments)
expect(CARMA::Client::Client.instance).to receive(:upload_attachments_stub).with(
carma_client = double
expect(carma_client).not_to receive(:upload_attachments)
expect(carma_client).to receive(:upload_attachments_stub).with(
expected_request_payload
).and_return(
expected_response
)

5.times { subject.submit! }
5.times { subject.submit!(carma_client) }

expect(subject.response).to eq(expected_response)
expect(subject.has_errors).to eq(false)
Expand Down Expand Up @@ -222,14 +224,15 @@
]
}

expect(CARMA::Client::Client.instance).not_to receive(:upload_attachments_stub)
expect(CARMA::Client::Client.instance).to receive(:upload_attachments).with(
carma_client = double
expect(carma_client).not_to receive(:upload_attachments_stub)
expect(carma_client).to receive(:upload_attachments).with(
expected_request_payload
).and_return(
expected_response
)

subject.submit!
subject.submit!(carma_client)

expect(subject.response).to eq(expected_response)
expect(subject.has_errors).to eq(false)
Expand Down Expand Up @@ -273,14 +276,15 @@
]
}

expect(CARMA::Client::Client.instance).not_to receive(:upload_attachments_stub)
expect(CARMA::Client::Client.instance).to receive(:upload_attachments).with(
carma_client = double
expect(carma_client).not_to receive(:upload_attachments_stub)
expect(carma_client).to receive(:upload_attachments).with(
expected_request_payload
).and_return(
expected_response
)

5.times { subject.submit! }
5.times { subject.submit!(carma_client) }

expect(subject.response).to eq(expected_response)
expect(subject.has_errors).to eq(false)
Expand Down
16 changes: 9 additions & 7 deletions spec/lib/carma/models/submission_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -325,7 +325,7 @@
submission.submitted_at = DateTime.now.iso8601
submission.carma_case_id = 'aB935000000A9GoCAK'

expect { submission.submit! }.to raise_error('This submission has already been submitted to CARMA')
expect { submission.submit!(double) }.to raise_error('This submission has already been submitted to CARMA')
end
end

Expand All @@ -342,17 +342,18 @@

expect(Flipper).to receive(:enabled?).with(:stub_carma_responses).and_return(false)

expect(CARMA::Client::Client.instance).not_to receive(:create_submission_stub)
carma_client = double
expect(carma_client).not_to receive(:create_submission_stub)

expect(submission.carma_case_id).to eq(nil)
expect(submission.submitted_at).to eq(nil)
expect(submission.submitted?).to eq(false)

expect(CARMA::Client::Client.instance).to receive(:create_submission).and_return(
expect(carma_client).to receive(:create_submission).and_return(
expected_response
)

submission.submit!
submission.submit!(carma_client)

expect(submission.carma_case_id).to eq(expected_response['data']['carmacase']['id'])
expect(submission.submitted_at).to eq(expected_response['data']['carmacase']['createdAt'])
Expand All @@ -375,8 +376,9 @@

expect(submission).to receive(:to_request_payload).and_return(:REQUEST_PAYLOAD)

expect(CARMA::Client::Client.instance).not_to receive(:create_submission)
expect(CARMA::Client::Client.instance).to receive(
carma_client = double
expect(carma_client).not_to receive(:create_submission)
expect(carma_client).to receive(
:create_submission_stub
).with(
:REQUEST_PAYLOAD
Expand All @@ -388,7 +390,7 @@
expect(submission.submitted_at).to eq(nil)
expect(submission.submitted?).to eq(false)

submission.submit!
submission.submit!(carma_client)

expect(submission.carma_case_id).to eq(expected_response['data']['carmacase']['id'])
expect(submission.submitted_at).to eq(expected_response['data']['carmacase']['createdAt'])
Expand Down
5 changes: 3 additions & 2 deletions spec/services/form1010cg/service_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -496,14 +496,16 @@
)
)

emis_service = double
expect(EMIS::VeteranStatusService).to receive(:new).with(no_args).and_return(emis_service)

# Only two calls should be made to eMIS for the six calls of :is_veteran below
2.times do |index|
expected_form_subject = index.zero? ? 'veteran' : 'primaryCaregiver'
expected_icn = "ICN_#{index}".to_sym

expect(subject).to receive(:icn_for).with(expected_form_subject).and_return(expected_icn)

emis_service = double
emis_response_title38_value = index.zero? ? 'V1' : 'V4'
emis_response = double(
error?: false,
Expand All @@ -514,7 +516,6 @@
]
)

expect(EMIS::VeteranStatusService).to receive(:new).with(no_args).and_return(emis_service)
expect(emis_service).to receive(:get_veteran_status).with(
icn: expected_icn
).and_return(
Expand Down

0 comments on commit a9fd7be

Please sign in to comment.