Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Im/add routes and controllers for provider partnerships #4767

Draft
wants to merge 17 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions app/components/add_course_button_partnerships.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<% if required_organisation_details_present? %>
<%= govuk_button_link_to(
"Add course",
new_publish_provider_recruitment_cycle_course_path(
provider_code: provider.provider_code,
recruitment_cycle_year: provider.recruitment_cycle_year
),
class: "govuk-!-margin-bottom-6"
) %>

<% else %>

<div class="govuk-inset-text">
<p class="govuk-body">Before adding a course, you need to:</p>
<ul class="govuk-list govuk-list--bullet">
<% incomplete_sections.each do |section| %>
<li>
<%= govuk_link_to(section.name, section.path) %>
</li>
<% end %>
</ul>
</div>
<% end %>
69 changes: 69 additions & 0 deletions app/components/add_course_button_partnerships.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
# frozen_string_literal: true

class AddCourseButtonPartnerships < ViewComponent::Base
include RecruitmentCycleHelper
attr_reader :provider

Section = Struct.new(:name, :path, keyword_init: true)

def initialize(provider:)
@provider = provider
super
end

private

def incomplete_sections
incomplete_sections_hash.keys.select { |section| send(section) }.map do |section|
Section.new(name: "add #{incomplete_section_article(section)} #{incomplete_section_label_suffix(section)}", path: incomplete_sections_hash[section])
end
end

def incomplete_sections_hash
{
site_not_present?: publish_provider_recruitment_cycle_schools_path(provider.provider_code, provider.recruitment_cycle_year),
accredited_partner_not_present?: publish_provider_recruitment_cycle_accredited_partnerships_path(provider.provider_code, provider.recruitment_cycle_year)
}
end

def incomplete_section_label_suffix(section)
labels = {
accredited_partner_not_present?: 'accredited partner',
site_not_present?: 'school'
}

labels[section]
end

def required_organisation_details_present?
accredited_partner_present? && site_present?
end

def accredited_partner_present?
return true if accredited_provider?

provider.accredited_partners.any?
end

def site_present?
provider.sites.any?
end

def accredited_partner_not_present?
return false if provider.accredited_provider?

!accredited_partner_present?
end

def site_not_present?
!site_present?
end

def accredited_provider?
provider.accredited_provider?
end

def incomplete_section_article(section)
incomplete_section_label_suffix(section) == 'accredited partner' ? 'an' : 'a'
end
end
172 changes: 172 additions & 0 deletions app/controllers/publish/courses/ratifying_provider_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,172 @@
# frozen_string_literal: true

module Publish
module Courses
class RatifyingProviderController < PublishController
include CourseBasicDetailConcern
before_action :build_course, only: %i[edit update]
before_action :build_course_params, only: :continue
helper_method :accredited_partners

def accredited_partners
if Settings.features.provider_partnerships
@provider.accredited_partners
else
@provider.accredited_bodies
end
end

def show
@course = build_course&.decorate
render_not_found if @course.accrediting_provider.blank?
end

def edit
build_provider
end

def continue
authorize(@provider, :can_create_course?)

code = course_params[:accredited_provider_code]
query = @accredited_provider

@errors = errors_for_search_query(code, query)

if @errors.present?
render :new
elsif other_selected_with_no_autocompleted_code?(code)
redirect_to(
search_new_publish_provider_recruitment_cycle_courses_accredited_provider_path(
query: @accredited_provider,
course: course_params
)
)
else
params[:course][:accredited_provider_code] = @autocompleted_provider_code if @autocompleted_provider_code.present?
super
end
end

def search_new
authorize(provider, :can_create_course?)

# These are not before_action hooks as they conflict with hooks
# defined within the CourseBasicDetailConcern and cannot be overridden
# without causing failures in other routes in this controller
build_new_course
build_provider
build_previous_course_creation_params
@query = params[:query]
@provider_suggestions = recruitment_cycle.providers.with_findable_courses.provider_search(@query).limit(10)
end

def update
build_provider
begin
code = update_course_params[:accredited_provider_code]
query = update_course_params[:accredited_provider]
rescue ActionController::ParameterMissing
@errors = errors_for_search_query(code, query)
return render :edit if @errors.present?
end

if update_params[:accredited_provider_code] == 'other'
redirect_to_provider_search
elsif @course.update(update_params)
course_updated_message('Accredited provider')
redirect_to_update_successful
else
@errors = @course.errors.messages
render :edit
end
end

def search
build_course
@query = params[:query]
@provider_suggestions = recruitment_cycle.providers.with_findable_courses.provider_search(@query).limit(10)
end

private

def build_provider
@provider = RecruitmentCycle.find_by(year: params[:recruitment_cycle_year])
.providers
.find_by(provider_code: params[:provider_code])
end

def error_keys
[:accredited_provider_code]
end

