Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add task for changing qualification certificate dates #2172

Merged
merged 3 commits into from
Apr 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions app/models/timeline_event.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
# assignee_id :bigint
# creator_id :integer
# note_id :bigint
# qualification_id :bigint
# requestable_id :bigint
# work_history_id :bigint
#
Expand All @@ -39,6 +40,7 @@
# index_timeline_events_on_assessment_section_id (assessment_section_id)
# index_timeline_events_on_assignee_id (assignee_id)
# index_timeline_events_on_note_id (note_id)
# index_timeline_events_on_qualification_id (qualification_id)
# index_timeline_events_on_requestable (requestable_type,requestable_id)
# index_timeline_events_on_work_history_id (work_history_id)
#
Expand All @@ -49,6 +51,7 @@
# fk_rails_... (assessment_section_id => assessment_sections.id)
# fk_rails_... (assignee_id => staff.id)
# fk_rails_... (note_id => notes.id)
# fk_rails_... (qualification_id => qualifications.id)
# fk_rails_... (work_history_id => work_histories.id)
#
class TimelineEvent < ApplicationRecord
Expand All @@ -58,6 +61,7 @@ class TimelineEvent < ApplicationRecord
belongs_to :assignee, class_name: "Staff", optional: true
belongs_to :creator, polymorphic: true, optional: true
belongs_to :note, optional: true
belongs_to :qualification, optional: true
belongs_to :requestable, polymorphic: true, optional: true
belongs_to :work_history, optional: true

Expand Down Expand Up @@ -165,8 +169,9 @@ class TimelineEvent < ApplicationRecord
unless: -> { requestable_reviewed? || requestable_verified? }

validates :column_name, presence: true, if: :information_changed?
validates :work_history_id,
:column_name,
validates :column_name,
:qualification_id,
:work_history_id,
absence: true,
unless: :information_changed?

Expand Down
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
1 change: 1 addition & 0 deletions config/analytics.yml
Original file line number Diff line number Diff line change
Expand Up @@ -384,6 +384,7 @@
- new_value
- note_id
- old_value
- qualification_id
- requestable_id
- requestable_type
- subjects
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class AddQualificationToTimelineEvents < ActiveRecord::Migration[7.1]
def change
add_reference :timeline_events, :qualification, foreign_key: true
end
end
5 changes: 4 additions & 1 deletion db/schema.rb

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

22 changes: 22 additions & 0 deletions lib/tasks/application_forms.rake
Original file line number Diff line number Diff line change
Expand Up @@ -92,4 +92,26 @@ namespace :application_forms do
user:,
)
end

desc "Change the certificate date of a qualification for an application form."
task :update_teaching_qualification_certificate_date,
%i[reference staff_email certificate_date] =>
:environment do |_task, args|
application_form = ApplicationForm.find_by!(reference: args[:reference])
qualification = application_form.teaching_qualification
user = Staff.find_by!(email: args[:staff_email])
certificate_date = Date.parse(args[:certificate_date])

UpdateQualificationCertificateDate.call(
qualification:,
user:,
certificate_date:,
)

note =
"Amended date to accurately reflect the qualification (deemed by the assessor). " \
"This was approved by Humzah and Natalie on 16/04/2024."

CreateNote.call(application_form:, author: user, text: note)
end
end
3 changes: 3 additions & 0 deletions spec/factories/timeline_events.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
# assignee_id :bigint
# creator_id :integer
# note_id :bigint
# qualification_id :bigint
# requestable_id :bigint
# work_history_id :bigint
#
Expand All @@ -37,6 +38,7 @@
# index_timeline_events_on_assessment_section_id (assessment_section_id)
# index_timeline_events_on_assignee_id (assignee_id)
# index_timeline_events_on_note_id (note_id)
# index_timeline_events_on_qualification_id (qualification_id)
# index_timeline_events_on_requestable (requestable_type,requestable_id)
# index_timeline_events_on_work_history_id (work_history_id)
#
Expand All @@ -47,6 +49,7 @@
# fk_rails_... (assessment_section_id => assessment_sections.id)
# fk_rails_... (assignee_id => staff.id)
# fk_rails_... (note_id => notes.id)
# fk_rails_... (qualification_id => qualifications.id)
# fk_rails_... (work_history_id => work_histories.id)
#
FactoryBot.define do
Expand Down
3 changes: 3 additions & 0 deletions spec/models/timeline_event_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
# assignee_id :bigint
# creator_id :integer
# note_id :bigint
# qualification_id :bigint
# requestable_id :bigint
# work_history_id :bigint
#
Expand All @@ -37,6 +38,7 @@
# index_timeline_events_on_assessment_section_id (assessment_section_id)
# index_timeline_events_on_assignee_id (assignee_id)
# index_timeline_events_on_note_id (note_id)
# index_timeline_events_on_qualification_id (qualification_id)
# index_timeline_events_on_requestable (requestable_type,requestable_id)
# index_timeline_events_on_work_history_id (work_history_id)
#
Expand All @@ -47,6 +49,7 @@
# fk_rails_... (assessment_section_id => assessment_sections.id)
# fk_rails_... (assignee_id => staff.id)
# fk_rails_... (note_id => notes.id)
# fk_rails_... (qualification_id => qualifications.id)
# fk_rails_... (work_history_id => work_histories.id)
#
require "rails_helper"
Expand Down
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
Loading