2020-02-25 20:46:44 +08:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
class CreateProjectService
|
2021-10-14 19:13:29 +08:00
|
|
|
include Canaid::Helpers::PermissionsHelper
|
|
|
|
|
2020-02-25 20:46:44 +08:00
|
|
|
def initialize(user, team, params)
|
|
|
|
@params = params
|
|
|
|
@user = user
|
|
|
|
@team = team
|
|
|
|
end
|
|
|
|
|
|
|
|
def call
|
2021-10-14 19:13:29 +08:00
|
|
|
return unless can_create_projects?(@user, @team)
|
|
|
|
|
2020-02-25 20:46:44 +08:00
|
|
|
ActiveRecord::Base.transaction do
|
|
|
|
@params[:created_by] = @user
|
|
|
|
@params[:last_modified_by] = @user
|
|
|
|
|
2021-10-14 19:13:29 +08:00
|
|
|
@project = @team.projects.create!(@params)
|
|
|
|
create_project_activity
|
2020-02-25 20:46:44 +08:00
|
|
|
end
|
2021-10-14 19:13:29 +08:00
|
|
|
@project
|
2020-02-25 20:46:44 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def create_project_activity
|
|
|
|
Activities::CreateActivityService
|
|
|
|
.call(activity_type: :create_project,
|
|
|
|
owner: @user,
|
|
|
|
subject: @project,
|
|
|
|
team: @team,
|
|
|
|
project: @project,
|
|
|
|
message_items: { project: @project.id })
|
|
|
|
end
|
|
|
|
end
|