Browse Source

feat: copy behaviour for scheme reactivation (wip)

CLDC-1672-reactivate-scheme
natdeanlewissoftwire 2 years ago
parent
commit
3d579e8745
  1. 41
      app/controllers/schemes_controller.rb
  2. 17
      app/helpers/toggle_active_scheme_helper.rb
  3. 24
      app/models/scheme.rb
  4. 2
      app/views/schemes/deactivate_confirm.html.erb
  5. 16
      app/views/schemes/toggle_active.html.erb
  6. 9
      config/locales/en.yml
  7. 2
      config/routes.rb

41
app/controllers/schemes_controller.rb

@ -50,10 +50,23 @@ class SchemesController < ApplicationController
redirect_to scheme_details_path(@scheme) redirect_to scheme_details_path(@scheme)
end end
def reactivate def new_reactivation
render "toggle_active", locals: { action: "reactivate" } render "toggle_active", locals: { action: "reactivate" }
end end
def reactivate
@scheme.run_reactivation_validations!
@scheme.reactivation_date = reactivation_date
@scheme.reactivation_date_type = params[:scheme][:reactivation_date_type]
if @scheme.valid? && @scheme.location_deactivation_periods.deactivations_without_reactivation.update!(reactivation_date:)
flash[:notice] = reactivate_success_notice
redirect_to scheme_details_path(@scheme)
else
render "toggle_active", locals: { action: "deactivate" }, status: :unprocessable_entity
end
end
def new def new
@scheme = Scheme.new @scheme = Scheme.new
end end
@ -303,24 +316,36 @@ private
end end
end end
def reactivate_success_notice
"#{@scheme.service_name} has been reactivated"
end
def deactivation_date def deactivation_date
toggle_date("deactivation_date")
end
def reactivation_date
toggle_date("reactivation_date")
end
def toggle_date(key)
if params[:scheme].blank? if params[:scheme].blank?
return return
elsif params[:scheme][:deactivation_date_type] == "default" elsif params[:scheme]["#{key}_type".to_sym] == "default"
return FormHandler.instance.current_collection_start_date return FormHandler.instance.current_collection_start_date
elsif params[:scheme][:deactivation_date].present? elsif params[:scheme][key.to_sym].present?
return params[:scheme][:deactivation_date] return params[:scheme][key.to_sym]
end end
day = params[:scheme]["deactivation_date(3i)"] day = params[:scheme]["#{key}(3i)"]
month = params[:scheme]["deactivation_date(2i)"] month = params[:scheme]["#{key}(2i)"]
year = params[:scheme]["deactivation_date(1i)"] year = params[:scheme]["#{key}(1i)"]
return nil if [day, month, year].any?(&:blank?) return nil if [day, month, year].any?(&:blank?)
Time.zone.local(year.to_i, month.to_i, day.to_i) if Date.valid_date?(year.to_i, month.to_i, day.to_i) Time.zone.local(year.to_i, month.to_i, day.to_i) if Date.valid_date?(year.to_i, month.to_i, day.to_i)
end end
def update_affected_logs def update_affected_logs
@scheme.lettings_logs.filter_by_before_startdate(deactivation_date.to_time).update!(location: nil, scheme: nil) @scheme.lettings_logs.filter_by_before_startdate(deactivation_date.to_time).update!(scheme: nil, location: nil)
end end
end end

17
app/helpers/toggle_active_scheme_helper.rb

@ -0,0 +1,17 @@
module ToggleActiveSchemeHelper
def toggle_scheme_form_path(action, scheme_id)
if action == "deactivate"
scheme_new_deactivation_path(scheme_id:)
else
scheme_reactivate_path(scheme_id:)
end
end
def date_type_question(action)
action == "deactivate" ? :deactivation_date_type : :reactivation_date_type
end
def date_question(action)
action == "deactivate" ? :deactivation_date : :reactivation_date
end
end

24
app/models/scheme.rb

