feature/IO-3000-messaging-sockets-migration2 - Final Modifications

Signed-off-by: Dave Richer <dave@imexsystems.ca>
This commit is contained in:
Dave Richer
2024-11-25 12:46:10 -08:00
parent 62dd3d7e8e
commit c3c66f9646
4 changed files with 77 additions and 129 deletions

View File

@@ -46,16 +46,20 @@ export const registerMessagingHandlers = ({ socket, client }) => {
variables: queryVariables
});
const existingConversations = queryResults?.conversations || [];
const enrichedConversation = enrichConversation(newConversation, isoutbound);
client.cache.modify({
id: "ROOT_QUERY",
fields: {
conversations(existingConversations = []) {
return [enrichedConversation, ...existingConversations];
// Avoid adding duplicate conversations
if (!existingConversations.some((conv) => conv.id === enrichedConversation.id)) {
client.cache.modify({
id: "ROOT_QUERY",
fields: {
conversations(existingConversations = []) {
return [enrichedConversation, ...existingConversations];
}
}
}
});
});
}
} catch (error) {
console.error("Error updating cache for new conversation:", error);
}
@@ -192,7 +196,10 @@ export const registerMessagingHandlers = ({ socket, client }) => {
}
});
logLocal("handleNewMessageDetailed - Message appended successfully", { conversationId, newMessage });
logLocal("handleNewMessageDetailed - Message appended successfully", {
conversationId,
newMessage
});
} catch (error) {
console.error("Error updating conversation messages in cache:", error);
}
@@ -255,7 +262,10 @@ export const registerMessagingHandlers = ({ socket, client }) => {
}
});
logLocal("handleMessageChanged - Message updated successfully", { messageId: message.id, type: message.type });
logLocal("handleMessageChanged - Message updated successfully", {
messageId: message.id,
type: message.type
});
} catch (error) {
console.error("handleMessageChanged - Error modifying cache:", error);
}
@@ -348,11 +358,21 @@ export const registerMessagingHandlers = ({ socket, client }) => {
include: [CONVERSATION_LIST_QUERY, ...(detailsExist ? [GET_CONVERSATION_DETAILS] : [])],
variables: [
{ query: CONVERSATION_LIST_QUERY, variables: listQueryVariables },
...(detailsExist ? [{ query: GET_CONVERSATION_DETAILS, variables: detailsQueryVariables }] : [])
...(detailsExist
? [
{
query: GET_CONVERSATION_DETAILS,
variables: detailsQueryVariables
}
]
: [])
]
});
logLocal("handleConversationChanged - Refetched queries after state change", { conversationId, type });
logLocal("handleConversationChanged - Refetched queries after state change", {
conversationId,
type
});
} catch (error) {
console.error("Error refetching queries after conversation state change:", error);
}
@@ -393,60 +413,8 @@ export const registerMessagingHandlers = ({ socket, client }) => {
}
};
const handleNewMessage = ({ conversationId, message }) => {
if (!conversationId || !message?.id || !message?.text) {
logLocal("handleNewMessage - Missing conversationId or message details", { conversationId, message });
return;
}
logLocal("handleNewMessage - Start", { conversationId, message });
try {
// Add the new message to the cache
client.cache.modify({
id: client.cache.identify({ __typename: "conversations", id: conversationId }),
fields: {
messages(existing = []) {
// Write the new message to the cache
const newMessageRef = client.cache.writeFragment({
data: {
__typename: "messages",
id: message.id,
text: message.text,
selectedMedia: message.image_path || [],
imexshopid: message.userid,
status: message.status,
created_at: message.created_at,
read: message.read
},
fragment: gql`
fragment NewMessage on messages {
id
text
selectedMedia
imexshopid
status
created_at
read
}
`
});
// The merge function defined in the cache will handle deduplication
return [...existing, newMessageRef];
}
}
});
logLocal("handleNewMessage - Message added to cache", { conversationId, messageId: message.id });
} catch (error) {
console.error("handleNewMessage - Error modifying cache:", error);
}
};
socket.on("new-message-summary", handleNewMessageSummary);
socket.on("new-message-detailed", handleNewMessageDetailed);
socket.on("new-message", handleNewMessage);
socket.on("message-changed", handleMessageChanged);
socket.on("conversation-changed", handleConversationChanged);
};

View File

