Skip to content

Commit

Permalink
feat(cms): ability to update participating schools
Browse files Browse the repository at this point in the history
Jira: PWNN-1638
  • Loading branch information
EdwinKruglov committed Sep 28, 2023
1 parent 0573084 commit e6fa00d
Show file tree
Hide file tree
Showing 34 changed files with 498 additions and 174 deletions.
28 changes: 28 additions & 0 deletions app/controllers/support/cases/confirm_organisation_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
module Support
module Cases
class ConfirmOrganisationController < Cases::ApplicationController
def show
@back_url = edit_support_case_organisation_path(current_case)
@new_organisation = get_organisation
end

def update
organisation = get_organisation
CaseManagement::AssignOrganisationToCase.new.call(
support_case_id: current_case.id,
agent_id: current_agent.id,
organisation_id: organisation.id,
organisation_type: organisation.class.name,
)
redirect_to support_case_path(current_case, anchor: "school-details"), notice: I18n.t("support.case_organisation.flash.updated")
end

private

def get_organisation
type = params[:type] == Support::EstablishmentGroup.name ? Support::EstablishmentGroup : Support::Organisation
type.find(params[:id])
end
end
end
end
11 changes: 8 additions & 3 deletions app/controllers/support/cases/organisations_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,14 @@ def edit
def update
@case_organisation_form = CaseOrganisationForm.from_validation(validation)

if validation.success? && @case_organisation_form.assign_organisation_to_case(current_case, current_agent.id)
redirect_to support_case_path(current_case, anchor: "school-details"),
notice: I18n.t("support.case_organisation.flash.updated")
if validation.success?
if !current_case.participating_schools.empty?
redirect_to support_case_confirm_organisation_path(current_case, id: @case_organisation_form.organisation_id, type: @case_organisation_form.organisation_type)
else
@case_organisation_form.assign_organisation_to_case(current_case, current_agent.id)
redirect_to support_case_path(current_case, anchor: "school-details"),
notice: I18n.t("support.case_organisation.flash.updated")
end
else
render :edit
end
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
module Support
module Cases
module SchoolDetails
class ParticipatingSchoolsController < Cases::ApplicationController
def show
@back_url = support_case_school_details_path
@participating_schools = @current_case.participating_schools.map { |s| Support::OrganisationPresenter.new(s) }
end

def edit
@back_url = support_case_school_details_participating_schools_path
@case_school_picker = current_case.school_picker
@group = current_case.organisation
@all_schools = @group.organisations.order(:name)
@filters = @all_schools.filtering(form_params[:filters] || {})
@filtered_schools = @filters.results.map { |s| Support::OrganisationPresenter.new(s) }
end

def update
@case_school_picker = current_case.school_picker(school_urns: form_params[:school_urns].compact_blank)
@case_school_picker.save!
redirect_to support_case_school_details_participating_schools_path
end

private

def form_params
params.fetch(:participating_schools, {}).permit(filters: params.key?(:clear) ? nil : { local_authorities: [], phases: [] }, school_urns: [])
end
end
end
end
end
11 changes: 11 additions & 0 deletions app/controllers/support/cases/school_details_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
module Support
module Cases
class SchoolDetailsController < Cases::ApplicationController
private

def current_case
@current_case ||= CasePresenter.new(super)
end
end
end
end
5 changes: 4 additions & 1 deletion app/cores/case_management/assign_organisation_to_case.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@ class AssignOrganisationToCase

def call(support_case_id:, agent_id:, organisation_id:, organisation_type:)
support_case = Support::Case.find(support_case_id)
support_case.update!(organisation_id:, organisation_type:)
support_case.case_organisations.destroy_all
support_case.organisation_id = organisation_id
support_case.organisation_type = organisation_type
support_case.save!

