Skip to content

Commit 789c916

Browse files
authored
map legacy working patterns from profile (#8529)
1 parent 4c16b5d commit 789c916

File tree

7 files changed

+74
-8
lines changed

7 files changed

+74
-8
lines changed

app/services/jobseekers/job_applications/prefill_job_application_from_jobseeker_profile.rb

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ def initialize(jobseeker, new_job_application)
66

77
def call
88
copy_personal_info
9+
copy_job_preferences(jobseeker_profile.job_preferences) if jobseeker_profile.job_preferences.present?
910
copy_associations(jobseeker_profile.qualifications)
1011
copy_associations(jobseeker_profile.employments)
1112
copy_associations(jobseeker_profile.training_and_cpds)
@@ -29,8 +30,16 @@ def copy_personal_info
2930
qts_age_range_and_subject: jobseeker_profile.qts_age_range_and_subject,
3031
teacher_reference_number: jobseeker_profile.teacher_reference_number,
3132
has_right_to_work_in_uk: jobseeker_profile.personal_details&.has_right_to_work_in_uk?,
32-
working_patterns: jobseeker_profile.job_preferences&.working_patterns,
33-
working_pattern_details: jobseeker_profile.job_preferences&.working_pattern_details,
33+
)
34+
end
35+
36+
def copy_job_preferences(job_preferences)
37+
# JobPreference working patterns are stored as strings, but JobApplication working_patterns are stored as
38+
# mapped integers. This is defensive code to stop crashes if/when these 2 items get out of alignment
39+
# (which has happened recently with the retirement of 'flexible' and 'term_time' options)
40+
new_job_application.assign_attributes(
41+
working_patterns: job_preferences.working_patterns.select { |wp| JobApplication.working_patterns.key?(wp.to_sym) },
42+
working_pattern_details: job_preferences.working_pattern_details,
3443
)
3544
end
3645

db/seeds.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@
126126
qualifications: FactoryBot.build_list(:qualification, 1, job_application: nil),
127127
employments: FactoryBot.build_list(:employment, 1, :jobseeker_profile_employment),
128128
jobseeker: jobseeker) do |jobseeker_profile|
129-
FactoryBot.create(:job_preferences, jobseeker_profile: jobseeker_profile) do |job_preferences|
129+
FactoryBot.create(:job_preferences, :for_seed_data, jobseeker_profile: jobseeker_profile) do |job_preferences|
130130
FactoryBot.create(:job_preferences_location, job_preferences:, name: location_preference_names.sample)
131131
end
132132
end
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
desc "Migrate legacy job preferences"
2+
task migrate_legacy_job_preferences: :environment do
3+
JobPreferences.where.not(working_patterns: nil).find_each do |jp|
4+
if jp.working_patterns.include?("term_time")
5+
jp.assign_attributes(working_patterns: (jp.working_patterns - %w[term_time] + %w[full_time]).uniq)
6+
end
7+
if jp.working_patterns.include?("flexible")
8+
jp.assign_attributes(working_patterns: (jp.working_patterns - %w[flexible] + %w[part_time]).uniq)
9+
end
10+
jp.save!(validate: false, touch: false)
11+
end
12+
end

spec/factories/job_preferences.rb

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
FactoryBot.define do
22
factory :job_preferences do
3-
roles { factory_rand_sample(Vacancy.job_roles.keys, 1..Vacancy.job_roles.keys.count) }
4-
phases { factory_rand_sample(Vacancy.phases.keys, 1..Vacancy.phases.keys.count) }
3+
roles { Vacancy.job_roles.keys }
4+
phases { Vacancy.phases.keys }
55
key_stages { key_stages_for_phases }
6-
subjects { factory_rand_sample(SUBJECT_OPTIONS.map(&:first), 1..3) }
7-
working_patterns { factory_rand_sample(Vacancy.working_patterns.keys, 1..2) }
6+
subjects { SUBJECT_OPTIONS.map(&:first) }
7+
working_patterns { Vacancy.working_patterns.keys }
88
working_pattern_details { "Strictly no Mondays" }
99
builder_completed { true }
1010
completed_steps do
@@ -24,6 +24,13 @@
2424
locations { [build(:job_preferences_location, name: Faker::Address.postcode, radius: Faker::Number.between(from: 1, to: 200), job_preferences: instance)] }
2525
end
2626

