Browse Source

Update affected logs when a scheme gets deactivated

pull/1004/head
Kat 4 years ago
parent
commit
13e8687c65
  1. 6
      app/controllers/schemes_controller.rb
  2. 2
      app/models/lettings_log.rb
  3. 5
      app/models/scheme.rb
  4. 3
      app/models/scheme_deactivation_period.rb
  5. 55
      spec/requests/schemes_controller_spec.rb

6
app/controllers/schemes_controller.rb

@ -44,7 +44,7 @@ class SchemesController < ApplicationController
def deactivate def deactivate
@scheme.run_deactivation_validations! @scheme.run_deactivation_validations!
if @scheme.update!(deactivation_date:) if @scheme.scheme_deactivation_periods.create!(deactivation_date:) && update_affected_logs
flash[:notice] = deactivate_success_notice flash[:notice] = deactivate_success_notice
end end
redirect_to scheme_details_path(@scheme) redirect_to scheme_details_path(@scheme)
@ -319,4 +319,8 @@ private
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
@scheme.lettings_logs.filter_by_before_startdate(deactivation_date.to_time).update!(location: nil, scheme: nil)
end
end end

2
app/models/lettings_log.rb

@ -45,7 +45,7 @@ class LettingsLog < Log
.or(filter_by_postcode(param)) .or(filter_by_postcode(param))
.or(filter_by_id(param)) .or(filter_by_id(param))
} }
scope :filter_by_before_startdate, ->(date) { left_joins(:location).where("lettings_logs.startdate >= ?", date) } scope :filter_by_before_startdate, ->(date) { where("lettings_logs.startdate >= ?", date) }
AUTOGENERATED_FIELDS = %w[id status created_at updated_at discarded_at].freeze AUTOGENERATED_FIELDS = %w[id status created_at updated_at discarded_at].freeze
OPTIONAL_FIELDS = %w[first_time_property_let_as_social_housing tenancycode propcode].freeze OPTIONAL_FIELDS = %w[first_time_property_let_as_social_housing tenancycode propcode].freeze

5
app/models/scheme.rb

