Skip to content

Commit

Permalink
feat(crm): Adding non participating schools in a group
Browse files Browse the repository at this point in the history
  • Loading branch information
DevMagnataur committed Aug 29, 2024
1 parent ef732fb commit 58630ec
Show file tree
Hide file tree
Showing 19 changed files with 397 additions and 19 deletions.
77 changes: 77 additions & 0 deletions app/controllers/support/cases/additional_contacts_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
module Support
class Cases::AdditionalContactsController < Cases::ApplicationController
before_action :set_current_case, only: %i[create update edit new]
before_action :set_additional_contact, only: %i[edit update destroy]
before_action :set_additional_contacts, only: %i[index]
before_action :get_emails_of_contacts, only: %i[create update]

def index; end

def new
@additional_contact = @current_case&.case_additional_contacts&.build
end

def create
@current_case = Support::Case.find(additional_contact_params[:support_case_id]) if @current_case.blank?
@additional_contact = Support::CaseAdditionalContact.build(additional_contact_params)

if validation.success? && !@emails.include?(additional_contact_params[:email])
@additional_contact.save!
redirect_to support_case_additional_contacts_path(case_id: @current_case.id), notice: I18n.t("support.case_contact_details.flash.success")
else
flash.now[:notice] = I18n.t("support.case_contact_details.flash.already_a_contact") if @emails.include?(additional_contact_params[:email])
render :new
end
end

def edit
@current_case = Support::Case.find(@additional_contact.support_case_id)
end

def update
@emails.delete(@additional_contact.email)
if validation.success? && !@emails.include?(additional_contact_params[:email])
@additional_contact.update!(additional_contact_params)
redirect_to support_case_additional_contacts_path(case_id: @current_case.id), notice: I18n.t("support.case.label.non_participating_schools.success.message") if @additional_contact.update(additional_contact_params)

else
flash.now[:notice] = I18n.t("support.case_contact_details.flash.already_a_contact") if @emails.include?(additional_contact_params[:email])
render :edit
end
end

def destroy
@additional_contact.destroy!
redirect_to support_case_additional_contacts_path(case_id: @current_case.id), notice: I18n.t("support.case_contact_details.flash.destroyed")
end

private

def set_current_case
@current_case = Support::Case.find(params[:case_id])
end

def validation
CaseAdditionalContactFormSchema.new.call(**additional_contact_params)
end

def set_additional_contact
@additional_contact = Support::CaseAdditionalContact.find(params[:id])
end

def set_additional_contacts
@additional_contacts = @current_case.case_additional_contacts
end

def get_emails_of_contacts
@emails = @current_case.case_additional_contacts.pluck(:email)
@emails << @current_case.email
end

def additional_contact_params
params.require(:support_case_additional_contact).permit(:first_name, :last_name, :email, :phone_number, :extension_number, :support_case_id, :organisation_id, role: []).tap do |p|
p[:role].reject!(&:blank?)
end
end
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
module Support
module Cases
module SchoolDetails
class OtherSchoolsController < Cases::ApplicationController
before_action :set_instances, only: %i[confirmation_message remove_school non_beneficiery_schools]

def non_beneficiery_schools
@back_url = support_case_path(@current_case, anchor: "school-details")
@other_schools = @current_case.other_schools.map { |s| Support::OrganisationPresenter.new(s) }
end

def other_school
@other_school_form = OtherSchoolForm.new if @other_school_form.blank?
end

def add_other_school
@organisation = Support::Organisation.find_by(id: other_school_form_params[:school_id])
@other_school_form = OtherSchoolForm.from_validation(validation)
if validation.success?
if (@current_case.other_school_urns.include? @organisation.urn) || (@current_case.organisation.organisations.pluck(:urn).include? @organisation.urn)
message = if @current_case.other_school_urns.include? @organisation.urn
I18n.t("support.case.label.school_details.non_participating_schools.error.already_a_member_message", name: @organisation.name)
else
I18n.t("support.case.label.school_details.non_participating_schools.error.part_of_group_message", name: @organisation.name)
end
flash[:error] = { message:, class: "govuk-error" }
render :other_school
else
urns = @current_case.other_school_urns << @organisation.urn
@current_case.update!(other_school_urns: urns)
redirect_to non_beneficiery_schools_support_case_school_details_other_schools_path(@current_case)
flash[:notice] = I18n.t("support.case.label.school_details.non_participating_schools.success.message")
end
else
flash[:error] = { class: "remove-message" }
render :other_school
end
end

