2020-02-25 20:46:44 +08:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
class CreateExperimentService
|
|
|
|
def initialize(user, team, params)
|
|
|
|
@params = params
|
|
|
|
@user = user
|
|
|
|
@team = team
|
|
|
|
end
|
|
|
|
|
|
|
|
def call
|
2020-03-10 20:16:56 +08:00
|
|
|
new_experiment = nil
|
2020-02-25 20:46:44 +08:00
|
|
|
ActiveRecord::Base.transaction do
|
|
|
|
unless @params[:project].class == Project
|
|
|
|
@params[:project] = CreateProjectService.new(@user, @team, @params[:project]).call
|
|
|
|
end
|
2020-03-10 20:16:56 +08:00
|
|
|
unless @params[:project]&.errors&.empty?
|
|
|
|
new_experiment = @params[:project]
|
|
|
|
raise ActiveRecord::Rollback
|
|
|
|
end
|
2020-02-25 20:46:44 +08:00
|
|
|
|
|
|
|
@params[:created_by] = @user
|
|
|
|
@params[:last_modified_by] = @user
|
|
|
|
|
2020-03-10 20:16:56 +08:00
|
|
|
@experiment = @params[:project].experiments.new(@params)
|
|
|
|
|
|
|
|
create_experiment_activity if @experiment.save
|
|
|
|
|
|
|
|
new_experiment = @experiment
|
2020-02-25 20:46:44 +08:00
|
|
|
end
|
2020-03-10 20:16:56 +08:00
|
|
|
new_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
|