@ -3,6 +3,7 @@ class Scheme < ApplicationRecord
belongs_to :managing_organisation, optional: true, class_name: "Organisation" belongs_to :managing_organisation, optional: true, class_name: "Organisation"
has_many :locations, dependent: :delete_all has_many :locations, dependent: :delete_all
has_many :lettings_logs, class_name: "LettingsLog", dependent: :delete_all has_many :lettings_logs, class_name: "LettingsLog", dependent: :delete_all
has_many :scheme_deactivation_periods, class_name: "SchemeDeactivationPeriod"
has_paper_trail has_paper_trail
@ -22,7 +23,7 @@ class Scheme < ApplicationRecord
auto_strip_attributes :service_name auto_strip_attributes :service_name
attr_accessor :deactivation_date_type, :run_deactivation_validations attr_accessor :deactivation_date_type, :deactivation_date, :run_deactivation_validations
SENSITIVE = { SENSITIVE = {
No: 0, No: 0,
@ -245,7 +246,7 @@ class Scheme < ApplicationRecord
end end
else else
collection_start_date = FormHandler.instance.current_collection_start_date collection_start_date = FormHandler.instance.current_collection_start_date
unless deactivation_date.between?(collection_start_date, Date.new(2200, 1, 1)) unless deactivation_date.between?(collection_start_date, Time.zone.local(2200, 1, 1))
errors.add(:deactivation_date, message: I18n.t("validations.scheme.deactivation_date.out_of_range", date: collection_start_date.to_formatted_s(:govuk_date))) errors.add(:deactivation_date, message: I18n.t("validations.scheme.deactivation_date.out_of_range", date: collection_start_date.to_formatted_s(:govuk_date)))
end end
end end

3
app/models/scheme_deactivation_period.rb

@ -0,0 +1,3 @@
class SchemeDeactivationPeriod < ApplicationRecord
scope :deactivations_without_reactivation, -> { where(reactivation_date: nil) }
end

55
spec/requests/schemes_controller_spec.rb

@ -250,41 +250,15 @@ RSpec.describe SchemesController, type: :request do
before do before do
Timecop.freeze(Time.utc(2022, 10, 10)) Timecop.freeze(Time.utc(2022, 10, 10))
sign_in user sign_in user
scheme.deactivation_date = deactivation_date
scheme.deactivation_date_type = deactivation_date_type
scheme.save!
get "/schemes/#{scheme.id}" get "/schemes/#{scheme.id}"
end end
context "with active scheme" do context "with active scheme" do
let(:deactivation_date) { nil }
let(:deactivation_date_type) { nil }
it "renders deactivate this scheme" do it "renders deactivate this scheme" do
expect(response).to have_http_status(:ok) expect(response).to have_http_status(:ok)
expect(page).to have_link("Deactivate this scheme", href: "/schemes/#{scheme.id}/new-deactivation") expect(page).to have_link("Deactivate this scheme", href: "/schemes/#{scheme.id}/new-deactivation")
end end
end end
context "with deactivated scheme" do
let(:deactivation_date) { Time.utc(2022, 10, 9) }
let(:deactivation_date_type) { "other" }
it "renders reactivate this scheme" do
expect(response).to have_http_status(:ok)
expect(page).to have_link("Reactivate this scheme", href: "/schemes/#{scheme.id}/reactivate")
end
end
context "with scheme that's deactivating soon" do
let(:deactivation_date) { Time.utc(2022, 10, 12) }
let(:deactivation_date_type) { "other" }
it "renders reactivate this scheme" do
expect(response).to have_http_status(:ok)
expect(page).to have_link("Reactivate this scheme", href: "/schemes/#{scheme.id}/reactivate")
end
end
end end
end end
@ -1768,8 +1742,11 @@ RSpec.describe SchemesController, type: :request do
context "when signed in as a data coordinator" do context "when signed in as a data coordinator" do
let(:user) { FactoryBot.create(:user, :data_coordinator) } let(:user) { FactoryBot.create(:user, :data_coordinator) }
let!(:scheme) { FactoryBot.create(:scheme, owning_organisation: user.organisation) } let!(:scheme) { FactoryBot.create(:scheme, owning_organisation: user.organisation) }
let!(:location) { FactoryBot.create(:location, scheme:) }
let(:startdate) { Time.utc(2021, 1, 2) } let(:startdate) { Time.utc(2021, 1, 2) }
let(:deactivation_date) { Time.utc(2022, 10, 10) } let(:deactivation_date) { Time.utc(2022, 10, 10) }
let!(:lettings_log) { FactoryBot.create(:lettings_log, :sh, location:, scheme:, startdate:, owning_organisation: user.organisation) }
let(:startdate) { Time.utc(2022, 10, 11) }
before do before do
Timecop.freeze(Time.utc(2022, 10, 10)) Timecop.freeze(Time.utc(2022, 10, 10))
@ -1810,9 +1787,31 @@ RSpec.describe SchemesController, type: :request do
follow_redirect! follow_redirect!
follow_redirect! follow_redirect!
expect(response).to have_http_status(:ok) expect(response).to have_http_status(:ok)
expect(page).to have_css(".govuk-notification-banner.govuk-notification-banner--success")
scheme.reload scheme.reload
expect(scheme.deactivation_date).to eq(deactivation_date) expect(scheme.scheme_deactivation_periods.count).to eq(1)
expect(scheme.scheme_deactivation_periods.first.deactivation_date).to eq(deactivation_date)
end
context "and a log startdate is after scheme deactivation date" do
it "clears the scheme and scheme answers" do
expect(lettings_log.scheme).to eq(scheme)
expect(lettings_log.scheme).to eq(scheme)
lettings_log.reload
expect(lettings_log.scheme).to eq(nil)
expect(lettings_log.scheme).to eq(nil)
end
end
context "and a log startdate is before scheme deactivation date" do
let(:startdate) { Time.utc(2022, 10, 9) }
it "does not update the log" do
expect(lettings_log.scheme).to eq(scheme)
expect(lettings_log.scheme).to eq(scheme)
lettings_log.reload
expect(lettings_log.scheme).to eq(scheme)
expect(lettings_log.scheme).to eq(scheme)
end
end end
end end

Loading…
Cancel
Save