Skip to content

Commit 9ad72b6

Browse files
committed
FDF 133550 forms service class separation
1 parent 21b9af7 commit 9ad72b6

File tree

11 files changed

+300
-213
lines changed

11 files changed

+300
-213
lines changed

config/settings.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -377,6 +377,9 @@ dhp:
377377
digital_forms_api:
378378
base_url: <%= ENV['digital_forms_api__base_url'] %>
379379
breakers_error_threshold: 80
380+
cache_ttl:
381+
schema: 1440 # minutes == 24 hours
382+
template: 1440 # minutes == 24 hours
380383
include_request: false
381384
jwt_secret: <%= ENV['digital_forms_api__jwt_secret'] %>
382385
mock: false

config/settings/development.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -377,6 +377,9 @@ dhp:
377377
digital_forms_api:
378378
base_url: https://digital-forms-api-fake.fake.bip.va.gov/api/v1/rest/
379379
breakers_error_threshold: 80
380+
cache_ttl:
381+
schema: 1
382+
template: 1
380383
include_request: false
381384
jwt_secret: fake-secret
382385
mock: false

config/settings/test.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -376,6 +376,9 @@ dhp:
376376
digital_forms_api:
377377
base_url: https://digital-forms-api-fake.fake.bip.va.gov/api/v1/rest/
378378
breakers_error_threshold: 80
379+
cache_ttl:
380+
schema: 1
381+
template: 1
379382
include_request: false
380383
jwt_secret: fake-secret
381384
mock: false

modules/digital_forms_api/app/controllers/digital_forms_api/submissions_controller.rb

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# frozen_string_literal: true
22

3-
require 'digital_forms_api/service/forms'
43
require 'digital_forms_api/service/submissions'
4+
require 'digital_forms_api/service/templates'
55

66
module DigitalFormsApi
77
# The Fully Digital Forms controller that handles fetching form submissions and templates
@@ -17,7 +17,7 @@ def show
1717
return render json: { error: 'Forbidden' }, status: :forbidden
1818
end
1919

20-
template = forms_service.template('21-686c')
20+
template = templates_service.template('21-686c')
2121
render json: { submission: submission.body['envelope']['payload'],
2222
template: template['formTemplate']['formTemplate']['21-686c'] }
2323
rescue Common::Client::Errors::ClientError => e
@@ -27,9 +27,6 @@ def show
2727
Rails.logger.error('Digital Forms API - error status from BIP Forms API', { status: e.status })
2828
render json: { error: 'Internal server error' }, status: :internal_server_error
2929
end
30-
rescue => e
31-
Rails.logger.error('Digital Forms API - unexpected error', { message: e.message })
32-
render json: { error: 'Internal server error' }, status: :internal_server_error
3330
end
3431

3532
private
@@ -40,8 +37,8 @@ def check_flipper_flag
4037
end
4138

4239
# Instantiate service for interacting with the /forms endpoints
43-
def forms_service
44-
DigitalFormsApi::Service::Forms.new
40+
def templates_service
41+
DigitalFormsApi::Service::Templates.new
4542
end
4643

4744
# Instantiate service for interacting with the /submissions endpoints

modules/digital_forms_api/lib/digital_forms_api/service/forms.rb

Lines changed: 0 additions & 77 deletions
This file was deleted.
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
# frozen_string_literal: true
2+
3+
require 'digital_forms_api/service/base'
4+
5+
module DigitalFormsApi
6+
module Service
7+
# Schemas API
8+
class Schemas < Base
9+
# Cache TTL values for form schemas
10+
CACHE_TTL = (Settings.digital_forms_api.cache_ttl.schema.to_i || 5).minutes
11+
12+
# Build the cache key for a given form template.
13+
def self.cache_key(form_id)
14+
"digital_forms_api:schema:#{form_id}"
15+
end
16+
17+
# @see #schema
18+
def self.get(form_id)
19+
new.schema(form_id)
20+
end
21+
22+
# GET a form schema (with caching)
23+
# Caches only the parsed response body to avoid persisting sensitive
24+
# request metadata (e.g., Authorization headers) from the Faraday::Env.
25+
def schema(form_id)
26+
cache_key = self.class.cache_key(form_id)
27+
28+
# @see DigitalFormsApi::Service::Base#context
29+
tags = { form_id: }
30+
@context = tags.merge(tags:)
31+
32+
Rails.cache.fetch(cache_key, expires_in: CACHE_TTL, race_condition_ttl: 10.seconds) do
33+
perform(:get, "forms/#{form_id}/schema", {}, {}).body
34+
end
35+
end
36+
37+
private
38+
39+
# @see DigitalFormsApi::Service::Base#endpoint
40+
def endpoint
41+
'schemas'
42+
end
43+
44+
# end Schemas
45+
end
46+
47+
# end Service
48+
end
49+
50+
# end DigitalFormsApi
51+
end
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
# frozen_string_literal: true
2+
3+
require 'digital_forms_api/service/base'
4+
5+
module DigitalFormsApi
6+
module Service
7+
# Templates API
8+
class Templates < Base
9+
# Cache TTL values for form templates
10+
CACHE_TTL = (Settings.digital_forms_api.cache_ttl.template.to_i || 5).minutes
11+
12+
# Build the cache key for a given form template.
13+
def self.cache_key(form_id)
14+
"digital_forms_api:template:#{form_id}"
15+
end
16+
17+
# @see #template
18+
def self.get(form_id)
19+
new.template(form_id)
20+
end
21+
22+
# GET a form template (with caching)
23+
# Caches only the parsed response body to avoid persisting sensitive
24+
# request metadata (e.g., Authorization headers) from the Faraday::Env.
25+
def template(form_id)
26+
cache_key = self.class.cache_key(form_id)
27+
28+
# @see DigitalFormsApi::Service::Base#context
29+
tags = { form_id: }
30+
@context = tags.merge(tags:)
31+
32+
Rails.cache.fetch(cache_key, expires_in: CACHE_TTL, race_condition_ttl: 10.seconds) do
33+
perform(:get, "forms/#{form_id}/template", {}, {}).body
34+
end
35+
end
36+
37+
private
38+
39+
# @see DigitalFormsApi::Service::Base#endpoint
40+
def endpoint
41+
'templates'
42+
end
43+
44+
# end Templates
45+
end
46+
47+
# end Service
48+
end
49+
50+
# end DigitalFormsApi
51+
end

modules/digital_forms_api/spec/controllers/digital_forms_api/submissions_controller_spec.rb

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,5 +101,16 @@ def retrieve_submission!
101101
expect(response).to have_http_status(:forbidden)
102102
end
103103
end
104+
105+
context 'when a non-FormsAPI error occurs' do
106+
let(:cassette) { 'retrieve_686c' }
107+
108+
it 'returns a 500 error' do
109+
expect_any_instance_of(DigitalFormsApi::SubmissionsController).to receive(:show).and_raise RuntimeError
110+
111+
retrieve_submission!
112+
expect(response).to have_http_status(:internal_server_error)
113+
end
114+
end
104115
end
105116
end

modules/digital_forms_api/spec/lib/digital_forms_api/service/forms_spec.rb

Lines changed: 0 additions & 129 deletions
This file was deleted.

0 commit comments

Comments
 (0)