This PR target two issues ### CC & BCC not updated correctly When moving from one conversation to another, the store may not have the list of all the messages. A fetch is subsequently made to get the messages. However, this update does not trigger the `currentChat` watcher. This PR fixes it by adding a new watcher on `currentChat.messages`. We also update the `setCCAndToEmailsFromLastChat` method to reset the `cc`, `bcc` and `to` fields if the last email is not found. This ensures that the data is not carried forward from a previous email Fixes: https://github.com/chatwoot/chatwoot/issues/10477 ### To address are not added correctly to the `CC` If the `to` address of a previous email has multiple recipient, there was no case to add them to the CC. Fixes: https://github.com/chatwoot/chatwoot/issues/8925 --- Depends on: https://github.com/chatwoot/utils/pull/41
114 lines
4.0 KiB
JavaScript
114 lines
4.0 KiB
JavaScript
import { MESSAGE_TYPE } from 'shared/constants/messages';
|
|
import { applyPageFilters, sortComparator } from './helpers';
|
|
import filterQueryGenerator from 'dashboard/helper/filterQueryGenerator';
|
|
import camelcaseKeys from 'camelcase-keys';
|
|
|
|
export const getSelectedChatConversation = ({
|
|
allConversations,
|
|
selectedChatId,
|
|
}) =>
|
|
allConversations.filter(conversation => conversation.id === selectedChatId);
|
|
|
|
const getters = {
|
|
getAllConversations: ({ allConversations, chatSortFilter: sortKey }) => {
|
|
return allConversations.sort((a, b) => sortComparator(a, b, sortKey));
|
|
},
|
|
getSelectedChat: ({ selectedChatId, allConversations }) => {
|
|
const selectedChat = allConversations.find(
|
|
conversation => conversation.id === selectedChatId
|
|
);
|
|
return selectedChat || {};
|
|
},
|
|
getSelectedChatAttachments: ({ selectedChatId, attachments }) => {
|
|
return attachments[selectedChatId] || [];
|
|
},
|
|
getChatListFilters: ({ conversationFilters }) => conversationFilters,
|
|
getLastEmailInSelectedChat: (stage, _getters) => {
|
|
const selectedChat = _getters.getSelectedChat;
|
|
const { messages = [] } = selectedChat;
|
|
const lastEmail = [...messages].reverse().find(message => {
|
|
const { message_type: messageType } = message;
|
|
if (message.private) return false;
|
|
|
|
return [MESSAGE_TYPE.OUTGOING, MESSAGE_TYPE.INCOMING].includes(
|
|
messageType
|
|
);
|
|
});
|
|
|
|
return lastEmail;
|
|
},
|
|
getMineChats: (_state, _, __, rootGetters) => activeFilters => {
|
|
const currentUserID = rootGetters.getCurrentUser?.id;
|
|
|
|
return _state.allConversations.filter(conversation => {
|
|
const { assignee } = conversation.meta;
|
|
const isAssignedToMe = assignee && assignee.id === currentUserID;
|
|
const shouldFilter = applyPageFilters(conversation, activeFilters);
|
|
const isChatMine = isAssignedToMe && shouldFilter;
|
|
|
|
return isChatMine;
|
|
});
|
|
},
|
|
getAppliedConversationFiltersV2: _state => {
|
|
// TODO: Replace existing one with V2 after migrating the filters to use camelcase
|
|
return _state.appliedFilters.map(camelcaseKeys);
|
|
},
|
|
getAppliedConversationFilters: _state => {
|
|
return _state.appliedFilters;
|
|
},
|
|
getAppliedConversationFiltersQuery: _state => {
|
|
const hasAppliedFilters = _state.appliedFilters.length !== 0;
|
|
return hasAppliedFilters ? filterQueryGenerator(_state.appliedFilters) : [];
|
|
},
|
|
getUnAssignedChats: _state => activeFilters => {
|
|
return _state.allConversations.filter(conversation => {
|
|
const isUnAssigned = !conversation.meta.assignee;
|
|
const shouldFilter = applyPageFilters(conversation, activeFilters);
|
|
return isUnAssigned && shouldFilter;
|
|
});
|
|
},
|
|
getAllStatusChats: _state => activeFilters => {
|
|
return _state.allConversations.filter(conversation => {
|
|
const shouldFilter = applyPageFilters(conversation, activeFilters);
|
|
return shouldFilter;
|
|
});
|
|
},
|
|
getChatListLoadingStatus: ({ listLoadingStatus }) => listLoadingStatus,
|
|
getAllMessagesLoaded(_state) {
|
|
const [chat] = getSelectedChatConversation(_state);
|
|
return !chat || chat.allMessagesLoaded === undefined
|
|
? false
|
|
: chat.allMessagesLoaded;
|
|
},
|
|
getUnreadCount(_state) {
|
|
const [chat] = getSelectedChatConversation(_state);
|
|
if (!chat) return [];
|
|
return chat.messages.filter(
|
|
chatMessage =>
|
|
chatMessage.created_at * 1000 > chat.agent_last_seen_at * 1000 &&
|
|
chatMessage.message_type === 0 &&
|
|
chatMessage.private !== true
|
|
).length;
|
|
},
|
|
getChatStatusFilter: ({ chatStatusFilter }) => chatStatusFilter,
|
|
getChatSortFilter: ({ chatSortFilter }) => chatSortFilter,
|
|
getSelectedInbox: ({ currentInbox }) => currentInbox,
|
|
getConversationById: _state => conversationId => {
|
|
return _state.allConversations.find(
|
|
value => value.id === Number(conversationId)
|
|
);
|
|
},
|
|
getConversationParticipants: _state => {
|
|
return _state.conversationParticipants;
|
|
},
|
|
getConversationLastSeen: _state => {
|
|
return _state.conversationLastSeen;
|
|
},
|
|
|
|
getContextMenuChatId: _state => {
|
|
return _state.contextMenuChatId;
|
|
},
|
|
};
|
|
|
|
export default getters;
|