Skip to content

Commit

Permalink
treat differently participants added before and after 2023
Browse files Browse the repository at this point in the history
  • Loading branch information
ltello committed Apr 27, 2023
1 parent e81d85a commit b1e6af7
Show file tree
Hide file tree
Showing 9 changed files with 238 additions and 157 deletions.
1 change: 1 addition & 0 deletions app/models/participant_profile.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ class ParticipantProfile < ApplicationRecord
has_many :participant_declarations
has_many :participant_profile_schedules
has_many :participant_profile_states
has_many :sync_dqt_induction_start_date_errors
has_many :validation_decisions, class_name: "ProfileValidationDecision"

has_one :ecf_participant_eligibility
Expand Down
2 changes: 1 addition & 1 deletion app/models/sync_dqt_induction_start_date_error.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@ class SyncDqtInductionStartDateError < ApplicationRecord
belongs_to :participant_profile

validates :participant_profile, presence: true
validates :error_message, presence: true
validates :message, presence: true
end
7 changes: 6 additions & 1 deletion app/services/induction/amend_participant_cohort.rb
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,11 @@ def active?(participant_profile)
message: ->(form, _) { I18n.t("errors.cohort.blank", year: form.target_cohort_start_year, where: form.school&.name) },
}

validates :induction_programme,
presence: {
message: ->(form, _) { I18n.t("errors.induction_programme.blank", year: form.target_cohort_start_year, school: form.school&.name) },
}

delegate :school, to: :induction_record, allow_nil: true

def save
Expand Down Expand Up @@ -119,7 +124,7 @@ def historical_target_school_cohort(school)
end

def induction_programme
@induction_programme ||= target_school_cohort.default_induction_programme
@induction_programme ||= target_school_cohort&.default_induction_programme
end

def induction_record
Expand Down
85 changes: 50 additions & 35 deletions app/services/participants/sync_dqt_induction_start_date.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,59 +2,74 @@

module Participants
class SyncDqtInductionStartDate < BaseService
FIRST_2021_ACADEMIC_DATE = Date.new(2021, 9, 1)
FIRST_2023_REGISTRATION_DATE = Cohort.find_by(start_year: 2023)&.registration_start_date || Date.new(2023, 6, 1)

def initialize(dqt_induction_start_date, participant_profile)
@dqt_induction_start_date = dqt_induction_start_date
@participant_profile = participant_profile
end

def call
return false unless FeatureFlag.active?(:cohortless_dashboard)
return false if @dqt_induction_start_date.nil?
return false if @participant_profile.induction_start_date.present?

current_induction_record = @participant_profile.induction_records.latest
participant_cohort = current_induction_record.cohort
dqt_cohort = Cohort.containing_date(@dqt_induction_start_date)
return false if dqt_cohort.nil?

if dqt_cohort == participant_cohort
update_induction_start_date
true
else
ActiveRecord::Base.transaction do
amend_cohort = amend_cohort_form(dqt_cohort, participant_cohort)
if amend_cohort.save
update_induction_start_date
delete_sync_error
return true
else
save_error_message(amend_cohort)
return false
end
end
end
return false unless dqt_induction_start_date
return update_induction_start_date if pre_2021_dqt_induction_start_date? || pre_2023_participant?
return cohort_missing unless target_cohort

update_participant
rescue StandardError
false
end

private

def save_error_message(amend_cohort)
error = SyncDqtInductionStartDateError.find_or_create_by(participant_profile: @participant_profile) # rubocop:disable Rails/SaveBang
error.error_message = amend_cohort.errors.full_messages.join(", ")
error.save!
attr_reader :dqt_induction_start_date, :participant_profile

delegate :current_induction_record, to: :participant_profile
delegate :cohort, to: :current_induction_record, prefix: :participant

def amend_cohort
@amend_cohort ||= Induction::AmendParticipantCohort.new(participant_profile:,
source_cohort_start_year: participant_cohort.start_year,
target_cohort_start_year: target_cohort.start_year)
end

def delete_sync_error
SyncDqtInductionStartDateError.where(participant_profile: @participant_profile).destroy_all
def clear_participant_sync_errors
SyncDqtInductionStartDateError.where(participant_profile:).destroy_all
end

def amend_cohort_form(dqt_cohort, participant_cohort)
Induction::AmendParticipantCohort.new(participant_profile: @participant_profile,
source_cohort_start_year: participant_cohort.start_year,
target_cohort_start_year: dqt_cohort.start_year)
def pre_2021_dqt_induction_start_date?
dqt_induction_start_date < FIRST_2021_ACADEMIC_DATE
end

def pre_2023_participant?
participant_profile.created_at < FIRST_2023_REGISTRATION_DATE
end

