Skip to content

Commit

Permalink
[207] On Create a case, allow Category to be 'undefined' (#848)
Browse files Browse the repository at this point in the history
  • Loading branch information
d-a-v-e authored Feb 4, 2022
1 parent 30ce394 commit 89b630c
Show file tree
Hide file tree
Showing 18 changed files with 195 additions and 17 deletions.
33 changes: 33 additions & 0 deletions app/assets/javascripts/supported_create_case.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/*
Reveal the category selection dropdown
*/

function changeCategorySelectState(state) {
const categorySelect = document.querySelectorAll("select[name='create_case_form[category_id]']")[0];

if (state == "hidden") {
categorySelect.selectedIndex = 0;
categorySelect.parentNode.classList.add("govuk-!-display-none");

} else if (state == "visible") {
categorySelect.parentNode.classList.remove("govuk-!-display-none");
}
}

window.addEventListener("load", () => {
const requestTypeYes = document.querySelectorAll("input[value=true]")[0];
const requestTypeNo = document.querySelectorAll("input[value=false]")[0];

requestTypeYes.addEventListener("change", () => {
if (requestTypeYes.checked) {
changeCategorySelectState("visible");
}
});

requestTypeNo.addEventListener("change", () => {
if (requestTypeNo.checked) {
changeCategorySelectState("hidden");
}
});
});

5 changes: 5 additions & 0 deletions app/controllers/support/cases/categorisations_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,11 @@ def update
end
end

def destroy
current_case.update!(category_id: nil)
redirect_to @back_url, notice: I18n.t("support.case_categorisations.flash.deleted")
end

private

def set_categories
Expand Down
1 change: 1 addition & 0 deletions app/controllers/support/cases/previews_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ def form_params
:estimated_savings,
:hub_notes,
:progress_notes,
:request_type,
)
end
end
Expand Down
1 change: 1 addition & 0 deletions app/controllers/support/cases_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ def form_params
:estimated_savings,
:hub_notes,
:progress_notes,
:request_type,
)
end
end
Expand Down
8 changes: 7 additions & 1 deletion app/forms/support/create_case_form.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ class CreateCaseForm
option :estimated_savings, optional: true
option :hub_notes, optional: true
option :progress_notes, optional: true
option :request_type, Types::Params::Bool | Types.Constructor(::TrueClass, &:present?), optional: true

# @see Support::Case#source
# @return [String]
Expand All @@ -33,11 +34,16 @@ def case_type
# @return [Hash] form parms
def to_h
self.class.dry_initializer.attributes(self)
.except(:messages)
.except(:messages, :request_type)
.merge(source: case_type)
.compact
end

# @return [Boolean]
def request_type?
instance_variable_get :@request_type
end

private

def north_west_check
Expand Down
5 changes: 5 additions & 0 deletions app/forms/support/create_case_form_schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ class CreateCaseFormSchema < ::Support::Schema
required(:first_name).value(:string)
required(:last_name).value(:string)
required(:email).value(:string)
optional(:request_type).value(:bool)
optional(:organisation_id).value(:string)
optional(:phone_number).value(:string)
optional(:category_id).value(:string)
Expand All @@ -36,6 +37,10 @@ class CreateCaseFormSchema < ::Support::Schema
key(:email).failure(:missing) if value.blank?
end

rule(:category_id) do
key.failure(:missing) if values[:request_type].presence && value.blank?
end

private

def invalid_school_urn?(value)
Expand Down
18 changes: 15 additions & 3 deletions app/views/support/cases/categorisations/edit.html.erb
Original file line number Diff line number Diff line change
@@ -1,11 +1,21 @@
<%= form_with model: @case_categorisation_form, scope: :case_categorisation_form, url: support_case_categorisation_path(@current_case), method: :patch do |form| %>
<%= form_with model: @case_categorisation_form, scope: :case_categorisation_form, url: support_case_categorisation_path(@current_case), method: :patch, class: "govuk-!-display-inline" do |form| %>
<%= form.govuk_error_summary %>

<%= form.govuk_radio_buttons_fieldset :category_id, legend: { text: I18n.t("support.case_categorisations.edit.header"), size: "l" } do %>
<%= form.hidden_field :category_id, value: nil %>

<div class="govuk-radios govuk-radios--small">
<% @categories.each_with_index do |category, i| %>
<% @categories.where.not(title: 'Or').each_with_index do |category, i| %>
<div class="govuk-radios__divider">
<span class="divider-caption"><%= category.title %></span>
</div>