@ -20,10 +20,11 @@ class Scheme < ApplicationRecord
validate :validate_confirmed validate :validate_confirmed
validate :deactivation_date_errors validate :deactivation_date_errors
validate :reactivation_date_errors
auto_strip_attributes :service_name auto_strip_attributes :service_name
attr_accessor :deactivation_date_type, :deactivation_date, :run_deactivation_validations attr_accessor :deactivation_date_type, :deactivation_date, :run_deactivation_validations, :reactivation_date_type, :reactivation_date, :run_reactivation_validations
SENSITIVE = { SENSITIVE = {
No: 0, No: 0,
@ -252,4 +253,25 @@ class Scheme < ApplicationRecord
end end
end end
end end
def run_reactivation_validations!
@run_reactivation_validations = true
end
def reactivation_date_errors
return unless @run_reactivation_validations
if reactivation_date.blank?
if reactivation_date_type.blank?
errors.add(:reactivation_date_type, message: I18n.t("validations.scheme.reactivation_date.not_selected"))
elsif reactivation_date_type == "other"
errors.add(:reactivation_date, message: I18n.t("validations.scheme.reactivation_date.invalid"))
end
else
collection_start_date = FormHandler.instance.current_collection_start_date
unless reactivation_date.between?(collection_start_date, Time.zone.local(2200, 1, 1))
errors.add(:reactivation_date, message: I18n.t("validations.scheme.reactivation_date.out_of_range", date: collection_start_date.to_formatted_s(:govuk_date)))
end
end
end
end end

2
app/views/schemes/deactivate_confirm.html.erb

@ -8,7 +8,7 @@
<span class="govuk-caption-l"><%= @scheme.service_name %></span> <span class="govuk-caption-l"><%= @scheme.service_name %></span>
This change will affect <%= @scheme.lettings_logs.count %> logs This change will affect <%= @scheme.lettings_logs.count %> logs
</h1> </h1>
<%= govuk_warning_text text: I18n.t("warnings.scheme.deactivation.review_logs") %> <%= govuk_warning_text text: I18n.t("warnings.scheme.deactivate.review_logs") %>
<%= f.hidden_field :confirm, value: true %> <%= f.hidden_field :confirm, value: true %>
<%= f.hidden_field :deactivation_date, value: @deactivation_date %> <%= f.hidden_field :deactivation_date, value: @deactivation_date %>
<%= f.hidden_field :deactivation_date_type, value: @deactivation_date_type %> <%= f.hidden_field :deactivation_date_type, value: @deactivation_date_type %>

16
app/views/schemes/toggle_active.html.erb

@ -6,24 +6,24 @@
href: scheme_details_path(@scheme), href: scheme_details_path(@scheme),
) %> ) %>
<% end %> <% end %>
<%= form_with model: @scheme, url: scheme_new_deactivation_path(@scheme), method: "patch", local: true do |f| %> <%= form_with model: @scheme, url: toggle_scheme_form_path(action, @scheme.id), method: "patch", local: true do |f| %>
<div class="govuk-grid-row"> <div class="govuk-grid-row">
<div class="govuk-grid-column-two-thirds"> <div class="govuk-grid-column-two-thirds">
<% collection_start_date = FormHandler.instance.current_collection_start_date %> <% collection_start_date = FormHandler.instance.current_collection_start_date %>
<%= f.govuk_error_summary %> <%= f.govuk_error_summary %>
<%= f.govuk_radio_buttons_fieldset :deactivation_date_type, <%= f.govuk_radio_buttons_fieldset date_type_question(action),
legend: { text: I18n.t("questions.scheme.deactivation.apply_from") }, legend: { text: I18n.t("questions.scheme.deactivate.apply_from") },
caption: { text: title }, caption: { text: title },
hint: { text: I18n.t("hints.scheme.deactivation", date: collection_start_date.to_formatted_s(:govuk_date)) } do %> hint: { text: I18n.t("hints.scheme.deactivate", date: collection_start_date.to_formatted_s(:govuk_date)) } do %>
<%= govuk_warning_text text: I18n.t("warnings.scheme.deactivation.existing_logs") %> <%= govuk_warning_text text: I18n.t("warnings.scheme.#{action}.existing_logs") %>
<%= f.govuk_radio_button :deactivation_date_type, <%= f.govuk_radio_button date_type_question(action),
"default", "default",
label: { text: "From the start of the current collection period (#{collection_start_date.to_formatted_s(:govuk_date)})" } %> label: { text: "From the start of the current collection period (#{collection_start_date.to_formatted_s(:govuk_date)})" } %>
<%= f.govuk_radio_button :deactivation_date_type, <%= f.govuk_radio_button date_type_question(action),
"other", "other",
label: { text: "For tenancies starting after a certain date" }, label: { text: "For tenancies starting after a certain date" },
**basic_conditional_html_attributes({ "deactivation_date" => %w[other] }, "scheme") do %> **basic_conditional_html_attributes({ "deactivation_date" => %w[other] }, "scheme") do %>
<%= f.govuk_date_field :deactivation_date, <%= f.govuk_date_field date_question(action),
legend: { text: "Date", size: "m" }, legend: { text: "Date", size: "m" },
hint: { text: "For example, 27 3 2022" }, hint: { text: "For example, 27 3 2022" },
width: 20 %> width: 20 %>

