2021-06-27 19:22:19 +08:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
module UserAssignments
|
2022-10-20 21:35:43 +08:00
|
|
|
class ProjectGroupAssignmentJob < ApplicationJob
|
2021-06-27 19:22:19 +08:00
|
|
|
queue_as :high_priority
|
|
|
|
|
2023-08-11 21:31:29 +08:00
|
|
|
def perform(team, project, assigned_by_id)
|
2021-06-27 19:22:19 +08:00
|
|
|
@team = team
|
2023-08-11 21:31:29 +08:00
|
|
|
@assigned_by = User.find_by(id: assigned_by_id)
|
2021-06-27 19:22:19 +08:00
|
|
|
|
2023-04-21 17:58:34 +08:00
|
|
|
return unless project.visible?
|
|
|
|
|
2021-06-27 19:22:19 +08:00
|
|
|
ActiveRecord::Base.transaction do
|
2023-04-24 22:27:59 +08:00
|
|
|
team.users.find_each do |user|
|
2021-12-01 23:23:24 +08:00
|
|
|
user_assignment = UserAssignment.find_or_initialize_by(
|
2021-06-27 19:22:19 +08:00
|
|
|
user: user,
|
2021-12-01 23:23:24 +08:00
|
|
|
assignable: project
|
|
|
|
)
|
|
|
|
|
|
|
|
next if user_assignment.manually_assigned?
|
|
|
|
|
|
|
|
user_assignment.update!(
|
2022-10-20 21:35:43 +08:00
|
|
|
user_role: project.default_public_user_role || UserRole.find_predefined_viewer_role,
|
2021-12-01 23:23:24 +08:00
|
|
|
assigned_by: @assigned_by
|
2021-06-27 19:22:19 +08:00
|
|
|
)
|
2021-12-01 23:23:24 +08:00
|
|
|
|
2021-06-27 19:22:19 +08:00
|
|
|
# make sure all related experiments and my modules are assigned
|
|
|
|
UserAssignments::PropagateAssignmentJob.perform_later(
|
|
|
|
project,
|
2023-08-11 21:31:29 +08:00
|
|
|
user.id,
|
2022-10-20 21:35:43 +08:00
|
|
|
project.default_public_user_role || UserRole.find_predefined_viewer_role,
|
2023-08-16 20:34:42 +08:00
|
|
|
@assigned_by&.id
|
2021-06-27 19:22:19 +08:00
|
|
|
)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|