Skip to content

Commit

Permalink
feat(cms): enable case requests from local authorities
Browse files Browse the repository at this point in the history
  • Loading branch information
EdwinKruglov committed Apr 5, 2024
1 parent 89be5fc commit 7492361
Show file tree
Hide file tree
Showing 49 changed files with 705 additions and 119 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,12 @@ def update
private

def get_organisation
type = params[:type] == Support::EstablishmentGroup.name ? Support::EstablishmentGroup : Support::Organisation
type =
case params[:type]
when "Support::Organisation" then Support::Organisation
when "Support::EstablishmentGroup" then Support::EstablishmentGroup
when "LocalAuthority" then LocalAuthority
end
type.find(params[:id])
end
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ 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) }
@participating_schools = @current_case.participating_schools.includes([:local_authority]).map { |s| Support::OrganisationPresenter.new(s) }
end

def edit
Expand Down
1 change: 1 addition & 0 deletions app/controllers/support/concerns/filter_parameters.rb
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ def permitted_params
:level,
:sort_by,
:sort_order,
:exact_match,
{
category: [],
agent: [],
Expand Down
2 changes: 1 addition & 1 deletion app/controllers/support/establishments_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ class EstablishmentsController < ::ApplicationController
def index
respond_to do |format|
format.json do
render json: EstablishmentSearch.omnisearch(params[:q]).as_json
render json: EstablishmentSearch.omnisearch(params[:q]).as_json(methods: %i[autocomplete_template])
end
end
end
Expand Down
2 changes: 1 addition & 1 deletion app/helpers/school_picker_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ def show_school_picker_phase_filters?(organisations)
end

def show_school_picker_la_filters?(organisations)
organisations.pluck(:local_authority).uniq.length > 1
organisations.map(&:local_authority).uniq.length > 1
end

def show_school_picker_filters?(organisations)
Expand Down
2 changes: 1 addition & 1 deletion app/helpers/support/case_helper.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
module Support
module CaseHelper
def other_cases_by_case_org_path(case_organisation, back_to:)
support_case_search_index_path(search_case_form: { search_term: case_organisation.name, state: Support::Case.states.keys.excluding("closed") }, back_to:)
support_case_search_index_path(search_case_form: { search_term: case_organisation.name, state: Support::Case.states.keys.excluding("closed"), exact_match: true }, back_to:)
end

def other_cases_by_case_org_exist?(case_organisation)
Expand Down
2 changes: 1 addition & 1 deletion app/models/case_request/school_pickable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ def selected_schools
end

def eligible_for_school_picker?
return false unless organisation.is_a?(Support::EstablishmentGroup)
return false unless organisation.class.in?([Support::EstablishmentGroup, LocalAuthority])

organisation.eligible_for_school_picker?
end
Expand Down
1 change: 1 addition & 0 deletions app/models/case_request/school_picker.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
class CaseRequest::SchoolPicker
include ActiveModel::Model
include CaseRequest::SchoolPicker::Presentable

attr_accessor(
:case_request,
Expand Down
10 changes: 10 additions & 0 deletions app/models/case_request/school_picker/presentable.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
module CaseRequest::SchoolPicker::Presentable
extend ActiveSupport::Concern

def organisation_type
case case_request.organisation_type
when "Support::EstablishmentGroup" then "academy trust or federation"
when "LocalAuthority" then "local authority"
end
end
end
4 changes: 4 additions & 0 deletions app/models/local_authority.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,8 @@ class LocalAuthority < ApplicationRecord

validates :la_code, uniqueness: true
validates :name, uniqueness: true

def eligible_for_school_picker?
organisations.count > 1
end
end
3 changes: 2 additions & 1 deletion app/models/support/case/filtering.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ class Support::Case::Filtering
attribute :legacy_stage, default: -> { [] }
attribute :sort_by
attribute :sort_order
attribute :exact_match, default: -> { false }

validates :search_term,
presence: true,
Expand Down Expand Up @@ -48,7 +49,7 @@ def filters
level: Support::Concerns::ScopeFilter.new(level, scope: :by_level),
procurement_stage: Support::Concerns::ScopeFilter.new(procurement_stage, scope: :by_procurement_stage),
has_org: Support::Concerns::ScopeFilter.new(ActiveModel::Type::Boolean.new.cast(has_org), scope: :by_has_org, multiple: false),
search_term: Support::Concerns::ScopeFilter.new(search_term, scope: :by_search_term, multiple: false),
search_term: Support::Concerns::ScopeFilterForSearch.new(search_term, exact_match: ActiveModel::Type::Boolean.new.cast(exact_match)),
legacy_stage: Support::Concerns::ScopeFilter.new(legacy_stage, scope: :by_legacy_stage),
}
end
Expand Down
2 changes: 1 addition & 1 deletion app/models/support/case/searchable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@ module Support::Case::Searchable
included do
has_many :support_case_searches, class_name: "Support::CaseSearch"

scope :by_search_term, ->(terms) { joins(:support_case_searches).merge(Support::CaseSearch.find_a_case(terms)) }
scope :by_search_term, ->(terms, exact_match: false) { joins(:support_case_searches).merge(Support::CaseSearch.find_a_case(terms, exact_match:)) }
end
end
17 changes: 10 additions & 7 deletions app/models/support/case_search.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,20 @@ class CaseSearch < ApplicationRecord
find_a_case(query).limit(30)
}