def confirmation_message
@back_url = non_beneficiery_schools_support_case_school_details_other_schools_path(@current_case)
end

def remove_school
school_urns = @current_case.other_school_urns - [@organisation.urn]
@current_case.update!(other_school_urns: school_urns)
redirect_to non_beneficiery_schools_support_case_school_details_other_schools_path(@current_case)
end

private

def validation
OtherSchoolFormSchema.new.call(**other_school_form_params)
end

def set_instances
@organisation = Support::Organisation.find_by(id: params[:school_id])
@current_case = Support::Case.find_by(id: params[:case_id])
end

def other_school_form_params
params.require(:case_organisation_form).permit(:school_id, :organisation_name)
end
end
end
end
end
8 changes: 8 additions & 0 deletions app/controllers/support/establishments_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,14 @@ def index
end
end

def list_for_non_participating_establishment
respond_to do |format|
format.json do
render json: EstablishmentSearch.omnisearch(params[:q]).where("establishment_type not in (?)", ["Federation", "Trust", "Single-academy Trust", "Multi-academy Trust", "Umbrella trust"]).as_json(methods: %i[autocomplete_template])
end
end
end

protected

def authorize_agent_scope = :access_establishment_search?
Expand Down
20 changes: 20 additions & 0 deletions app/forms/support/other_school_form.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# :nocov:
module Support
class OtherSchoolForm
extend Dry::Initializer
include Concerns::ValidatableForm

option :school_id, Types::Params::String, optional: true
option :organisation_name, Types::Params::String, optional: true

def assign_organisation_to_case(kase, agent_id)
CaseManagement::AssignOrganisationToCase.new.call(
support_case_id: kase.id,
agent_id:,
organisation_id:,
organisation_type:,
)
end
end
end
# :nocov:
21 changes: 21 additions & 0 deletions app/forms/support/other_school_form_schema.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# :nocov:
module Support
class OtherSchoolFormSchema < Dry::Validation::Contract
include Concerns::TranslatableFormSchema

params do
required(:school_id).value(:string)
required(:organisation_name).value(:string)
end

rule(:school_id) do
key(:school_id).failure(:missing) if value.blank?
end

rule(:organisation_name) do
# intentional use of organisation_id - the user should only see 1 error around organisation
key(:school_id).failure(:missing) if value.blank?
end
end
end
# :nocov:
5 changes: 5 additions & 0 deletions app/models/support/case.rb
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,11 @@ def schools
organisation.is_a?(Support::Organisation) ? [organisation] : participating_schools
end

# If this case is associated with a MAT, there may be several participating_schools that may not be the part of MAT
def other_schools
other_school_urns.map { |urn| Support::Organisation.find_by(urn:) }
end

