-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add UpdateQualificationCertificateDate
This adds a service that makes it possible to change the qualification certificate date of an application after it has been submitted.
- Loading branch information
1 parent
c0fab05
commit 983ed86
Showing
2 changed files
with
76 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
# frozen_string_literal: true | ||
|
||
class UpdateQualificationCertificateDate | ||
include ServicePattern | ||
|
||
def initialize(qualification:, user:, certificate_date:) | ||
@qualification = qualification | ||
@user = user | ||
@certificate_date = certificate_date | ||
end | ||
|
||
def call | ||
old_certificate_date = qualification.certificate_date | ||
|
||
ActiveRecord::Base.transaction do | ||
qualification.update!(certificate_date:) | ||
create_timeline_event(old_certificate_date:) | ||
end | ||
end | ||
|
||
private | ||
|
||
attr_reader :qualification, :user, :certificate_date | ||
|
||
delegate :application_form, to: :qualification | ||
|
||
def create_timeline_event(old_certificate_date:) | ||
CreateTimelineEvent.call( | ||
"information_changed", | ||
application_form:, | ||
user:, | ||
qualification:, | ||
column_name: "certificate_date", | ||
old_value: old_certificate_date.strftime("%B %Y").strip, | ||
new_value: certificate_date.strftime("%B %Y").strip, | ||
) | ||
end | ||
end |
38 changes: 38 additions & 0 deletions
38
spec/services/update_qualification_certificate_date_spec.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
# frozen_string_literal: true | ||
|
||
require "rails_helper" | ||
|
||
RSpec.describe UpdateQualificationCertificateDate do | ||
let(:qualification) do | ||
create(:qualification, certificate_date: Date.new(2020, 1, 1)) | ||
end | ||
let(:application_form) { qualification.application_form } | ||
let(:user) { create(:staff) } | ||
let(:new_certificate_date) { Date.new(2021, 1, 1) } | ||
|
||
subject(:call) do | ||
described_class.call( | ||
qualification:, | ||
user:, | ||
certificate_date: new_certificate_date, | ||
) | ||
end | ||
|
||
it "changes the certificate date" do | ||
expect { call }.to change(qualification, :certificate_date).to( | ||
new_certificate_date, | ||
) | ||
end | ||
|
||
it "records a timeline event" do | ||
expect { call }.to have_recorded_timeline_event( | ||
:information_changed, | ||
creator: user, | ||
application_form:, | ||
qualification:, | ||
column_name: "certificate_date", | ||
old_value: "January 2020", | ||
new_value: "January 2021", | ||
) | ||
end | ||
end |