broadcast(:case_organisation_changed, { case_id: support_case_id, agent_id:, organisation_id: })
end
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
module FrameworkRequestHelper
module SchoolPickerHelper
def school_picker_la_filter_options(organisations)
CheckboxOption
.from(
Expand All @@ -10,7 +10,7 @@ def school_picker_la_filter_options(organisations)

def school_picker_phase_filter_options(organisations)
phases = organisations.pluck(:phase).uniq.map { |phase| %w[middle_primary middle_secondary].include?(phase) ? "middle" : phase }.uniq
CheckboxOption.from(I18nOption.from("faf.school_picker.phases.%%key%%", phases))
CheckboxOption.from(I18nOption.from("components.school_picker.phases.%%key%%", phases))
end

def show_school_picker_phase_filters?(organisations)
Expand Down
7 changes: 4 additions & 3 deletions app/javascript/controllers/school_picker_controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,13 @@ export default class extends Controller {
"selectAll",
"schoolTableBody",
];
static values = { scope: String };

initialize() {
this.getPreviouslySelectedSchoolUrns().forEach(urn => {
const input = document.createElement("input");
input.setAttribute("type", "hidden");
input.setAttribute("name", "framework_support_form[school_urns][]");
input.setAttribute("name", `${this.scopeValue}[school_urns][]`);
input.setAttribute("value", urn);
this.getForm().appendChild(input);
});
Expand Down Expand Up @@ -58,12 +59,12 @@ export default class extends Controller {
}

getSchoolUrnsFromForm() {
return new FormData(this.getForm()).getAll("framework_support_form[school_urns][]").filter(urn => urn && urn !== "all");
return new FormData(this.getForm()).getAll(`${this.scopeValue}[school_urns][]`).filter(urn => urn && urn !== "all");
}

getSchoolUrnsFromParams() {
const params = new URLSearchParams(window.location.search);
return params.getAll("framework_support_form[school_urns][]");
return params.getAll(`${this.scopeValue}[school_urns][]`);
}

getForm() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { Controller } from "@hotwired/stimulus"
export default class extends Controller {
static targets = ["schoolUrns"]
static outlets = ["school-picker"]
static values = { scope: String };

connect() {
this.assignCheckboxHandlers(this.element);
Expand All @@ -23,7 +24,7 @@ export default class extends Controller {
this.schoolPickerOutlet.getSchoolUrns().forEach(urn => {
const input = document.createElement("input");
input.setAttribute("type", "hidden");
input.setAttribute("name", "framework_support_form[school_urns][]");
input.setAttribute("name", `${this.scopeValue}[school_urns][]`);
input.setAttribute("value", urn);
this.element.appendChild(input);
});
Expand Down
1 change: 1 addition & 0 deletions app/models/support/case.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ class Case < ApplicationRecord
include Historyable
include QuickEditable
include Summarisable
include SchoolPickable

belongs_to :category, class_name: "Support::Category", optional: true
belongs_to :query, class_name: "Support::Query", optional: true
Expand Down
13 changes: 13 additions & 0 deletions app/models/support/case/school_pickable.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
module Support::Case::SchoolPickable
extend ActiveSupport::Concern

def school_picker(school_urns: participating_schools.map(&:urn))
Support::Case::SchoolPicker.new(support_case: self, school_urns:)
end

def pick_schools(school_urns)
schools = Support::Organisation.where(urn: school_urns)
self.participating_schools = schools
save!
end
end
12 changes: 12 additions & 0 deletions app/models/support/case/school_picker.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
class Support::Case::SchoolPicker
include ActiveModel::Model

attr_accessor(
:support_case,
:school_urns,
)

def save!
support_case.pick_schools(school_urns)
end
end
6 changes: 6 additions & 0 deletions app/presenters/support/concerns/has_organisation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,12 @@ def decorated_organisation

"#{organisation_type}Presenter".safe_constantize.new(organisation)
end

def eligible_for_school_picker?
return false unless organisation

decorated_organisation.eligible_for_school_picker?
end
end
end
end
4 changes: 4 additions & 0 deletions app/presenters/support/establishment_group_presenter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -52,5 +52,9 @@ def group_type
def ukprn
super.presence || I18n.t("generic.not_provided")
end

def eligible_for_school_picker?
(mat_or_trust? || federation?) && organisations.count > 1
end
end
end
4 changes: 4 additions & 0 deletions app/presenters/support/organisation_presenter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -55,5 +55,9 @@ def gias_label
def ukprn
super.presence || I18n.t("generic.not_provided")
end

def eligible_for_school_picker?
false
end
end
end
14 changes: 14 additions & 0 deletions app/views/components/school_picker/_school_picker.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<div class="govuk-grid-row">
<div class="govuk-grid-column-one-third">
<%= render "components/school_picker/school_picker_filters", model: filters_model, url: filters_url, scope: %>
</div>
<div class="govuk-grid-column-two-thirds">
<%= form_with model: results_model, scope:, url: update_url, method: do |form| %>
<%= form.govuk_error_summary %>

<%= render "components/school_picker/school_picker_results", form: form, scope: %>

<%= form.submit I18n.t("generic.button.save"), class: "govuk-button", role: "button" %>
<% end %>
</div>
</div>
10 changes: 10 additions & 0 deletions app/views/components/school_picker/_school_picker_filters.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<div class="filters-panel">
<%= form_with model:, scope:, method: :get, url:, html: { "data-controller" => "school-picker-filters", "data-school-picker-filters-school-picker-outlet" => ".school-picker", "data-school-picker-filters-scope-value" => scope } do |form| %>
<%= form.submit I18n.t("components.school_picker.filters.clear"), class: "govuk-button govuk-button--secondary", name: :clear, data: { action: "school-picker-filters#setSchoolUrns" } %>

<%= form.fields :filters, model: do |f| %>
<%= f.govuk_collection_check_boxes :phases, school_picker_phase_filter_options(@all_schools), :id, :title, small: true, legend: { text: I18n.t("components.school_picker.filters.phase"), size: "s" } if show_school_picker_phase_filters?(@all_schools) %>
<%= f.govuk_collection_check_boxes :local_authorities, school_picker_la_filter_options(@all_schools), :id, :title, small: true, legend: { text: I18n.t("components.school_picker.filters.local_authority"), size: "s" } if show_school_picker_la_filters?(@all_schools) %>
<% end %>
<% end %>
</div>
32 changes: 32 additions & 0 deletions app/views/components/school_picker/_school_picker_results.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<div class="school-picker" data-controller="school-picker" data-school-picker-scope-value=<%= scope %>>
<p class="govuk-body">
<b data-school-picker-target="selectedCount"></b>
<%= I18n.t("components.school_picker.table.selected", total: @all_schools.count, group_name: @group.name).html_safe %>
</p>
<%= form.govuk_check_boxes_fieldset :school_urns, legend: nil do %>
<table class="govuk-table" aria-label="Available schools">
<thead class="govuk-table__head">
<tr class="govuk-table__row">
<th scope="col" class="govuk-table__header">
<%= form.govuk_check_box :school_urns, :all, label: { text: I18n.t("components.school_picker.table.school_name").html_safe }, "data-school-picker-target": "selectAll", "data-action": "school-picker#toggleSelectAll" %>
</th>
<th scope="col" class="govuk-table__header"><%= I18n.t("components.school_picker.table.address") %></th>
<th scope="col" class="govuk-table__header"><%= I18n.t("components.school_picker.table.phase") %></th>
<th scope="col" class="govuk-table__header"><%= I18n.t("components.school_picker.table.local_authority") %></th>
</tr>
</thead>
<tbody class="govuk-table__body" data-school-picker-target="schoolTableBody">
<% @filtered_schools.each do |school| %>
<tr class="govuk-table__row">
<td class="govuk-table__cell">
<%= form.govuk_check_box :school_urns, school.urn, label: { text: school.name }, "data-action": "school-picker#toggleSelection" %>
</td>
<td class="govuk-table__cell"><%= school.formatted_address %></td>
<td class="govuk-table__cell"><%= school.phase %></td>
<td class="govuk-table__cell"><%= school.local_authority %></td>
</tr>
<% end %>
</tbody>
</table>
<% end %>
</div>
6 changes: 3 additions & 3 deletions app/views/framework_requests/confirm_schools/_form.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@
<thead class="govuk-table__head">
<tr class="govuk-table__row">
<th scope="col" class="govuk-table__header"><%= I18n.t("faf.confirm_schools.table.school_name") %></th>
<th scope="col" class="govuk-table__header"><%= I18n.t("faf.school_picker.table.address") %></th>
<th scope="col" class="govuk-table__header"><%= I18n.t("faf.school_picker.table.phase") %></th>
<th scope="col" class="govuk-table__header"><%= I18n.t("faf.school_picker.table.local_authority") %></th>
<th scope="col" class="govuk-table__header"><%= I18n.t("components.school_picker.table.address") %></th>
<th scope="col" class="govuk-table__header"><%= I18n.t("components.school_picker.table.phase") %></th>
<th scope="col" class="govuk-table__header"><%= I18n.t("components.school_picker.table.local_authority") %></th>
</tr>
</thead>
<tbody class="govuk-table__body" data-school-picker-target="schoolTableBody">
Expand Down
11 changes: 1 addition & 10 deletions app/views/framework_requests/school_pickers/_filters.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,4 @@
url = edit ? edit_framework_request_school_picker_path : school_picker_framework_requests_path
%>

<div class="filters-panel">
<%= form_with model: @form, scope: :framework_support_form, method: :get, url:, html: { "data-controller" => "school-picker-filters", "data-school-picker-filters-school-picker-outlet" => ".school-picker" } do |form| %>
<%= form.submit I18n.t("faf.school_picker.filters.clear"), class: "govuk-button govuk-button--secondary", name: :clear, data: { action: "school-picker-filters#setSchoolUrns" } %>

<%= form.fields :filters, model: @form.filters do |f| %>
<%= f.govuk_collection_check_boxes :phases, school_picker_phase_filter_options(@all_schools), :id, :title, small: true, legend: { text: I18n.t("faf.school_picker.filters.phase"), size: "s" } if show_school_picker_phase_filters?(@all_schools) %>
<%= f.govuk_collection_check_boxes :local_authorities, school_picker_la_filter_options(@all_schools), :id, :title, small: true, legend: { text: I18n.t("faf.school_picker.filters.local_authority"), size: "s" } if show_school_picker_la_filters?(@all_schools) %>
<% end %>
<% end %>
</div>
<%= render "components/school_picker/school_picker_filters", model: @form.filters, url:, scope: :framework_support_form %>
33 changes: 1 addition & 32 deletions app/views/framework_requests/school_pickers/_form.html.erb
Original file line number Diff line number Diff line change
@@ -1,35 +1,4 @@
<%= render "framework_requests/common_form_fields", form: form %>
<%= form.hidden_field :source %>

<div class="school-picker" data-controller="school-picker">
<p class="govuk-body">
<b data-school-picker-target="selectedCount"></b>
<%= I18n.t("faf.school_picker.table.selected", total: @all_schools.count, group_name: @group.name).html_safe %>
</p>
<%= form.govuk_check_boxes_fieldset :school_urns, legend: nil do %>
<table class="govuk-table" aria-label="Available schools">
<thead class="govuk-table__head">
<tr class="govuk-table__row">
<th scope="col" class="govuk-table__header">
<%= form.govuk_check_box :school_urns, :all, label: { text: I18n.t("faf.school_picker.table.school_name").html_safe }, "data-school-picker-target": "selectAll", "data-action": "school-picker#toggleSelectAll" %>
</th>
<th scope="col" class="govuk-table__header"><%= I18n.t("faf.school_picker.table.address") %></th>
<th scope="col" class="govuk-table__header"><%= I18n.t("faf.school_picker.table.phase") %></th>
<th scope="col" class="govuk-table__header"><%= I18n.t("faf.school_picker.table.local_authority") %></th>
</tr>
</thead>
<tbody class="govuk-table__body" data-school-picker-target="schoolTableBody">
<% @filtered_schools.each do |school| %>
<tr class="govuk-table__row">
<td class="govuk-table__cell">
<%= form.govuk_check_box :school_urns, school.urn, label: { text: school.name }, "data-action": "school-picker#toggleSelection" %>
</td>
<td class="govuk-table__cell"><%= school.formatted_address %></td>
<td class="govuk-table__cell"><%= school.phase %></td>
<td class="govuk-table__cell"><%= school.local_authority %></td>
</tr>
<% end %>
</tbody>
</table>
<% end %>
</div>
<%= render "components/school_picker/school_picker_results", form: form, scope: :framework_support_form %>
Loading

0 comments on commit e6fa00d

Please sign in to comment.