Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
23 changes: 23 additions & 0 deletions app/controllers/concerns/handle_application_form_section.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ def handle_application_form_section(
redirect_to check_path || %i[teacher_interface application_form]
end
else
send_errors_to_big_query(form)

render if_failure_then_render, status: :unprocessable_entity
end
end
Expand All @@ -31,4 +33,25 @@ def handle_application_form_section(
def history_stack
@history_stack ||= HistoryStack.new(session:)
end

def send_errors_to_big_query(form)
error_events = []

form.errors.each do |error|
error_events << DfE::Analytics::Event
.new
.with_type(:form_validation_failure)
.with_user(current_teacher)
.with_request_details(request)
.with_data(
data: {
form_class: form.class.to_s,
error_attribute: error.attribute,
error_message: error.message,
},
)
end

DfE::Analytics::SendEvents.do(error_events) if error_events.present?
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

module TeacherInterface
class ApplicationFormsController < BaseController
include HandleApplicationFormSection
include HistoryTrackable

before_action :redirect_if_application_form_active, only: %i[new create]
Expand Down Expand Up @@ -39,6 +40,8 @@ def create
elsif @country_region_form.save(validate: true)
redirect_to teacher_interface_application_form_path
else
send_errors_to_big_query(@country_region_form)

render :new, status: :unprocessable_entity
end
end
Expand Down Expand Up @@ -78,6 +81,8 @@ def update
SubmitApplicationForm.call(application_form:, user: current_teacher)
redirect_to %i[teacher_interface application_form]
else
send_errors_to_big_query(@sanction_confirmation_form)

render :edit, status: :unprocessable_entity
end
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,8 @@ def submit_add_another
end
end
else
send_errors_to_big_query(@form)

render :add_another, status: :unprocessable_entity
end
end
Expand Down Expand Up @@ -209,6 +211,8 @@ def destroy
if @form.save(validate: true)
redirect_to %i[check teacher_interface application_form qualifications]
else
send_errors_to_big_query(@form)

render :delete, status: :unprocessable_entity
end
end
Expand Down
2 changes: 2 additions & 0 deletions app/controllers/teacher_interface/uploads_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,8 @@ def destroy
if @form.save(validate: true)
redirect_to [:teacher_interface, :application_form, document]
else
send_errors_to_big_query(@form)

render :delete, status: :unprocessable_entity
end
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,8 @@ def submit_add_another
end
end
else
send_errors_to_big_query(@form)

render :add_another, status: :unprocessable_entity
end
end
Expand Down Expand Up @@ -226,6 +228,8 @@ def destroy
if @form.save(validate: true)
redirect_to %i[check teacher_interface application_form work_histories]
else
send_errors_to_big_query(@form)

render :delete, status: :unprocessable_entity
end
end
Expand Down
2 changes: 2 additions & 0 deletions config/analytics_custom_events.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
shared:
- form_validation_failure
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,19 @@
end
end
let(:session) { {} }
let(:current_teacher) { create(:teacher) }
let(:request) do
ActionController::TestRequest.new({}, {}, TeacherInterface::BaseController)
end

before { allow(controller).to receive_messages(params:, session:) }
before do
allow(controller).to receive_messages(
params:,
session:,
current_teacher:,
request:,
)
end

describe "#handle_application_form_section" do
subject(:handle_application_form_section) do
Expand All @@ -23,7 +34,7 @@
)
end

let(:form) { double }
let(:form) { instance_double(TeacherInterface::BaseForm, errors: []) }
let(:if_success_then_redirect) { :redirect }
let(:if_failure_then_render) { :render }

Expand All @@ -40,11 +51,30 @@
end

context "when form is invalid" do
let(:error) do
ActiveModel::Error.new(
TeacherInterface::NameAndDateOfBirthForm.new,
:given_names,
)
end

let(:form) do
instance_double(
TeacherInterface::NameAndDateOfBirthForm,
errors: [error],
)
end

before { allow(form).to receive(:save).and_return(false) }

it "renders the failure" do
it "renders the failure and sends a custom form_validation_failure event to BigQuery" do
expect(controller).to receive(:render)

handle_application_form_section

expect(
:form_validation_failure,
).to have_been_enqueued_as_analytics_events
end
end
end
Expand Down
1 change: 1 addition & 0 deletions spec/rails_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

require "capybara/cuprite"
require "dfe/analytics/testing"
require "dfe/analytics/rspec/matchers"
require "support/page_helpers"
require "site_prism"
require "site_prism/all_there"
Expand Down
Loading