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

[EPIC] Withdrawals v3 #4832

Draft
wants to merge 15 commits into
base: main
Choose a base branch
from
Draft
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
44 changes: 22 additions & 22 deletions .rubocop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ Govuk/GovukSubmit:
- "app/components/personas/view.html.erb"

Register/RegisterFormWith:
Include:
Include:
- "app/views/**/*"
- "app/components/**/*"

Expand All @@ -97,36 +97,36 @@ Metrics/CollectionLiteralLength:

Rails/FindEach:
Exclude:
- 'spec/services/trainees/create_from_apply_spec.rb'
- 'db/data/20230612133512_backfill_disability_uuids_from_dfe_reference_gem.rb'
- "spec/services/trainees/create_from_apply_spec.rb"
- "db/data/20230612133512_backfill_disability_uuids_from_dfe_reference_gem.rb"

Rails/RedundantActiveRecordAllMethod:
Exclude:
- 'spec/models/service_update_spec.rb'
- 'spec/lib/dqt/params/update_spec.rb'
- 'app/lib/dfe_reference/degrees_query.rb'
- 'app/services/degrees/fix_to_match_reference_data.rb'
- 'spec/factories/degrees.rb'
- 'spec/lib/dfe_reference/degrees_query_spec.rb'
- 'spec/lib/dqt/params/trn_request_spec.rb'
- 'spec/lib/dqt/params/update_spec.rb'
- 'spec/models/degree_spec.rb'
- "spec/models/service_update_spec.rb"
- "spec/lib/dqt/params/update_spec.rb"
- "app/lib/dfe_reference/degrees_query.rb"
- "app/services/degrees/fix_to_match_reference_data.rb"
- "spec/factories/degrees.rb"
- "spec/lib/dfe_reference/degrees_query_spec.rb"
- "spec/lib/dqt/params/trn_request_spec.rb"
- "spec/lib/dqt/params/update_spec.rb"
- "spec/models/degree_spec.rb"

Rails/FilePath:
Exclude:
- 'spec/data_migrations/**/*'
- 'spec/models/api/**/*'
- 'spec/services/api/**/*'
- 'spec/requests/api/**/*'
- 'spec/serializers/api/**/*'
- "spec/data_migrations/**/*"
- "spec/models/api/**/*"
- "spec/services/api/**/*"
- "spec/requests/api/**/*"
- "spec/serializers/api/**/*"

RSpec/SpecFilePathFormat:
Exclude:
- 'spec/data_migrations/**/*'
- 'spec/models/api/**/*'
- 'spec/services/api/**/*'
- 'spec/requests/api/**/*'
- 'spec/serializers/api/**/*'
- "spec/data_migrations/**/*"
- "spec/models/api/**/*"
- "spec/services/api/**/*"
- "spec/requests/api/**/*"
- "spec/serializers/api/**/*"

Capybara/NegationMatcher:
EnforcedStyle: not_to
2 changes: 1 addition & 1 deletion app/components/record_actions/view.rb
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ def course_started_but_no_specified_start_date?

def relevant_redirect_path
if trainee.trainee_start_date.present?
edit_trainee_withdrawal_date_path(trainee)
trainee_withdrawal_start_path(trainee)
else
trainee_start_date_verification_path(trainee, context: :withdraw)
end
Expand Down
66 changes: 50 additions & 16 deletions app/components/withdrawal/view.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@ def trainee
def rows
@rows ||= [
start_date,
withdraw_date,
withdraw_date_from_course,
withdrawal_trigger,
reasons,
details,
details_dfe,
future_interest_in_teaching,
].compact
end

Expand All @@ -37,14 +37,38 @@ def start_date
)
end

def withdraw_date
def withdraw_date_from_course
mappable_field(
data_model.withdraw_date&.strftime(Date::DATE_FORMATS[:govuk]) || "-",
withdraw_date&.strftime(Date::DATE_FORMATS[:govuk]) || "-",
"Date the trainee withdrew",
edit_trainee_withdrawal_date_path(trainee),
)
end

def withdraw_date
if data_model.is_a?(Trainee)
data_model.trainee_withdrawals&.last&.date
else
data_model.withdraw_date
end
end

def withdrawal_trigger
mappable_field(
t("views.forms.withdrawal_trigger.#{trigger}.label"),
"How the trainee withdrew",
edit_trainee_withdrawal_trigger_path(trainee),
)
end

def trigger
if data_model.is_a?(Trainee)
data_model.trainee_withdrawals&.last&.trigger
else
data_model.trigger
end
end

