2019-09-05 07:31:36 +00:00
|
|
|
import Vue from 'vue';
|
2021-11-12 08:11:43 +00:00
|
|
|
import types from '../../mutation-types';
|
2019-09-05 07:31:36 +00:00
|
|
|
import ConversationApi from '../../../api/inbox/conversation';
|
2019-10-27 13:31:59 +00:00
|
|
|
import MessageApi from '../../../api/inbox/message';
|
2020-12-25 07:45:01 +00:00
|
|
|
import { MESSAGE_STATUS, MESSAGE_TYPE } from 'shared/constants/messages';
|
2021-01-06 12:26:29 +00:00
|
|
|
import { createPendingMessage } from 'dashboard/helper/commons';
|
2021-12-09 05:50:14 +00:00
|
|
|
import {
|
|
|
|
buildConversationList,
|
|
|
|
isOnMentionsView,
|
|
|
|
} from './helpers/actionHelpers';
|
2019-09-05 07:31:36 +00:00
|
|
|
|
|
|
|
// actions
|
|
|
|
const actions = {
|
2020-03-08 16:38:25 +00:00
|
|
|
getConversation: async ({ commit }, conversationId) => {
|
|
|
|
try {
|
|
|
|
const response = await ConversationApi.show(conversationId);
|
2021-11-12 08:11:43 +00:00
|
|
|
commit(types.UPDATE_CONVERSATION, response.data);
|
|
|
|
commit(`contacts/${types.SET_CONTACT_ITEM}`, response.data.meta.sender);
|
2020-03-08 16:38:25 +00:00
|
|
|
} catch (error) {
|
|
|
|
// Ignore error
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2020-02-26 15:45:01 +00:00
|
|
|
fetchAllConversations: async ({ commit, dispatch }, params) => {
|
2021-11-12 08:11:43 +00:00
|
|
|
commit(types.SET_LIST_LOADING_STATUS);
|
2019-10-27 13:31:59 +00:00
|
|
|
try {
|
2021-09-29 14:03:51 +00:00
|
|
|
const {
|
2021-11-18 13:45:02 +00:00
|
|
|
data: { data },
|
|
|
|
} = await ConversationApi.get(params);
|
|
|
|
buildConversationList(
|
|
|
|
{ commit, dispatch },
|
|
|
|
params,
|
|
|
|
data,
|
|
|
|
params.assigneeType
|
2020-06-02 17:29:02 +00:00
|
|
|
);
|
2021-11-18 13:45:02 +00:00
|
|
|
} catch (error) {
|
|
|
|
// Handle error
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
fetchFilteredConversations: async ({ commit, dispatch }, params) => {
|
|
|
|
commit(types.SET_LIST_LOADING_STATUS);
|
|
|
|
try {
|
|
|
|
const { data } = await ConversationApi.filter(params);
|
|
|
|
buildConversationList(
|
|
|
|
{ commit, dispatch },
|
|
|
|
params,
|
|
|
|
data,
|
|
|
|
'appliedFilters'
|
2020-02-26 15:45:01 +00:00
|
|
|
);
|
2019-10-27 13:31:59 +00:00
|
|
|
} catch (error) {
|
|
|
|
// Handle error
|
|
|
|
}
|
2019-09-05 07:31:36 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
emptyAllConversations({ commit }) {
|
2021-11-12 08:11:43 +00:00
|
|
|
commit(types.EMPTY_ALL_CONVERSATION);
|
2019-09-05 07:31:36 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
clearSelectedState({ commit }) {
|
2021-11-12 08:11:43 +00:00
|
|
|
commit(types.CLEAR_CURRENT_CHAT_WINDOW);
|
2019-09-05 07:31:36 +00:00
|
|
|
},
|
|
|
|
|
2019-10-27 13:31:59 +00:00
|
|
|
fetchPreviousMessages: async ({ commit }, data) => {
|
|
|
|
try {
|
2020-01-01 17:00:43 +00:00
|
|
|
const {
|
|
|
|
data: { meta, payload },
|
|
|
|
} = await MessageApi.getPreviousMessages(data);
|
2021-11-12 08:11:43 +00:00
|
|
|
commit(`conversationMetadata/${types.SET_CONVERSATION_METADATA}`, {
|
|
|
|
id: data.conversationId,
|
|
|
|
data: meta,
|
|
|
|
});
|
|
|
|
commit(types.SET_PREVIOUS_CONVERSATIONS, {
|
2019-10-27 13:31:59 +00:00
|
|
|
id: data.conversationId,
|
2020-01-01 17:00:43 +00:00
|
|
|
data: payload,
|
2019-10-27 13:31:59 +00:00
|
|
|
});
|
2020-01-01 17:00:43 +00:00
|
|
|
if (payload.length < 20) {
|
2021-11-12 08:11:43 +00:00
|
|
|
commit(types.SET_ALL_MESSAGES_LOADED);
|
2019-10-27 13:31:59 +00:00
|
|
|
}
|
|
|
|
} catch (error) {
|
|
|
|
// Handle error
|
|
|
|
}
|
2019-09-05 07:31:36 +00:00
|
|
|
},
|
|
|
|
|
2020-07-23 18:03:17 +00:00
|
|
|
async setActiveChat({ commit, dispatch }, data) {
|
2021-11-12 08:11:43 +00:00
|
|
|
commit(types.SET_CURRENT_CHAT_WINDOW, data);
|
|
|
|
commit(types.CLEAR_ALL_MESSAGES_LOADED);
|
2019-09-05 07:31:36 +00:00
|
|
|
|
|
|
|
if (data.dataFetched === undefined) {
|
2020-07-23 18:03:17 +00:00
|
|
|
try {
|
|
|
|
await dispatch('fetchPreviousMessages', {
|
2019-10-27 13:31:59 +00:00
|
|
|
conversationId: data.id,
|
2019-09-05 07:31:36 +00:00
|
|
|
before: data.messages[0].id,
|
2020-07-23 18:03:17 +00:00
|
|
|
});
|
|
|
|
Vue.set(data, 'dataFetched', true);
|
|
|
|
} catch (error) {
|
|
|
|
// Ignore error
|
|
|
|
}
|
2019-09-05 07:31:36 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2021-03-15 13:07:03 +00:00
|
|
|
assignAgent: async ({ dispatch }, { conversationId, agentId }) => {
|
2019-10-27 13:31:59 +00:00
|
|
|
try {
|
|
|
|
const response = await ConversationApi.assignAgent({
|
|
|
|
conversationId,
|
|
|
|
agentId,
|
2019-09-05 07:31:36 +00:00
|
|
|
});
|
2021-03-15 13:07:03 +00:00
|
|
|
dispatch('setCurrentChatAssignee', response.data);
|
2019-10-27 13:31:59 +00:00
|
|
|
} catch (error) {
|
|
|
|
// Handle error
|
|
|
|
}
|
2019-09-05 07:31:36 +00:00
|
|
|
},
|
|
|
|
|
2021-03-15 13:07:03 +00:00
|
|
|
setCurrentChatAssignee({ commit }, assignee) {
|
2021-11-12 08:11:43 +00:00
|
|
|
commit(types.ASSIGN_AGENT, assignee);
|
2021-03-15 13:07:03 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
assignTeam: async ({ dispatch }, { conversationId, teamId }) => {
|
|
|
|
try {
|
|
|
|
const response = await ConversationApi.assignTeam({
|
|
|
|
conversationId,
|
|
|
|
teamId,
|
|
|
|
});
|
|
|
|
dispatch('setCurrentChatTeam', response.data);
|
|
|
|
} catch (error) {
|
|
|
|
// Handle error
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
setCurrentChatTeam({ commit }, team) {
|
2021-11-12 08:11:43 +00:00
|
|
|
commit(types.ASSIGN_TEAM, team);
|
2021-03-15 13:07:03 +00:00
|
|
|
},
|
|
|
|
|
2021-07-23 09:54:07 +00:00
|
|
|
toggleStatus: async (
|
|
|
|
{ commit },
|
|
|
|
{ conversationId, status, snoozedUntil = null }
|
|
|
|
) => {
|
2019-10-27 13:31:59 +00:00
|
|
|
try {
|
2021-09-29 14:03:51 +00:00
|
|
|
const {
|
|
|
|
data: {
|
|
|
|
payload: {
|
|
|
|
current_status: updatedStatus,
|
|
|
|
snoozed_until: updatedSnoozedUntil,
|
|
|
|
} = {},
|
|
|
|
} = {},
|
|
|
|
} = await ConversationApi.toggleStatus({
|
2021-04-07 06:14:58 +00:00
|
|
|
conversationId,
|
|
|
|
status,
|
2021-07-23 09:54:07 +00:00
|
|
|
snoozedUntil,
|
2021-04-07 06:14:58 +00:00
|
|
|
});
|
2021-11-12 08:11:43 +00:00
|
|
|
commit(types.CHANGE_CONVERSATION_STATUS, {
|
2021-08-02 07:41:07 +00:00
|
|
|
conversationId,
|
2021-09-29 14:03:51 +00:00
|
|
|
status: updatedStatus,
|
|
|
|
snoozedUntil: updatedSnoozedUntil,
|
2021-08-02 07:41:07 +00:00
|
|
|
});
|
2019-10-27 13:31:59 +00:00
|
|
|
} catch (error) {
|
|
|
|
// Handle error
|
|
|
|
}
|
2019-09-05 07:31:36 +00:00
|
|
|
},
|
|
|
|
|
2021-12-20 18:37:33 +00:00
|
|
|
createPendingMessageAndSend: async ({ dispatch }, data) => {
|
|
|
|
const pendingMessage = createPendingMessage(data);
|
|
|
|
dispatch('sendMessageWithData', pendingMessage);
|
|
|
|
},
|
|
|
|
|
|
|
|
sendMessageWithData: async ({ commit }, pendingMessage) => {
|
2019-10-27 13:31:59 +00:00
|
|
|
try {
|
2021-12-20 18:37:33 +00:00
|
|
|
commit(types.ADD_MESSAGE, {
|
|
|
|
...pendingMessage,
|
|
|
|
status: MESSAGE_STATUS.PROGRESS,
|
|
|
|
});
|
2020-12-25 07:45:01 +00:00
|
|
|
const response = await MessageApi.create(pendingMessage);
|
2021-11-12 08:11:43 +00:00
|
|
|
commit(types.ADD_MESSAGE, {
|
2020-12-25 07:45:01 +00:00
|
|
|
...response.data,
|
|
|
|
status: MESSAGE_STATUS.SENT,
|
|
|
|
});
|
2019-10-27 13:31:59 +00:00
|
|
|
} catch (error) {
|
2021-12-20 18:37:33 +00:00
|
|
|
const errorMessage = error.response
|
|
|
|
? error.response.data.error
|
|
|
|
: undefined;
|
|
|
|
commit(types.ADD_MESSAGE, {
|
|
|
|
...pendingMessage,
|
|
|
|
meta: {
|
|
|
|
error: errorMessage,
|
|
|
|
},
|
|
|
|
status: MESSAGE_STATUS.FAILED,
|
|
|
|
});
|
2021-09-29 07:30:08 +00:00
|
|
|
throw error;
|
2019-10-27 13:31:59 +00:00
|
|
|
}
|
2019-09-05 07:31:36 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
addMessage({ commit }, message) {
|
2021-11-12 08:11:43 +00:00
|
|
|
commit(types.ADD_MESSAGE, message);
|
2020-07-25 17:24:45 +00:00
|
|
|
if (message.message_type === MESSAGE_TYPE.INCOMING) {
|
2021-11-12 08:11:43 +00:00
|
|
|
commit(types.SET_CONVERSATION_CAN_REPLY, {
|
2020-07-25 17:24:45 +00:00
|
|
|
conversationId: message.conversation_id,
|
|
|
|
canReply: true,
|
|
|
|
});
|
|
|
|
}
|
2019-09-05 07:31:36 +00:00
|
|
|
},
|
|
|
|
|
2022-04-20 10:33:12 +00:00
|
|
|
updateConversationRead({ commit }, timestamp) {
|
|
|
|
commit(types.SET_CONVERSATION_LAST_SEEN, timestamp);
|
|
|
|
},
|
|
|
|
|
2020-04-17 15:45:20 +00:00
|
|
|
updateMessage({ commit }, message) {
|
2021-11-12 08:11:43 +00:00
|
|
|
commit(types.ADD_MESSAGE, message);
|
2020-04-17 15:45:20 +00:00
|
|
|
},
|
|
|
|
|
2021-06-29 05:49:08 +00:00
|
|
|
deleteMessage: async function deleteLabels(
|
|
|
|
{ commit },
|
|
|
|
{ conversationId, messageId }
|
|
|
|
) {
|
|
|
|
try {
|
|
|
|
const response = await MessageApi.delete(conversationId, messageId);
|
|
|
|
const { data } = response;
|
|
|
|
// The delete message is actually deleting the content.
|
2021-11-12 08:11:43 +00:00
|
|
|
commit(types.ADD_MESSAGE, data);
|
2021-06-29 05:49:08 +00:00
|
|
|
} catch (error) {
|
|
|
|
throw new Error(error);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2021-12-09 05:50:14 +00:00
|
|
|
addConversation({ commit, state, dispatch, rootState }, conversation) {
|
2021-11-18 13:45:02 +00:00
|
|
|
const { currentInbox, appliedFilters } = state;
|
2020-07-04 14:16:17 +00:00
|
|
|
const {
|
|
|
|
inbox_id: inboxId,
|
|
|
|
meta: { sender },
|
|
|
|
} = conversation;
|
2021-12-09 05:50:14 +00:00
|
|
|
|
2021-11-18 13:45:02 +00:00
|
|
|
const hasAppliedFilters = !!appliedFilters.length;
|
|
|
|
const isMatchingInboxFilter =
|
|
|
|
!currentInbox || Number(currentInbox) === inboxId;
|
2021-12-09 05:50:14 +00:00
|
|
|
if (
|
|
|
|
!hasAppliedFilters &&
|
|
|
|
!isOnMentionsView(rootState) &&
|
|
|
|
isMatchingInboxFilter
|
|
|
|
) {
|
2021-11-12 08:11:43 +00:00
|
|
|
commit(types.ADD_CONVERSATION, conversation);
|
2020-07-04 14:16:17 +00:00
|
|
|
dispatch('contacts/setContact', sender);
|
2020-04-18 14:55:58 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2021-12-09 05:50:14 +00:00
|
|
|
addMentions({ dispatch, rootState }, conversation) {
|
|
|
|
if (isOnMentionsView(rootState)) {
|
|
|
|
dispatch('updateConversation', conversation);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2020-07-04 14:16:17 +00:00
|
|
|
updateConversation({ commit, dispatch }, conversation) {
|
|
|
|
const {
|
|
|
|
meta: { sender },
|
|
|
|
} = conversation;
|
2021-11-12 08:11:43 +00:00
|
|
|
commit(types.UPDATE_CONVERSATION, conversation);
|
2020-07-04 14:16:17 +00:00
|
|
|
dispatch('contacts/setContact', sender);
|
2019-09-05 07:31:36 +00:00
|
|
|
},
|
|
|
|
|
2019-10-27 13:31:59 +00:00
|
|
|
markMessagesRead: async ({ commit }, data) => {
|
|
|
|
try {
|
2020-08-03 08:10:20 +00:00
|
|
|
const {
|
|
|
|
data: { id, agent_last_seen_at: lastSeen },
|
|
|
|
} = await ConversationApi.markMessageRead(data);
|
2021-11-12 08:11:43 +00:00
|
|
|
setTimeout(() => commit(types.MARK_MESSAGE_READ, { id, lastSeen }), 4000);
|
2019-10-27 13:31:59 +00:00
|
|
|
} catch (error) {
|
|
|
|
// Handle error
|
|
|
|
}
|
2019-09-05 07:31:36 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
setChatFilter({ commit }, data) {
|
2021-11-12 08:11:43 +00:00
|
|
|
commit(types.CHANGE_CHAT_STATUS_FILTER, data);
|
2019-09-05 07:31:36 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
updateAssignee({ commit }, data) {
|
2021-11-12 08:11:43 +00:00
|
|
|
commit(types.UPDATE_ASSIGNEE, data);
|
2019-09-05 07:31:36 +00:00
|
|
|
},
|
|
|
|
|
2020-06-02 17:29:02 +00:00
|
|
|
updateConversationContact({ commit }, data) {
|
|
|
|
if (data.id) {
|
2021-11-12 08:11:43 +00:00
|
|
|
commit(`contacts/${types.SET_CONTACT_ITEM}`, data);
|
2020-06-02 17:29:02 +00:00
|
|
|
}
|
2021-11-12 08:11:43 +00:00
|
|
|
commit(types.UPDATE_CONVERSATION_CONTACT, data);
|
2020-06-02 17:29:02 +00:00
|
|
|
},
|
|
|
|
|
2019-09-05 07:31:36 +00:00
|
|
|
setActiveInbox({ commit }, inboxId) {
|
2021-11-12 08:11:43 +00:00
|
|
|
commit(types.SET_ACTIVE_INBOX, inboxId);
|
2019-09-05 07:31:36 +00:00
|
|
|
},
|
2020-03-22 10:24:36 +00:00
|
|
|
|
2020-05-26 12:13:59 +00:00
|
|
|
muteConversation: async ({ commit }, conversationId) => {
|
|
|
|
try {
|
|
|
|
await ConversationApi.mute(conversationId);
|
2021-11-12 08:11:43 +00:00
|
|
|
commit(types.MUTE_CONVERSATION);
|
2020-05-26 12:13:59 +00:00
|
|
|
} catch (error) {
|
|
|
|
//
|
|
|
|
}
|
|
|
|
},
|
2020-08-17 05:55:13 +00:00
|
|
|
|
2020-10-08 06:32:08 +00:00
|
|
|
unmuteConversation: async ({ commit }, conversationId) => {
|
|
|
|
try {
|
|
|
|
await ConversationApi.unmute(conversationId);
|
2021-11-12 08:11:43 +00:00
|
|
|
commit(types.UNMUTE_CONVERSATION);
|
2020-10-08 06:32:08 +00:00
|
|
|
} catch (error) {
|
|
|
|
//
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2020-08-17 05:55:13 +00:00
|
|
|
sendEmailTranscript: async (_, { conversationId, email }) => {
|
|
|
|
try {
|
|
|
|
await ConversationApi.sendEmailTranscript({ conversationId, email });
|
|
|
|
} catch (error) {
|
|
|
|
throw new Error(error);
|
|
|
|
}
|
|
|
|
},
|
2021-10-30 01:39:46 +00:00
|
|
|
|
|
|
|
updateCustomAttributes: async (
|
|
|
|
{ commit },
|
|
|
|
{ conversationId, customAttributes }
|
|
|
|
) => {
|
|
|
|
try {
|
|
|
|
const response = await ConversationApi.updateCustomAttributes({
|
|
|
|
conversationId,
|
|
|
|
customAttributes,
|
|
|
|
});
|
|
|
|
const { custom_attributes } = response.data;
|
2021-11-12 08:11:43 +00:00
|
|
|
commit(types.UPDATE_CONVERSATION_CUSTOM_ATTRIBUTES, custom_attributes);
|
2021-10-30 01:39:46 +00:00
|
|
|
} catch (error) {
|
|
|
|
// Handle error
|
|
|
|
}
|
|
|
|
},
|
2021-11-18 13:45:02 +00:00
|
|
|
|
|
|
|
setConversationFilters({ commit }, data) {
|
|
|
|
commit(types.SET_CONVERSATION_FILTERS, data);
|
|
|
|
},
|
|
|
|
|
|
|
|
clearConversationFilters({ commit }) {
|
|
|
|
commit(types.CLEAR_CONVERSATION_FILTERS);
|
|
|
|
},
|
2019-09-05 07:31:36 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
export default actions;
|