feature/IO-3000-messaging-sockets-migrations2 -
- harden openMessageByPhone Signed-off-by: Dave Richer <dave@imexsystems.ca>
This commit is contained in:
@@ -286,23 +286,6 @@ export const registerMessagingHandlers = ({ socket, client }) => {
|
||||
return;
|
||||
}
|
||||
|
||||
if (type === "conversation-unarchived" || type === "conversation-archived") {
|
||||
try {
|
||||
// Refetch the conversation list and details queries
|
||||
await client.refetchQueries({
|
||||
include: [CONVERSATION_LIST_QUERY, GET_CONVERSATION_DETAILS],
|
||||
variables: [
|
||||
{ query: CONVERSATION_LIST_QUERY, variables: { offset: 0 } },
|
||||
{ query: GET_CONVERSATION_DETAILS, variables: { conversationId } }
|
||||
]
|
||||
});
|
||||
} catch (error) {
|
||||
console.error("Error refetching queries after conversation state change:", error);
|
||||
}
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
const cacheId = client.cache.identify({
|
||||
__typename: "conversations",
|
||||
id: conversationId
|
||||
@@ -313,6 +296,27 @@ export const registerMessagingHandlers = ({ socket, client }) => {
|
||||
return;
|
||||
}
|
||||
|
||||
if (type === "conversation-unarchived" || type === "conversation-archived") {
|
||||
try {
|
||||
const listQueryVariables = { offset: 0 };
|
||||
const detailsQueryVariables = { conversationId };
|
||||
|
||||
// Refetch the conversation list and details queries
|
||||
await client.refetchQueries({
|
||||
include: [CONVERSATION_LIST_QUERY, GET_CONVERSATION_DETAILS],
|
||||
variables: [
|
||||
{ query: CONVERSATION_LIST_QUERY, variables: listQueryVariables },
|
||||
{ query: GET_CONVERSATION_DETAILS, variables: detailsQueryVariables }
|
||||
]
|
||||
});
|
||||
|
||||
console.log("Refetched conversation list and details after state change.");
|
||||
} catch (error) {
|
||||
console.error("Error refetching queries after conversation state change:", error);
|
||||
}
|
||||
|
||||
return;
|
||||
}
|
||||
// Handle other types of updates (e.g., marked read, tags added/removed)
|
||||
client.cache.modify({
|
||||
id: cacheId,
|
||||
|
||||
@@ -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