|
3 | 3 | module V0 |
4 | 4 | class Form212680Controller < ApplicationController |
5 | 5 | include RetriableConcern |
6 | | - include PdfFill::Forms::FormHelper |
7 | | - |
8 | 6 | service_tag 'form-21-2680' |
9 | | - skip_before_action :authenticate, only: %i[download_pdf] |
| 7 | + skip_before_action :authenticate |
10 | 8 | before_action :load_user |
11 | 9 | before_action :check_feature_enabled |
12 | 10 |
|
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} |
14 | 29 | # Generate and download a pre-filled PDF with veteran sections (I-V) completed |
15 | 30 | # Physician sections (VI-VIII) are left blank for manual completion |
| 31 | + # |
16 | 32 | 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 |
26 | 38 |
|
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] |
31 | 45 | 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) |
38 | 47 | end |
39 | 48 |
|
40 | 49 | private |
41 | 50 |
|
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' |
56 | 53 | end |
57 | 54 |
|
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) |
64 | 57 | end |
65 | 58 |
|
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 |
71 | 62 |
|
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 |
76 | 66 |
|
77 | | - pdf_path |
| 67 | + def stats_key |
| 68 | + 'api.form212680' |
78 | 69 | end |
79 | 70 |
|
80 | 71 | def check_feature_enabled |
|
0 commit comments