9
config/locales/en.yml

@ -377,7 +377,7 @@ en:
deactivation: deactivation:
apply_from: "When should this change apply?" apply_from: "When should this change apply?"
scheme: scheme:
deactivation: deactivate:
apply_from: "When should this change apply?" apply_from: "When should this change apply?"
descriptions: descriptions:
location: location:
@ -393,7 +393,7 @@ en:
units: "A unit can be a bedroom in a shared house or flat, or a house with 4 bedrooms. Do not include bedrooms used for wardens, managers, volunteers or sleep-in staff." units: "A unit can be a bedroom in a shared house or flat, or a house with 4 bedrooms. Do not include bedrooms used for wardens, managers, volunteers or sleep-in staff."
deactivation: "If the date is before %{date}, select ‘From the start of the current collection period’ because the previous period has now closed." deactivation: "If the date is before %{date}, select ‘From the start of the current collection period’ because the previous period has now closed."
scheme: scheme:
deactivation: "If the date is before %{date}, select ‘From the start of the current collection period’ because the previous period has now closed." deactivate: "If the date is before %{date}, select ‘From the start of the current collection period’ because the previous period has now closed."
warnings: warnings:
location: location:
@ -401,9 +401,12 @@ en:
existing_logs: "It will not be possible to add logs with this location if their tenancy start date is on or after the date you enter. Any existing logs may be affected." existing_logs: "It will not be possible to add logs with this location if their tenancy start date is on or after the date you enter. Any existing logs may be affected."
review_logs: "Your data providers will need to review these logs and answer a few questions again. We’ll email each log creator with a list of logs that need updating." review_logs: "Your data providers will need to review these logs and answer a few questions again. We’ll email each log creator with a list of logs that need updating."
scheme: scheme:
deactivation: deactivate:
existing_logs: "It will not be possible to add logs with this scheme if their tenancy start date is on or after the date you enter. Any existing logs may be affected." existing_logs: "It will not be possible to add logs with this scheme if their tenancy start date is on or after the date you enter. Any existing logs may be affected."
review_logs: "Your data providers will need to review these logs and answer a few questions again. We’ll email each log creator with a list of logs that need updating." review_logs: "Your data providers will need to review these logs and answer a few questions again. We’ll email each log creator with a list of logs that need updating."
reactivate:
existing_logs: "You’ll be able to add logs with this scheme if their tenancy start date is on or after the date you enter."
test: test:
one_argument: "This is based on the tenant’s work situation: %{ecstat1}" one_argument: "This is based on the tenant’s work situation: %{ecstat1}"

2
config/routes.rb

@ -52,8 +52,10 @@ Rails.application.routes.draw do
get "new-deactivation", to: "schemes#new_deactivation" get "new-deactivation", to: "schemes#new_deactivation"
get "deactivate-confirm", to: "schemes#deactivate_confirm" get "deactivate-confirm", to: "schemes#deactivate_confirm"
get "reactivate", to: "schemes#reactivate" get "reactivate", to: "schemes#reactivate"
get "new-reactivation", to: "schemes#new_reactivation"
patch "new-deactivation", to: "schemes#new_deactivation" patch "new-deactivation", to: "schemes#new_deactivation"
patch "deactivate", to: "schemes#deactivate" patch "deactivate", to: "schemes#deactivate"
patch "reactivate", to: "schemes#reactivate"
resources :locations do resources :locations do
get "edit-name", to: "locations#edit_name" get "edit-name", to: "locations#edit_name"

Loading…
Cancel
Save