diff --git a/app/controllers/support/providers/copy_courses_controller.rb b/app/controllers/support/providers/copy_courses_controller.rb index 35a2308117..f7def92f6c 100644 --- a/app/controllers/support/providers/copy_courses_controller.rb +++ b/app/controllers/support/providers/copy_courses_controller.rb @@ -4,25 +4,31 @@ module Support module Providers class CopyCoursesController < SupportController before_action :recruitment_cycle + def new - @provider = Provider.find(params[:provider_id]) + @target_provider = Provider.find(params[:provider_id]) + @copy_courses_form = CopyCoursesForm.new(@target_provider) end def create - @provider = Provider.find(params[:provider_id]) - @from_provider = recruitment_cycle.providers.find_by(provider_code: params[:course][:autocompleted_provider_code]) + @target_provider = Provider.find(params[:provider_id]) + @copy_courses_form = CopyCoursesForm.new(@target_provider, recruitment_cycle.providers.find_by(provider_code: params[:course][:autocompleted_provider_code])) - sites_copy_to_course = params[:schools] ? Sites::CopyToCourseService : ->(*) {} + if @copy_courses_form.valid? + sites_copy_to_course = params[:schools] ? Sites::CopyToCourseService : ->(*) {} - copier = ::Courses::CopyToProviderService.new(sites_copy_to_course:, enrichments_copy_to_course: Enrichments::CopyToCourseService.new, force: true) + copier = ::Courses::CopyToProviderService.new(sites_copy_to_course:, enrichments_copy_to_course: Enrichments::CopyToCourseService.new, force: true) - Provider.transaction do - @from_provider.courses.map do |course| - copier.execute(course:, new_provider: @provider) + Provider.transaction do + @copy_courses_form.provider.courses.map do |course| + copier.execute(course:, new_provider: @copy_courses_form.target_provider) + end end - end - redirect_to support_recruitment_cycle_provider_path(recruitment_cycle.year, @provider.id) + redirect_to support_recruitment_cycle_provider_path(recruitment_cycle.year, @copy_courses_form.target_provider.id) + else + render :new + end end end end diff --git a/app/forms/support/copy_courses_form.rb b/app/forms/support/copy_courses_form.rb new file mode 100644 index 0000000000..b415052187 --- /dev/null +++ b/app/forms/support/copy_courses_form.rb @@ -0,0 +1,16 @@ +# frozen_string_literal: true + +module Support + class CopyCoursesForm + include ActiveModel::Model + + attr_reader :target_provider, :provider + + def initialize(target_provider, provider = nil) + @target_provider = target_provider + @provider = provider + end + + validates :target_provider, :provider, presence: true + end +end diff --git a/app/views/support/providers/copy_courses/new.html.erb b/app/views/support/providers/copy_courses/new.html.erb index 348b6128b9..7128498ddd 100644 --- a/app/views/support/providers/copy_courses/new.html.erb +++ b/app/views/support/providers/copy_courses/new.html.erb @@ -1,26 +1,28 @@ -