fix: destroy bulk service (#5921)
This commit is contained in:
parent
b05d06a28a
commit
fc9fc5a661
6 changed files with 65 additions and 51 deletions
37
app/jobs/agents/destroy_job.rb
Normal file
37
app/jobs/agents/destroy_job.rb
Normal file
|
@ -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
|
|
@ -46,7 +46,7 @@ class AccountUser < ApplicationRecord
|
||||||
end
|
end
|
||||||
|
|
||||||
def remove_user_from_account
|
def remove_user_from_account
|
||||||
::Agents::DestroyService.new(account: account, user: user).perform
|
::Agents::DestroyJob.perform_later(account, user)
|
||||||
end
|
end
|
||||||
|
|
||||||
private
|
private
|
||||||
|
|
|
@ -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
|
|
|
@ -26,7 +26,9 @@ describe ::NotificationBuilder do
|
||||||
end
|
end
|
||||||
|
|
||||||
it 'will not throw error if notification setting is not present' do
|
it 'will not throw error if notification setting is not present' do
|
||||||
|
perform_enqueued_jobs do
|
||||||
user.account_users.destroy_all
|
user.account_users.destroy_all
|
||||||
|
end
|
||||||
expect(
|
expect(
|
||||||
described_class.new(
|
described_class.new(
|
||||||
notification_type: 'conversation_creation',
|
notification_type: 'conversation_creation',
|
||||||
|
|
|
@ -1,7 +1,9 @@
|
||||||
require 'rails_helper'
|
require 'rails_helper'
|
||||||
|
|
||||||
describe Agents::DestroyService do
|
RSpec.describe Agents::DestroyJob, type: :job do
|
||||||
let(:account) { create(:account) }
|
subject(:job) { described_class.perform_later(account, user) }
|
||||||
|
|
||||||
|
let!(:account) { create(:account) }
|
||||||
let(:user) { create(:user, account: account) }
|
let(:user) { create(:user, account: account) }
|
||||||
let(:team1) { create(:team, account: account) }
|
let(:team1) { create(:team, account: account) }
|
||||||
let!(:inbox) { create(:inbox, account: account) }
|
let!(:inbox) { create(:inbox, account: account) }
|
||||||
|
@ -12,9 +14,16 @@ describe Agents::DestroyService do
|
||||||
create(:conversation, account: account, assignee: user, inbox: inbox)
|
create(:conversation, account: account, assignee: user, inbox: inbox)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
it 'enqueues the job' do
|
||||||
|
expect { job }.to have_enqueued_job(described_class)
|
||||||
|
.with(account, user)
|
||||||
|
.on_queue('default')
|
||||||
|
end
|
||||||
|
|
||||||
describe '#perform' do
|
describe '#perform' do
|
||||||
it 'remove inboxes, teams, and conversations when removed from account' 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
|
user.reload
|
||||||
expect(user.teams.length).to eq 0
|
expect(user.teams.length).to eq 0
|
||||||
expect(user.inboxes.length).to eq 0
|
expect(user.inboxes.length).to eq 0
|
|
@ -3,13 +3,10 @@
|
||||||
require 'rails_helper'
|
require 'rails_helper'
|
||||||
|
|
||||||
RSpec.describe User do
|
RSpec.describe User do
|
||||||
let!(:account_user) { create(:account_user) }
|
include ActiveJob::TestHelper
|
||||||
let(:agent_destroy_service) { double }
|
|
||||||
|
|
||||||
before do
|
let!(:account_user) { create(:account_user) }
|
||||||
allow(Agents::DestroyService).to receive(:new).and_return(agent_destroy_service)
|
let!(:inbox) { create(:inbox, account: account_user.account) }
|
||||||
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
|
||||||
|
@ -22,12 +19,16 @@ RSpec.describe User do
|
||||||
|
|
||||||
describe 'destroy call agent::destroy service' do
|
describe 'destroy call agent::destroy service' do
|
||||||
it 'gets created with the right default settings' 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
|
user = account_user.user
|
||||||
account = account_user.account
|
|
||||||
|
expect(user.assigned_conversations.count).to eq(1)
|
||||||
|
|
||||||
|
perform_enqueued_jobs do
|
||||||
account_user.destroy!
|
account_user.destroy!
|
||||||
expect(Agents::DestroyService).to have_received(:new).with({
|
end
|
||||||
user: user, account: account
|
|
||||||
})
|
expect(user.assigned_conversations.count).to eq(0)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue