diff --git a/app/helpers/schemes_helper.rb b/app/helpers/schemes_helper.rb index 6ef82c570..6d12f6675 100644 --- a/app/helpers/schemes_helper.rb +++ b/app/helpers/schemes_helper.rb @@ -14,7 +14,7 @@ module SchemesHelper { name: "Secondary client group", value: scheme.secondary_client_group }, { name: "Level of support given", value: scheme.support_type }, { name: "Intended length of stay", value: scheme.intended_stay }, - { name: "Availability", value: "Available from #{scheme.available_from.to_formatted_s(:govuk_date)}" }, + { name: "Availability", value: scheme_availability(scheme) }, ] if FeatureToggle.scheme_toggle_enabled? @@ -26,4 +26,13 @@ module SchemesHelper end base_attributes 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? + end + availability + end end diff --git a/spec/helpers/schemes_helper_spec.rb b/spec/helpers/schemes_helper_spec.rb index 46dfbd7c2..1924a6cc5 100644 --- a/spec/helpers/schemes_helper_spec.rb +++ b/spec/helpers/schemes_helper_spec.rb @@ -18,10 +18,33 @@ RSpec.describe SchemesHelper do { name: "Secondary client group", value: scheme.secondary_client_group }, { name: "Level of support given", value: scheme.support_type }, { name: "Intended length of stay", value: scheme.intended_stay }, - { name: "Availability", value: "Available from 8 August 2022" }, + { name: "Availability", value: "Active from 8 August 2022" }, { name: "Status", value: :active }, ] expect(display_scheme_attributes(scheme)).to eq(attributes) end + + context "when viewing availability" do + context "with are no deactivations" do + it "displays created_at as availability date" do + availability_attribute = display_scheme_attributes(scheme).find { |x| x[:name] == "Availability" }[:value] + + expect(availability_attribute).to eq("Active from #{scheme.created_at.to_formatted_s(:govuk_date)}") + end + end + + context "with previous deactivations" do + before do + scheme.scheme_deactivation_periods << FactoryBot.create(:scheme_deactivation_period, deactivation_date: Time.zone.local(2022, 8, 10), reactivation_date: Time.zone.local(2022, 9, 1)) + scheme.scheme_deactivation_periods << FactoryBot.create(:scheme_deactivation_period, deactivation_date: Time.zone.local(2022, 9, 15), reactivation_date: nil) + end + + it "displays the timeline of availability" do + availability_attribute = display_scheme_attributes(scheme).find { |x| x[:name] == "Availability" }[:value] + + expect(availability_attribute).to eq("Active from 8 August 2022 to 9 August 2022\nDeactivated on 10 August 2022\nActive from 1 September 2022 to 14 September 2022\nDeactivated on 15 September 2022") + end + end + end end end