From 5ec33c793862e3ccf22f54c52d02049066e1904e Mon Sep 17 00:00:00 2001
From: kosiakkatrina <54268893+kosiakkatrina@users.noreply.github.com>
Date: Tue, 29 Aug 2023 16:49:30 +0200
Subject: [PATCH] CLDC-2642 Make other client group mandatory (#1871)
* Make has_other_client_group a mandatory question
* Add a rake task to update has_other_client_group
* test and lint
* Set has_other_client group on import
---
app/models/scheme.rb | 2 +-
.../imports/scheme_location_import_service.rb | 3 +
app/views/schemes/confirm_secondary.html.erb | 2 +
db/seeds.rb | 3 +
...correct_has_other_client_group_values.rake | 5 ++
spec/factories/scheme.rb | 2 +
...rect_has_other_client_group_values_spec.rb | 56 +++++++++++++++++++
.../scheme_location_import_service_spec.rb | 1 +
8 files changed, 73 insertions(+), 1 deletion(-)
create mode 100644 lib/tasks/correct_has_other_client_group_values.rake
create mode 100644 spec/lib/tasks/correct_has_other_client_group_values_spec.rb
diff --git a/app/models/scheme.rb b/app/models/scheme.rb
index 08d7dedd5..6afe8cd81 100644
--- a/app/models/scheme.rb
+++ b/app/models/scheme.rb
@@ -238,7 +238,7 @@ class Scheme < ApplicationRecord
end
def validate_confirmed
- required_attributes = attribute_names - %w[id created_at updated_at old_id old_visible_id confirmed end_date sensitive secondary_client_group total_units has_other_client_group deactivation_date deactivation_date_type]
+ required_attributes = attribute_names - %w[id created_at updated_at old_id old_visible_id confirmed end_date sensitive secondary_client_group total_units deactivation_date deactivation_date_type]
if confirmed == true
required_attributes.any? do |attribute|
diff --git a/app/services/imports/scheme_location_import_service.rb b/app/services/imports/scheme_location_import_service.rb
index 79c35f337..b09a9cd56 100644
--- a/app/services/imports/scheme_location_import_service.rb
+++ b/app/services/imports/scheme_location_import_service.rb
@@ -34,6 +34,7 @@ module Imports
intended_stay: attributes["intended_stay"],
primary_client_group: attributes["primary_client_group"],
secondary_client_group: attributes["secondary_client_group"],
+ has_other_client_group: attributes["has_other_client_group"],
sensitive: attributes["sensitive"],
# These values were set by the scheme import (management groups)
owning_organisation_id: source_scheme.owning_organisation_id,
@@ -56,6 +57,7 @@ module Imports
support_type: attributes["support_type"],
intended_stay: attributes["intended_stay"],
primary_client_group: attributes["primary_client_group"],
+ has_other_client_group: attributes["has_other_client_group"],
secondary_client_group: attributes["secondary_client_group"],
sensitive: attributes["sensitive"],
}
@@ -83,6 +85,7 @@ module Imports
attributes["primary_client_group"] = string_or_nil(xml_doc, "client-group-1")
attributes["secondary_client_group"] = string_or_nil(xml_doc, "client-group-2")
attributes["secondary_client_group"] = nil if attributes["primary_client_group"] == attributes["secondary_client_group"]
+ attributes["has_other_client_group"] = attributes["secondary_client_group"].present? ? 1 : 0
attributes["sensitive"] = sensitive(xml_doc)
attributes["start_date"] = parse_date(xml_doc, "start-date")
attributes["end_date"] = parse_date(xml_doc, "end-date")
diff --git a/app/views/schemes/confirm_secondary.html.erb b/app/views/schemes/confirm_secondary.html.erb
index e267b3289..706d14c83 100644
--- a/app/views/schemes/confirm_secondary.html.erb
+++ b/app/views/schemes/confirm_secondary.html.erb
@@ -11,6 +11,8 @@
<%= form_for(@scheme, method: :patch) do |f| %>
+ <%= f.govuk_error_summary %>
+
<% selection = [OpenStruct.new(id: "Yes", name: "Yes"), OpenStruct.new(id: "No", name: "No")] %>
<%= f.govuk_collection_radio_buttons :has_other_client_group,
selection,
diff --git a/db/seeds.rb b/db/seeds.rb
index 0358216d0..94d36a7fb 100644
--- a/db/seeds.rb
+++ b/db/seeds.rb
@@ -253,6 +253,7 @@ unless Rails.env.test?
scheme_type: 4,
intended_stay: "M",
primary_client_group: "O",
+ has_other_client_group: 1,
secondary_client_group: "H",
owning_organisation: org,
arrangement_type: "D",
@@ -269,6 +270,7 @@ unless Rails.env.test?
intended_stay: "S",
primary_client_group: "D",
secondary_client_group: "E",
+ has_other_client_group: 1,
owning_organisation: org,
arrangement_type: "D",
confirmed: true,
@@ -283,6 +285,7 @@ unless Rails.env.test?
scheme_type: 7,
intended_stay: "X",
primary_client_group: "G",
+ has_other_client_group: 1,
secondary_client_group: "R",
owning_organisation: dummy_org,
arrangement_type: "D",
diff --git a/lib/tasks/correct_has_other_client_group_values.rake b/lib/tasks/correct_has_other_client_group_values.rake
new file mode 100644
index 000000000..1ee51a5e4
--- /dev/null
+++ b/lib/tasks/correct_has_other_client_group_values.rake
@@ -0,0 +1,5 @@
+desc "Update has_other_client_group values for schemes"
+task correct_has_other_client_group_values: :environment do
+ Scheme.where(confirmed: true, secondary_client_group: nil).update_all(has_other_client_group: 0)
+ Scheme.where(confirmed: true).where.not(secondary_client_group: nil).update_all(has_other_client_group: 1)
+end
diff --git a/spec/factories/scheme.rb b/spec/factories/scheme.rb
index 5441df32e..3877e3c99 100644
--- a/spec/factories/scheme.rb
+++ b/spec/factories/scheme.rb
@@ -9,6 +9,7 @@ FactoryBot.define do
intended_stay { %w[M P S V X].sample }
primary_client_group { %w[O H M L A G F B D E I S N R Q P X].sample }
secondary_client_group { %w[O H M L A G F B D E I S N R Q P X].sample }
+ has_other_client_group { 1 }
owning_organisation { FactoryBot.create(:organisation) }
confirmed { true }
created_at { Time.zone.local(2021, 4, 1) }
@@ -20,6 +21,7 @@ FactoryBot.define do
intended_stay { "M" }
primary_client_group { "G" }
secondary_client_group { "M" }
+ has_other_client_group { 1 }
end
trait :with_old_visible_id do
diff --git a/spec/lib/tasks/correct_has_other_client_group_values_spec.rb b/spec/lib/tasks/correct_has_other_client_group_values_spec.rb
new file mode 100644
index 000000000..9e021b05e
--- /dev/null
+++ b/spec/lib/tasks/correct_has_other_client_group_values_spec.rb
@@ -0,0 +1,56 @@
+require "rails_helper"
+require "rake"
+
+RSpec.describe "correct_has_other_client_group_values" do
+ describe ":correct_has_other_client_group_values", type: :task do
+ subject(:task) { Rake::Task["correct_has_other_client_group_values"] }
+
+ before do
+ Rake.application.rake_require("tasks/correct_has_other_client_group_values")
+ Rake::Task.define_task(:environment)
+ task.reenable
+ end
+
+ context "when the rake task is run" do
+ let!(:scheme) { create(:scheme, secondary_client_group: nil) }
+
+ before do
+ scheme.has_other_client_group = nil
+ scheme.save!(validate: false)
+ end
+
+ context "and the scheme is marked confirmed" do
+ it "updates schemes with secondary_client_group to have has_other_client_group 1 (yes)" do
+ scheme.secondary_client_group = "G"
+ scheme.save!(validate: false)
+ task.invoke
+ expect(scheme.reload.has_other_client_group).to eq("Yes")
+ end
+
+ it "updates schemes without secondary_client_group to have has_other_client_group 0 (no)" do
+ task.invoke
+ expect(scheme.reload.has_other_client_group).to eq("No")
+ end
+ end
+
+ context "and the scheme is not marked confirmed" do
+ before do
+ scheme.confirmed = false
+ scheme.save!(validate: false)
+ end
+
+ it "does not update schemes with secondary_client_group" do
+ scheme.secondary_client_group = "G"
+ scheme.save!(validate: false)
+ task.invoke
+ expect(scheme.reload.has_other_client_group).to eq(nil)
+ end
+
+ it "does not update schemes without secondary_client_group" do
+ task.invoke
+ expect(scheme.reload.has_other_client_group).to eq(nil)
+ end
+ end
+ end
+ end
+end
diff --git a/spec/services/imports/scheme_location_import_service_spec.rb b/spec/services/imports/scheme_location_import_service_spec.rb
index a1e20de63..f0cbde6fe 100644
--- a/spec/services/imports/scheme_location_import_service_spec.rb
+++ b/spec/services/imports/scheme_location_import_service_spec.rb
@@ -162,6 +162,7 @@ RSpec.describe Imports::SchemeLocationImportService do
expect(location.scheme.intended_stay).to eq("Permanent")
expect(location.scheme.primary_client_group).to eq("Older people with support needs")
expect(location.scheme.secondary_client_group).to be_nil
+ expect(location.scheme.has_other_client_group).to eq("No")
expect(location.scheme.sensitive).to eq("No")
expect(location.scheme.confirmed).to be_truthy
end