Browse Source

feat: add new features from locations branch

CLDC-1672-reactivate-scheme
natdeanlewissoftwire 2 years ago
parent
commit
ff405b77cb
  1. 13
      app/helpers/schemes_helper.rb
  2. 6
      app/helpers/toggle_active_scheme_helper.rb
  3. 36
      app/models/scheme.rb
  4. 2
      app/views/schemes/toggle_active.html.erb
  5. 5
      config/locales/en.yml

13
app/helpers/schemes_helper.rb

@ -28,11 +28,14 @@ module SchemesHelper
end
def scheme_availability(scheme)
availability = "Active from #{scheme.available_from.to_formatted_s(:govuk_date)}"
scheme.scheme_deactivation_periods.each do |deactivation|
availability << " to #{(deactivation.deactivation_date - 1.day).to_formatted_s(:govuk_date)}\nDeactivated on #{deactivation.deactivation_date.to_formatted_s(:govuk_date)}"
availability << "\nActive from #{deactivation.reactivation_date.to_formatted_s(:govuk_date)}" if deactivation.reactivation_date.present?
availability = ""
scheme.active_periods.each do |period|
if period.from.present?
availability << "\nActive from #{period.from.to_formatted_s(:govuk_date)}"
availability << " to #{(period.to - 1.day).to_formatted_s(:govuk_date)}\nDeactivated on #{period.to.to_formatted_s(:govuk_date)}" if period.to.present?
end
end
availability
availability.strip
end
end

6
app/helpers/toggle_active_scheme_helper.rb

@ -1,9 +1,9 @@
module ToggleActiveSchemeHelper
def toggle_scheme_form_path(action, scheme_id)
def toggle_scheme_form_path(action, scheme)
if action == "deactivate"
scheme_new_deactivation_path(scheme_id:)
scheme_new_deactivation_path(scheme)
else
scheme_reactivate_path(scheme_id:)
scheme_reactivate_path(scheme)
end
end

36
app/models/scheme.rb

@ -214,7 +214,7 @@ class Scheme < ApplicationRecord
end
def available_from
created_at
[created_at, FormHandler.instance.current_collection_start_date].min
end
def status
@ -227,6 +227,19 @@ class Scheme < ApplicationRecord
:active
end
ActivePeriod = Struct.new(:from, :to)
def active_periods
periods = [ActivePeriod.new(available_from, nil)]
sorted_deactivation_periods = scheme_deactivation_periods.sort_by(&:deactivation_date)
sorted_deactivation_periods.each do |deactivation|
periods.find { |x| x.to.nil? }.to = deactivation.deactivation_date
periods << ActivePeriod.new(deactivation.reactivation_date, nil)
end
periods.select { |period| (period.from != period.to) && (period.to.nil? || (period.from.present? && period.from <= period.to)) }
end
def active?
status == :active
end
@ -252,10 +265,11 @@ class Scheme < ApplicationRecord
elsif deactivation_date_type == "other"
errors.add(:deactivation_date, message: I18n.t("validations.scheme.toggle_date.invalid"))
end
elsif scheme_deactivation_periods.any? { |period| deactivation_date.between?(period.deactivation_date, period.reactivation_date - 1.day) }
errors.add(:deactivation_date, message: I18n.t("validations.scheme.deactivation.during_deactivated_period"))
else
collection_start_date = FormHandler.instance.current_collection_start_date
unless deactivation_date.between?(collection_start_date, Time.zone.local(2200, 1, 1))
errors.add(:deactivation_date, message: I18n.t("validations.scheme.toggle_date.out_of_range", date: collection_start_date.to_formatted_s(:govuk_date)))
unless deactivation_date.between?(available_from, Time.zone.local(2200, 1, 1))
errors.add(:deactivation_date, message: I18n.t("validations.scheme.toggle_date.out_of_range", date: available_from.to_formatted_s(:govuk_date)))
end
end
end
@ -267,17 +281,17 @@ class Scheme < ApplicationRecord
def reactivation_date_errors
return unless @run_reactivation_validations
recent_deactivation = scheme_deactivation_periods.deactivations_without_reactivation.first
if reactivation_date.blank?
if reactivation_date_type.blank?
errors.add(:reactivation_date_type, message: I18n.t("validations.scheme.reactivation_date.not_selected"))
errors.add(:reactivation_date_type, message: I18n.t("validations.scheme.toggle_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)))
errors.add(:reactivation_date, message: I18n.t("validations.scheme.toggle_date.invalid"))
end
elsif !reactivation_date.between?(available_from, Time.zone.local(2200, 1, 1))
errors.add(:reactivation_date, message: I18n.t("validations.scheme.toggle_date.out_of_range", date: available_from.to_formatted_s(:govuk_date)))
elsif reactivation_date < recent_deactivation.deactivation_date
errors.add(:reactivation_date, message: I18n.t("validations.scheme.reactivation.before_deactivation", date: recent_deactivation.deactivation_date.to_formatted_s(:govuk_date)))
end
end
end

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

@ -6,7 +6,7 @@
href: scheme_details_path(@scheme),
) %>
<% end %>
<%= form_with model: @scheme, url: toggle_scheme_form_path(action, @scheme.id), method: "patch", local: true do |f| %>
<%= form_with model: @scheme, url: toggle_scheme_form_path(action, @scheme), method: "patch", local: true do |f| %>
<div class="govuk-grid-row">
<div class="govuk-grid-column-two-thirds">
<% collection_start_date = FormHandler.instance.current_collection_start_date %>

5
config/locales/en.yml

@ -317,6 +317,11 @@ en:
not_selected: "Select one of the options"
invalid: "Enter a valid day, month and year"
out_of_range: "The date must be on or after the %{date}"
reactivation:
before_deactivation: "This scheme was deactivated on %{date}\nThe reactivation date must be on or after deactivation date"
deactivation:
during_deactivated_period: "The scheme is already deactivated during this date, please enter a different date"
location:
deactivation_date:

Loading…
Cancel
Save