<% category.sub_categories.ordered_by_title.each do |sub_category| %>
<%= form.govuk_radio_button :category_id, sub_category.id, label: { text: sub_category.title } %>
<% end %>
<% end %>

<% @categories.where(title: 'Or').each_with_index do |category, i| %>
<div class="govuk-radios__divider">
<span class="divider-caption"><%= category.title %></span>
</div>
Expand All @@ -18,4 +28,6 @@
<% end %>

<%= form.submit I18n.t("support.case_categorisations.edit.submit"), class: "govuk-button", role: "button" %>
<% end %>
<% end %>

<%= button_to(I18n.t("support.case_categorisations.edit.delete"), support_case_categorisation_path(@current_case), method: :delete, class: "govuk-button govuk-button--warning", form_class: "govuk-!-display-inline") if @current_case.category_id %>
35 changes: 28 additions & 7 deletions app/views/support/cases/forms/_create_case_form.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -36,14 +36,33 @@
label: { text: I18n.t("support.case_hub_migration.label.phone_number", optional: I18n.t("support.generic.form.optional")) }
%>

<%= form.govuk_select(:category_id, label: { text: I18n.t("support.case_hub_migration.label.category") }) do %>
<option data-tags="<%= I18n.t("support.case_categorisations.label.none") %>" value=""><%= I18n.t("support.case_categorisations.label.none") %></option>
<%=
form.govuk_collection_radio_buttons :request_type,
[
OpenStruct.new(value: true, label: I18n.t("support.generic.yes")),
OpenStruct.new(value: false, label: I18n.t("support.generic.no")),
],
:value,
:label,
inline: true,
hint: { text: I18n.t("support.case_hub_migration.request_type.hint") },
legend: { text: I18n.t("support.case_hub_migration.request_type.legend"), size: "l" }
%>

<%
category_id_hidden = unless form.object.category_id && form.object.request_type
"govuk-!-display-none"
end
%>

<%= form.govuk_select(:category_id, form_group: { class: "govuk-inset-text #{category_id_hidden}" }, label: { text: I18n.t("support.case_hub_migration.label.category") }) do %>
<option data-tags="<%= I18n.t("support.case_categorisations.label.none") %>" value=""><%= I18n.t("support.case_categorisations.label.please_select") %></option>
<% Support::Category.grouped_opts.each do |parent_name, sub_categories| %>
<optgroup label="<%= parent_name%>">
<% sub_categories.each do |name, value| %>
<option data-tags="<%= name %>" value="<%= value %>"><%= name %></option>
<% end %>
</optgroup>
<optgroup label="<%= parent_name%>">
<% sub_categories.each do |name, value| %>
<option data-tags="<%= name %>" value="<%= value %>"><%= name %></option>
<% end %>
</optgroup>
<% end %>
<% end %>

Expand Down Expand Up @@ -84,3 +103,5 @@

<%= form.submit I18n.t("support.case_hub_migration.submit"), class: "govuk-button", role: "button" %>
<% end %>

<%= javascript_include_tag "supported_create_case" %>
10 changes: 9 additions & 1 deletion config/locales/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -413,6 +413,8 @@ en:
#
support:
generic:
'yes': 'Yes'
'no': 'No'
link:
opens_in_new_tab: (opens in new tab)
form:
Expand Down Expand Up @@ -514,10 +516,13 @@ en:
edit:
header: Select a category
submit: Save and continue
delete: Remove procurement category
flash:
updated: Successfully changed case category
deleted: Successfully removed case category
label:
none: Not Yet Known
none: Not applicable
please_select: Please select

case_contract:
existing:
Expand Down Expand Up @@ -608,6 +613,9 @@ en:
progress_notes_hint: Enter any additional information relating to the correspondance between the Hub and the establishment
message: Message
referer: Referer
request_type:
legend: Request type
hint: Is the request related to a procurement category?
submit: Save and continue
flash:
created: Case created/migrated successfully
Expand Down
7 changes: 7 additions & 0 deletions config/locales/validation/support/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@ en:
case_migration_form:
rules:
school_urn: "" # Omitted
request_type: "" # Omitted
category_id: "" # Omitted
first_name: First name
last_name: Last name
email: Email
Expand All @@ -63,6 +65,9 @@ en:

errors:
rules:
request_type:
bool?: Select the request type
missing: Select the request type
school_urn:
missing: You must select a school
invalid: Invalid school URN
Expand All @@ -84,6 +89,8 @@ en:

