From d88e3e3596d20e0397198933787a8785ae07f5ba Mon Sep 17 00:00:00 2001 From: Pranav Raj S Date: Mon, 2 Aug 2021 13:11:07 +0530 Subject: [PATCH] fix: Resolve conversation with id instead of current conversation (#2731) --- .../store/modules/conversations/actions.js | 8 +++--- .../store/modules/conversations/getters.js | 4 ++- .../store/modules/conversations/index.js | 7 ++--- .../specs/conversations/actions.spec.js | 16 +++++++++++ .../specs/conversations/mutations.spec.js | 27 +++++++++++++++++++ 5 files changed, 54 insertions(+), 8 deletions(-) diff --git a/app/javascript/dashboard/store/modules/conversations/actions.js b/app/javascript/dashboard/store/modules/conversations/actions.js index 6030b7b8d..0415d8724 100644 --- a/app/javascript/dashboard/store/modules/conversations/actions.js +++ b/app/javascript/dashboard/store/modules/conversations/actions.js @@ -145,10 +145,10 @@ const actions = { status, snoozedUntil, }); - commit( - types.default.RESOLVE_CONVERSATION, - response.data.payload.current_status - ); + commit(types.default.RESOLVE_CONVERSATION, { + conversationId, + status: response.data.payload.current_status, + }); } catch (error) { // Handle error } diff --git a/app/javascript/dashboard/store/modules/conversations/getters.js b/app/javascript/dashboard/store/modules/conversations/getters.js index b7f277a1e..79bd0e50a 100644 --- a/app/javascript/dashboard/store/modules/conversations/getters.js +++ b/app/javascript/dashboard/store/modules/conversations/getters.js @@ -64,7 +64,9 @@ const getters = { getChatStatusFilter: ({ chatStatusFilter }) => chatStatusFilter, getSelectedInbox: ({ currentInbox }) => currentInbox, getConversationById: _state => conversationId => { - return _state.allConversations.find(value => value.id === conversationId); + return _state.allConversations.find( + value => value.id === Number(conversationId) + ); }, }; diff --git a/app/javascript/dashboard/store/modules/conversations/index.js b/app/javascript/dashboard/store/modules/conversations/index.js index d9721b4fc..f0005c627 100644 --- a/app/javascript/dashboard/store/modules/conversations/index.js +++ b/app/javascript/dashboard/store/modules/conversations/index.js @@ -69,9 +69,10 @@ export const mutations = { Vue.set(chat.meta, 'team', team); }, - [types.default.RESOLVE_CONVERSATION](_state, status) { - const [chat] = getSelectedChatConversation(_state); - chat.status = status; + [types.default.RESOLVE_CONVERSATION](_state, { conversationId, status }) { + const conversation = + getters.getConversationById(_state)(conversationId) || {}; + Vue.set(conversation, 'status', status); }, [types.default.MUTE_CONVERSATION](_state) { 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 ad794611c..ce939ea3c 100644 --- a/app/javascript/dashboard/store/modules/specs/conversations/actions.spec.js +++ b/app/javascript/dashboard/store/modules/specs/conversations/actions.spec.js @@ -214,6 +214,22 @@ describe('#actions', () => { }); }); + describe('#toggleStatus', () => { + it('sends correct mutations if toggle status is successful', async () => { + axios.post.mockResolvedValue({ + data: { payload: { conversation_id: 1, current_status: 'resolved' } }, + }); + await actions.toggleStatus( + { commit }, + { conversationId: 1, status: 'resolved' } + ); + expect(commit).toHaveBeenCalledTimes(1); + expect(commit.mock.calls).toEqual([ + ['RESOLVE_CONVERSATION', { conversationId: 1, status: 'resolved' }], + ]); + }); + }); + describe('#assignTeam', () => { it('sends correct mutations if assignment is successful', 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 1074d11eb..3ffc2b505 100644 --- a/app/javascript/dashboard/store/modules/specs/conversations/mutations.spec.js +++ b/app/javascript/dashboard/store/modules/specs/conversations/mutations.spec.js @@ -160,4 +160,31 @@ describe('#mutations', () => { expect(global.bus.$emit).not.toHaveBeenCalled(); }); }); + + describe('#RESOLVE_CONVERSATION', () => { + it('updates the conversation status correctly', () => { + const state = { + allConversations: [ + { + id: 1, + messages: [], + status: 'open', + }, + ], + }; + + mutations[types.RESOLVE_CONVERSATION](state, { + conversationId: '1', + status: 'resolved', + }); + + expect(state.allConversations).toEqual([ + { + id: 1, + messages: [], + status: 'resolved', + }, + ]); + }); + }); });