2020-02-25 20:46:44 +08:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
class CreateExperimentService
|
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
|
|
|
|
ActiveRecord::Base.transaction do
|
2021-10-14 19:13:29 +08:00
|
|
|
unless @params[:project].instance_of?(Project)
|
2020-02-25 20:46:44 +08:00
|
|
|
@params[:project] = CreateProjectService.new(@user, @team, @params[:project]).call
|
|
|
|
end
|
2021-10-14 19:13:29 +08:00
|
|
|
|
|
|
|
raise ActiveRecord::Rollback unless @params[:project]&.valid? &&
|
|
|
|
can_create_project_experiments?(@user, @params[:project])
|
2020-02-25 20:46:44 +08:00
|
|
|
|
|
|
|
@params[:created_by] = @user
|
|
|
|
@params[:last_modified_by] = @user
|
|
|
|
|
2021-10-14 19:13:29 +08:00
|
|
|
@experiment = @params[:project].experiments.create!(@params)
|
|
|
|
create_experiment_activity
|
2020-02-25 20:46:44 +08:00
|
|
|
end
|
2021-10-14 19:13:29 +08:00
|
|
|
@experiment
|
2020-02-25 20:46:44 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def create_experiment_activity
|
|
|
|
Activities::CreateActivityService
|
|
|
|
.call(activity_type: :create_experiment,
|
|
|
|
owner: @user,
|
|
|
|
subject: @experiment,
|
|
|
|
team: @team,
|
|
|
|
project: @experiment.project,
|
|
|
|
message_items: { experiment: @experiment.id })
|
|
|
|
end
|
|
|
|
end
|