Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Log whenever Form Upload Flow prefill data is changed #20030

Merged
merged 6 commits into from
Jan 6, 2025
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ module V1
class ScannedFormUploadsController < ApplicationController
def submit
Datadog::Tracing.active_trace&.set_tag('form_id', params[:form_number])
check_for_changes

render json: upload_response
end

Expand Down Expand Up @@ -49,13 +51,11 @@ def find_attachment_path(confirmation_code)

def validated_metadata
raw_metadata = {
'veteranFirstName' => @current_user.first_name,
'veteranLastName' => @current_user.last_name,
'fileNumber' => params.dig(:options, :ssn) ||
params.dig(:options, :va_file_number) ||
@current_user.ssn,
'zipCode' => params.dig(:options, :zip_code) ||
@current_user.address[:postal_code],
'veteranFirstName' => params.dig(:form_data, :full_name, :first),
'veteranLastName' => params.dig(:form_data, :full_name, :last),
'fileNumber' => params.dig(:form_data, :id_number, :ssn) ||
params.dig(:form_data, :id_number, :va_file_number),
'zipCode' => params.dig(:form_data, :postal_code),
'source' => 'VA Platform Digital Forms',
'docType' => params[:form_number],
'businessLine' => 'CMP'
Expand Down Expand Up @@ -103,6 +103,16 @@ def perform_pdf_upload(location, file_path, metadata)
upload_url: location
)
end

def check_for_changes
in_progress_form = InProgressForm.form_for_user('FORM-UPLOAD-FLOW', @current_user)
if in_progress_form
prefill_data_service = SimpleFormsApi::PrefillDataService.new(prefill_data: in_progress_form.form_data,
form_data: params[:form_data],
LindseySaari marked this conversation as resolved.
Show resolved Hide resolved
form_id: params[:form_number])
prefill_data_service.check_for_changes
end
end
end
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# frozen_string_literal: true

module SimpleFormsApi
class PrefillDataService
attr_reader :prefill_data, :form_data, :form_id

def initialize(prefill_data:, form_data:, form_id:)
@prefill_data = JSON.parse(prefill_data)
@form_data = form_data
@form_id = form_id
end

def check_for_changes
LindseySaari marked this conversation as resolved.
Show resolved Hide resolved
changed_fields = []
changed_fields << 'first_name' if prefill_data.dig('full_name', 'first') != form_data.dig('full_name', 'first')
LindseySaari marked this conversation as resolved.
Show resolved Hide resolved
changed_fields << 'last_name' if prefill_data.dig('full_name', 'last') != form_data.dig('full_name', 'last')
changed_fields << 'postal_code' if prefill_data.dig('address', 'postal_code') != form_data['postal_code']
changed_fields << 'ssn' if prefill_data.dig('veteran', 'ssn') != form_data.dig('id_number', 'ssn')
changed_fields << 'email' if prefill_data['email'] != form_data['email']

changed_fields.each do |field|
Rails.logger.info('Simple forms api - Form Upload Flow changed data', { field:, form_id: })
end
end
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,27 @@
end

describe '#submit' do
let(:form_number) { '21-0779' }
let(:metadata_file) { "#{file_seed}.SimpleFormsApi.metadata.json" }
let(:file_seed) { 'tmp/some-unique-simple-forms-file-seed' }
let(:random_string) { 'some-unique-simple-forms-file-seed' }
let(:pdf_path) { Rails.root.join('spec', 'fixtures', 'files', 'doctors-note.pdf') }
let(:pdf_stamper) { double(stamp_pdf: nil) }
let(:confirmation_code) { 'a-random-guid' }
let(:attachment) { double }
let(:params) do
{ form_number:, confirmation_code:, form_data: {
full_name: {
first: 'fake-first-name',
last: 'fake-last-name'
},
postal_code: '12345',
id_number: {
ssn: '444444444'
},
email: 'fake-email'
} }
end

before do
VCR.insert_cassette('lighthouse/benefits_intake/200_lighthouse_intake_upload_location')
Expand All @@ -40,15 +54,35 @@
it 'makes the request' do
expect(PersistentAttachment).to receive(:find_by).with(guid: confirmation_code).and_return(attachment)

