From 8e12f5dfff62c9fe6038447b1a912d66d7df113a Mon Sep 17 00:00:00 2001 From: fumimowdan Date: Mon, 2 Oct 2023 13:41:25 +0100 Subject: [PATCH] Update UpdateForm and SubmitForm services These service will now send some events about the forms --- app/services/submit_form.rb | 1 + app/services/update_form.rb | 9 +++++++++ spec/services/submit_form_spec.rb | 5 ++++- spec/services/update_form_spec.rb | 33 ++++++++++++++++++++++++++++++- 4 files changed, 46 insertions(+), 2 deletions(-) diff --git a/app/services/submit_form.rb b/app/services/submit_form.rb index 23270446..a6ccb4d5 100644 --- a/app/services/submit_form.rb +++ b/app/services/submit_form.rb @@ -95,6 +95,7 @@ def create_application def delete_form form.destroy! + Event.publish(:deleted, form) end def send_applicant_email diff --git a/app/services/update_form.rb b/app/services/update_form.rb index 139c6b45..a17e55ef 100644 --- a/app/services/update_form.rb +++ b/app/services/update_form.rb @@ -4,6 +4,7 @@ def self.call(...) return service unless service.valid? service.update_form! + service.capture_form_analytics service end @@ -27,6 +28,14 @@ def update_form! form.update(**parsed_params) end + def capture_form_analytics + changes = form.saved_changes + action = :updated + action = :created if changes.key?(:id) + + Event.publish(action, form, changes) if changes.present? + end + private def parsed_params diff --git a/spec/services/submit_form_spec.rb b/spec/services/submit_form_spec.rb index 022582e0..c7faac40 100644 --- a/spec/services/submit_form_spec.rb +++ b/spec/services/submit_form_spec.rb @@ -32,7 +32,10 @@ end context "returns true when form is submitted" do - before { service.submit_form! } + before do + form.save + service.submit_form! + end it { expect(service).to be_success } end diff --git a/spec/services/update_form_spec.rb b/spec/services/update_form_spec.rb index 9009f118..15ff3866 100644 --- a/spec/services/update_form_spec.rb +++ b/spec/services/update_form_spec.rb @@ -3,7 +3,7 @@ RSpec.describe UpdateForm do subject(:update_form) { described_class.new(step, params) } - let(:form) { build(:form) } + let(:form) { build(:trainee_form) } let(:step) { ApplicationRouteStep.new(form) } let(:params) { { "application_route" => application_route } } let(:application_route) { "teacher" } @@ -40,4 +40,35 @@ update_form.update_form! end end + + describe "capture_form_analytics" do + it "when the form has not changes not event is triggered" do + expect(Event).not_to receive(:publish) + + update_form.capture_form_analytics + end + + context "when the form has changes" do + before { form.save } + + it "a created event is triggered" do + expect(Event).to receive(:publish) + .with( + :created, + form, + hash_including( + "application_route" => [nil, "salaried_trainee"], + "id" => [nil, form.id], + ), + ) + update_form.capture_form_analytics + end + + it "a updated event is triggered" do + form.update(given_name: "foo") + expect(Event).to receive(:publish).with(:updated, form, hash_including("given_name" => [nil, "foo"])) + update_form.capture_form_analytics + end + end + end end