2019-12-04 20:38:33 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
module Milestones
|
|
|
|
class FindOrCreateService
|
|
|
|
attr_accessor :project, :current_user, :params
|
|
|
|
|
|
|
|
def initialize(project, user, params = {})
|
2021-04-29 21:17:54 +05:30
|
|
|
@project = project
|
|
|
|
@current_user = user
|
|
|
|
@params = params.dup
|
2019-12-04 20:38:33 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
def execute
|
|
|
|
find_milestone || create_milestone
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
# rubocop: disable CodeReuse/ActiveRecord
|
|
|
|
def find_milestone
|
|
|
|
groups = project.group&.self_and_ancestors_ids
|
|
|
|
Milestone.for_projects_and_groups([project.id], groups).find_by(title: params["title"])
|
|
|
|
end
|
|
|
|
# rubocop: enable CodeReuse/ActiveRecord
|
|
|
|
|
|
|
|
def create_milestone
|
|
|
|
return unless current_user.can?(:admin_milestone, project)
|
|
|
|
|
|
|
|
new_milestone if new_milestone.persisted?
|
|
|
|
end
|
|
|
|
|
|
|
|
def new_milestone
|
|
|
|
@new_milestone ||= CreateService.new(project, current_user, params).execute
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|