diff --git a/app/controllers/engagement/case_requests/school_pickers_controller.rb b/app/controllers/engagement/case_requests/school_pickers_controller.rb
new file mode 100644
index 000000000..0a4c97d14
--- /dev/null
+++ b/app/controllers/engagement/case_requests/school_pickers_controller.rb
@@ -0,0 +1,28 @@
+module Engagement
+ module CaseRequests
+ class SchoolPickersController < ApplicationController
+ def edit
+ @case_request = CaseRequest.find(params[:id])
+ @back_url = params[:source] == "change_link" ? engagement_case_request_path(@case_request) : edit_engagement_case_request_path(@case_request)
+ @school_picker = @case_request.school_picker
+ @group = @case_request.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_request = CaseRequest.find(params[:id])
+ @school_picker = @case_request.school_picker(school_urns: form_params[:school_urns].compact_blank)
+ @school_picker.save!
+ redirect_to engagement_case_request_path(@case_request)
+ end
+
+ private
+
+ def form_params
+ params.fetch(:case_request, {}).permit(filters: params.key?(:clear) ? nil : { local_authorities: [], phases: [] }, school_urns: [])
+ end
+ end
+ end
+end
diff --git a/app/controllers/engagement/case_requests_controller.rb b/app/controllers/engagement/case_requests_controller.rb
index 80947abdc..9977399fb 100644
--- a/app/controllers/engagement/case_requests_controller.rb
+++ b/app/controllers/engagement/case_requests_controller.rb
@@ -1,31 +1,38 @@
module Engagement
class CaseRequestsController < ApplicationController
- before_action -> { @back_url = engagement_cases_path }, only: %i[new create]
+ def create
+ @case_request = CaseRequest.new(source: :engagement_and_outreach_cms, creation_source: :engagement_and_outreach_team, created_by: current_agent)
+ @case_request.save!(validate: false)
+ redirect_to edit_engagement_case_request_path(@case_request, source: :create)
+ end
- def new
- @case_request = CaseRequest.new
- @upload_reference = SecureRandom.hex
+ def show
+ @case_request = CaseRequest.find(params[:id])
end
- def create
- @case_request = CaseRequest.new(form_params.except(:upload_reference))
- @case_request.created_by = current_agent
+ def edit
+ @case_request = CaseRequest.find(params[:id])
+ @back_url = params[:source] == "create" ? engagement_cases_path : engagement_case_request_path(@case_request)
+ end
+
+ def update
+ @case_request = CaseRequest.find(params[:id])
+ @back_url = engagement_case_request_path(@case_request)
+ @case_request.assign_attributes(form_params.compact_blank)
if @case_request.valid?
+ org_changed = @case_request.organisation_changed?
@case_request.save!
- # kase = @case_request.create_case
-
- # CaseFiles::SubmitCaseUpload.new.call(upload_reference: form_params[:upload_reference], support_case_id: kase.id)
-
- # create_interaction(kase.id, "create_case", "Case created", @case_request.attributes.slice(:source, :category))
-
- redirect_to @case_request.eligible_for_school_picker? ? edit_support_case_request_school_picker_path(@case_request) : engagement_case_request_path(@case_request)
+ redirect_to(org_changed && @case_request.eligible_for_school_picker? ? edit_engagement_case_request_school_picker_path(@case_request) : engagement_case_request_path(@case_request))
else
- render :new
+ render :edit
end
end
def submit
+ @case_request = CaseRequest.find(params[:id])
+ @case_request.create_case
+ redirect_to engagement_cases_path
end
private
@@ -47,11 +54,7 @@ def form_params
:other_category,
:other_query,
:procurement_amount,
- :upload_reference,
- ).merge({
- source: :engagement_and_outreach_cms.to_s,
- creation_source: :engagement_and_outreach_team.to_s,
- })
+ )
end
end
end
diff --git a/app/controllers/engagement/cases/uploads_controller.rb b/app/controllers/engagement/cases/uploads_controller.rb
index 918ea9046..163ff7925 100644
--- a/app/controllers/engagement/cases/uploads_controller.rb
+++ b/app/controllers/engagement/cases/uploads_controller.rb
@@ -7,13 +7,13 @@ def upload
if file_is_safe?
upload = EngagementCaseUpload.pending.create!(
file: params[:file],
- upload_reference: params[:upload_reference],
+ case_request_id: params[:upload_reference],
filename: params[:file].original_filename,
filesize: File.size(params[:file].tempfile.path),
)
render status: :ok, json: { file_id: upload.id }.to_json
else
- Rollbar.error("Infected file uploaded", upload_reference: params[:upload_reference])
+ Rollbar.error("Infected file uploaded", case_request_id: params[:upload_reference])
params[:file].tempfile.delete
@@ -29,7 +29,7 @@ def remove
def list
reference = params[:upload_reference]
- files = EngagementCaseUpload.where(upload_reference: reference)
+ files = EngagementCaseUpload.where(case_request_id: reference)
result = files.map do |f|
{
file_id: f.id,
diff --git a/app/controllers/support/case_requests_controller.rb b/app/controllers/support/case_requests_controller.rb
index 0d3b10d88..3ffa884c2 100644
--- a/app/controllers/support/case_requests_controller.rb
+++ b/app/controllers/support/case_requests_controller.rb
@@ -1,28 +1,37 @@
module Support
class CaseRequestsController < ApplicationController
- before_action -> { @back_url = support_cases_path }, only: %i[new create]
+ def create
+ @case_request = CaseRequest.new(created_by: current_agent)
+ @case_request.save!(validate: false)
+ redirect_to edit_support_case_request_path(@case_request, source: :create)
+ end
- def new
- @case_request = CaseRequest.new
+ def show
+ @case_request = CaseRequest.find(params[:id])
end
- def create
- @case_request = CaseRequest.new(form_params.except(:upload_reference))
- @case_request.created_by = current_agent
+ def edit
+ @case_request = CaseRequest.find(params[:id])
+ @back_url = params[:source] == "create" ? support_cases_path : support_case_request_path(@case_request)
+ end
+
+ def update
+ @case_request = ::CaseRequest.find(params[:id])
+ @back_url = support_case_request_path(@case_request)
+ @case_request.assign_attributes(form_params.compact_blank)
if @case_request.valid?
@case_request.save!
- # kase = @case_request.create_case
-
- # create_interaction(kase.id, "create_case", "Case created", @case_request.attributes.slice(:source, :category))
-
redirect_to support_case_request_path(@case_request)
else
- render :new
+ render :edit
end
end
def submit
+ @case_request = ::CaseRequest.find(params[:id])
+ kase = @case_request.create_case
+ redirect_to support_case_path(kase)
end
private
diff --git a/app/controllers/support/cases/request_details/participating_schools_controller.rb b/app/controllers/support/cases/request_details/participating_schools_controller.rb
new file mode 100644
index 000000000..85cb27132
--- /dev/null
+++ b/app/controllers/support/cases/request_details/participating_schools_controller.rb
@@ -0,0 +1,12 @@
+module Support
+ module Cases
+ module RequestDetails
+ class ParticipatingSchoolsController < Cases::ApplicationController
+ def show
+ @back_url = support_case_request_details_path
+ @participating_schools = @current_case.request.selected_schools.map { |s| Support::OrganisationPresenter.new(s) }
+ end
+ end
+ end
+ end
+end
diff --git a/app/controllers/support/cases/requests_controller.rb b/app/controllers/support/cases/request_details_controller.rb
similarity index 54%
rename from app/controllers/support/cases/requests_controller.rb
rename to app/controllers/support/cases/request_details_controller.rb
index 696eca7f8..ce53ee87c 100644
--- a/app/controllers/support/cases/requests_controller.rb
+++ b/app/controllers/support/cases/request_details_controller.rb
@@ -1,8 +1,8 @@
module Support
module Cases
- class RequestsController < Cases::ApplicationController
+ class RequestDetailsController < Cases::ApplicationController
def show
- @request = FrameworkRequestPresenter.new(@current_case.framework_request)
+ @request = FrameworkRequestPresenter.new(@current_case.request)
end
private
diff --git a/app/controllers/support/cases/requests/participating_schools_controller.rb b/app/controllers/support/cases/requests/participating_schools_controller.rb
deleted file mode 100644
index bd0deb028..000000000
--- a/app/controllers/support/cases/requests/participating_schools_controller.rb
+++ /dev/null
@@ -1,12 +0,0 @@
-module Support
- module Cases
- module Requests
- class ParticipatingSchoolsController < Cases::ApplicationController
- def show
- @back_url = support_case_request_path
- @participating_schools = @current_case.framework_request.selected_schools.map { |s| Support::OrganisationPresenter.new(s) }
- end
- end
- end
- end
-end
diff --git a/app/models/case_request.rb b/app/models/case_request.rb
index 72b7b6a93..c9d2540c1 100644
--- a/app/models/case_request.rb
+++ b/app/models/case_request.rb
@@ -1,10 +1,16 @@
class CaseRequest < ApplicationRecord
+ include SchoolPickable
+
+ before_save :clear_school_urns, if: -> { attribute_changed?(:organisation_id) }
+
belongs_to :category, class_name: "Support::Category", optional: true
belongs_to :query, class_name: "Support::Query", optional: true
belongs_to :organisation, polymorphic: true, optional: true
belongs_to :created_by, class_name: "Support::Agent", optional: true
belongs_to :support_case, class_name: "Support::Case", optional: true
+ has_many :engagement_case_uploads, class_name: "EngagementCaseUpload"
+
enum source: { digital: 0, nw_hub: 1, sw_hub: 2, incoming_email: 3, faf: 4, engagement_and_outreach: 5, schools_commercial_team: 6, engagement_and_outreach_cms: 7 }
enum creation_source: { default: 0, engagement_and_outreach_team: 5 }
@@ -18,16 +24,10 @@ class CaseRequest < ApplicationRecord
validates :other_category, presence: true, if: -> { category_id == Support::Category.other_category_id }
validates :other_query, presence: true, if: -> { query_id == Support::Query.other_query_id }
- def organisation_name
- return nil unless organisation
-
- organisation.name
- end
-
- def organisation_urn
- return nil unless organisation
+ attribute :creation_source, default: :default
- organisation.urn
+ def full_name
+ "#{first_name} #{last_name}"
end
def request_type
@@ -36,18 +36,64 @@ def request_type
category.present?
end
+ def type
+ request_type ? I18n.t("support.case_hub_migration.label.procurement") : I18n.t("support.case_hub_migration.label.non_procurement")
+ end
+
+ def contract_length = nil
+
+ def flow = nil
+
+ def same_supplier_used = nil
+
+ def origin = nil
+
+ def selected_schools
+ school_urns.map { |urn| Support::Organisation.find_by(urn:) }
+ end
+
def eligible_for_school_picker?
return false unless organisation.is_a?(Support::EstablishmentGroup)
organisation.eligible_for_school_picker?
end
+ def multischool?
+ school_urns.present?
+ end
+
def create_case
- CreateCase.new(attributes).call
+ transaction do
+ attrs = attributes.symbolize_keys.merge(participating_schools: school_urns.map { |urn| Support::Organisation.find_by(urn:) }, creator: created_by)
+ kase = Support::CreateCase.new(attrs).call
+ submit_uploads(kase.id) if engagement_case_uploads.present?
+ Support::CreateInteraction.new(kase.id, "create_case", created_by.id, { body: "Case created", additional_data: attrs.slice(:source, :category).compact }).call
+ update!(support_case: kase)
+ kase
+ end
end
private
+ def submit_uploads(support_case_id)
+ engagement_case_uploads.update_all(upload_status: :submitted, support_case_id:)
+
+ engagement_case_uploads.each do |upload|
+ Support::CaseAttachment.create(
+ attachable: upload,
+ support_case_id:,
+ custom_name: upload.file_name,
+ description: "User uploaded attachment",
+ created_at: upload.created_at,
+ updated_at: upload.updated_at,
+ )
+ end
+ end
+
+ def clear_school_urns
+ self.school_urns = []
+ end
+
def request_type_validation
return if category.present? || query.present?
diff --git a/app/models/case_request/school_pickable.rb b/app/models/case_request/school_pickable.rb
new file mode 100644
index 000000000..89fe6c483
--- /dev/null
+++ b/app/models/case_request/school_pickable.rb
@@ -0,0 +1,12 @@
+module CaseRequest::SchoolPickable
+ extend ActiveSupport::Concern
+
+ def school_picker(school_urns: self.school_urns)
+ CaseRequest::SchoolPicker.new(case_request: self, school_urns:)
+ end
+
+ def pick_schools(school_urns)
+ self.school_urns = school_urns
+ save!
+ end
+end
diff --git a/app/models/case_request/school_picker.rb b/app/models/case_request/school_picker.rb
new file mode 100644
index 000000000..7921a9b62
--- /dev/null
+++ b/app/models/case_request/school_picker.rb
@@ -0,0 +1,12 @@
+class CaseRequest::SchoolPicker
+ include ActiveModel::Model
+
+ attr_accessor(
+ :case_request,
+ :school_urns,
+ )
+
+ def save!
+ case_request.pick_schools(school_urns)
+ end
+end
diff --git a/app/models/support/establishment_group.rb b/app/models/support/establishment_group.rb
index a373ebddf..7d667ba0d 100644
--- a/app/models/support/establishment_group.rb
+++ b/app/models/support/establishment_group.rb
@@ -11,6 +11,10 @@ def formatted_name
"#{uid} - #{name}"
end
+ def postcode
+ address["postcode"]
+ end
+
def organisations
return Support::Organisation.where(federation_code: uid) if federation?
diff --git a/app/views/components/_new_case_request_components.html.erb b/app/views/components/_new_case_request_components.html.erb
index 1eb51d761..0af83caca 100644
--- a/app/views/components/_new_case_request_components.html.erb
+++ b/app/views/components/_new_case_request_components.html.erb
@@ -9,11 +9,10 @@
element_name: "case_request[organisation_name]",
template_suggestion: "{{name}}, {{postcode}}
{{establishment_type}}
URN: {{urn}} - UKPRN: {{ukprn}}",
value_field: :name,
- default_value: form.object.organisation_name,
+ default_value: form.object.organisation&.name,
hidden_fields: {
- 'case_request[organisation_id]' => [:id, form.object.organisation_id],
- 'case_request[organisation_type]' => [:source, form.object.organisation_type],
- 'case_request[organisation_urn]' => [:urn, form.object.organisation_urn],
+ 'case_request[organisation_id]' => [:id, form.object.organisation&.id],
+ 'case_request[organisation_type]' => [:source, form.object.organisation.class&.name],
},
query_url: support_establishments_path(format: :json, q: "{{QUERY}}") %>
diff --git a/app/views/engagement/case_requests/new.html.erb b/app/views/engagement/case_requests/edit.html.erb
similarity index 88%
rename from app/views/engagement/case_requests/new.html.erb
rename to app/views/engagement/case_requests/edit.html.erb
index b86a4cfb7..52097867c 100644
--- a/app/views/engagement/case_requests/new.html.erb
+++ b/app/views/engagement/case_requests/edit.html.erb
@@ -1,12 +1,11 @@
+ <%= I18n.t("support.case_hub_migration.edit.label.confirm") %> +
+ + <%= button_to I18n.t("support.case_hub_migration.edit.submit"), submit_engagement_case_request_path, class: "govuk-button", data: { disable_with: "Submitting..." } %> +- <%= I18n.t("support.case_hub_migration.edit.label.confirm") %> -
++ <%= I18n.t("support.case_hub_migration.edit.label.confirm") %> +
- <%= form.button I18n.t("support.case_hub_migration.edit.submit"), class: "govuk-button", value: "create" %> - <% end %> + <%= button_to I18n.t("support.case_hub_migration.edit.submit"), submit_support_case_request_path, class: "govuk-button", data: { disable_with: "Submitting..." } %>Loading...