From b4bcfca6787984015d69fae2fed521d3475d7f98 Mon Sep 17 00:00:00 2001 From: iamsivin Date: Thu, 5 Jun 2025 13:58:16 +0530 Subject: [PATCH] chore: Fix redirect issue and add specs --- .../dashboard/components/ChatList.vue | 48 +++++++++++-------- .../conversation/contextMenu/Index.vue | 10 ++-- .../specs/conversations/actions.spec.js | 22 +++++++++ .../specs/conversations/mutations.spec.js | 11 +++++ 4 files changed, 68 insertions(+), 23 deletions(-) diff --git a/app/javascript/dashboard/components/ChatList.vue b/app/javascript/dashboard/components/ChatList.vue index 9d6340e4c..f78470fed 100644 --- a/app/javascript/dashboard/components/ChatList.vue +++ b/app/javascript/dashboard/components/ChatList.vue @@ -82,6 +82,7 @@ const emit = defineEmits(['conversationLoad']); const { uiSettings } = useUISettings(); const { t } = useI18n(); const router = useRouter(); +const route = useRoute(); const store = useStore(); const conversationListRef = ref(null); @@ -646,6 +647,30 @@ function openLastItemAfterDeleteInFolder() { } } +function redirectToConversationList() { + const { + params: { accountId, inbox_id: inboxId, label, teamId }, + name, + } = route; + + let conversationType = ''; + if (isOnMentionsView({ route: { name } })) { + conversationType = 'mention'; + } else if (isOnUnattendedView({ route: { name } })) { + conversationType = 'unattended'; + } + router.push( + conversationListPageURL({ + accountId, + conversationType: conversationType, + customViewId: props.foldersId, + inboxId, + label, + teamId, + }) + ); +} + async function assignPriority(priority, conversationId = null) { store.dispatch('setCurrentChatPriority', { priority, @@ -670,26 +695,7 @@ async function markAsUnread(conversationId) { await store.dispatch('markMessagesUnread', { id: conversationId, }); - const { - params: { accountId, inbox_id: inboxId, label, teamId }, - name, - } = useRoute(); - let conversationType = ''; - if (isOnMentionsView({ route: { name } })) { - conversationType = 'mention'; - } else if (isOnUnattendedView({ route: { name } })) { - conversationType = 'unattended'; - } - router.push( - conversationListPageURL({ - accountId, - conversationType: conversationType, - customViewId: props.foldersId, - inboxId, - label, - teamId, - }) - ); + redirectToConversationList(); } catch (error) { // Ignore error } @@ -707,11 +713,13 @@ async function markAsRead(conversationId) { async function deleteConversation(conversationId) { try { await store.dispatch('deleteConversation', conversationId); + redirectToConversationList(); useAlert(t('CONVERSATION.SUCCESS_DELETE_CONVERSATION')); } catch (error) { useAlert(t('CONVERSATION.FAIL_DELETE_CONVERSATION')); } } + async function onAssignTeam(team, conversationId = null) { try { await store.dispatch('assignTeam', { diff --git a/app/javascript/dashboard/components/widgets/conversation/contextMenu/Index.vue b/app/javascript/dashboard/components/widgets/conversation/contextMenu/Index.vue index a136f30bf..f502bfddf 100644 --- a/app/javascript/dashboard/components/widgets/conversation/contextMenu/Index.vue +++ b/app/javascript/dashboard/components/widgets/conversation/contextMenu/Index.vue @@ -8,6 +8,7 @@ import MenuItem from './menuItem.vue'; import MenuItemWithSubmenu from './menuItemWithSubmenu.vue'; import wootConstants from 'dashboard/constants/globals'; import AgentLoadingPlaceholder from './agentLoadingPlaceholder.vue'; +import { useAdmin } from 'dashboard/composables/useAdmin'; export default { components: { @@ -47,6 +48,12 @@ export default { 'assignLabel', 'deleteConversation', ], + setup() { + const { isAdmin } = useAdmin(); + return { + isAdmin, + }; + }, data() { return { STATUS_TYPE: wootConstants.STATUS_TYPE, @@ -168,9 +175,6 @@ export default { // Don't show snooze if the conversation is already snoozed/resolved/pending return this.status === wootConstants.STATUS_TYPE.OPEN; }, - isAdmin() { - return this.currentUser?.role === 'administrator'; - }, }, mounted() { this.$store.dispatch('inboxAssignableAgents/fetch', [this.inboxId]); diff --git a/app/javascript/dashboard/store/modules/specs/conversations/actions.spec.js b/app/javascript/dashboard/store/modules/specs/conversations/actions.spec.js index dc2bf8d3a..161ae914b 100644 --- a/app/javascript/dashboard/store/modules/specs/conversations/actions.spec.js +++ b/app/javascript/dashboard/store/modules/specs/conversations/actions.spec.js @@ -513,6 +513,28 @@ describe('#deleteMessage', () => { expect(commit.mock.calls).toEqual([]); }); + describe('#deleteConversation', () => { + it('send correct actions if API is success', async () => { + axios.delete.mockResolvedValue({ + data: { id: 1 }, + }); + await actions.deleteConversation({ commit, dispatch }, 1); + expect(commit.mock.calls).toEqual([[types.DELETE_CONVERSATION, 1]]); + expect(dispatch.mock.calls).toEqual([ + ['conversationStats/get', {}, { root: true }], + ]); + }); + + it('send no actions if API is error', async () => { + axios.delete.mockRejectedValue({ message: 'Incorrect header' }); + await expect( + actions.deleteConversation({ commit, dispatch }, 1) + ).rejects.toThrow(Error); + expect(commit.mock.calls).toEqual([]); + expect(dispatch.mock.calls).toEqual([]); + }); + }); + describe('#updateCustomAttributes', () => { it('update conversation custom attributes', async () => { axios.post.mockResolvedValue({ diff --git a/app/javascript/dashboard/store/modules/specs/conversations/mutations.spec.js b/app/javascript/dashboard/store/modules/specs/conversations/mutations.spec.js index 091dc3daa..b8660b20d 100644 --- a/app/javascript/dashboard/store/modules/specs/conversations/mutations.spec.js +++ b/app/javascript/dashboard/store/modules/specs/conversations/mutations.spec.js @@ -884,6 +884,17 @@ describe('#mutations', () => { }); }); + describe('#DELETE_CONVERSATION', () => { + it('should delete a conversation', () => { + const state = { + allConversations: [{ id: 1, messages: [] }], + }; + + mutations[types.DELETE_CONVERSATION](state, 1); + expect(state.allConversations).toEqual([]); + }); + }); + describe('#SET_LIST_LOADING_STATUS', () => { it('should set listLoadingStatus to true', () => { const state = {