Skip to content

OpenProject is the leading open source project management software for product, project and portfolio management. A powerful Jira alternative with agile planning, issue tracking, roadmaps, Gantt charts, time tracking, collaboration features, and more. Available on premises or in the cloud. ⭐ Star us on GitHub

feat[Op#55966]: define bulk services

Kabiru Mwenja710a7db926f9
diff --git a/modules/storages/app/controllers/storages/admin/storages/project_storages_controller.rb b/modules/storages/app/controllers/storages/admin/storages/project_storages_controller.rb
index a039704b6f6..6c3405b65dc 100644
--- a/modules/storages/app/controllers/storages/admin/storages/project_storages_controller.rb
+++ b/modules/storages/app/controllers/storages/admin/storages/project_storages_controller.rb
@@ -31,6 +31,7 @@
class Storages::Admin::Storages::ProjectStoragesController < ApplicationController
include OpTurbo::ComponentStream
include OpTurbo::DialogStreamHelper
+ include FlashMessagesOutputSafetyHelper
layout "admin"
@@ -57,7 +58,25 @@ class Storages::Admin::Storages::ProjectStoragesController < ApplicationControll
respond_with_dialog Storages::Admin::Storages::AddProjectsModalComponent.new(project_storage: @project_storage)
end
- def create; end
+ def create
+ create_service = ::Storages::ProjectStorages::BulkCreateService
+ .new(user: current_user, projects: @projects, storage: @storage,
+ project_folder_mode: params.to_unsafe_h[:storages_project_storage][:project_folder_mode],
+ include_sub_projects: include_sub_projects?)
+ .call
+
+ create_service.on_success { update_project_list_via_turbo_stream(url_for_action: :index) }
+
+ create_service.on_failure do
+ update_flash_message_via_turbo_stream(
+ message: join_flash_messages(create_service.errors),
+ full: true, dismiss_scheme: :hide, scheme: :danger
+ )
+ end
+
+ respond_with_turbo_streams(status: create_service.success? ? :ok : :unprocessable_entity)
+ end
+
def destroy; end
private
@@ -85,9 +104,12 @@ class Storages::Admin::Storages::ProjectStoragesController < ApplicationControll
respond_with_turbo_streams
end
- def update_project_list_via_turbo_stream
+ def update_project_list_via_turbo_stream(url_for_action: action_name)
update_via_turbo_stream(
- component: Storages::ProjectStorages::Projects::TableComponent.new(query: storage_projects_query, params:)
+ component: Storages::ProjectStorages::Projects::TableComponent.new(
+ query: storage_projects_query,
+ params: { url_for_action: }
+ )
)
end
@@ -105,4 +127,8 @@ class Storages::Admin::Storages::ProjectStoragesController < ApplicationControll
.call(storage: @storage)
.result
end
+
+ def include_sub_projects?
+ ActiveRecord::Type::Boolean.new.cast(params.to_unsafe_h[:storages_project_storage][:include_sub_projects])
+ end
end
diff --git a/modules/storages/app/services/storages/last_project_folders/bulk_create_service.rb b/modules/storages/app/services/storages/last_project_folders/bulk_create_service.rb
new file mode 100644
index 00000000000..254e864076c
--- /dev/null
+++ b/modules/storages/app/services/storages/last_project_folders/bulk_create_service.rb
@@ -0,0 +1,110 @@
+# frozen_string_literal: true
+
+#-- copyright
+# OpenProject is an open source project management software.
+# Copyright (C) 2012-2024 the OpenProject GmbH
+#
+# This program is free software; you can redistribute it and/or
+# modify it under the terms of the GNU General Public License version 3.
+#
+# OpenProject is a fork of ChiliProject, which is a fork of Redmine. The copyright follows:
+# Copyright (C) 2006-2013 Jean-Philippe Lang
+# Copyright (C) 2010-2013 the ChiliProject Team
+#
+# This program is free software; you can redistribute it and/or
+# modify it under the terms of the GNU General Public License
+# as published by the Free Software Foundation; either version 2
+# of the License, or (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, write to the Free Software
+# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+#
+# See COPYRIGHT and LICENSE files for more details.
+#++
+
+module Storages::LastProjectFolders
+ class BulkCreateService < ::BaseServices::BaseCallable
+ def initialize(user:, project_storages:)
+ super()
+ @user = user
+ @project_storages = project_storages
+ end
+
+ def perform
+ service_call = validate_permissions
+ service_call = validate_contract(service_call) if service_call.success?
+ service_call = perform_bulk_create(service_call) if service_call.success?
+
+ service_call
+ end
+
+ private
+
+ def validate_permissions
+ return ServiceResult.failure(errors: I18n.t(:label_not_found)) if incoming_projects.empty?
+
+ if @user.allowed_in_project?(:manage_files_in_project, incoming_projects)
+ ServiceResult.success
+ else
+ ServiceResult.failure(errors: I18n.t("activerecord.errors.messages.error_unauthorized"))
+ end
+ end
+
+ def validate_contract(service_call)
+ set_attributes_results = @project_storages.map do |project_storage|
+ set_attributes(project_storage_id: project_storage.id, origin_folder_id: project_storage.project_folder_id,
+ mode: project_storage.project_folder_mode)
+ end
+
+ if (failures = set_attributes_results.select(&:failure?)).any?
+ service_call.success = false
+ service_call.errors = failures.map(&:errors)
+ else
+ service_call.result = set_attributes_results.map(&:result)
+ end
+
+ service_call
+ end
+
+ def perform_bulk_create(service_call)
+ bulk_insertion = ::Storages::LastProjectFolder.insert_all(
+ service_call.result.map { |model| model.attributes.slice("project_storage_id", "origin_folder_id", "mode") },
+ returning: %w[id]
+ )
+ service_call.result = ::Storages::LastProjectFolder.where(id: bulk_insertion.rows.flatten)
+
+ service_call
+ end
+
+ def incoming_projects
+ @incoming_projects ||= Project.where(id: @project_storages.pluck(:project_id))
+ end
+
+ def set_attributes(params)
+ attributes_service_class
+ .new(user: @user,
+ model: instance(params),
+ contract_class: default_contract_class,
+ contract_options: {})
+ .call(params)
+ end
+
+ def instance(params)
+ ::Storages::LastProjectFolder.new(params)
+ end
+
+ def attributes_service_class
+ Storages::LastProjectFolders::SetAttributesService
+ end
+
+ def default_contract_class
+ Storages::LastProjectFolders::CreateContract
+ end
+ end
+end
diff --git a/modules/storages/app/services/storages/project_storages/bulk_create_service.rb b/modules/storages/app/services/storages/project_storages/bulk_create_service.rb
new file mode 100644
index 00000000000..5019a63576c
--- /dev/null
+++ b/modules/storages/app/services/storages/project_storages/bulk_create_service.rb
@@ -0,0 +1,152 @@
+# frozen_string_literal: true
+
+#-- copyright
+# OpenProject is an open source project management software.
+# Copyright (C) 2012-2024 the OpenProject GmbH
+#
+# This program is free software; you can redistribute it and/or
+# modify it under the terms of the GNU General Public License version 3.
+#
+# OpenProject is a fork of ChiliProject, which is a fork of Redmine. The copyright follows:
+# Copyright (C) 2006-2013 Jean-Philippe Lang
+# Copyright (C) 2010-2013 the ChiliProject Team
+#
+# This program is free software; you can redistribute it and/or
+# modify it under the terms of the GNU General Public License
+# as published by the Free Software Foundation; either version 2
+# of the License, or (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, write to the Free Software
+# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+#
+# See COPYRIGHT and LICENSE files for more details.
+#++
+
+module Storages::ProjectStorages
+ class BulkCreateService < ::BaseServices::BaseCallable
+ def initialize(user:, projects:, storage:, project_folder_mode:, include_sub_projects: false)
+ super()
+ @user = user
+ @projects = projects
+ @storage = storage
+ @project_folder_mode = project_folder_mode
+ @include_sub_projects = include_sub_projects
+ end
+
+ def perform
+ service_call = validate_permissions
+ service_call = validate_contract(service_call, incoming_activations_ids) if service_call.success?
+ service_call = perform_bulk_create(service_call) if service_call.success?
+ service_call = create_last_project_folders(service_call) if service_call.success?
+ broadcast_project_storages_created if service_call.success?
+
+ service_call
+ end
+
+ private
+
+ def validate_permissions
+ return ServiceResult.failure(errors: I18n.t(:label_not_found)) if incoming_projects.empty?
+
+ if @user.allowed_in_project?(:manage_files_in_project, incoming_projects)
+ ServiceResult.success
+ else
+ ServiceResult.failure(errors: I18n.t("activerecord.errors.messages.error_unauthorized"))
+ end
+ end
+
+ def validate_contract(service_call, project_ids)
+ set_attributes_results = project_ids.map do |id|
+ set_attributes(project_id: id, storage_id: @storage.id, project_folder_mode: @project_folder_mode)
+ end
+
+ if (failures = set_attributes_results.select(&:failure?)).any?
+ service_call.success = false
+ service_call.errors = failures.map(&:errors)
+ else
+ service_call.result = set_attributes_results.map(&:result)
+ end
+
+ service_call
+ end
+
+ def perform_bulk_create(service_call)
+ bulk_insertion = ::Storages::ProjectStorage.insert_all(
+ service_call.result.map do |model|
+ model.attributes.slice("project_id", "storage_id", "project_folder_mode", "creator_id")
+ end,
+ unique_by: %i[project_id storage_id],
+ returning: %w[id]
+ )
+ service_call.result = ::Storages::ProjectStorage.where(id: bulk_insertion.rows.flatten)
+
+ service_call
+ end
+
+ def create_last_project_folders(service_call)
+ return service_call if @project_folder_mode.to_sym == :inactive
+
+ last_project_folders = ::Storages::LastProjectFolders::BulkCreateService
+ .new(user: @user, project_storages: service_call.result)
+ .call
+
+ service_call.add_dependent!(last_project_folders)
+ service_call
+ end
+
+ def broadcast_project_storages_created
+ OpenProject::Notifications.send(
+ OpenProject::Events::PROJECT_STORAGE_CREATED,
+ project_folder_mode: @project_folder_mode,
+ storage: @storage
+ )
+ end
+
+ def incoming_activations_ids
+ project_ids = incoming_projects.pluck(:id)
+ project_ids - existing_project_storages(project_ids)
+ end
+
+ def incoming_projects
+ @projects.each_with_object(Set.new) do |project, projects_set|
+ next unless project.active?
+
+ projects_set << project
+ projects_set.merge(project.active_subprojects.to_a) if @include_sub_projects
+ end.to_a
+ end
+
+ def existing_project_storages(project_ids)
+ ::Storages::ProjectStorage
+ .where(storage_id: @storage.id, project_id: project_ids)
+ .pluck(:project_id)
+ end
+
+ def set_attributes(params)
+ attributes_service_class
+ .new(user: @user,
+ model: instance(params),
+ contract_class: default_contract_class,
+ contract_options: {})
+ .call(params)
+ end
+
+ def instance(params)
+ ::Storages::ProjectStorage.new(params)
+ end
+
+ def attributes_service_class
+ ::Storages::ProjectStorages::SetAttributesService
+ end
+
+ def default_contract_class
+ ::Storages::ProjectStorages::CreateContract
+ end
+ end
+end