# The Local Education Authorities of the schools currently associated with the case, sorted alphabetically and with duplicates removed.
# The 'if sch.respond_to?(:local_authority)' catches the edge case where a MAT is the organisation but none of its schools are selected.
def leas
Expand Down
43 changes: 31 additions & 12 deletions app/views/layouts/_messages.html.erb
Original file line number Diff line number Diff line change
@@ -1,16 +1,35 @@
<% flash.each do |name, msg| %>
<% if msg.is_a?(String) %>
<div class="govuk-notification-banner govuk-notification-banner--<%= name %>" role="alert" aria-labelledby="govuk-notification-banner-title" data-module="govuk-notification-banner">
<div class="govuk-notification-banner__header">
<h2 class="govuk-notification-banner__title" id="govuk-notification-banner-title">
<%= name.capitalize %>
</h2>
<% if flash[:error].present? && flash[:error][:class].present? %>
<%if flash[:error][:class]!= "remove-message"%>
<div class="govuk-error-summary" data-module="govuk-error-summary">
<div role="alert">
<h2 class="govuk-error-summary__title" data-error-summary-target="title">
There is a problem
</h2>
<div class="govuk-error-summary__body" data-error-summary-target="body" aria-live="polite">
<ul class="govuk-list govuk-error-summary__list">
<li>
<a data-turbo="false" href="#case-organisation-form-organisation-id-field-error"> <%= flash[:error][:message] %></a>
</li>
</ul>
</div>
</div>
</div>
<div class="govuk-notification-banner__content">
<h3 class="govuk-notification-banner__heading">
<%= content_tag :div, msg, id: "flash_#{name}" %>
</h3>
<%end%>
<%else%>
<% if msg.is_a?(String) %>
<div class="govuk-notification-banner govuk-notification-banner--<%= name %>" role="alert" aria-labelledby="govuk-notification-banner-title" data-module="govuk-notification-banner">
<div class="govuk-notification-banner__header">
<h2 class="govuk-notification-banner__title" id="govuk-notification-banner-title">
<%= name.capitalize %>
</h2>
</div>
<div class="govuk-notification-banner__content">
<h3 class="govuk-notification-banner__heading">
<%= content_tag :div, msg, id: "flash_#{name}" %>
</h3>
</div>
</div>
</div>
<% end %>
<% end %>
<%end%>
<% end %>
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<%= content_for :title, I18n.t("support.case.label.school_details.non_participating_schools.removal_modal.title") %>
<div class="govuk-grid-row">
<div class="govuk-grid-column-two-thirds">
<h1 class="govuk-heading-l"><%= I18n.t("support.case.label.school_details.non_participating_schools.removal_modal.title") %></h1>
<table class="govuk-table">
<tbody class="govuk-table__body">
<tr class="govuk-table__row">
<th class="govuk-table__header" scope="row" ><%= I18n.t("support.case.label.school_details.non_participating_schools.removal_modal.name") %></th>
<td class="govuk-table__cell"><%= @organisation.name %></td>
</tr>
<tr class="govuk-table__row">
<th class="govuk-table__header" scope="row" ><%= I18n.t("support.case.label.school_details.non_participating_schools.removal_modal.organisation") %></th>
<td class="govuk-table__cell"><%= @organisation.org_type %></td>
</tr>
<tr class="govuk-table__row">
<th class="govuk-table__header" scope="row" ><%= I18n.t("support.case.label.school_details.non_participating_schools.removal_modal.status") %></th>
<td class="govuk-table__cell"><%= I18n.t("components.school_picker.statuses.#{@organisation.status}") %></td>
</tr>
<%if @organisation.trust_name.present?%>
<tr class="govuk-table__row">
<th class="govuk-table__header" scope="row" ><%= I18n.t("support.case.label.school_details.non_participating_schools.removal_modal.parent") %></th>
<td class="govuk-table__cell"><%= @organisation.trust_name %></td>
</tr>
<%end%>
</tbody>
</table>

<br>

