diff --git a/app/controllers/support/providers/accredited_partners_controller.rb b/app/controllers/support/providers/accredited_partners_controller.rb new file mode 100644 index 0000000000..6c8cc61b72 --- /dev/null +++ b/app/controllers/support/providers/accredited_partners_controller.rb @@ -0,0 +1,103 @@ +# frozen_string_literal: true + +module Support + module Providers + class AccreditedPartnersController < ApplicationController + include ClearStashable + + helper_method :accredited_provider_id + + before_action :reset_accredited_provider_form, only: %i[index] + + def index + @pagy, @accredited_partnerships = pagy(provider.accredited_partnerships) + render layout: 'provider_record' + end + + def new + accredited_provider_form + end + + def edit + provider + accredited_provider + @accredited_provider_form = ::AccreditedProviderForm.new(current_user, provider, params: provider.accredited_body(params[:accredited_provider_code])) + end + + def create + @accredited_provider_form = ::AccreditedProviderForm.new(current_user, provider, params: accredited_provider_params) + if @accredited_provider_form.stash + redirect_to check_support_recruitment_cycle_provider_accredited_providers_path + else + render :new + end + end + + def update + @accredited_provider_form = ::AccreditedProviderForm.new(current_user, provider, params: accredited_provider_params) + + if @accredited_provider_form.save! + redirect_to support_recruitment_cycle_provider_accredited_providers_path( + recruitment_cycle_year: @recruitment_cycle.year, + provider_id: @provider.id + ) + + flash[:success] = t('support.providers.accredited_providers.edit.updated') + else + accredited_provider + render(:edit) + end + end + + def delete + cannot_delete + end + + def destroy + return if cannot_delete + + provider.accrediting_provider_enrichments = accrediting_provider_enrichments + provider.save + + flash[:success] = t('support.providers.accredited_providers.delete.updated') + + redirect_to support_recruitment_cycle_provider_accredited_providers_path( + recruitment_cycle_year: @recruitment_cycle.year, + provider_id: @provider.id + ) + end + + private + + def cannot_delete + @cannot_delete ||= provider.courses.exists?(accredited_provider_code: accredited_provider.provider_code) + end + + def accrediting_provider_enrichments + provider.accrediting_provider_enrichments.reject { |enrichment| enrichment.UcasProviderCode == params['accredited_provider_code'] } + end + + def accredited_provider + @accredited_provider ||= @recruitment_cycle.providers.find_by(provider_code: params[:accredited_provider_code]) + end + + def provider + @provider ||= recruitment_cycle.providers.find(params[:provider_id]) + end + + def accredited_provider_id + params[:accredited_provider_id] || @accredited_provider_form.accredited_provider_id + end + + def accredited_provider_form + @accredited_provider_form ||= ::AccreditedProviderForm.new(current_user, provider) + end + + def accredited_provider_params + params.require(:accredited_provider_form) + .except(:goto_confirmation) + .permit(::AccreditedProviderForm::FIELDS) + end + end + end +end diff --git a/app/views/support/providers/accredited_partners/index.html.erb b/app/views/support/providers/accredited_partners/index.html.erb new file mode 100644 index 0000000000..a6a0967f89 --- /dev/null +++ b/app/views/support/providers/accredited_partners/index.html.erb @@ -0,0 +1,20 @@ +<%= render PageTitle.new(title: "support.providers.accredited_providers.index") %> + +<%= govuk_button_link_to("Add accredited partner", search_support_recruitment_cycle_provider_accredited_providers_path) %> + +<% if @accredited_partnerships.none? %> +
There are no accredited partnerships for <%= @provider.provider_name %>.
+<% else %> + <% @accredited_partnerships.each do |partnership| %> + <%= render AccreditedProviderComponent.new( + provider_name: govuk_link_to(partnership.accredited_provider.provider_name, support_recruitment_cycle_provider_path(partnership.accredited_provider.recruitment_cycle_year, partnership.accredited_provider)), + remove_path: delete_support_recruitment_cycle_provider_accredited_provider_path( + accredited_provider_code: partnership.accredited_provider.provider_code + ), + about_accredited_provider: partnership.description, + change_about_accredited_provider_path: edit_support_recruitment_cycle_provider_accredited_provider_path( + accredited_provider_code: partnership.accredited_provider.provider_code + ) + ) %> + <% end %> +<% end %> diff --git a/spec/features/support/providers/accredited_partners_spec.rb b/spec/features/support/providers/accredited_partners_spec.rb new file mode 100644 index 0000000000..96c645b68c --- /dev/null +++ b/spec/features/support/providers/accredited_partners_spec.rb @@ -0,0 +1,137 @@ +# frozen_string_literal: true + +require 'rails_helper' + +feature 'Accredited provider flow', { can_edit_current_and_next_cycles: false } do + before do + allow(Settings.features).to receive(:provider_partnerships).and_return(true) + given_i_am_authenticated_as_an_admin_user + and_there_are_accredited_providers_in_the_database + and_i_visit_the_index_page + end + + scenario 'i can view the accredited providers tab when there are none' do + then_i_see_the_correct_text_for_no_accredited_providers + end + + scenario 'i can view accredited providers on the index page' do + and_my_provider_has_accrediting_providers + and_i_click_on_the_accredited_provider_tab + then_i_should_see_the_accredited_provider_name_displayed + end + + private + + def and_i_see_the_remove_success_message + expect(page).to have_content('Accredited provider removed') + end + + def and_i_see_the_remove_success_message; end + + def and_i_click_remove_ap + click_link_or_button 'Remove accredited provider' + end + + def and_i_confirm_the_changes + click_link_or_button 'Add accredited provider' + end + + def when_i_input_new_information + fill_in 'About the accredited provider', with: 'New AP description' + click_link_or_button 'Continue' + end + + def and_i_select_the_provider + choose @accredited_provider.provider_name + click_link_or_button 'Continue' + end + + def form_title + 'Enter a provider name, UKPRN or postcode' + end + + def and_i_search_for_an_accredited_provider_with_a_valid_query + fill_in form_title, with: @accredited_provider.provider_name + click_link_or_button 'Continue' + end + + def and_i_click_add_accredited_provider + click_link_or_button 'Add accredited provider' + end + + def and_i_click_remove + click_link_or_button 'Remove' + end + + def then_i_should_see_the_cannot_remove_text + expect(page).to have_css('h1', text: 'You cannot remove this accredited provider') + end + + def given_i_am_authenticated_as_an_admin_user + given_i_am_authenticated(user: create(:user, :admin)) + end + + def and_there_are_accredited_providers_in_the_database + @provider = create(:provider, :lead_school) + @accredited_provider = create(:provider, :accredited_provider, provider_name: 'UCL', users: [create(:user)]) + @accredited_provider_two = create(:provider, :accredited_provider, provider_name: 'Accredited provider two') + @accredited_provider_three = create(:provider, :accredited_provider, provider_name: 'Accredited provider three') + end + + def then_i_return_to_the_index_page + expect(page).to have_current_path(support_recruitment_cycle_provider_accredited_partnerships_path( + recruitment_cycle_year: Settings.current_recruitment_cycle_year, + provider_id: @provider.id + )) + end + + def and_i_visit_the_index_page + visit support_recruitment_cycle_provider_accredited_partners_path( + recruitment_cycle_year: Settings.current_recruitment_cycle_year, + provider_id: @provider.id + ) + end + + def and_i_click_change + click_link_or_button('Change') + end + + def when_i_click_the_back_link + click_link_or_button 'Back' + end + + def and_i_see_the_success_message + expect(page).to have_content('About the accredited provider updated') + end + + def then_i_should_see_the_updated_description + expect(page).to have_text('update the AP description') + end + + def when_i_input_updated_description + fill_in 'About the accredited provider', with: 'update the AP description' + click_link_or_button 'Update description' + end + + def then_i_see_the_correct_text_for_no_accredited_providers + expect(page).to have_text("There are no accredited partnerships for #{@provider.provider_name}") + end + + def and_i_click_on_the_accredited_provider_tab + click_link_or_button 'Accredited partnerships' + end + + def and_my_provider_has_accrediting_providers + course = build(:course, accrediting_provider: build(:provider, :accredited_provider, provider_name: 'Accrediting provider name')) + + @provider.accredited_partnerships.create(accredited_provider: course.accrediting_provider, description: 'Description') + @provider.courses << course + end + + def then_i_should_see_the_accredited_provider_name_displayed + expect(page).to have_css( + 'h2.govuk-summary-card__title a.govuk-link', + text: 'Accrediting provider name' + ) + end +end