Skip to content

Commit

Permalink
Add UpdateQualificationCertificateDate
Browse files Browse the repository at this point in the history
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
thomasleese committed Apr 25, 2024
1 parent c0fab05 commit 983ed86
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 0 deletions.
38 changes: 38 additions & 0 deletions app/services/update_qualification_certificate_date.rb
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 spec/services/update_qualification_certificate_date_spec.rb
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

0 comments on commit 983ed86

Please sign in to comment.