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;
|
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({
|
const cacheId = client.cache.identify({
|
||||||
__typename: "conversations",
|
__typename: "conversations",
|
||||||
id: conversationId
|
id: conversationId
|
||||||
@@ -313,6 +296,27 @@ export const registerMessagingHandlers = ({ socket, client }) => {
|
|||||||
return;
|
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)
|
// Handle other types of updates (e.g., marked read, tags added/removed)
|
||||||
client.cache.modify({
|
client.cache.modify({
|
||||||
id: cacheId,
|
id: cacheId,
|
||||||
|
|||||||
@@ -68,24 +68,23 @@ export function* openChatByPhone({ payload }) {
|
|||||||
|
|
||||||
const createdConversation = newConversations[0]; // Get the newly created conversation
|
const createdConversation = newConversations[0]; // Get the newly created conversation
|
||||||
|
|
||||||
// // Emit event for new conversation with full details
|
// Emit event for new conversation with full details
|
||||||
if (socket) {
|
socket.emit("conversation-modified", {
|
||||||
socket.emit("conversation-modified", {
|
bodyshopId: bodyshop.id,
|
||||||
bodyshopId: bodyshop.id,
|
type: "conversation-created",
|
||||||
type: "conversation-created",
|
...createdConversation
|
||||||
...createdConversation
|
});
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
// Set the newly created conversation as selected
|
// Set the newly created conversation as selected
|
||||||
yield put(setSelectedConversation(createdConversation.id));
|
yield put(setSelectedConversation(createdConversation.id));
|
||||||
} else if (conversations.length === 1) {
|
} else if (conversations.length === 1) {
|
||||||
const conversation = conversations[0];
|
const conversation = conversations[0];
|
||||||
|
let updatedConversation = conversation;
|
||||||
|
|
||||||
if (conversation.archived) {
|
if (conversation.archived) {
|
||||||
// Conversation is archived, unarchive it in the DB
|
// Conversation is archived, unarchive it in the DB
|
||||||
const {
|
const {
|
||||||
data: { update_conversations_by_pk: updatedConversation }
|
data: { update_conversations_by_pk: unarchivedConversation }
|
||||||
} = yield client.mutate({
|
} = yield client.mutate({
|
||||||
mutation: TOGGLE_CONVERSATION_ARCHIVE,
|
mutation: TOGGLE_CONVERSATION_ARCHIVE,
|
||||||
variables: {
|
variables: {
|
||||||
@@ -94,67 +93,26 @@ export function* openChatByPhone({ payload }) {
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
if (socket) {
|
updatedConversation = unarchivedConversation;
|
||||||
socket.emit("conversation-modified", {
|
|
||||||
type: "conversation-unarchived",
|
|
||||||
conversationId: updatedConversation.id,
|
|
||||||
bodyshopId: bodyshop.id,
|
|
||||||
archived: false
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
// Update the conversation list in the cache
|
// Emit the unarchived event only once
|
||||||
const existingConversations = client.cache.readQuery({
|
socket.emit("conversation-modified", {
|
||||||
query: CONVERSATION_LIST_QUERY,
|
type: "conversation-unarchived",
|
||||||
variables: { offset: 0 }
|
conversationId: unarchivedConversation.id,
|
||||||
});
|
bodyshopId: bodyshop.id,
|
||||||
|
archived: false
|
||||||
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
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
// Open the conversation
|
// Open the conversation
|
||||||
yield put(setSelectedConversation(conversation.id));
|
yield put(setSelectedConversation(updatedConversation.id));
|
||||||
|
|
||||||
// Check and add job tag if needed
|
// 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({
|
yield client.mutate({
|
||||||
mutation: INSERT_CONVERSATION_TAG,
|
mutation: INSERT_CONVERSATION_TAG,
|
||||||
variables: {
|
variables: {
|
||||||
conversationId: conversation.id,
|
conversationId: updatedConversation.id,
|
||||||
jobId: jobid
|
jobId: jobid
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|||||||
Reference in New Issue
Block a user