fix: delete user record if belongs to no account (#6664)

This commit is contained in:
Tejaswini Chile
2023-03-15 21:49:49 +05:30
committed by GitHub
parent 6848433a4c
commit cf487c76a0
2 changed files with 29 additions and 4 deletions

View File

@@ -19,6 +19,7 @@ class Api::V1::Accounts::AgentsController < Api::V1::Accounts::BaseController
def destroy def destroy
@agent.current_account_user.destroy! @agent.current_account_user.destroy!
delete_user_record(@agent)
head :ok head :ok
end end
@@ -74,4 +75,8 @@ class Api::V1::Accounts::AgentsController < Api::V1::Accounts::BaseController
def validate_limit def validate_limit
render_payment_required('Account limit exceeded. Please purchase more licenses') if agents.count >= Current.account.usage_limits[:agents] render_payment_required('Account limit exceeded. Please purchase more licenses') if agents.count >= Current.account.usage_limits[:agents]
end end
def delete_user_record(agent)
DeleteObjectJob.perform_later(agent) if agent.reload.account_users.blank?
end
end end

View File

@@ -1,6 +1,8 @@
require 'rails_helper' require 'rails_helper'
RSpec.describe 'Agents API', type: :request do RSpec.describe 'Agents API', type: :request do
include ActiveJob::TestHelper
let(:account) { create(:account) } let(:account) { create(:account) }
let(:admin) { create(:user, custom_attributes: { test: 'test' }, account: account, role: :administrator) } let(:admin) { create(:user, custom_attributes: { test: 'test' }, account: account, role: :administrator) }
let(:agent) { create(:user, account: account, role: :agent) } let(:agent) { create(:user, account: account, role: :agent) }
@@ -60,13 +62,31 @@ RSpec.describe 'Agents API', type: :request do
expect(response).to have_http_status(:unauthorized) expect(response).to have_http_status(:unauthorized)
end end
it 'deletes an agent' do it 'deletes the agent and user object if associated with only one account' do
delete "/api/v1/accounts/#{account.id}/agents/#{other_agent.id}", perform_enqueued_jobs(only: DeleteObjectJob) do
headers: admin.create_new_auth_token, delete "/api/v1/accounts/#{account.id}/agents/#{other_agent.id}",
as: :json headers: admin.create_new_auth_token,
as: :json
end
expect(response).to have_http_status(:success)
expect(account.reload.users.size).to eq(1)
expect(User.count).to eq(account.reload.users.size)
end
it 'deletes only the agent object when user is associated with multiple accounts' do
other_account = create(:account)
create(:account_user, account_id: other_account.id, user_id: other_agent.id)
perform_enqueued_jobs(only: DeleteObjectJob) do
delete "/api/v1/accounts/#{account.id}/agents/#{other_agent.id}",
headers: admin.create_new_auth_token,
as: :json
end
expect(response).to have_http_status(:success) expect(response).to have_http_status(:success)
expect(account.users.size).to eq(1) expect(account.users.size).to eq(1)
expect(User.count).to eq(account.reload.users.size + 1)
end end
end end
end end