diff --git a/app/jobs/agents/destroy_job.rb b/app/jobs/agents/destroy_job.rb new file mode 100644 index 000000000..1c40827b6 --- /dev/null +++ b/app/jobs/agents/destroy_job.rb @@ -0,0 +1,37 @@ +class Agents::DestroyJob < ApplicationJob + queue_as :default + + def perform(account, user) + ActiveRecord::Base.transaction do + destroy_notification_setting(account, user) + remove_user_from_teams(account, user) + remove_user_from_inboxes(account, user) + unassign_conversations(account, user) + end + end + + private + + def remove_user_from_inboxes(account, user) + 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(account, user) + teams = account.teams.all + team_members = user.team_members.where(team_id: teams.pluck(:id)) + team_members.destroy_all + end + + def destroy_notification_setting(account, user) + setting = user.notification_settings.find_by(account_id: account.id) + setting&.destroy! + end + + def unassign_conversations(account, user) + # rubocop:disable Rails/SkipsModelValidations + user.assigned_conversations.where(account: account).in_batches.update_all(assignee_id: nil) + # rubocop:enable Rails/SkipsModelValidations + end +end diff --git a/app/models/account_user.rb b/app/models/account_user.rb index a6bf3988a..2e913c60f 100644 --- a/app/models/account_user.rb +++ b/app/models/account_user.rb @@ -46,7 +46,7 @@ class AccountUser < ApplicationRecord end def remove_user_from_account - ::Agents::DestroyService.new(account: account, user: user).perform + ::Agents::DestroyJob.perform_later(account, user) end private diff --git a/app/services/agents/destroy_service.rb b/app/services/agents/destroy_service.rb deleted file mode 100644 index 3bac64282..000000000 --- a/app/services/agents/destroy_service.rb +++ /dev/null @@ -1,35 +0,0 @@ -class Agents::DestroyService - pattr_initialize [:account!, :user!] - - def perform - ActiveRecord::Base.transaction do - destroy_notification_setting - remove_user_from_teams - remove_user_from_inboxes - unassign_conversations - 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 - - def unassign_conversations - user.assigned_conversations.where(account: account).find_each(&:update_assignee) - end -end diff --git a/spec/builders/notification_builder_spec.rb b/spec/builders/notification_builder_spec.rb index 74dda0780..4db4ea00d 100644 --- a/spec/builders/notification_builder_spec.rb +++ b/spec/builders/notification_builder_spec.rb @@ -26,7 +26,9 @@ describe ::NotificationBuilder do end it 'will not throw error if notification setting is not present' do - user.account_users.destroy_all + perform_enqueued_jobs do + user.account_users.destroy_all + end expect( described_class.new( notification_type: 'conversation_creation', diff --git a/spec/services/agents/destroy_service_spec.rb b/spec/jobs/agents/destroy_job_spec.rb similarity index 67% rename from spec/services/agents/destroy_service_spec.rb rename to spec/jobs/agents/destroy_job_spec.rb index f3aa24607..38786dbc0 100644 --- a/spec/services/agents/destroy_service_spec.rb +++ b/spec/jobs/agents/destroy_job_spec.rb @@ -1,7 +1,9 @@ require 'rails_helper' -describe Agents::DestroyService do - let(:account) { create(:account) } +RSpec.describe Agents::DestroyJob, type: :job do + subject(:job) { described_class.perform_later(account, user) } + + let!(:account) { create(:account) } let(:user) { create(:user, account: account) } let(:team1) { create(:team, account: account) } let!(:inbox) { create(:inbox, account: account) } @@ -12,9 +14,16 @@ describe Agents::DestroyService do create(:conversation, account: account, assignee: user, inbox: inbox) end + it 'enqueues the job' do + expect { job }.to have_enqueued_job(described_class) + .with(account, user) + .on_queue('default') + end + describe '#perform' do it 'remove inboxes, teams, and conversations when removed from account' do - described_class.new(account: account, user: user).perform + described_class.perform_now(account, user) + user.reload expect(user.teams.length).to eq 0 expect(user.inboxes.length).to eq 0 diff --git a/spec/models/account_user_spec.rb b/spec/models/account_user_spec.rb index 00b66dc77..be6471050 100644 --- a/spec/models/account_user_spec.rb +++ b/spec/models/account_user_spec.rb @@ -3,13 +3,10 @@ require 'rails_helper' RSpec.describe User do - let!(:account_user) { create(:account_user) } - let(:agent_destroy_service) { double } + include ActiveJob::TestHelper - 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 + let!(:account_user) { create(:account_user) } + let!(:inbox) { create(:inbox, account: account_user.account) } describe 'notification_settings' do it 'gets created with the right default settings' do @@ -22,12 +19,16 @@ RSpec.describe User do describe 'destroy call agent::destroy service' do it 'gets created with the right default settings' do + create(:conversation, account: account_user.account, assignee: account_user.user, inbox: inbox) user = account_user.user - account = account_user.account - account_user.destroy! - expect(Agents::DestroyService).to have_received(:new).with({ - user: user, account: account - }) + + expect(user.assigned_conversations.count).to eq(1) + + perform_enqueued_jobs do + account_user.destroy! + end + + expect(user.assigned_conversations.count).to eq(0) end end end