Skip to content

Commit

Permalink
Update UpdateForm and SubmitForm services
Browse files Browse the repository at this point in the history
These service will now send some events about the forms
  • Loading branch information
fumimowdan committed Oct 6, 2023
1 parent 0dbc168 commit 8e12f5d
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 2 deletions.
1 change: 1 addition & 0 deletions app/services/submit_form.rb
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ def create_application

def delete_form
form.destroy!
Event.publish(:deleted, form)
end

def send_applicant_email
Expand Down
9 changes: 9 additions & 0 deletions app/services/update_form.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ def self.call(...)
return service unless service.valid?

service.update_form!
service.capture_form_analytics
service
end

Expand All @@ -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
Expand Down
5 changes: 4 additions & 1 deletion spec/services/submit_form_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
33 changes: 32 additions & 1 deletion spec/services/update_form_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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" }
Expand Down Expand Up @@ -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

0 comments on commit 8e12f5d

Please sign in to comment.