Skip to content

Commit

Permalink
chore[Op#57579]: elevate bulk creation to custom fields DRY up projec…
Browse files Browse the repository at this point in the history
…t attributes
  • Loading branch information
akabiru committed Sep 6, 2024
1 parent 299aaa4 commit 7344b21
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 98 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,10 @@ def perform

private

def validate_permissions
def validate_permissions(permission: :select_custom_fields)
return ServiceResult.failure(errors: I18n.t(:label_not_found)) if incoming_projects.empty?

if @user.allowed_in_project?(:select_custom_fields, incoming_projects)
if @user.allowed_in_project?(permission, incoming_projects)
ServiceResult.success
else
ServiceResult.failure(errors: I18n.t("activerecord.errors.messages.error_unauthorized"))
Expand Down Expand Up @@ -117,15 +117,8 @@ def instance(params)
custom_field_project_mapping_class.new(params)
end

def attributes_service_class
custom_field_project_mapping_module::SetAttributesService
end

def default_contract_class
custom_field_project_mapping_module::UpdateContract
end

def custom_field_project_mapping_module = CustomFields::CustomFieldProjects
def attributes_service_class = CustomFields::CustomFieldProjects::SetAttributesService
def default_contract_class = CustomFields::CustomFieldProjects::UpdateContract
def custom_field_project_mapping_class = CustomFieldsProject
end
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,99 +29,19 @@
#++

module ProjectCustomFieldProjectMappings
class BulkCreateService < ::BaseServices::BaseCallable
class BulkCreateService < ::CustomFields::CustomFieldProjects::BulkCreateService
def initialize(user:, projects:, project_custom_field:, include_sub_projects: false)
super()
@user = user
@projects = projects
@project_custom_field = project_custom_field
@include_sub_projects = include_sub_projects
end

def perform
service_call = validate_permissions
service_call = validate_contract(service_call, incoming_mapping_ids) if service_call.success?
service_call = perform_bulk_create(service_call) if service_call.success?

service_call
super(user:, projects:, custom_field: project_custom_field, include_sub_projects:)
end

private

def validate_permissions
return ServiceResult.failure(errors: I18n.t(:label_not_found)) if incoming_projects.empty?

if @user.allowed_in_project?(:select_project_custom_fields, 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, custom_field_id: @project_custom_field.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)
ProjectCustomFieldProjectMapping.insert_all(
service_call.result.map { |model| model.attributes.slice("project_id", "custom_field_id") },
unique_by: %i[project_id custom_field_id]
)

service_call
end

def incoming_mapping_ids
project_ids = incoming_projects.pluck(:id)
project_ids - existing_project_mappings(project_ids)
def validate_permissions(permission: :select_project_custom_fields)
super
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)
ProjectCustomFieldProjectMapping.where(
custom_field_id: @project_custom_field.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)
ProjectCustomFieldProjectMapping.new(params)
end

def attributes_service_class
ProjectCustomFieldProjectMappings::SetAttributesService
end

def default_contract_class
ProjectCustomFieldProjectMappings::UpdateContract
end
def attributes_service_class = ProjectCustomFieldProjectMappings::SetAttributesService
def default_contract_class = ProjectCustomFieldProjectMappings::UpdateContract
def custom_field_project_mapping_class = ProjectCustomFieldProjectMapping
end
end

0 comments on commit 7344b21

Please sign in to comment.