mirror of
				https://github.com/lingble/chatwoot.git
				synced 2025-10-31 19:17:48 +00:00 
			
		
		
		
	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 |   accepts_nested_attributes_for :account | ||||||
|  |  | ||||||
|   after_create_commit :notify_creation, :create_notification_setting |   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 } |   validates :user_id, uniqueness: { scope: :account_id } | ||||||
|  |  | ||||||
| @@ -43,9 +43,8 @@ class AccountUser < ApplicationRecord | |||||||
|     setting.save! |     setting.save! | ||||||
|   end |   end | ||||||
|  |  | ||||||
|   def destroy_notification_setting |   def remove_user_from_account | ||||||
|     setting = user.notification_settings.find_by(account_id: account.id) |     ::Agents::DestroyService.new(account: account, user: user).perform | ||||||
|     setting.destroy! |  | ||||||
|   end |   end | ||||||
|  |  | ||||||
|   private |   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 | ||||||
| @@ -4,6 +4,12 @@ require 'rails_helper' | |||||||
|  |  | ||||||
| RSpec.describe User do | RSpec.describe User do | ||||||
|   let!(:account_user) { create(:account_user) } |   let!(:account_user) { create(:account_user) } | ||||||
|  |   let(:agent_destroy_service) { double } | ||||||
|  |  | ||||||
|  |   before do | ||||||
|  |     allow(Agents::DestroyService).to receive(:new).and_return(agent_destroy_service) | ||||||
|  |     allow(agent_destroy_service).to receive(:perform).and_return(agent_destroy_service) | ||||||
|  |   end | ||||||
|  |  | ||||||
|   describe 'notification_settings' do |   describe 'notification_settings' do | ||||||
|     it 'gets created with the right default settings' do |     it 'gets created with the right default settings' do | ||||||
| @@ -13,4 +19,15 @@ RSpec.describe User do | |||||||
|       expect(account_user.user.notification_settings.first.email_conversation_assignment?).to eq(true) |       expect(account_user.user.notification_settings.first.email_conversation_assignment?).to eq(true) | ||||||
|     end |     end | ||||||
|   end |   end | ||||||
|  |  | ||||||
|  |   describe 'destroy call agent::destroy service' do | ||||||
|  |     it 'gets created with the right default settings' do | ||||||
|  |       user = account_user.user | ||||||
|  |       account = account_user.account | ||||||
|  |       account_user.destroy! | ||||||
|  |       expect(Agents::DestroyService).to have_received(:new).with({ | ||||||
|  |                                                                    user: user, account: account | ||||||
|  |                                                                  }) | ||||||
|  |     end | ||||||
|  |   end | ||||||
| end | end | ||||||
|   | |||||||
							
								
								
									
										23
									
								
								spec/services/agents/destroy_service_spec.rb
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										23
									
								
								spec/services/agents/destroy_service_spec.rb
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,23 @@ | |||||||
|  | require 'rails_helper' | ||||||
|  |  | ||||||
|  | describe Agents::DestroyService do | ||||||
|  |   let(:account) { create(:account) } | ||||||
|  |   let(:user) { create(:user, account: account) } | ||||||
|  |   let(:team1) { create(:team, account: account) } | ||||||
|  |   let!(:inbox) { create(:inbox, account: account) } | ||||||
|  |  | ||||||
|  |   before do | ||||||
|  |     create(:team_member, team: team1, user: user) | ||||||
|  |     create(:inbox_member, inbox: inbox, user: user) | ||||||
|  |   end | ||||||
|  |  | ||||||
|  |   describe '#perform' do | ||||||
|  |     it 'remove inboxes and teams when removed from account' do | ||||||
|  |       described_class.new(account: account, user: user).perform | ||||||
|  |       user.reload | ||||||
|  |       expect(user.teams.length).to eq 0 | ||||||
|  |       expect(user.inboxes.length).to eq 0 | ||||||
|  |       expect(user.notification_settings.length).to eq 0 | ||||||
|  |     end | ||||||
|  |   end | ||||||
|  | end | ||||||
		Reference in New Issue
	
	Block a user
	 Pranav Raj S
					Pranav Raj S