Skip to content

Commit 86c7878

Browse files
voidspooksLinnJSannaswims
authored
form 21-2680 controller refactor and tests (#25320)
* form 21-2680 controller refactor and tests smaller change, better test undo conflicting changes refactor 21-2680 controller update comment Fix spec * fix lint * PR feedback --------- Co-authored-by: Justin Linn <justin@linnjs.com> Co-authored-by: Anna Carey <acarey@gmail.com>
1 parent f98ef4f commit 86c7878

File tree

9 files changed

+321
-199
lines changed

9 files changed

+321
-199
lines changed

app/controllers/v0/form212680_controller.rb

Lines changed: 44 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -3,78 +3,69 @@
33
module V0
44
class Form212680Controller < ApplicationController
55
include RetriableConcern
6-
include PdfFill::Forms::FormHelper
7-
86
service_tag 'form-21-2680'
9-
skip_before_action :authenticate, only: %i[download_pdf]
7+
skip_before_action :authenticate
108
before_action :load_user
119
before_action :check_feature_enabled
1210

13-
# POST /v0/form212680/download_pdf
11+
def create
12+
claim = saved_claim_class.new(form: filtered_params)
13+
Rails.logger.info "Begin ClaimGUID=#{claim.guid} Form=#{claim.class::FORM} UserID=#{current_user&.uuid}"
14+
if claim.save
15+
# NOTE: we are not calling process_attachments! because we are not submitting yet
16+
StatsD.increment("#{stats_key}.success")
17+
Rails.logger.info "Submitted job ClaimID=#{claim.confirmation_number} Form=#{claim.class::FORM} " \
18+
"UserID=#{current_user&.uuid}"
19+
render json: SavedClaimSerializer.new(claim)
20+
else
21+
StatsD.increment("#{stats_key}.failure")
22+
raise Common::Exceptions::ValidationErrors, claim
23+
end
24+
rescue JSON::ParserError
25+
raise Common::Exceptions::ParameterMissing, 'form'
26+
end
27+
28+
# get /v0/form212680/download_pdf/{guid}
1429
# Generate and download a pre-filled PDF with veteran sections (I-V) completed
1530
# Physician sections (VI-VIII) are left blank for manual completion
31+
#
1632
def download_pdf
17-
# using request.raw_post to avoid the middleware that transforms the JSON keys to snake case
18-
pdf_path = nil
19-
parsed_body = JSON.parse(request.raw_post)
20-
form_data = parsed_body['form']
21-
22-
raise Common::Exceptions::ParameterMissing, 'form' unless form_data
23-
24-
# Transform 3-character country codes to 2-character codes for PDF compatibility
25-
transform_country_codes!(form_data)
33+
claim = saved_claim_class.find_by!(guid: params[:guid])
34+
source_file_path = with_retries('Generate 21-2680 PDF') do
35+
claim.generate_prefilled_pdf
36+
end
37+
raise Common::Exceptions::InternalServerError, 'Failed to generate PDF' unless source_file_path
2638

27-
claim = create_claim_from_form_data(form_data)
28-
pdf_path = generate_and_send_pdf(claim)
29-
rescue JSON::ParserError
30-
raise Common::Exceptions::ParameterMissing, 'form'
39+
send_data File.read(source_file_path),
40+
filename: download_file_name(claim),
41+
type: 'application/pdf',
42+
disposition: 'attachment'
43+
rescue ActiveRecord::RecordNotFound
44+
raise Common::Exceptions::RecordNotFound, params[:guid]
3145
ensure
32-
# Delete the temporary PDF file
33-
begin
34-
File.delete(pdf_path) if pdf_path
35-
rescue Errno::ENOENT
36-
# Ignore if file doesn't exist
37-
end
46+
File.delete(source_file_path) if defined?(source_file_path) && source_file_path && File.exist?(source_file_path)
3847
end
3948

4049
private
4150

42-
def transform_country_codes!(form_data)
43-
# Transform claimant address country code
44-
claimant_address = form_data.dig('claimantInformation', 'address')
45-
if claimant_address&.key?('country')
46-
transformed_country = extract_country(claimant_address)
47-
claimant_address['country'] = transformed_country if transformed_country
48-
end
49-
50-
# Transform hospital address country code
51-
hospital_address = form_data.dig('additionalInformation', 'hospitalAddress')
52-
if hospital_address&.key?('country')
53-
transformed_country = extract_country(hospital_address)
54-
hospital_address['country'] = transformed_country if transformed_country
55-
end
51+
def short_name
52+
'house_bound_status_claim'
5653
end
5754

58-
def create_claim_from_form_data(form_data)
59-
form_body = form_data.to_json
60-
claim = SavedClaim::Form212680.new(form: form_body)
61-
raise(Common::Exceptions::ValidationErrors, claim) unless claim.save
62-
63-
claim
55+
def filtered_params
56+
params.require(:form)
6457
end
6558

66-
def generate_and_send_pdf(claim)
67-
pdf_path = with_retries('Generate 21-2680 PDF') do
68-
claim.generate_prefilled_pdf
69-
end
70-
file_data = File.read(pdf_path)
59+
def download_file_name(claim)
60+
"21-2680_#{claim.veteran_first_last_name.gsub(' ', '_')}.pdf"
61+
end
7162

72-
send_data file_data,
73-
filename: "VA_Form_21-2680_#{Time.current.strftime('%Y%m%d_%H%M%S')}.pdf",
74-
type: 'application/pdf',
75-
disposition: 'attachment'
63+
def saved_claim_class
64+
SavedClaim::Form212680
65+
end
7666

77-
pdf_path
67+
def stats_key
68+
'api.form212680'
7869
end
7970

8071
def check_feature_enabled

app/models/saved_claim/form212680.rb

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ def document_type
2828

2929
# Generate pre-filled PDF with veteran sections
3030
def generate_prefilled_pdf
31-
pdf_path = to_pdf(SecureRandom.uuid)
31+
pdf_path = to_pdf
3232

3333
# Update metadata to track PDF generation
3434
update_metadata_with_pdf_generation
@@ -60,6 +60,13 @@ def attachment_keys
6060
[].freeze
6161
end
6262

63+
def veteran_first_last_name
64+
full_name = parsed_form.dig('veteranInformation', 'fullName')
65+
return 'Veteran' unless full_name.is_a?(Hash)
66+
67+
"#{full_name['first']} #{full_name['last']}"
68+
end
69+
6370
private
6471

6572
def update_metadata_with_pdf_generation

app/swagger/swagger/requests/form212680.rb

Lines changed: 32 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,30 +6,53 @@ class Form212680
66
include Swagger::Blocks
77
FORM_ID = '21-2680'
88

9-
swagger_path '/v0/form212680/download_pdf' do
9+
swagger_path '/v0/form212680' do
1010
operation :post do
11-
extend Swagger::Responses::ValidationError
12-
extend Swagger::Responses::UnprocessableEntityError
13-
extend Swagger::Responses::InternalServerError
1411
extend Swagger::Responses::BadRequestError
1512
extend Swagger::Responses::RecordNotFoundError
13+
extend Swagger::Responses::SavedForm
14+
extend Swagger::Responses::UnprocessableEntityError
15+
extend Swagger::Responses::ValidationError
1616

1717
key :description,
18-
'Generate and download a pre-filled 21-2680 PDF form ' \
19-
'(Examination for Housebound Status or Permanent Need for Regular Aid and Attendance)'
20-
key :operationId, 'downloadForm212680Pdf'
18+
"Create a SavedClaim for #{FORM_ID}" \
19+
'(Examination for Housebound Status or Permanent Need for Regular Aid and Attendance),' \
20+
'to be used later for PDF generation'
21+
key :operationId, 'createForm212680Pdf'
2122
key :tags, %w[benefits_forms]
22-
key :produces, ['application/pdf', 'application/json']
23+
key :produces, ['application/json']
2324
parameter :optional_authorization
25+
2426
parameter do
2527
key :name, :form
2628
key :in, :body
27-
key :description, 'Form 21-2680 data for PDF generation'
29+
key :description, "Form #{FORM_ID} submission data"
2830
key :required, true
2931
schema do
3032
VetsJsonSchema::SCHEMAS[FORM_ID]['properties']
3133
end
3234
end
35+
end
36+
end
37+
38+
swagger_path '/v0/form212680/download_pdf/{guid}' do
39+
operation :get do
40+
extend Swagger::Responses::RecordNotFoundError
41+
42+
key :description, "Download the submitted #{FORM_ID} PDF form"
43+
key :operationId, 'downloadForm212680Pdf'
44+
key :tags, %w[benefits_forms]
45+
key :produces, ['application/pdf', 'application/json']
46+
47+
parameter :optional_authorization
48+
49+
parameter do
50+
key :name, 'guid'
51+
key :in, :path
52+
key :description, 'the guid from the form submission response'
53+
key :required, true
54+
key :type, :string
55+
end
3356

3457
response 200 do
3558
key :description, 'PDF file successfully generated and ready for download'

config/routes.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,9 +72,9 @@
7272
end
7373
end
7474

75-
resources :form212680, only: [] do
75+
resources :form212680, only: [:create] do
7676
collection do
77-
post :download_pdf
77+
get('download_pdf/:guid', action: :download_pdf, as: :download_pdf)
7878
end
7979
end
8080

0 commit comments

Comments
 (0)