@@ -49,14 +49,6 @@ function ChatSendMessageComponent({ conversation, bodyshop, sendMessage, isSendi
imexshopid: bodyshop.imexshopid
};
sendMessage(newMessage);
if (socket) {
const lastMessage = conversation.messages?.[conversation.messages.length - 1]; // Get the last message
socket.emit("message-added", {
conversationId: conversation.id,
bodyshopId: bodyshop.id,
message: lastMessage
});
}
setSelectedMedia(
selectedMedia.map((i) => {
return { ...i, isSelected: false };

View File

@@ -33,22 +33,62 @@ export function* openChatByPhone({ payload }) {
logImEXEvent("messaging_open_by_phone");
const { socket, phone_num, jobid } = payload;
if (!socket || !phone_num) return;
const p = parsePhoneNumber(phone_num, "CA");
const bodyshop = yield select(selectBodyshop);
try {
// Fetch conversations including archived ones
const {
data: { conversations }
} = yield client.query({
query: CONVERSATION_ID_BY_PHONE,
variables: { phone: p.number },
// THIS NEEDS TO REMAIN NO CACHE, IT CHECKS FOR NEW MESSAGES FOR SYNC
fetchPolicy: "no-cache"
fetchPolicy: "no-cache" // Ensure the query always gets the latest data
});
const existingConversation = conversations?.find((c) => c.phone_num === phone_num);
const existingConversation = conversations?.find((c) => c.phone_num === p.number);
if (!existingConversation) {
if (existingConversation) {
let updatedConversation = existingConversation;
if (existingConversation.archived) {
// If the conversation is archived, unarchive it
const {
data: { update_conversations_by_pk: unarchivedConversation }
} = yield client.mutate({
mutation: TOGGLE_CONVERSATION_ARCHIVE,
variables: {
id: existingConversation.id,
archived: false
}
});
updatedConversation = unarchivedConversation;
// Emit an event indicating the conversation was unarchived
socket.emit("conversation-modified", {
type: "conversation-unarchived",
conversationId: unarchivedConversation.id,
bodyshopId: bodyshop.id,
archived: false
});
}
// Set the unarchived or already active conversation as selected
yield put(setSelectedConversation(updatedConversation.id));
// Add job tag if needed
if (jobid && !updatedConversation.job_conversations.find((jc) => jc.jobid === jobid)) {
yield client.mutate({
mutation: INSERT_CONVERSATION_TAG,
variables: {
conversationId: updatedConversation.id,
jobId: jobid
}
});
}
} else {
// No conversation exists, create a new one
const {
data: {
@@ -67,9 +107,9 @@ export function* openChatByPhone({ payload }) {
}
});
const createdConversation = newConversations[0]; // Get the newly created conversation
const createdConversation = newConversations[0];
// Emit event for new conversation with full details
// Emit event for the new conversation with full details
socket.emit("conversation-modified", {
bodyshopId: bodyshop.id,
type: "conversation-created",
@@ -78,45 +118,6 @@ export function* openChatByPhone({ payload }) {
// Set the newly created conversation as selected
yield put(setSelectedConversation(createdConversation.id));
} else {
let updatedConversation = existingConversation;
if (existingConversation.archived) {
// Conversation is archived, unarchive it in the DB
const {
data: { update_conversations_by_pk: unarchivedConversation }
} = yield client.mutate({
mutation: TOGGLE_CONVERSATION_ARCHIVE,
variables: {
id: existingConversation.id,
archived: false
}
});
updatedConversation = unarchivedConversation;
// 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(updatedConversation.id));
// Check and add job tag if needed
if (jobid && !updatedConversation.job_conversations.find((jc) => jc.jobid === jobid)) {
yield client.mutate({
mutation: INSERT_CONVERSATION_TAG,
variables: {
conversationId: updatedConversation.id,
jobId: jobid
}
});
}
}
} catch (error) {
console.error("Error in openChatByPhone saga.", error);

View File

@@ -188,19 +188,6 @@ const redisSocketEvents = ({
}
};
const messageAdded = ({ bodyshopId, conversationId, message }) => {
try {
const room = getBodyshopConversationRoom({ bodyshopId, conversationId });
io.to(room).emit("new-message", { message, conversationId });
} catch (error) {
logger.log("Failed to handle new message", "error", "io-redis", null, {
error: error.message,
stack: error.stack
});
}
};
socket.on("message-added", messageAdded);
socket.on("conversation-modified", conversationModified);
socket.on("join-bodyshop-conversation", joinConversationRoom);
socket.on("leave-bodyshop-conversation", leaveConversationRoom);