scinote-web/app/jobs/user_assignments/protocol_group_assignment_job.rb

29 lines
752 B
Ruby
Raw Normal View History

2023-02-17 21:27:20 +08:00
# frozen_string_literal: true
module UserAssignments
class ProtocolGroupAssignmentJob < ApplicationJob
queue_as :high_priority
2023-02-22 21:59:48 +08:00
def perform(team, protocol, assigned_by)
2023-02-17 21:27:20 +08:00
@team = team
@assigned_by = assigned_by
ActiveRecord::Base.transaction do
team.users.where.not(id: assigned_by).find_each do |user|
user_assignment = UserAssignment.find_or_initialize_by(
user: user,
2023-02-22 21:59:48 +08:00
assignable: protocol
2023-02-17 21:27:20 +08:00
)
next if user_assignment.manually_assigned?
user_assignment.update!(
user_role: protocol.default_public_user_role || UserRole.find_predefined_viewer_role,
assigned_by: @assigned_by
)
end
end
end
end
end