def redirect_to_provider_search
redirect_to(
accredited_provider_search_provider_recruitment_cycle_course_path(
@course.provider_code,
@course.recruitment_cycle_year,
@course.course_code,
query: update_course_params[:accredited_provider]
)
)
end

def redirect_to_update_successful
redirect_to(
details_publish_provider_recruitment_cycle_course_path(
@course.provider_code,
@course.recruitment_cycle_year,
@course.course_code
)
)
end

def current_step
:accredited_provider
end

def build_course_params
@accredited_provider = params[:course].delete(:accredited_provider)
@autocompleted_provider_code = params[:course].delete(:autocompleted_provider_code)
end

def errors_for_search_query(code, query)
errors = {}

if other_selected_with_no_autocompleted_code?(code) && query.length < 2
errors = { accredited_provider: ['Accredited provider search too short, enter 2 or more characters'] }
elsif code.blank?
errors = { accredited_provider_code: ['Select an accredited provider'] }
end

errors
end

def update_course_params
params.require(:course).permit(
:autocompleted_provider_code,
:accredited_provider_code,
:accredited_provider
)
end

def update_params
autocompleted_code = update_course_params[:autocompleted_provider_code]
code = update_course_params[:accredited_provider_code]

{
accredited_provider_code: autocompleted_code.presence || code
}
end

def other_selected_with_no_autocompleted_code?(code)
code == 'other' && @autocompleted_provider_code.blank?
end

def build_course
super
authorize @course
end
end
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
# frozen_string_literal: true

module Publish
module Providers
module AccreditedPartnerships
class ChecksController < PublishController
def show
provider_partnership_form
end

def update
@partnership = provider.accredited_partnerships.build(accredited_provider: provider_partnership_form.accredited_provider, description: provider_partnership_form.description)

if @partnership.save
notify_accredited_provider_users

redirect_to publish_provider_recruitment_cycle_accredited_partnerships_path(@provider.provider_code, @provider.recruitment_cycle_year), flash: { success: 'Accredited partnership added' }
else
render :show
end
end

private

def provider_partnership_form
@provider_partnership_form ||= ProviderPartnershipForm.new(current_user, provider)
end

def notify_accredited_provider_users
@partnership.accredited_provider.users.each do |user|
::Users::OrganisationMailer.added_as_an_organisation_to_training_partner(
recipient: user,
provider: provider,
accredited_provider: @partnership.accredited_provider
).deliver_later
end
end
end
end
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
# frozen_string_literal: true

module Publish
module Providers
class AccreditedPartnershipsController < PublishController
helper_method :accredited_provider_id

def index; end

def new
provider_partnership = provider.accredited_partnerships.build
@provider_partnership_form = ProviderPartnershipForm.new(current_user, provider_partnership, params: { accredited_provider_id: params[:accredited_provider_id] })
end

def edit
provider_partnership = provider.accredited_partnerships.find_by(accredited_provider: partner)

params = { accredited_provider_id: partner.id, description: provider_partnership.description }
@provider_partnership_form = ProviderPartnershipForm.new(current_user, provider_partnership, params:)
end

def create
@provider_partnership_form = ProviderPartnershipForm.new(current_user, @provider_partnership, params: partnership_params)

if @provider_partnership_form.stash
redirect_to check_publish_provider_recruitment_cycle_accredited_partnerships_path(@provider.provider_code, recruitment_cycle.year)
else
render :new
end
end

def update
@provider_partnership = provider.accredited_partnerships.find_by(accredited_provider_id: partner.id)
@provider_partnership_form = ProviderPartnershipForm.new(current_user, @provider_partnership, params: partnership_params)

if @provider_partnership_form.save!
flash[:success] = t('.edit.updated')
redirect_to publish_provider_recruitment_cycle_accredited_partnerships_path(@provider.provider_code, recruitment_cycle.year)
else
render :edit
end
end

def delete
@provider_partnership = provider.accredited_partnerships.find_by(accredited_provider: partner)
cannot_delete
end

def destroy
@partnership = provider.accredited_partnerships.find_by(accredited_provider_id: partner.id)

if @partnership.destroy
flash[:success] = t('.delete.updated')
redirect_to publish_provider_recruitment_cycle_accredited_partnerships_path(@provider.provider_code, recruitment_cycle.year)
else
render :delete
end
end

private

def cannot_delete
@cannot_delete ||= provider.courses.exists?(accredited_provider_code: params[:accredited_provider_code])
end

def provider
@provider = recruitment_cycle.providers.find_by(provider_code: params[:provider_code])
end

def partner
recruitment_cycle.providers.find_by(provider_code: params[:accredited_provider_code])
end

def partnership_params
params.require(:provider_partnership_form).permit(:accredited_provider_id, :description)
end
end
end
end
Loading
Loading