fix: destroy bulk service (#5921)

This commit is contained in:
Tejaswini Chile 2022-11-25 22:46:50 +05:30 committed by GitHub
parent b05d06a28a
commit fc9fc5a661
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 65 additions and 51 deletions

View 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

View file

@ -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

View file

@ -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

View file

@ -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',

View file

@ -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

View file

@ -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