def reasons
mappable_field(
reasons_html_safe,
Expand All @@ -54,27 +78,37 @@ def reasons
end

def reasons_html_safe
data_model.withdrawal_reasons.map do |reason|
return unless withdrawal_reasons

withdrawal_reasons.map do |reason|
t("components.withdrawal_details.reasons.#{reason.name}")
end.join("<br>").html_safe
end

def details
mappable_field(
data_model.withdraw_reasons_details.presence || "-",
"Details of why the trainee withdrew",
edit_trainee_withdrawal_extra_information_path(trainee),
)
def withdrawal_reasons
if data_model.is_a?(Trainee)
data_model.trainee_withdrawals&.last&.withdrawal_reasons
else
data_model.withdrawal_reasons
end
end

def details_dfe
def future_interest_in_teaching
mappable_field(
data_model.withdraw_reasons_dfe_details.presence || "No",
details_dfe_label,
edit_trainee_withdrawal_extra_information_path(trainee),
t("views.forms.withdrawal_future_interest.#{future_interest}.label"),
"Future interest in teaching",
edit_trainee_withdrawal_future_interest_path(trainee),
)
end

def future_interest
if data_model.is_a?(Trainee)
data_model.trainee_withdrawals&.last&.future_interest
else
data_model.future_interest
end
end

def details_dfe_label
if data_model.withdraw_reasons_dfe_details.blank?
"Could the Department for Education have done anything to avoid the candidate withdrawing?"
Expand Down
2 changes: 1 addition & 1 deletion app/controllers/trainees/withdrawal/dates_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ def attribute_names
end

def next_page
edit_trainee_withdrawal_reason_path(trainee)
edit_trainee_withdrawal_trigger_path(trainee)
end

def form_params
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@

module Trainees
module Withdrawal
class ExtraInformationsController < Base
class FutureInterestsController < Base
private

def form_class
::Withdrawal::ExtraInformationForm
::Withdrawal::FutureInterestForm
end

def attribute_names
%i[withdraw_reasons_details withdraw_reasons_dfe_details]
:future_interest
end

def next_page
Expand Down
4 changes: 2 additions & 2 deletions app/controllers/trainees/withdrawal/reasons_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@ def form_class
end

def attribute_names
:reason_ids
form_class::FIELDS
end

def next_page
edit_trainee_withdrawal_extra_information_path(trainee)
edit_trainee_withdrawal_future_interest_path(trainee)
end
end
end
Expand Down
11 changes: 11 additions & 0 deletions app/controllers/trainees/withdrawal/starts_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# frozen_string_literal: true

module Trainees
module Withdrawal
class StartsController < Base
def show
# No additional logic needed for now
end
end
end
end
21 changes: 21 additions & 0 deletions app/controllers/trainees/withdrawal/triggers_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# frozen_string_literal: true

module Trainees
module Withdrawal
class TriggersController < Base
private

def form_class
::Withdrawal::TriggerForm
end

def attribute_names
:trigger
end

def next_page
edit_trainee_withdrawal_reason_path(trainee)
end
end
end
end
3 changes: 2 additions & 1 deletion app/forms/undo_withdrawal_form.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ def initialize(trainee:, session:, comment: nil, ticket: nil)
def save
return false unless valid?

trainee.withdrawal_reasons.clear
withdrawal = trainee.trainee_withdrawals.last
withdrawal.update(discarded_at: Time.zone.now)
trainee.update(
state: previous_state,
withdraw_reasons_details: nil,
Expand Down
21 changes: 14 additions & 7 deletions app/forms/withdrawal/confirm_details_form.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,30 +5,36 @@ class ConfirmDetailsForm < TraineeForm
FIELDS = [
*DateForm::FIELDS,
*ReasonForm::FIELDS,
*ExtraInformationForm::FIELDS,
].freeze

attr_reader(:trainee, :reasons_form, :extra_information_form, :date_form, :start_date_form)
attr_reader(:trainee, :reasons_form, :trigger_form, :date_form, :start_date_form, :future_interest_form)
attr_accessor(*FIELDS)

delegate :id, to: :trainee
delegate :withdrawal_reasons, to: :reasons_form
delegate :future_interest, to: :future_interest_form
delegate :trigger, to: :trigger_form

def initialize(trainee)
@trainee = trainee
@reasons_form = ReasonForm.new(trainee)
@extra_information_form = ExtraInformationForm.new(trainee)
@trigger_form = TriggerForm.new(trainee)
@date_form = DateForm.new(trainee)
@start_date_form = ::TraineeStartStatusForm.new(trainee)
@future_interest_form = FutureInterestForm.new(trainee)
@fields = compute_fields
assign_attributes(fields)
end

def save!
if valid?
save_forms
trainee.withdraw!
Trainees::Withdraw.call(trainee:)
ActiveRecord::Base.transaction do
# Create empty withdrawal record that the forms can update
trainee.trainee_withdrawals.create!
save_forms
trainee.withdraw!
Trainees::Withdraw.call(trainee:)
end
else
false
end
Expand All @@ -48,9 +54,10 @@ def trainee_start_date

def withdrawal_forms
@withdrawal_forms ||= [
extra_information_form,
trigger_form,
reasons_form,
date_form,
future_interest_form,
(start_date_form unless exclude_start_date_form?),
].compact
end
Expand Down
3 changes: 1 addition & 2 deletions app/forms/withdrawal/date_form.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,7 @@ def fields
end

def save!
assign_attributes_to_trainee
trainee.save
trainee.trainee_withdrawals.last.update!(date: withdraw_date)
clear_stash
end

Expand Down
35 changes: 0 additions & 35 deletions app/forms/withdrawal/extra_information_form.rb

This file was deleted.

32 changes: 32 additions & 0 deletions app/forms/withdrawal/future_interest_form.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# frozen_string_literal: true

module Withdrawal
class FutureInterestForm < TraineeForm
FIELDS = %i[future_interest].freeze

attr_accessor(*FIELDS)

validates :future_interest, presence: true, inclusion: { in: %w[yes no unknown] }

def save!
withdrawal = trainee.trainee_withdrawals.last
withdrawal.update!(future_interest:)

clear_stash
end

private

def form_store_key
:future_interest
end

def compute_fields
trainee.attributes.symbolize_keys.slice(*FIELDS).merge(new_attributes)
end

def new_attributes
fields_from_store.merge(params).symbolize_keys.slice(*FIELDS)
end
end
end
Loading
Loading