feature/IO-3000-messaging-sockets-migrations2 -
- harden openMessageByPhone Signed-off-by: Dave Richer <dave@imexsystems.ca>
This commit is contained in:
@@ -68,24 +68,23 @@ export function* openChatByPhone({ payload }) {
|
||||
|
||||
const createdConversation = newConversations[0]; // Get the newly created conversation
|
||||
|
||||
// // Emit event for new conversation with full details
|
||||
if (socket) {
|
||||
socket.emit("conversation-modified", {
|
||||
bodyshopId: bodyshop.id,
|
||||
type: "conversation-created",
|
||||
...createdConversation
|
||||
});
|
||||
}
|
||||
// Emit event for new conversation with full details
|
||||
socket.emit("conversation-modified", {
|
||||
bodyshopId: bodyshop.id,
|
||||
type: "conversation-created",
|
||||
...createdConversation
|
||||
});
|
||||
|
||||
// Set the newly created conversation as selected
|
||||
yield put(setSelectedConversation(createdConversation.id));
|
||||
} else if (conversations.length === 1) {
|
||||
const conversation = conversations[0];
|
||||
let updatedConversation = conversation;
|
||||
|
||||
if (conversation.archived) {
|
||||
// Conversation is archived, unarchive it in the DB
|
||||
const {
|
||||
data: { update_conversations_by_pk: updatedConversation }
|
||||
data: { update_conversations_by_pk: unarchivedConversation }
|
||||
} = yield client.mutate({
|
||||
mutation: TOGGLE_CONVERSATION_ARCHIVE,
|
||||
variables: {
|
||||
@@ -94,67 +93,26 @@ export function* openChatByPhone({ payload }) {
|
||||
}
|
||||
});
|
||||
|
||||
if (socket) {
|
||||
socket.emit("conversation-modified", {
|
||||
type: "conversation-unarchived",
|
||||
conversationId: updatedConversation.id,
|
||||
bodyshopId: bodyshop.id,
|
||||
archived: false
|
||||
});
|
||||
}
|
||||
updatedConversation = unarchivedConversation;
|
||||
|
||||
// Update the conversation list in the cache
|
||||
const existingConversations = client.cache.readQuery({
|
||||
query: CONVERSATION_LIST_QUERY,
|
||||
variables: { offset: 0 }
|
||||
});
|
||||
|
||||
client.cache.writeQuery({
|
||||
query: CONVERSATION_LIST_QUERY,
|
||||
variables: { offset: 0 },
|
||||
data: {
|
||||
conversations: [
|
||||
{
|
||||
...conversation,
|
||||
archived: false,
|
||||
updated_at: new Date().toISOString()
|
||||
},
|
||||
...(existingConversations?.conversations || [])
|
||||
]
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
// Check if the conversation exists in the cache
|
||||
const cacheId = client.cache.identify({
|
||||
__typename: "conversations",
|
||||
id: conversation.id
|
||||
});
|
||||
|
||||
if (!cacheId) {
|
||||
// Fetch the conversation details from the database
|
||||
const { data } = yield client.query({
|
||||
query: GET_CONVERSATION_DETAILS,
|
||||
variables: { conversationId: conversation.id }
|
||||
});
|
||||
|
||||
// Write fetched data to the cache
|
||||
client.cache.writeQuery({
|
||||
query: GET_CONVERSATION_DETAILS,
|
||||
variables: { conversationId: conversation.id },
|
||||
data
|
||||
// Emit the unarchived event only once
|
||||
socket.emit("conversation-modified", {
|
||||
type: "conversation-unarchived",
|
||||
conversationId: unarchivedConversation.id,
|
||||
bodyshopId: bodyshop.id,
|
||||
archived: false
|
||||
});
|
||||
}
|
||||
|
||||
// Open the conversation
|
||||
yield put(setSelectedConversation(conversation.id));
|
||||
yield put(setSelectedConversation(updatedConversation.id));
|
||||
|
||||
// Check and add job tag if needed
|
||||
if (jobid && !conversation.job_conversations.find((jc) => jc.jobid === jobid)) {
|
||||
if (jobid && !updatedConversation.job_conversations.find((jc) => jc.jobid === jobid)) {
|
||||
yield client.mutate({
|
||||
mutation: INSERT_CONVERSATION_TAG,
|
||||
variables: {
|
||||
conversationId: conversation.id,
|
||||
conversationId: updatedConversation.id,
|
||||
jobId: jobid
|
||||
}
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user