def save_errors(*messages)
messages.each do |message|
SyncDqtInductionStartDateError.find_or_create_by!(participant_profile:, message:)
end

false
end
alias_method :save_error, :save_errors

def target_cohort
@target_cohort ||= Cohort.containing_date(dqt_induction_start_date)
end

def cohort_missing
save_error("Cohort containing date #{dqt_induction_start_date.to_s(:govuk)} not setup in the service!")
end

def update_induction_start_date
@participant_profile.update!(induction_start_date: @dqt_induction_start_date)
participant_profile.update!(induction_start_date: dqt_induction_start_date)
end

def update_participant
clear_participant_sync_errors
amend_cohort.save ? update_induction_start_date : save_errors(*amend_cohort.errors.full_messages)
end
end
end
4 changes: 3 additions & 1 deletion config/locales/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -157,9 +157,11 @@ en:
appropriate_body:
blank: "Choose an appropriate body"
cohort:
blank: "Cohort starting on %{year} not setup on %{where}"
blank: "starting on %{year} not setup on %{where}"
excluded_start_year: "Invalid value. Must be different to %{year}"
invalid_start_year: "Invalid value. Must be an integer between %{start} and %{end}"
induction_programme:
blank: "default for %{year} not setup on %{school}"
induction_record:
blank: "The participant is not enrolled on the cohort starting on %{year}"
participant_declarations:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@

class CreateSyncDqtInductionStartDateError < ActiveRecord::Migration[6.1]
def change
create_table :sync_dqt_induction_start_date_errors, id: false do |t|
t.references :participant_profile, null: false, primary_key: true, foreign_key: true, type: :uuid, index: { name: "dqt_sync_participant_profile_id" }
t.text :error_message
create_table :sync_dqt_induction_start_date_errors do |t|
t.belongs_to :participant_profile, null: false, foreign_key: true, type: :uuid, index: { name: "dqt_sync_participant_profile_id" }
t.text :message

t.timestamps
end
end
Expand Down
7 changes: 4 additions & 3 deletions db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
#
# It's strongly recommended that you check this file into your version control system.

ActiveRecord::Schema.define(version: 2023_04_19_084407) do
ActiveRecord::Schema.define(version: 2023_04_24_093735) do

# These are extensions that must be enabled in order to support this database
enable_extension "citext"
Expand Down Expand Up @@ -1016,8 +1016,9 @@
t.index ["cpd_lead_provider_id"], name: "index_statements_on_cpd_lead_provider_id"
end

create_table "sync_dqt_induction_start_date_errors", primary_key: "participant_profile_id", id: :uuid, default: -> { "gen_random_uuid()" }, force: :cascade do |t|
t.text "error_message"
create_table "sync_dqt_induction_start_date_errors", id: :uuid, default: -> { "gen_random_uuid()" }, force: :cascade do |t|
t.uuid "participant_profile_id", null: false
t.text "message"
t.datetime "created_at", precision: 6, null: false
t.datetime "updated_at", precision: 6, null: false
t.index ["participant_profile_id"], name: "dqt_sync_participant_profile_id"
Expand Down
22 changes: 19 additions & 3 deletions spec/services/induction/amend_participant_cohort_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@
expect(form.save).to be_falsey
expect(form.errors.first.attribute).to eq(:target_cohort)
expect(form.errors.first.message)
.to eq("Cohort starting on #{target_cohort_start_year} not setup on the service")
.to eq("starting on #{target_cohort_start_year} not setup on the service")
end
end

Expand Down Expand Up @@ -154,11 +154,27 @@
expect(form.save).to be_falsey
expect(form.errors.first.attribute).to eq(:target_school_cohort)
expect(form.errors.first.message)
.to eq("Cohort starting on #{target_cohort_start_year} not setup on #{school.name}")
.to eq("starting on #{target_cohort_start_year} not setup on #{school.name}")
end
end

context "when the school has setup the target cohort" do
context "when the school has not setup a default induction programme for the target cohort" do
let!(:target_school_cohort) { create(:school_cohort, :fip, cohort: target_cohort, school:) }

before do
Induction::Enrol.call(participant_profile:,
induction_programme: source_school_cohort.default_induction_programme)
end

it "returns false and set errors" do
expect(form.save).to be_falsey
expect(form.errors.first.attribute).to eq(:induction_programme)
expect(form.errors.first.message)
.to eq("default for #{target_cohort_start_year} not setup on #{school.name}")
end
end

context "when the school has setup the target cohort with default induction programme" do
let!(:target_school_cohort) { create(:school_cohort, :fip, cohort: target_cohort, school:) }

before do
Expand Down
Loading

0 comments on commit b1e6af7

Please sign in to comment.