2021-06-27 19:22:19 +08:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
module UserAssignments
|
2022-10-20 21:35:43 +08:00
|
|
|
class ProjectGroupUnAssignmentJob < ApplicationJob
|
2021-06-27 19:22:19 +08:00
|
|
|
queue_as :high_priority
|
|
|
|
|
|
|
|
def perform(project)
|
|
|
|
ActiveRecord::Base.transaction do
|
|
|
|
user_assignments = project.user_assignments.where(assigned: :automatically)
|
2022-09-09 19:31:16 +08:00
|
|
|
users = User.where(id: user_assignments.select(:user_id))
|
2021-06-27 19:22:19 +08:00
|
|
|
|
2022-09-09 19:31:16 +08:00
|
|
|
remove_users_from_my_modules(project, users)
|
|
|
|
remove_users_from_experiments(project, users)
|
|
|
|
user_assignments.destroy_all
|
2021-06-27 19:22:19 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-09-09 19:31:16 +08:00
|
|
|
def remove_users_from_experiments(project, users)
|
|
|
|
experiments = project.experiments.joins(:user_assignments).where(user_assignments: { user: users })
|
|
|
|
# rubocop:disable Rails/SkipsModelValidations
|
|
|
|
UserAssignment.where(assignable_type: 'Experiment', assignable_id: experiments, user: users).delete_all
|
|
|
|
experiments.update_all(updated_at: Time.current)
|
|
|
|
# rubocop:enable Rails/SkipsModelValidations
|
|
|
|
end
|
|
|
|
|
|
|
|
def remove_users_from_my_modules(project, users)
|
|
|
|
my_modules = MyModule.joins(:user_assignments, experiment: :project)
|
|
|
|
.where(user_assignments: { user: users })
|
|
|
|
.where(projects: project)
|
|
|
|
# rubocop:disable Rails/SkipsModelValidations
|
|
|
|
UserAssignment.where(assignable_type: 'MyModule', assignable_id: my_modules, user: users).delete_all
|
|
|
|
my_modules.update_all(updated_at: Time.current)
|
|
|
|
# rubocop:enable Rails/SkipsModelValidations
|
2021-06-27 19:22:19 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|