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

3.7 KB
Raw Blame
# 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.
#++

class WorkPackages::CreateService < BaseServices::BaseCallable
  include ::WorkPackages::Shared::UpdateAncestors
  include ::Shared::ServiceContext

  attr_reader :user, :contract_class

  def initialize(user:, contract_class: WorkPackages::CreateContract)
    super()
    @user = user
    @contract_class = contract_class
  end

  def perform
    attributes = params.except(:send_notifications, :work_package)
    work_package = params[:work_package] || WorkPackage.new

    in_user_context(send_notifications: params[:send_notifications]) do
      create(attributes, work_package)
    end
  end

  protected

  def create(attributes, work_package)
    result = set_attributes(attributes, work_package)

    result.success =
      if result.success
        work_package.attachments = work_package.attachments_replacements if work_package.attachments_replacements
        work_package.save

        set_templated_subject(work_package)
      end

    if result.success?
      # update ancestors before rescheduling, as the parent might switch to automatic mode
      update_ancestors_all_attributes(result.all_results).each do |ancestor_result|
        result.merge!(ancestor_result)
      end

      result.merge!(reschedule_related(work_package))

      set_user_as_watcher(work_package)
    end

    result
  end

  def set_templated_subject(work_package)
    return true unless work_package.type&.replacement_pattern_defined_for?(:subject)

    work_package.subject = work_package.type.enabled_patterns[:subject].resolve(work_package)
    work_package.save
  end

  def set_attributes(attributes, work_package)
    attributes_service_class.new(user:, model: work_package, contract_class:).call(attributes)
  end

  def reschedule_related(work_package)
    # Force work package to keep its scheduling mode if it's automatic.
    # This is necessary in bulk duplicate scenarios.
    switching_to_automatic_mode = []
    switching_to_automatic_mode << work_package if work_package.schedule_automatically?
    result = WorkPackages::SetScheduleService.new(user:, work_package:, switching_to_automatic_mode:).call

    result.self_and_dependent.each do |r|
      unless r.result.save
        result.success = false
        r.errors = r.result.errors
      end
    end

    result
  end

  def set_user_as_watcher(work_package)
    # We don't care if it fails here. If it does
    # the user simply does not become watcher
    Services::CreateWatcher.new(work_package, user).run(send_notifications: false)
  end

  def attributes_service_class
    ::WorkPackages::SetAttributesService
  end
end