fix: Remove teams on account user destroy (#1766)
This commit is contained in:
@@ -32,7 +32,7 @@ class AccountUser < ApplicationRecord
|
||||
accepts_nested_attributes_for :account
|
||||
|
||||
after_create_commit :notify_creation, :create_notification_setting
|
||||
after_destroy :notify_deletion, :destroy_notification_setting
|
||||
after_destroy :notify_deletion, :remove_user_from_account
|
||||
|
||||
validates :user_id, uniqueness: { scope: :account_id }
|
||||
|
||||
@@ -43,9 +43,8 @@ class AccountUser < ApplicationRecord
|
||||
setting.save!
|
||||
end
|
||||
|
||||
def destroy_notification_setting
|
||||
setting = user.notification_settings.find_by(account_id: account.id)
|
||||
setting.destroy!
|
||||
def remove_user_from_account
|
||||
::Agents::DestroyService.new(account: account, user: user).perform
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
30
app/services/agents/destroy_service.rb
Normal file
30
app/services/agents/destroy_service.rb
Normal file
@@ -0,0 +1,30 @@
|
||||
class Agents::DestroyService
|
||||
pattr_initialize [:account!, :user!]
|
||||
|
||||
def perform
|
||||
ActiveRecord::Base.transaction do
|
||||
destroy_notification_setting
|
||||
remove_user_from_teams
|
||||
remove_user_from_inboxes
|
||||
end
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def remove_user_from_inboxes
|
||||
inboxes = account.inboxes.all
|
||||
inbox_members = user.inbox_members.where(inbox_id: inboxes.pluck(:id))
|
||||
inbox_members.destroy_all
|
||||
end
|
||||
|
||||
def remove_user_from_teams
|
||||
teams = account.teams.all
|
||||
team_members = user.team_members.where(team_id: teams.pluck(:id))
|
||||
team_members.destroy_all
|
||||
end
|
||||
|
||||
def destroy_notification_setting
|
||||
setting = user.notification_settings.find_by(account_id: account.id)
|
||||
setting&.destroy!
|
||||
end
|
||||
end
|
||||
Reference in New Issue
Block a user