<div class="govuk-button-group">
<%= link_to I18n.t("support.case.label.school_details.non_participating_schools.removal_modal.remove"), remove_school_support_case_school_details_other_schools_path(@current_case, :school_id => @organisation.id), method: :patch, class: "govuk-button govuk-button--warning", role: "button" %>
<%= link_to I18n.t("support.case.label.school_details.non_participating_schools.removal_modal.cancel"), @back_url, class: "govuk-link" %>
</div>
</div>
</div>
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<%
=begin%>
<%= link_to I18n.t("support.case.label.school_details.back"), @back_url, class: "govuk-back-link pull-up" %>
<%
=end%>
<h2 class="govuk-heading-l">
<%= I18n.t("support.case.label.school_details.non_participating_schools.heading")%>
</h2>
<p class="govuk-hint"><%= I18n.t("support.case.label.school_details.non_participating_schools.description")%></p>
<p class="govuk-hint"><%= I18n.t("support.case.label.school_details.non_participating_schools.description_other")%></p>
<table class="govuk-table" aria-label="Non Participating schools">
<thead class="govuk-table__head">
<tr class="govuk-table__row">
<th scope="col" class="govuk-table__header"><%= I18n.t("support.case.label.school_details.non_participating_schools.table.school_name") %></th>
<th scope="col" class="govuk-table__header"><%= I18n.t("support.case.label.school_details.non_participating_schools.table.address") %></th>
<th scope="col" class="govuk-table__header"><%= I18n.t("support.case.label.school_details.non_participating_schools.table.organisation_type") %></th>
<th scope="col" class="govuk-table__header"><%= I18n.t("support.case.label.school_details.non_participating_schools.table.status") %></th>
<th scope="col" class="govuk-table__header"><%= %></th>
</tr>
</thead>
<tbody class="govuk-table__body">
<% @other_schools.each do |school| %>
<tr class="govuk-table__row">
<th scope="col" class="govuk-table__header"><%= "#{school.name} <br>( URN #{school.urn} )".html_safe %></th>
<td class="govuk-table__cell"><%= school.formatted_address %></td>
<td class="govuk-table__cell"><%= school.org_type %></td>
<%time = (school.status == "opening" || school.status == "closing") && school.opened_date.present? ? " ( #{school.opened_date&.strftime("%B, %d, %Y")} )" : ""%>
<td class="govuk-table__cell"><%= I18n.t("components.school_picker.statuses.#{school.status}")%></td>
<td class="govuk-table__cell">
<%= link_to "remove", confirmation_message_support_case_school_details_other_schools_path(@current_case, :school_id => school.id), class: "govuk-link", target: "_top" %>
</td>
</tr>
<% end %>
</tbody>
</table>
<%= link_to "Add", other_school_support_case_school_details_other_schools_path(@current_case), class: "govuk-button govuk-button--secondary", target: "_top" %>


Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<%= form_with model:@other_school_form, scope: :other_school_form, url: add_other_school_support_case_school_details_other_schools_path(@current_case), method: :patch do |form| %>
<%= form.govuk_error_summary %>
<%= link_to "Back", non_beneficiery_schools_support_case_school_details_other_schools_path(@current_case), class: "govuk-back-link pull-up" %>
<h1 class="govuk-heading-l">
<%= I18n.t("support.case.label.school_details.non_participating_schools.add_school") %>
</h1>
<%= render "components/autocomplete",
container_id: "organisation-autocomplete-container",
label_text: I18n.t("support.case.label.school_details.non_participating_schools.add_school_hint"),
label_class: "govuk-hint",
element_id: "organisation-autocomplete",
element_name: "case_organisation_form[organisation_name]",
template_suggestion: "{{autocomplete_template}}",
value_field: :name,
hidden_fields: {
'case_organisation_form[school_id]' => :id,
},
query_url: support_list_for_non_participating_establishment_path(format: :json, q: "{{QUERY}}") %>
<%= form.submit I18n.t("support.case.label.school_details.non_participating_schools.success.continue"), class: "govuk-button" %>
<% end %>
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,5 @@
</tbody>
</table>
<% end %>


14 changes: 14 additions & 0 deletions app/views/support/cases/school_details/show.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,20 @@
</dd>
</div>
<% end %>
<% if @current_case.eligible_for_school_picker? %>
<div class="govuk-summary-list__row">
<dt class="govuk-summary-list__key">
<%= I18n.t("support.case.label.non_participating_schools") %>
</dt>
<dd class="govuk-summary-list__value">
<%= @current_case.other_school_urns.size %>
</dd>
<dd class="govuk-summary-list__actions">
<%= link_to I18n.t("support.generic.view"), non_beneficiery_schools_support_case_school_details_other_schools_path(@current_case), class: "govuk-link", target: "_top"%>
</dd>
</div>
<% end %>
</dl>

<% if @current_case.organisation.present? %>
Expand Down
Loading

0 comments on commit 58630ec

Please sign in to comment.