From b576c4aed8fca971d65f6e4e11c719340fd35b5b Mon Sep 17 00:00:00 2001 From: Edwin Kruglov Date: Thu, 24 Feb 2022 13:52:10 +0000 Subject: [PATCH 1/3] Add case problem description edit view --- app/controllers/support/cases_controller.rb | 26 ++++++++++- app/forms/support/edit_case_form.rb | 15 +++++++ app/forms/support/edit_case_form_schema.rb | 7 +++ app/presenters/support/case_presenter.rb | 7 +++ .../support/cases/_case_details.html.erb | 6 ++- app/views/support/cases/edit.html.erb | 11 +++++ config/locales/en.yml | 7 +++ .../features/support/case_description_spec.rb | 45 +++++++++++++++++++ spec/forms/support/edit_case_form_spec.rb | 13 ++++++ .../presenters/support/case_presenter_spec.rb | 24 ++++++++++ 10 files changed, 159 insertions(+), 2 deletions(-) create mode 100644 app/forms/support/edit_case_form.rb create mode 100644 app/forms/support/edit_case_form_schema.rb create mode 100644 app/views/support/cases/edit.html.erb create mode 100644 spec/features/support/case_description_spec.rb create mode 100644 spec/forms/support/edit_case_form_spec.rb diff --git a/app/controllers/support/cases_controller.rb b/app/controllers/support/cases_controller.rb index 9a34c6846..ace1c2fce 100644 --- a/app/controllers/support/cases_controller.rb +++ b/app/controllers/support/cases_controller.rb @@ -1,7 +1,7 @@ module Support class CasesController < Cases::ApplicationController require "will_paginate/array" - before_action :current_case, only: %i[show] + before_action :current_case, only: %i[show edit] include Concerns::HasInteraction @@ -41,6 +41,22 @@ def create end end + def edit + return redirect_to support_case_path(current_case) unless current_case.created_manually? + + @back_url = support_case_path(current_case) + end + + def update + @form = EditCaseForm.from_validation(edit_validation) + if edit_validation.success? + current_case.update!(**@form.to_h) + redirect_to support_case_path(current_case), notice: I18n.t("support.case_description.flash.updated") + else + render :edit + end + end + private # @return [CasePresenter, nil] @@ -52,6 +68,10 @@ def validation CreateCaseFormSchema.new.call(**form_params) end + def edit_validation + EditCaseFormSchema.new.call(**edit_form_params) + end + def form_params params.require(:create_case_form).permit( :organisation_urn, @@ -71,5 +91,9 @@ def form_params :request_type, ) end + + def edit_form_params + params.require(:edit_case_form).permit(:request_text) + end end end diff --git a/app/forms/support/edit_case_form.rb b/app/forms/support/edit_case_form.rb new file mode 100644 index 000000000..bfed875bc --- /dev/null +++ b/app/forms/support/edit_case_form.rb @@ -0,0 +1,15 @@ +# frozen_string_literal: true + +module Support + class EditCaseForm + extend Dry::Initializer + include Concerns::ValidatableForm + + option :request_text, optional: true + + # @return [Hash] form parms as request attributes + def to_h + self.class.dry_initializer.attributes(self).except(:messages) + end + end +end diff --git a/app/forms/support/edit_case_form_schema.rb b/app/forms/support/edit_case_form_schema.rb new file mode 100644 index 000000000..5e5146f8b --- /dev/null +++ b/app/forms/support/edit_case_form_schema.rb @@ -0,0 +1,7 @@ +module Support + class EditCaseFormSchema < ::Support::Schema + params do + optional(:request_text).value(:string) + end + end +end diff --git a/app/presenters/support/case_presenter.rb b/app/presenters/support/case_presenter.rb index 1cd356b10..b270a8828 100644 --- a/app/presenters/support/case_presenter.rb +++ b/app/presenters/support/case_presenter.rb @@ -125,6 +125,13 @@ def last_updated_at_date Time.zone.parse(last_updated_at) end + # true if the case source is `nw_hub`, `sw_hub` or nil + # + # @return [Boolean] + def created_manually? + ["nw_hub", "sw_hub", nil].any? { |t| t == source } + end + private # @return [String] 20 March 2021 at 12:00 diff --git a/app/views/support/cases/_case_details.html.erb b/app/views/support/cases/_case_details.html.erb index 2b69405f6..d7856aff8 100644 --- a/app/views/support/cases/_case_details.html.erb +++ b/app/views/support/cases/_case_details.html.erb @@ -22,7 +22,11 @@
<%= current_case.request_text %>
-
+
+ <% if current_case.created_manually? %> + <%= link_to I18n.t("support.case.link.edit_description"), edit_support_case_path(current_case), class: "govuk-link" %> + <% end %> +
diff --git a/app/views/support/cases/edit.html.erb b/app/views/support/cases/edit.html.erb new file mode 100644 index 000000000..3a4014e20 --- /dev/null +++ b/app/views/support/cases/edit.html.erb @@ -0,0 +1,11 @@ +
+
+ <%= form_with model: @current_case, scope: :edit_case_form, url: support_case_path(@current_case), method: :patch do |form| %> + <%= form.govuk_error_summary %> + + <%= form.govuk_text_area :request_text, autofocus: true, rows: 5, label: { text: I18n.t("support.case_description.header"), size: "l" } %> + + <%= form.submit I18n.t("support.case_description.submit"), class: "govuk-button" %> + <% end %> +
+
diff --git a/config/locales/en.yml b/config/locales/en.yml index 5830661b9..55a2be449 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -599,6 +599,7 @@ en: all_cases: All cases link: edit_category: Change + edit_description: Change view_school_information: View school information (opens in a new tab) view_group_information: View group information (opens in a new tab) @@ -835,6 +836,12 @@ en: cases: my_cases: header_link: My cases + + case_description: + header: Update description of problem + flash: + updated: Description of problem updated successfully + submit: Save changes emails: header_link: Notifications diff --git a/spec/features/support/case_description_spec.rb b/spec/features/support/case_description_spec.rb new file mode 100644 index 000000000..fc08e34b0 --- /dev/null +++ b/spec/features/support/case_description_spec.rb @@ -0,0 +1,45 @@ +RSpec.describe "Case problem description" do + include_context "with an agent" + + before do + click_button "Agent Login" + end + + context "when a case is created via 'create a case'" do + # 'create a case' includes case source types 'nw_hub', 'sw_hub' and nil + + let(:support_case) { create(:support_case, source: nil) } + + before do + visit "/support/cases/#{support_case.id}" + end + + it "shows a change link for the problem description in case details" do + within("div#case-details") do + within(all("div.govuk-summary-list__row")[1]) do + expect(page).to have_text "Description of problem" + expect(page).to have_text "This is an example request for support - please help!" + expect(page).to have_link "Change", href: "/support/cases/#{support_case.id}/edit", class: "govuk-link" + end + end + end + + it "allows a user to update the problem description" do + within("div#case-details") do + within(all("div.govuk-summary-list__row")[1]) do + click_on "Change" + end + end + + fill_in "edit_case_form[request_text]", with: "updated problem" + click_button "Save changes" + expect(page).to have_text "Description of problem updated successfully" + + within("div#case-details") do + within(all("div.govuk-summary-list__row")[1]) do + expect(page).to have_text "updated problem" + end + end + end + end +end diff --git a/spec/forms/support/edit_case_form_spec.rb b/spec/forms/support/edit_case_form_spec.rb new file mode 100644 index 000000000..ecd645789 --- /dev/null +++ b/spec/forms/support/edit_case_form_spec.rb @@ -0,0 +1,13 @@ +RSpec.describe Support::EditCaseForm, type: :model do + subject(:form) { described_class.new } + + describe "#to_h" do + subject(:form) { described_class.new(request_text: "help") } + + it "returns form values" do + expect(form.to_h).to eql({ + request_text: "help", + }) + end + end +end diff --git a/spec/presenters/support/case_presenter_spec.rb b/spec/presenters/support/case_presenter_spec.rb index bd2e45732..313eb9b98 100644 --- a/spec/presenters/support/case_presenter_spec.rb +++ b/spec/presenters/support/case_presenter_spec.rb @@ -127,4 +127,28 @@ end end end + + describe "#created_manually?" do + context "when case source is 'nw_hub', 'sw_hub' or nil" do + it "is true" do + support_case.nw_hub! + expect(presenter.created_manually?).to eq true + support_case.sw_hub! + expect(presenter.created_manually?).to eq true + support_case.source = nil + expect(presenter.created_manually?).to eq true + end + end + + context "when case source is not 'nw_hub', 'sw_hub' or nil" do + it "is false" do + support_case.digital! + expect(presenter.created_manually?).to eq false + support_case.incoming_email! + expect(presenter.created_manually?).to eq false + support_case.faf! + expect(presenter.created_manually?).to eq false + end + end + end end From 0bf4a01fe8c6a3b25da019d5f9293cad9b9cc4bb Mon Sep 17 00:00:00 2001 From: Edwin Kruglov Date: Thu, 24 Feb 2022 14:31:37 +0000 Subject: [PATCH 2/3] Update changelog --- CHANGELOG.case_management.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.case_management.md b/CHANGELOG.case_management.md index a5711f492..1f3dd9671 100644 --- a/CHANGELOG.case_management.md +++ b/CHANGELOG.case_management.md @@ -6,6 +6,7 @@ The format is based on [Keep a Changelog 1.0.0]. ## [unreleased] +- Agent can update the problem description on a case [#ref](https://github.com/DFE-Digital/buy-for-your-school/pull/899) - Agent can save email attachments to a case [#ref](https://github.com/DFE-Digital/buy-for-your-school/pull/896)