estimated_procurement_completion_date:
format?: must follow the format of DD/MM/YYYY
category_id:
missing: Please select a procurement category

case_procurement_details_form:
rules:
Expand Down
2 changes: 1 addition & 1 deletion config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@
end
resource :organisation, only: %i[edit update]
resource :contact_details, only: %i[edit update]
resource :categorisation, only: %i[edit update]
resource :categorisation, only: %i[edit update destroy]
resource :savings, only: %i[edit update]
resource :procurement_details, only: %i[edit update]
resources :documents, only: %i[show]
Expand Down
7 changes: 7 additions & 0 deletions config/support/categories.yml
Original file line number Diff line number Diff line change
Expand Up @@ -174,3 +174,10 @@
- title: Boilers and plumbing services
- title: Solar

- title: Or
description:
slug: or
sub_categories:
- title: Not yet known
- title: No applicable category

6 changes: 6 additions & 0 deletions lib/tasks/case_management.rake
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,12 @@ namespace :case_management do
end
end

desc "Backfill case category"
task backfill_case_category: :environment do
category = Support::Category.find_by(title: "Not yet known")
Support::Case.where(category_id: nil).update_all(category_id: category.id)
end

desc "Populate shared inbox emails"
task seed_shared_inbox_emails: :environment do
Support::IncomingEmails::SharedMailbox.synchronize(folder: :inbox)
Expand Down
29 changes: 29 additions & 0 deletions spec/features/support/case_categorisation_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -55,4 +55,33 @@
end
end
end

describe "removing category from a case" do
context "when case has a category" do
before do
within ".govuk-summary-list__row", text: "Category" do
click_link "Change"
end
end

it "lets the user remove the category" do
click_button "Remove procurement category"
expect(find(".govuk-notification-banner__content")).to have_text "Successfully removed case category"
expect(find("#case-details")).to have_text "Not applicable"
end
end

context "when case has no category" do
before do
support_case.update!(category_id: nil)
within ".govuk-summary-list__row", text: "Category" do
click_link "Change"
end
end

it "does not show the delete button" do
expect(page).not_to have_button "Remove procurement category"
end
end
end
end
37 changes: 37 additions & 0 deletions spec/features/support/create_case_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@

expect(page).to have_current_path "/support/cases/preview"
expect(find("h1.govuk-heading-l")).to have_text "Check your answers before creating a new case"
expect(find("#changeCategory").sibling("dd")).to have_text "Not applicable"
end

it "allows you to change answers" do
Expand Down Expand Up @@ -166,12 +167,48 @@
end
end

context "with unchosen request type" do
it "only raises missing request type error" do
click_on "Save and continue"

within "div.govuk-error-summary" do
expect(page).to have_text "Select the request type"
expect(page).not_to have_text "Please select a procurement category"
end
end
end

context "with request type 'yes' and no procurement category selected", js: true do
it "only raises missing procurement category error" do
choose "Yes"
click_on "Save and continue"

within "div.govuk-error-summary" do
expect(page).not_to have_text "Select the request type"
expect(page).to have_text "Please select a procurement category"
end
end
end

context "with request type 'yes' and procurement category selected", js: true do
it "raises no errors" do
choose "Yes"
find("#create-case-form-category-id-field").find(:option, "Catering").select_option
click_on "Save and continue"
within "div.govuk-error-summary" do
expect(page).not_to have_text "Select the request type"
expect(page).not_to have_text "Please select a procurement category"
end
end
end

def valid_form_data
fill_in "create_case_form[school_urn]", with: "000001"
fill_in "create_case_form[first_name]", with: "first_name"
fill_in "create_case_form[last_name]", with: "last_name"
fill_in "create_case_form[email]", with: "test@example.com"
fill_in "create_case_form[phone_number]", with: "0778974653"
choose "No" # request type
end

def complete_valid_form
Expand Down
2 changes: 1 addition & 1 deletion spec/presenters/support/category_presenter_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

describe "#title" do
it "returns Not Yet Known for title" do
expect(presenter.title).to eq("Not Yet Known")
expect(presenter.title).to eq("Not applicable")
end
end
end
Expand Down
2 changes: 1 addition & 1 deletion spec/presenters/support/create_case_form_presenter_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@
end

it "gives Not Yet Known label" do
expect(presenter.category_name).to eq "Not Yet Known"
expect(presenter.category_name).to eq "Not applicable"
end
end
end
Expand Down
Loading

0 comments on commit 89b630c

Please sign in to comment.