27+
trait :for_seed_data do
28+
working_patterns { factory_rand_sample(Vacancy.working_patterns.keys, 1..2) }
29+
roles { factory_rand_sample(Vacancy.job_roles.keys, 1..Vacancy.job_roles.keys.count) }
30+
phases { factory_rand_sample(Vacancy.phases.keys, 1..Vacancy.phases.keys.count) }
31+
subjects { factory_rand_sample(SUBJECT_OPTIONS.map(&:first), 1..3) }
32+
end
33+
2734
trait :incomplete do
2835
builder_completed { false }
2936
completed_steps { {} }
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
require "rails_helper"
2+
3+
RSpec.describe "migrate_legacy_job_preferences" do
4+
include_context "rake"
5+
6+
let!(:pref_a) { create(:job_preferences, working_patterns: %w[term_time flexible part_time]) }
7+
let!(:pref_b) { create(:job_preferences, working_patterns: %w[flexible full_time]) }
8+
let!(:pref_c) { create(:job_preferences, working_patterns: %w[term_time]) }
9+
10+
# rubocop:disable RSpec/NamedSubject
11+
before do
12+
create(:job_preferences)
13+
create(:job_preferences, working_patterns: nil)
14+
subject.invoke
15+
end
16+
# rubocop:enable RSpec/NamedSubject
17+
18+
it "fixes up term time flexible" do
19+
expect(pref_a.reload.working_patterns).to match_array(%w[full_time part_time])
20+
end
21+
22+
it "fixes up flexible full_time" do
23+
expect(pref_b.reload.working_patterns).to match_array(%w[part_time full_time])
24+
end
25+
26+
it "fixes up term_time" do
27+
expect(pref_c.reload.working_patterns).to match_array(%w[full_time])
28+
end
29+
end

spec/services/jobseekers/job_applications/prefill_job_application_from_jobseeker_profile_spec.rb

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,11 @@
33
RSpec.describe Jobseekers::JobApplications::PrefillJobApplicationFromJobseekerProfile do
44
let(:jobseeker) { create(:jobseeker) }
55
let(:new_vacancy) { create(:vacancy) }
6-
let!(:jobseeker_profile) { create(:jobseeker_profile, :completed, jobseeker: jobseeker) }
6+
let!(:jobseeker_profile) do
7+
create(:jobseeker_profile, :completed,
8+
jobseeker: jobseeker,
9+
job_preferences: build(:job_preferences, working_patterns: build_stubbed(:job_preferences).working_patterns + %w[term_time flexible]))
10+
end
711
let(:new_job_application) { jobseeker.job_applications.create(vacancy: new_vacancy) }
812

913
subject { described_class.new(jobseeker, new_job_application).call }
@@ -20,6 +24,10 @@
2024
.to eq(jobseeker_profile.qualifications.map { |qualification| qualification.slice(*attributes_to_copy) })
2125
end
2226

27+
it "removes legacy working patterns" do
28+
expect(subject.working_patterns).to eq(%w[full_time part_time job_share])
29+
end
30+
2331
it "adds qualifications to in progress steps" do
2432
expect(subject.in_progress_steps).to include("qualifications")
2533
end

spec/system/publishers/publishers_can_view_candidate_messages_spec.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
visit publishers_candidate_messages_path
2929
# wait for page load
3030
find("form[action='/publishers/candidate_messages/toggle_archive']")
31+
find("footer")
3132
end
3233

3334
it "passes accessibility checks", :a11y do

0 commit comments

Comments
 (0)