Skip to content

Commit

Permalink
Log whenever Form Upload Flow prefill data is changed
Browse files Browse the repository at this point in the history
  • Loading branch information
Thrillberg committed Dec 26, 2024
1 parent 05b17dc commit 656f4cb
Show file tree
Hide file tree
Showing 4 changed files with 173 additions and 0 deletions.
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 @@ -98,6 +100,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],
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
changed_fields = []
changed_fields << 'first_name' if prefill_data.dig('full_name', 'first') != form_data.dig('full_name', 'first')
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 @@ -52,6 +52,28 @@

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

it 'checks if the prefill data has been changed' do
form_number = '21-0779'
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: { form_number:, confirmation_code:, form_data: { email: 'fake-email' } }

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

describe '#upload_scanned_form' do
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

0 comments on commit 656f4cb

Please sign in to comment.