scinote-web/app/services/user_assignments/remove_team_user_assignment_service.rb

56 lines
1.6 KiB
Ruby
Raw Normal View History

# frozen_string_literal: true
module UserAssignments
class RemoveTeamUserAssignmentService
def initialize(user, team)
@user = user
@team = team
end
def call
@team.projects.find_each do |project|
UserAssignments::PropagateAssignmentJob
.perform_now(project, @user, nil, nil, destroy: true, remove_from_team: true)
end
remove_repositories_assignments
remove_protocols_assignments
remove_reports_assignments
end
private
def remove_repositories_assignments
@team.repositories
.joins(:user_assignments)
.preload(:user_assignments)
.where(user_assignments: { user: @user, team: @team }).find_each do |repository|
repository.user_assignments
.select { |assignment| assignment.user_id == @user.id && assignment.team_id == @team.id }
.each(&:destroy!)
end
end
def remove_protocols_assignments
@team.repository_protocols
.joins(:user_assignments)
.preload(:user_assignments)
.where(user_assignments: { user: @user }).find_each do |protocol|
protocol.user_assignments
.select { |assignment| assignment.user_id == @user.id }
.each(&:destroy!)
end
end
def remove_reports_assignments
@team.reports
.joins(:user_assignments)
.preload(:user_assignments)
.where(user_assignments: { user: @user }).find_each do |report|
report.user_assignments
.select { |assignment| assignment.user_id == @user.id }
.each(&:destroy!)
end
end
end
end