From d45512df72fa895b9ee4cf1a9fa1c177d3fb4e67 Mon Sep 17 00:00:00 2001 From: Tejaswini Chile Date: Wed, 12 Apr 2023 13:54:01 +0530 Subject: [PATCH] feat: Account deletion with deleteObjectJob (#6885) Fixes: https://linear.app/chatwoot/issue/CW-1365/allow-super-admin-to-delete-an-account Co-authored-by: Sojan Jose --- .../super_admin/accounts_controller.rb | 7 +++++ config/routes.rb | 2 +- .../super_admin/accounts_controller_spec.rb | 27 ++++++++++++++++++- 3 files changed, 34 insertions(+), 2 deletions(-) diff --git a/app/controllers/super_admin/accounts_controller.rb b/app/controllers/super_admin/accounts_controller.rb index 3f1e15cdc..112357363 100644 --- a/app/controllers/super_admin/accounts_controller.rb +++ b/app/controllers/super_admin/accounts_controller.rb @@ -47,4 +47,11 @@ class SuperAdmin::AccountsController < SuperAdmin::ApplicationController Internal::SeedAccountJob.perform_later(requested_resource) redirect_back(fallback_location: [namespace, requested_resource], notice: 'Account seeding triggered') end + + def destroy + account = Account.find(params[:id]) + + DeleteObjectJob.perform_later(account) if account.present? + redirect_back(fallback_location: [namespace, requested_resource], notice: 'Account deletion is in progress.') + end end diff --git a/config/routes.rb b/config/routes.rb index 9fcc09081..158cd386e 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -387,7 +387,7 @@ Rails.application.routes.draw do resource :app_config, only: [:show, :create] # order of resources affect the order of sidebar navigation in super admin - resources :accounts, only: [:index, :new, :create, :show, :edit, :update] do + resources :accounts, only: [:index, :new, :create, :show, :edit, :update, :destroy] do post :seed, on: :member end resources :users, only: [:index, :new, :create, :show, :edit, :update, :destroy] diff --git a/spec/controllers/super_admin/accounts_controller_spec.rb b/spec/controllers/super_admin/accounts_controller_spec.rb index 1f155609a..23235888a 100644 --- a/spec/controllers/super_admin/accounts_controller_spec.rb +++ b/spec/controllers/super_admin/accounts_controller_spec.rb @@ -1,7 +1,10 @@ require 'rails_helper' RSpec.describe 'Super Admin accounts API', type: :request do - let(:super_admin) { create(:super_admin) } + include ActiveJob::TestHelper + + let!(:super_admin) { create(:super_admin) } + let!(:account) { create(:account) } describe 'GET /super_admin/accounts' do context 'when it is an unauthenticated user' do @@ -23,4 +26,26 @@ RSpec.describe 'Super Admin accounts API', type: :request do end end end + + describe 'DELETE /super_admin/accounts/{account_id}' do + context 'when it is an unauthenticated user' do + it 'returns unauthorized' do + delete "/super_admin/accounts/#{account.id}" + expect(response).to have_http_status(:redirect) + end + end + + context 'when it is an authenticated user' do + it 'Deletes the account' do + total_accounts = Account.count + sign_in(super_admin, scope: :super_admin) + + perform_enqueued_jobs(only: DeleteObjectJob) do + delete "/super_admin/accounts/#{account.id}" + end + + expect(Account.count).to eq(total_accounts - 1) + end + end + end end