post '/simple_forms_api/v1/submit_scanned_form', params: { form_number: '21-0779', confirmation_code: }
post '/simple_forms_api/v1/submit_scanned_form', params: params

expect(response).to have_http_status(:ok)
end

it 'stamps the pdf' do
expect(pdf_stamper).to receive(:stamp_pdf)

post '/simple_forms_api/v1/submit_scanned_form', params: { form_number: '21-0779', confirmation_code: }
post '/simple_forms_api/v1/submit_scanned_form', params: params

expect(response).to have_http_status(:ok)
end

it 'checks if the prefill data has been changed' do
prefill_data = double
prefill_data_service = double
in_progress_form = double(form_data: prefill_data)

allow(SimpleFormsApi::PrefillDataService).to receive(:new).with(
prefill_data:,
form_data: hash_including(:email),
form_id: form_number
).and_return(prefill_data_service)
allow(InProgressForm).to receive(:form_for_user).with('FORM-UPLOAD-FLOW',
anything).and_return(in_progress_form)

expect(prefill_data_service).to receive(:check_for_changes)

post '/simple_forms_api/v1/submit_scanned_form', params: params

expect(response).to have_http_status(:ok)
end
Expand Down
113 changes: 113 additions & 0 deletions modules/simple_forms_api/spec/services/prefill_data_service_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
# frozen_string_literal: true

require 'rails_helper'
require SimpleFormsApi::Engine.root.join('spec', 'spec_helper.rb')

describe SimpleFormsApi::PrefillDataService do
describe '#check_for_changes' do
let(:form_id) { '21-0779' }
let(:rails_logger) { double }
let(:prefill_data) do
{
'full_name' => {
'first' => 'fake-first-name',
'last' => 'fake-last-name'
},
'address' => {
'postal_code' => '12345'
},
'veteran' => {
'ssn' => 'fake-ssn'
},
'email' => 'fake-email'
}
end
let(:form_data) do
{
'full_name' => {
'first' => 'fake-first-name',
'last' => 'fake-last-name'
},
'postal_code' => '12345',
'id_number' => {
'ssn' => 'fake-ssn'
},
'email' => 'fake-email'
}
end
let(:prefill_data_service) do
SimpleFormsApi::PrefillDataService.new(
prefill_data: prefill_data.to_json, form_data: modified_form_data,
form_id:
)
end

before { allow(Rails).to receive(:logger).and_return(rails_logger) }

context 'first_name does not match' do
let(:modified_form_data) do
form_data.merge({ 'full_name' => {
'first' => 'new-first-name',
'last' => 'fake-last-name'
} })
end

it 'logs the first_name change' do
expect(rails_logger).to receive(:info).with(anything, { field: 'first_name', form_id: })

prefill_data_service.check_for_changes
end
end

context 'last_name does not match' do
let(:modified_form_data) do
form_data.merge({ 'full_name' => {
'first' => 'fake-first-name',
'last' => 'new-last-name'
} })
end

it 'logs the last_name change' do
expect(rails_logger).to receive(:info).with(anything, { field: 'last_name', form_id: })

prefill_data_service.check_for_changes
end
end

context 'postal_code does not match' do
let(:modified_form_data) do
form_data.merge({ 'postal_code' => '67890' })
end

it 'logs the postal_code change' do
expect(rails_logger).to receive(:info).with(anything, { field: 'postal_code', form_id: })

prefill_data_service.check_for_changes
end
end

context 'ssn does not match' do
let(:modified_form_data) do
form_data.merge({ 'id_number' => { 'ssn' => 'new-ssn' } })
end

it 'logs the ssn change' do
expect(rails_logger).to receive(:info).with(anything, { field: 'ssn', form_id: })

prefill_data_service.check_for_changes
end
end

context 'email does not match' do
let(:modified_form_data) do
form_data.merge({ 'email' => 'new-email' })
end

it 'logs the email change' do
expect(rails_logger).to receive(:info).with(anything, { field: 'email', form_id: })

prefill_data_service.check_for_changes
end
end
end
end
Loading