mirror of
https://github.com/scinote-eln/scinote-web.git
synced 2025-09-06 21:24:23 +08:00
Merge pull request #8760 from artoscinote/ma_SCI_12220
Fix assignment propagation and favorite, designation clenaup [SCI-12220]
This commit is contained in:
commit
5e4d8cd94a
3 changed files with 17 additions and 20 deletions
|
@ -2,6 +2,8 @@
|
|||
|
||||
module UserAssignments
|
||||
class PropagateAssignmentJob < ApplicationJob
|
||||
include Canaid::Helpers::PermissionsHelper
|
||||
|
||||
queue_as :high_priority
|
||||
|
||||
def perform(assignment, destroy: false, remove_from_team: false)
|
||||
|
@ -13,6 +15,7 @@ module UserAssignments
|
|||
|
||||
ActiveRecord::Base.transaction do
|
||||
@assignment.destroy! if destroy && !@assignment.destroyed?
|
||||
cleanup!(@assignment)
|
||||
sync_resource_user_associations(@resource)
|
||||
end
|
||||
end
|
||||
|
@ -39,7 +42,7 @@ module UserAssignments
|
|||
|
||||
child_associations.find_each do |child_association|
|
||||
if @destroy
|
||||
destroy_or_update_assignment(child_association)
|
||||
destroy_assignment(child_association)
|
||||
else
|
||||
create_or_update_assignment(child_association)
|
||||
end
|
||||
|
@ -47,7 +50,7 @@ module UserAssignments
|
|||
sync_resource_user_associations(child_association)
|
||||
end
|
||||
|
||||
destroy_or_update_assignment(resource) if resource.is_a?(Project) && @destroy
|
||||
destroy_assignment(resource) if resource.is_a?(Project) && @destroy
|
||||
end
|
||||
|
||||
def create_or_update_assignment(resource)
|
||||
|
@ -66,31 +69,24 @@ module UserAssignments
|
|||
)
|
||||
end
|
||||
|
||||
def destroy_or_update_assignment(resource)
|
||||
# also destroy user designations if it's a MyModule
|
||||
resource.user_my_modules.where(user: @user).destroy_all if resource.is_a?(MyModule)
|
||||
|
||||
def destroy_assignment(resource)
|
||||
assignment = resource.public_send(:"#{@type}_assignments").find_by(
|
||||
"#{@type}_id" => @assignment.public_send(@type).id
|
||||
)
|
||||
|
||||
return unless assignment
|
||||
|
||||
project = resource.is_a?(Project) ? resource : resource.project
|
||||
assignment.destroy!
|
||||
|
||||
if project.default_public_user_role_id && !@remove_from_team
|
||||
# if project is public, the assignment
|
||||
# will reset to the default public role
|
||||
cleanup!(assignment)
|
||||
end
|
||||
|
||||
assignment.update!(
|
||||
user_role_id: project.default_public_user_role_id,
|
||||
assigned: :automatically,
|
||||
assigned_by: @assignment.assigned_by
|
||||
)
|
||||
else
|
||||
resource.favorites.where(user: @user).destroy_all if resource.respond_to?(:favorites)
|
||||
assignment.destroy!
|
||||
end
|
||||
def cleanup!(assignment)
|
||||
# clean up designations and favorites if user is no longer assigned
|
||||
assigned_users = assignment.assignable.users
|
||||
|
||||
assignment.assignable.favorites.where.not(user: assigned_users).destroy_all if assignment.assignable.respond_to?(:favorites)
|
||||
assignment.assignable.user_my_modules.where.not(user: assigned_users).destroy_all if assignment.assignable.is_a?(MyModule)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -5,7 +5,7 @@ class TeamAssignment < ApplicationRecord
|
|||
belongs_to :team
|
||||
belongs_to :user_role
|
||||
belongs_to :assigned_by, class_name: 'User', optional: true
|
||||
has_many :users, through: :team
|
||||
delegate :users, to: :team
|
||||
|
||||
enum :assigned, { automatically: 0, manually: 1 }, suffix: true
|
||||
|
||||
|
|
|
@ -8,6 +8,7 @@ class UserGroupAssignment < ApplicationRecord
|
|||
belongs_to :user_group
|
||||
belongs_to :user_role
|
||||
belongs_to :assigned_by, class_name: 'User', optional: true
|
||||
has_many :users, through: :user_group
|
||||
|
||||
enum :assigned, { automatically: 0, manually: 1 }, suffix: true
|
||||
|
||||
|
|
Loading…
Add table
Reference in a new issue