2021-06-06 15:54:47 +08:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
module UserAssignments
|
|
|
|
class GenerateUserAssignmentsJob < ApplicationJob
|
|
|
|
queue_as :high_priority
|
|
|
|
|
|
|
|
def perform(object, assigned_by)
|
|
|
|
@assigned_by = assigned_by
|
|
|
|
ActiveRecord::Base.transaction do
|
2021-09-30 17:32:11 +08:00
|
|
|
case object
|
|
|
|
when Experiment
|
2021-06-06 15:54:47 +08:00
|
|
|
assign_users_to_experiment(object)
|
2021-09-30 17:32:11 +08:00
|
|
|
when MyModule
|
2021-06-06 15:54:47 +08:00
|
|
|
assign_users_to_my_module(object)
|
2022-10-18 16:43:51 +08:00
|
|
|
when Repository
|
|
|
|
assign_users_to_repository(object)
|
2022-10-19 23:26:13 +08:00
|
|
|
when Protocol
|
|
|
|
assign_users_to_protocol(object)
|
2022-10-20 21:35:43 +08:00
|
|
|
when Report
|
|
|
|
assign_users_to_report(object)
|
2021-06-06 15:54:47 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def assign_users_to_experiment(experiment)
|
|
|
|
project = experiment.project
|
|
|
|
project.user_assignments.find_each do |user_assignment|
|
|
|
|
create_or_update_user_assignment(user_assignment, experiment)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def assign_users_to_my_module(my_module)
|
2022-02-18 18:25:30 +08:00
|
|
|
experiment = my_module.experiment
|
|
|
|
experiment.user_assignments.find_each do |user_assignment|
|
2021-06-06 15:54:47 +08:00
|
|
|
create_or_update_user_assignment(user_assignment, my_module)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-10-18 16:43:51 +08:00
|
|
|
def assign_users_to_repository(repository)
|
|
|
|
team = repository.team
|
|
|
|
team.user_assignments.find_each do |user_assignment|
|
|
|
|
create_or_update_user_assignment(user_assignment, repository)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-10-19 23:26:13 +08:00
|
|
|
def assign_users_to_protocol(protocol)
|
2023-03-06 23:06:38 +08:00
|
|
|
if protocol.parent_id && protocol.in_repository_draft?
|
|
|
|
Protocol.transaction(requires_new: true) do
|
|
|
|
protocol.parent.user_assignments.find_each do |user_assignment|
|
|
|
|
protocol.parent.sync_child_protocol_user_assignment(user_assignment, protocol.id)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
2023-02-13 23:18:26 +08:00
|
|
|
return unless protocol.visible?
|
2022-10-19 23:26:13 +08:00
|
|
|
|
2023-04-04 19:42:20 +08:00
|
|
|
protocol.create_or_update_public_user_assignments!(@assigned_by)
|
2022-10-19 23:26:13 +08:00
|
|
|
end
|
|
|
|
|
2022-10-20 21:35:43 +08:00
|
|
|
def assign_users_to_report(report)
|
|
|
|
team = report.team
|
|
|
|
team.user_assignments.find_each do |user_assignment|
|
|
|
|
create_or_update_user_assignment(user_assignment, report)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-06-06 15:54:47 +08:00
|
|
|
def create_or_update_user_assignment(parent_user_assignment, object)
|
|
|
|
user_role = parent_user_assignment.user_role
|
|
|
|
user = parent_user_assignment.user
|
|
|
|
new_user_assignment = UserAssignment.find_or_initialize_by(user: user, assignable: object)
|
2021-06-12 16:27:53 +08:00
|
|
|
return if new_user_assignment.manually_assigned?
|
|
|
|
|
2021-06-06 15:54:47 +08:00
|
|
|
new_user_assignment.user_role = user_role
|
|
|
|
new_user_assignment.assigned_by = @assigned_by
|
2021-06-12 15:36:41 +08:00
|
|
|
new_user_assignment.assigned = :automatically
|
2021-06-06 15:54:47 +08:00
|
|
|
new_user_assignment.save!
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|