2021-05-09 00:06:07 +08:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
class ExperimentMember
|
|
|
|
include ActiveModel::Model
|
|
|
|
|
|
|
|
attr_accessor :user_id, :user_role_id
|
2021-05-16 17:04:04 +08:00
|
|
|
attr_reader :current_user, :experiment,
|
|
|
|
:user, :project, :user_role,
|
|
|
|
:user_assignment
|
2021-05-09 00:06:07 +08:00
|
|
|
|
2021-05-23 00:36:30 +08:00
|
|
|
def initialize(current_user, experiment, project, user = nil, user_assignment = nil)
|
2021-05-09 00:06:07 +08:00
|
|
|
@experiment = experiment
|
|
|
|
@current_user = current_user
|
|
|
|
@project = project
|
2021-05-23 00:36:30 +08:00
|
|
|
@user_assignment = user_assignment
|
2021-05-09 00:06:07 +08:00
|
|
|
|
|
|
|
if user
|
|
|
|
@user = user
|
|
|
|
@user_role = UserAssignment.find_by(assignable: experiment, user: user)&.user_role
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-05-23 00:36:30 +08:00
|
|
|
def update(params)
|
2021-11-05 22:44:14 +08:00
|
|
|
prepare_data(params)
|
2021-05-23 00:36:30 +08:00
|
|
|
|
|
|
|
ActiveRecord::Base.transaction do
|
2021-06-12 15:36:41 +08:00
|
|
|
user_assignment.update!(user_role: user_role, assigned: :manually)
|
2021-11-05 22:44:14 +08:00
|
|
|
log_change_activity
|
2021-05-23 00:36:30 +08:00
|
|
|
end
|
2021-11-09 23:09:12 +08:00
|
|
|
|
|
|
|
UserAssignments::PropagateAssignmentJob.perform_later(
|
|
|
|
@experiment,
|
|
|
|
@user,
|
|
|
|
user_role,
|
|
|
|
current_user
|
|
|
|
)
|
2021-05-23 00:36:30 +08:00
|
|
|
end
|
|
|
|
|
2021-11-15 18:12:31 +08:00
|
|
|
def reset(params)
|
|
|
|
ActiveRecord::Base.transaction do
|
|
|
|
prepare_data(params)
|
|
|
|
|
|
|
|
@user_role =
|
|
|
|
@experiment.project
|
|
|
|
.user_assignments
|
|
|
|
.find_by(user: user_assignment.user)
|
|
|
|
.user_role
|
|
|
|
|
|
|
|
user_assignment.update!(user_role: @user_role, assigned: :automatically)
|
|
|
|
log_change_activity
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-05-16 17:04:04 +08:00
|
|
|
private
|
|
|
|
|
2021-11-05 22:44:14 +08:00
|
|
|
def prepare_data(params)
|
2021-05-23 00:36:30 +08:00
|
|
|
self.user_role_id = params[:user_role_id]
|
|
|
|
self.user_id = params[:user_id]
|
|
|
|
|
2021-11-05 22:44:14 +08:00
|
|
|
@user = @project.users.find(user_id)
|
2021-05-23 00:36:30 +08:00
|
|
|
@user_role = UserRole.find_by(id: user_role_id)
|
|
|
|
@user_assignment ||= UserAssignment.find_by(assignable: experiment, user: user)
|
|
|
|
end
|
|
|
|
|
2021-11-05 22:44:14 +08:00
|
|
|
def log_change_activity
|
|
|
|
Activities::CreateActivityService.call(
|
|
|
|
activity_type: :change_user_role_on_experiment,
|
|
|
|
owner: current_user,
|
|
|
|
subject: experiment,
|
|
|
|
team: project.team,
|
|
|
|
project: project,
|
|
|
|
message_items: {
|
|
|
|
experiment: experiment.id,
|
|
|
|
user_target: user.id,
|
|
|
|
role: user_role.name
|
|
|
|
}
|
|
|
|
)
|
2021-05-22 19:59:27 +08:00
|
|
|
end
|
2021-05-09 00:06:07 +08:00
|
|
|
end
|