-
Notifications
You must be signed in to change notification settings - Fork 2.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore[Op#57902]: Introduce base bulk create service
DRY up common logic in: - `CustomFields::CustomFieldProjects::BulkCreateService` - `ProjectCustomFieldProjectMappings::BulkCreateService` - `::Storages::ProjectStorages::BulkCreateService`
- Loading branch information
Showing
4 changed files
with
172 additions
and
158 deletions.
There are no files selected for viewing
153 changes: 153 additions & 0 deletions
153
app/services/bulk_services/project_mappings/base_create_service.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,153 @@ | ||
# frozen_string_literal: true | ||
|
||
#-- copyright | ||
# OpenProject is an open source project management software. | ||
# Copyright (C) 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 BulkServices | ||
module ProjectMappings | ||
class BaseCreateService < ::BaseServices::BaseCallable | ||
def initialize(user:, projects:, model:, include_sub_projects: false) | ||
super() | ||
@user = user | ||
@projects = projects | ||
@model = model | ||
@include_sub_projects = include_sub_projects | ||
end | ||
|
||
def perform(params = {}) | ||
service_call = validate_permissions | ||
service_call = validate_contract(service_call, incoming_mapping_ids, params) if service_call.success? | ||
service_call = perform_bulk_create(service_call) if service_call.success? | ||
service_call = after_perform(service_call, params) 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?(permission, incoming_projects) | ||
ServiceResult.success | ||
else | ||
ServiceResult.failure(errors: I18n.t("activerecord.errors.messages.error_unauthorized")) | ||
end | ||
end | ||
|
||
def validate_contract(service_call, project_ids, _params) | ||
set_attributes_results = project_ids.map do |id| | ||
set_attributes(project_id: id, model_foreign_key_id => @model.id) | ||
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) | ||
mapping_model_class.insert_all( | ||
service_call.result.map { |model| model.attributes.slice("project_id", model_foreign_key_id.to_s) }, | ||
unique_by: %i[project_id].push(model_foreign_key_id.to_sym) | ||
) | ||
|
||
service_call | ||
end | ||
|
||
def after_perform(service_call, _params) | ||
service_call # Subclasses can override this method to add additional logic | ||
end | ||
|
||
def incoming_mapping_ids | ||
project_ids = incoming_projects.pluck(:id) | ||
project_ids - existing_project_mappings(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_mappings(project_ids) | ||
mapping_model_class.where( | ||
model_foreign_key_id => @model.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) | ||
mapping_model_class.new(params) | ||
end | ||
|
||
# @return [Symbol] the permission required to create the mapping | ||
def permission | ||
raise NotImplementedError | ||
end | ||
|
||
# @return [Symbol] the column name of the mapping | ||
def model_foreign_key_id | ||
raise NotImplementedError | ||
end | ||
|
||
# @return [Class] the model class of the mapping | ||
def mapping_model_class | ||
raise NotImplementedError | ||
end | ||
|
||
def attributes_service_class | ||
"#{namespace}::SetAttributesService".constantize | ||
end | ||
|
||
def default_contract_class | ||
"#{namespace}::UpdateContract".constantize | ||
end | ||
|
||
def namespace | ||
self.class.name.deconstantize.pluralize | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters