From 983ed862fa8e14e417751b1fbdbb15098f448fdb Mon Sep 17 00:00:00 2001 From: Thomas Leese Date: Thu, 25 Apr 2024 08:55:49 +0100 Subject: [PATCH] Add UpdateQualificationCertificateDate This adds a service that makes it possible to change the qualification certificate date of an application after it has been submitted. --- .../update_qualification_certificate_date.rb | 38 +++++++++++++++++++ ...ate_qualification_certificate_date_spec.rb | 38 +++++++++++++++++++ 2 files changed, 76 insertions(+) create mode 100644 app/services/update_qualification_certificate_date.rb create mode 100644 spec/services/update_qualification_certificate_date_spec.rb diff --git a/app/services/update_qualification_certificate_date.rb b/app/services/update_qualification_certificate_date.rb new file mode 100644 index 0000000000..46024d8c01 --- /dev/null +++ b/app/services/update_qualification_certificate_date.rb @@ -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 diff --git a/spec/services/update_qualification_certificate_date_spec.rb b/spec/services/update_qualification_certificate_date_spec.rb new file mode 100644 index 0000000000..e864e35328 --- /dev/null +++ b/spec/services/update_qualification_certificate_date_spec.rb @@ -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