Skip to content

Commit

Permalink
feat(e&o): add school picker to case creation
Browse files Browse the repository at this point in the history
Jira: PWNN-1659
  • Loading branch information
EdwinKruglov committed Oct 13, 2023
1 parent 2cf512d commit 1dde104
Show file tree
Hide file tree
Showing 28 changed files with 726 additions and 301 deletions.
Original file line number Diff line number Diff line change
@@ -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
43 changes: 23 additions & 20 deletions app/controllers/engagement/case_requests_controller.rb
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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
6 changes: 3 additions & 3 deletions app/controllers/engagement/cases/uploads_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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,
Expand Down
31 changes: 20 additions & 11 deletions app/controllers/support/case_requests_controller.rb
Original file line number Diff line number Diff line change
@@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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
Expand Down

This file was deleted.

66 changes: 56 additions & 10 deletions app/models/case_request.rb
Original file line number Diff line number Diff line change
@@ -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 }

Expand All @@ -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
Expand All @@ -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?

Expand Down
12 changes: 12 additions & 0 deletions app/models/case_request/school_pickable.rb
Original file line number Diff line number Diff line change
@@ -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
12 changes: 12 additions & 0 deletions app/models/case_request/school_picker.rb
Original file line number Diff line number Diff line change
@@ -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
4 changes: 4 additions & 0 deletions app/models/support/establishment_group.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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?

Expand Down
7 changes: 3 additions & 4 deletions app/views/components/_new_case_request_components.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,10 @@
element_name: "case_request[organisation_name]",
template_suggestion: "<strong>{{name}}</strong>, {{postcode}}<br />{{establishment_type}}<br />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}}") %>

Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
<div class="govuk-grid-row">
<div class="govuk-grid-column-two-thirds">
<%= form_with model: @case_request, scope: :case_request, url: engagement_case_requests_path, html: {
<%= form_with model: @case_request, scope: :case_request, method: :patch, url: engagement_case_request_path, html: {
"data-controller" => "case-attachment",
"data-action" => "submit->case-attachment#onFormSubmitted",
"data-case-attachment-target" => "form",
"data-case-attachment-dropzone-outlet" => ".drop-zone-container",
"data-case-attachment-error-summary-outlet" => ".govuk-error-summary",
"data-case-attachment-reference-value" => @upload_reference
"data-case-attachment-error-summary-outlet" => ".govuk-error-summary"
} do |form| %>
<%= form.govuk_error_summary %>

Expand All @@ -17,9 +16,9 @@
</h2>
<div class="drop-zone-container govuk-!-margin-bottom-5"
data-controller="dropzone"
data-dropzone-list-files-url-value="<%= list_uploads_engagement_cases_url(@upload_reference) %>"
data-dropzone-add-file-url-value="<%= add_uploads_engagement_cases_url(@upload_reference) %>"
data-dropzone-remove-file-url-value="<%= remove_uploads_engagement_cases_url(@upload_reference) %>">
data-dropzone-list-files-url-value="<%= list_uploads_engagement_cases_url(@case_request.id) %>"
data-dropzone-add-file-url-value="<%= add_uploads_engagement_cases_url(@case_request.id) %>"
data-dropzone-remove-file-url-value="<%= remove_uploads_engagement_cases_url(@case_request.id) %>">
<div class="upload-preview-template govuk-!-display-none" data-dropzone-target="previewTemplate">
<div class="upload-item">
<div class="upload-row file-details">
Expand Down Expand Up @@ -64,7 +63,6 @@
</div>

<div class="govuk-button-group">
<%= form.hidden_field :upload_reference, :value => @upload_reference %>
<%= form.submit I18n.t("support.case_hub_migration.submit"), class: "govuk-button", role: "button", "data-prevent-double-click" => true, "data-case-attachment-target" => "btnSubmit" %>
</div>
<% end %>
Expand Down
Loading

0 comments on commit 1dde104

Please sign in to comment.