Browse Source

Update location default deactivation date

pull/1721/head
Kat 2 years ago
parent
commit
d4991ce2d1
  1. 2
      app/controllers/locations_controller.rb
  2. 16
      app/models/form_handler.rb
  3. 4
      app/models/location_deactivation_period.rb
  4. 2
      app/views/locations/toggle_active.html.erb
  5. 2
      spec/requests/locations_controller_spec.rb

2
app/controllers/locations_controller.rb

@ -280,7 +280,7 @@ private
if params[:location_deactivation_period].blank? if params[:location_deactivation_period].blank?
return return
elsif params[:location_deactivation_period]["#{key}_type".to_sym] == "default" elsif params[:location_deactivation_period]["#{key}_type".to_sym] == "default"
return FormHandler.instance.start_date_of_earliest_open_collection_period return FormHandler.instance.start_date_of_earliest_open_for_editing_collection_period
elsif params[:location_deactivation_period][key.to_sym].present? elsif params[:location_deactivation_period][key.to_sym].present?
return params[:location_deactivation_period][key.to_sym] return params[:location_deactivation_period][key.to_sym]
end end

16
app/models/form_handler.rb

@ -103,10 +103,18 @@ class FormHandler
in_crossover_period? ? previous_collection_start_date : current_collection_start_date in_crossover_period? ? previous_collection_start_date : current_collection_start_date
end end
def start_date_of_earliest_open_for_editing_collection_period
in_edit_crossover_period? ? previous_collection_start_date : current_collection_start_date
end
def in_crossover_period?(now: Time.zone.now) def in_crossover_period?(now: Time.zone.now)
lettings_in_crossover_period?(now:) || sales_in_crossover_period?(now:) lettings_in_crossover_period?(now:) || sales_in_crossover_period?(now:)
end end
def in_edit_crossover_period?(now: Time.zone.now)
lettings_in_edit_crossover_period?(now:) || sales_in_edit_crossover_period?(now:)
end
def lettings_in_crossover_period?(now: Time.zone.now) def lettings_in_crossover_period?(now: Time.zone.now)
forms = lettings_forms.values forms = lettings_forms.values
forms.count { |form| now.between?(form.start_date, form.new_logs_end_date) } > 1 forms.count { |form| now.between?(form.start_date, form.new_logs_end_date) } > 1
@ -145,6 +153,14 @@ class FormHandler
end end
end end
def earliest_open_for_editing_collection_start_date(now: Time.zone.now)
if in_edit_crossover_period?(now:)
collection_start_date(now) - 1.year
else
collection_start_date(now)
end
end
private private
def get_all_forms def get_all_forms

4
app/models/location_deactivation_period.rb

@ -34,8 +34,8 @@ class LocationDeactivationPeriodValidator < ActiveModel::Validator
end end
elsif location.location_deactivation_periods.any? { |period| period.reactivation_date.present? && record.deactivation_date.between?(period.deactivation_date, period.reactivation_date - 1.day) } elsif location.location_deactivation_periods.any? { |period| period.reactivation_date.present? && record.deactivation_date.between?(period.deactivation_date, period.reactivation_date - 1.day) }
record.errors.add(:deactivation_date, message: I18n.t("validations.location.deactivation.during_deactivated_period")) record.errors.add(:deactivation_date, message: I18n.t("validations.location.deactivation.during_deactivated_period"))
elsif record.deactivation_date.before? FormHandler.instance.start_date_of_earliest_open_collection_period elsif record.deactivation_date.before? FormHandler.instance.start_date_of_earliest_open_for_editing_collection_period
record.errors.add(:deactivation_date, message: I18n.t("validations.location.toggle_date.out_of_range", date: FormHandler.instance.start_date_of_earliest_open_collection_period.to_formatted_s(:govuk_date))) record.errors.add(:deactivation_date, message: I18n.t("validations.location.toggle_date.out_of_range", date: FormHandler.instance.start_date_of_earliest_open_for_editing_collection_period.to_formatted_s(:govuk_date)))
elsif record.deactivation_date.before? location.available_from elsif record.deactivation_date.before? location.available_from
record.errors.add(:deactivation_date, message: I18n.t("validations.location.toggle_date.before_creation", date: location.available_from.to_formatted_s(:govuk_date))) record.errors.add(:deactivation_date, message: I18n.t("validations.location.toggle_date.before_creation", date: location.available_from.to_formatted_s(:govuk_date)))
end end

2
app/views/locations/toggle_active.html.erb

@ -10,7 +10,7 @@
<%= form_with model: @location_deactivation_period, url: toggle_location_form_path(action, @location), method: "patch", local: true do |f| %> <%= form_with model: @location_deactivation_period, url: toggle_location_form_path(action, @location), 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">
<% start_date = FormHandler.instance.earliest_open_collection_start_date(now: @location.available_from) %> <% start_date = FormHandler.instance.earliest_open_for_editing_collection_start_date %>
<%= f.govuk_error_summary %> <%= f.govuk_error_summary %>
<%= f.govuk_radio_buttons_fieldset date_type_question(action), <%= f.govuk_radio_buttons_fieldset date_type_question(action),
legend: { text: I18n.t("questions.location.toggle_active.apply_from") }, legend: { text: I18n.t("questions.location.toggle_active.apply_from") },

2
spec/requests/locations_controller_spec.rb

@ -1832,7 +1832,7 @@ RSpec.describe LocationsController, type: :request do
let(:startdate) { Time.utc(2022, 9, 11) } let(:startdate) { Time.utc(2022, 9, 11) }
before do before do
Timecop.freeze(Time.utc(2023, 1, 10)) Timecop.freeze(Time.utc(2023, 9, 10))
sign_in user sign_in user
create(:location_deactivation_period, deactivation_date:, location:) create(:location_deactivation_period, deactivation_date:, location:)
location.save! location.save!

Loading…
Cancel
Save