feat: Ability to delete a contact (#2984)

This change allows the administrator user to delete a contact and its related data like conversations, contact inboxes, and reports.

Fixes #1929
This commit is contained in:
Aswin Dev P.S
2021-09-23 12:52:49 +05:30
committed by GitHub
parent 0c24df96a8
commit 4f51a46c2b
22 changed files with 387 additions and 32 deletions

View File

@@ -376,4 +376,53 @@ RSpec.describe 'Contacts API', type: :request do
end
end
end
describe 'DELETE /api/v1/accounts/{account.id}/contacts/:id', :contact_delete do
let(:inbox) { create(:inbox, account: account) }
let(:contact) { create(:contact, account: account) }
let(:contact_inbox) { create(:contact_inbox, contact: contact, inbox: inbox) }
let(:conversation) { create(:conversation, account: account, inbox: inbox, contact: contact, contact_inbox: contact_inbox) }
context 'when it is an unauthenticated user' do
it 'returns unauthorized' do
delete "/api/v1/accounts/#{account.id}/contacts/#{contact.id}"
expect(response).to have_http_status(:unauthorized)
end
end
context 'when it is an authenticated user' do
let(:admin) { create(:user, account: account, role: :administrator) }
let(:agent) { create(:user, account: account, role: :agent) }
it 'deletes the contact for administrator user' do
allow(::OnlineStatusTracker).to receive(:get_presence).and_return(false)
delete "/api/v1/accounts/#{account.id}/contacts/#{contact.id}",
headers: admin.create_new_auth_token
expect(contact.conversations).to be_empty
expect(contact.inboxes).to be_empty
expect(contact.contact_inboxes).to be_empty
expect(contact.csat_survey_responses).to be_empty
expect { contact.reload }.to raise_error(ActiveRecord::RecordNotFound)
expect(response).to have_http_status(:success)
end
it 'does not delete the contact if online' do
allow(::OnlineStatusTracker).to receive(:get_presence).and_return(true)
delete "/api/v1/accounts/#{account.id}/contacts/#{contact.id}",
headers: admin.create_new_auth_token
expect(response).to have_http_status(:unprocessable_entity)
end
it 'returns unauthorized for agent user' do
delete "/api/v1/accounts/#{account.id}/contacts/#{contact.id}",
headers: agent.create_new_auth_token
expect(response).to have_http_status(:unauthorized)
end
end
end
end

View File

@@ -65,4 +65,19 @@ describe ActionCableListener do
listener.conversation_typing_off(event)
end
end
describe '#contact_deleted' do
let(:event_name) { :'contact.deleted' }
let!(:contact) { create(:contact, account: account) }
let!(:event) { Events::Base.new(event_name, Time.zone.now, contact: contact) }
it 'sends message to account admins, inbox agents' do
expect(ActionCableBroadcastJob).to receive(:perform_later).with(
[agent.pubsub_token, admin.pubsub_token],
'contact.deleted',
contact.push_event_data.merge(account_id: account.id)
)
listener.contact_deleted(event)
end
end
end