2019-01-15 20:45:05 +08:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
module Experiments
|
|
|
|
class MoveToProjectService
|
|
|
|
extend Service
|
2021-11-02 21:35:32 +08:00
|
|
|
include Canaid::Helpers::PermissionsHelper
|
2019-01-15 20:45:05 +08:00
|
|
|
|
|
|
|
attr_reader :errors
|
|
|
|
|
|
|
|
def initialize(experiment_id:, project_id:, user_id:)
|
|
|
|
@exp = Experiment.find experiment_id
|
|
|
|
@project = Project.find project_id
|
|
|
|
@user = User.find user_id
|
|
|
|
@original_project = @exp&.project
|
|
|
|
@errors = {}
|
|
|
|
end
|
|
|
|
|
|
|
|
def call
|
|
|
|
return self unless valid?
|
|
|
|
|
|
|
|
ActiveRecord::Base.transaction do
|
|
|
|
@exp.project = @project
|
2021-02-18 20:55:03 +08:00
|
|
|
@exp.my_modules.each do |my_module|
|
2021-10-27 19:48:52 +08:00
|
|
|
raise unless can_manage_my_module?(@user, my_module)
|
|
|
|
|
2021-06-13 00:51:55 +08:00
|
|
|
sync_user_assignments(my_module)
|
2021-10-29 21:16:30 +08:00
|
|
|
move_tags!(my_module)
|
2019-01-15 20:45:05 +08:00
|
|
|
end
|
|
|
|
|
2021-10-29 21:16:30 +08:00
|
|
|
move_activities!(@exp)
|
2021-02-18 20:55:03 +08:00
|
|
|
@exp.save!
|
2021-06-13 00:51:55 +08:00
|
|
|
sync_user_assignments(@exp)
|
2021-10-27 19:48:52 +08:00
|
|
|
rescue StandardError
|
|
|
|
if @exp.valid?
|
|
|
|
@errors[:main] = "Don't have permission for tasks manage"
|
|
|
|
else
|
|
|
|
@errors.merge!(@exp.errors.to_hash)
|
|
|
|
end
|
2021-02-26 23:29:56 +08:00
|
|
|
raise ActiveRecord::Rollback
|
2019-01-15 20:45:05 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
track_activity if succeed?
|
|
|
|
self
|
|
|
|
end
|
|
|
|
|
|
|
|
def succeed?
|
|
|
|
@errors.none?
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def valid?
|
|
|
|
unless @exp && @project && @user
|
|
|
|
@errors[:invalid_arguments] =
|
|
|
|
{ 'experiment': @exp,
|
|
|
|
'project': @project,
|
|
|
|
'user': @user }
|
|
|
|
.map do |key, value|
|
|
|
|
"Can't find #{key.capitalize}" if value.nil?
|
|
|
|
end.compact
|
|
|
|
return false
|
|
|
|
end
|
|
|
|
|
2021-09-30 17:32:11 +08:00
|
|
|
if @exp.movable_projects(@user).include?(@project)
|
|
|
|
true
|
|
|
|
else
|
2019-01-15 20:45:05 +08:00
|
|
|
@errors[:target_project_not_valid] =
|
|
|
|
['Experiment cannot be moved to this project']
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-10-29 21:16:30 +08:00
|
|
|
def move_tags!(my_module)
|
|
|
|
new_tags = []
|
|
|
|
my_module.tags.each do |tag|
|
|
|
|
new_tag = @project.tags.where.not(id: new_tags).find_by(name: tag.name, color: tag.color)
|
|
|
|
new_tag ||=
|
|
|
|
@project.tags.create!(name: tag.name, color: tag.color, created_by: @user, last_modified_by: @user)
|
|
|
|
new_tags << new_tag
|
2021-10-28 22:35:47 +08:00
|
|
|
end
|
2021-10-29 21:16:30 +08:00
|
|
|
my_module.tags.destroy_all
|
|
|
|
my_module.tags = new_tags
|
2021-10-28 22:35:47 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
# recursively move all activities in child associations to new project
|
|
|
|
def move_activities!(subject)
|
2021-11-10 18:55:38 +08:00
|
|
|
Activity.where(subject: subject).find_each do |activity|
|
|
|
|
activity.update!(project: @project)
|
|
|
|
end
|
2021-10-28 22:35:47 +08:00
|
|
|
|
|
|
|
child_associations = Extends::ACTIVITY_SUBJECT_CHILDREN[subject.class.name.underscore.to_sym]
|
|
|
|
return unless child_associations
|
|
|
|
|
|
|
|
child_associations.each do |child_association|
|
|
|
|
[subject.public_send(child_association)].flatten.each do |child_subject|
|
|
|
|
move_activities!(child_subject)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-06-13 00:51:55 +08:00
|
|
|
def sync_user_assignments(object)
|
|
|
|
# remove user assignments where the user are not present on the project
|
|
|
|
object.user_assignments.destroy_all
|
|
|
|
|
|
|
|
UserAssignments::GenerateUserAssignmentsJob.perform_later(object, @user)
|
|
|
|
end
|
|
|
|
|
2019-01-15 20:45:05 +08:00
|
|
|
def track_activity
|
2019-03-05 18:39:57 +08:00
|
|
|
Activities::CreateActivityService
|
|
|
|
.call(activity_type: :move_experiment,
|
|
|
|
owner: @user,
|
|
|
|
team: @project.team,
|
|
|
|
project: @project,
|
|
|
|
subject: @exp,
|
|
|
|
message_items: { experiment: @exp.id,
|
|
|
|
project_new: @project.id,
|
|
|
|
project_original: @original_project.id })
|
2019-01-15 20:45:05 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|