scope :find_a_case, lambda { |query|
scope :find_a_case, lambda { |query, exact_match: false|
comp = exact_match ? "=" : "LIKE"
query_str = exact_match ? query.downcase : "#{query.downcase}%"

sql = <<-SQL
lower(organisation_name) LIKE :q OR
lower(case_email) LIKE :q OR
lower(organisation_urn) LIKE :q OR
lower(agent_first_name) LIKE :q OR
lower(agent_last_name) LIKE :q
lower(organisation_name) #{comp} :q OR
lower(case_email) #{comp} :q OR
lower(organisation_urn) #{comp} :q OR
lower(agent_first_name) #{comp} :q OR
lower(agent_last_name) #{comp} :q
SQL

where("case_ref = ?", sprintf("%06d", query.to_i))
.or(where(sql, q: "#{query.downcase}%"))
.or(where(sql, q: query_str))
.order("case_ref DESC")
}
end
Expand Down
20 changes: 20 additions & 0 deletions app/models/support/concerns/scope_filter_for_search.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
class Support::Concerns::ScopeFilterForSearch
def initialize(value, exact_match: false)
@value = value
@exact_match = exact_match
end

def filter(records)
return records unless entered?

records.send(:by_search_term, value, exact_match:)
end

def entered?
value.present?
end

private

attr_reader :value, :exact_match
end
3 changes: 3 additions & 0 deletions app/models/support/establishment_search.rb
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
module Support
class EstablishmentSearch < ApplicationRecord
include Presentable

scope :omnisearch, lambda { |query|
sql = <<-SQL
urn LIKE :q OR
ukprn LIKE :q OR
code LIKE :q OR
lower(name) LIKE lower(:q) OR
lower(postcode) LIKE lower(:q)
SQL
Expand Down
16 changes: 16 additions & 0 deletions app/models/support/establishment_search/presentable.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
module Support::EstablishmentSearch::Presentable
extend ActiveSupport::Concern

def autocomplete_template
ApplicationController.render(partial: "support/establishment_search/#{source.underscore}/autocomplete_template", locals: autocomplete_template_vars)
end

private

def autocomplete_template_vars
case source
when "LocalAuthority" then { name:, establishment_type:, code: }
else { name:, postcode:, establishment_type:, urn:, ukprn: }
end
end
end
7 changes: 7 additions & 0 deletions app/presenters/local_authority_presenter.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
class LocalAuthorityPresenter < SimpleDelegator
def establishment_type_name
"Local authority"
end

def gias_url = nil
end
2 changes: 1 addition & 1 deletion app/presenters/support/organisation_presenter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ def postcode
def local_authority
return I18n.t("generic.not_provided") unless super

super["name"]
super.name
end

# @return [String]
Expand Down
2 changes: 1 addition & 1 deletion app/views/components/_new_case_request_components.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
label_class: "govuk-hint",
element_id: "school-urn-autocomplete",
element_name: "case_request[organisation_name]",
template_suggestion: "<strong>{{name}}</strong>, {{postcode}}<br />{{establishment_type}}<br />URN: {{urn}} - UKPRN: {{ukprn}}",
template_suggestion: "{{autocomplete_template}}",
value_field: :name,
default_value: form.object.organisation&.name,
hidden_fields: {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<h1 class="govuk-heading-l"><%= I18n.t("support.case_hub_migration.school_picker.title") %></h1>
<h1 class="govuk-heading-l"><%= I18n.t("support.case_hub_migration.school_picker.title", organisation_type: @school_picker.organisation_type) %></h1>

<%= render "components/school_picker/school_picker",
results_model: @school_picker,
Expand Down
Loading

0 comments on commit 7492361

Please sign in to comment.