From 656f4cbf0cb46efd78e3efcb7912ba19a13f0972 Mon Sep 17 00:00:00 2001 From: Eric Tillberg Date: Thu, 26 Dec 2024 12:04:42 -0500 Subject: [PATCH] Log whenever Form Upload Flow prefill data is changed --- .../v1/scanned_form_uploads_controller.rb | 12 ++ .../simple_forms_api/prefill_data_service.rb | 26 ++++ .../v1/scanned_form_uploads_spec.rb | 22 ++++ .../services/prefill_data_service_spec.rb | 113 ++++++++++++++++++ 4 files changed, 173 insertions(+) create mode 100644 modules/simple_forms_api/app/services/simple_forms_api/prefill_data_service.rb create mode 100644 modules/simple_forms_api/spec/services/prefill_data_service_spec.rb diff --git a/modules/simple_forms_api/app/controllers/simple_forms_api/v1/scanned_form_uploads_controller.rb b/modules/simple_forms_api/app/controllers/simple_forms_api/v1/scanned_form_uploads_controller.rb index 3d5845dc744..49fcc3f6b72 100644 --- a/modules/simple_forms_api/app/controllers/simple_forms_api/v1/scanned_form_uploads_controller.rb +++ b/modules/simple_forms_api/app/controllers/simple_forms_api/v1/scanned_form_uploads_controller.rb @@ -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 @@ -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 diff --git a/modules/simple_forms_api/app/services/simple_forms_api/prefill_data_service.rb b/modules/simple_forms_api/app/services/simple_forms_api/prefill_data_service.rb new file mode 100644 index 00000000000..e847961296d --- /dev/null +++ b/modules/simple_forms_api/app/services/simple_forms_api/prefill_data_service.rb @@ -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 diff --git a/modules/simple_forms_api/spec/requests/simple_forms_api/v1/scanned_form_uploads_spec.rb b/modules/simple_forms_api/spec/requests/simple_forms_api/v1/scanned_form_uploads_spec.rb index 5e2d3ff051f..7acb556b948 100644 --- a/modules/simple_forms_api/spec/requests/simple_forms_api/v1/scanned_form_uploads_spec.rb +++ b/modules/simple_forms_api/spec/requests/simple_forms_api/v1/scanned_form_uploads_spec.rb @@ -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 diff --git a/modules/simple_forms_api/spec/services/prefill_data_service_spec.rb b/modules/simple_forms_api/spec/services/prefill_data_service_spec.rb new file mode 100644 index 00000000000..f1d0f9e9514 --- /dev/null +++ b/modules/simple_forms_api/spec/services/prefill_data_service_spec.rb @@ -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