feature/IO-3000-messaging-sockets-migration2 - Final Modifications
Signed-off-by: Dave Richer <dave@imexsystems.ca>
This commit is contained in:
@@ -46,16 +46,20 @@ export const registerMessagingHandlers = ({ socket, client }) => {
|
|||||||
variables: queryVariables
|
variables: queryVariables
|
||||||
});
|
});
|
||||||
|
|
||||||
|
const existingConversations = queryResults?.conversations || [];
|
||||||
const enrichedConversation = enrichConversation(newConversation, isoutbound);
|
const enrichedConversation = enrichConversation(newConversation, isoutbound);
|
||||||
|
|
||||||
client.cache.modify({
|
// Avoid adding duplicate conversations
|
||||||
id: "ROOT_QUERY",
|
if (!existingConversations.some((conv) => conv.id === enrichedConversation.id)) {
|
||||||
fields: {
|
client.cache.modify({
|
||||||
conversations(existingConversations = []) {
|
id: "ROOT_QUERY",
|
||||||
return [enrichedConversation, ...existingConversations];
|
fields: {
|
||||||
|
conversations(existingConversations = []) {
|
||||||
|
return [enrichedConversation, ...existingConversations];
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
});
|
||||||
});
|
}
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error("Error updating cache for new conversation:", 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) {
|
} catch (error) {
|
||||||
console.error("Error updating conversation messages in cache:", 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) {
|
} catch (error) {
|
||||||
console.error("handleMessageChanged - Error modifying cache:", 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] : [])],
|
include: [CONVERSATION_LIST_QUERY, ...(detailsExist ? [GET_CONVERSATION_DETAILS] : [])],
|
||||||
variables: [
|
variables: [
|
||||||
{ query: CONVERSATION_LIST_QUERY, variables: listQueryVariables },
|
{ 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) {
|
} catch (error) {
|
||||||
console.error("Error refetching queries after conversation state change:", 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-summary", handleNewMessageSummary);
|
||||||
socket.on("new-message-detailed", handleNewMessageDetailed);
|
socket.on("new-message-detailed", handleNewMessageDetailed);
|
||||||
socket.on("new-message", handleNewMessage);
|
|
||||||
socket.on("message-changed", handleMessageChanged);
|
socket.on("message-changed", handleMessageChanged);
|
||||||
socket.on("conversation-changed", handleConversationChanged);
|
socket.on("conversation-changed", handleConversationChanged);
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -49,14 +49,6 @@ function ChatSendMessageComponent({ conversation, bodyshop, sendMessage, isSendi
|
|||||||
imexshopid: bodyshop.imexshopid
|
imexshopid: bodyshop.imexshopid
|
||||||
};
|
};
|
||||||
sendMessage(newMessage);
|
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(
|
setSelectedMedia(
|
||||||
selectedMedia.map((i) => {
|
selectedMedia.map((i) => {
|
||||||
return { ...i, isSelected: false };
|
return { ...i, isSelected: false };
|
||||||
|
|||||||
@@ -33,22 +33,62 @@ export function* openChatByPhone({ payload }) {
|
|||||||
logImEXEvent("messaging_open_by_phone");
|
logImEXEvent("messaging_open_by_phone");
|
||||||
const { socket, phone_num, jobid } = payload;
|
const { socket, phone_num, jobid } = payload;
|
||||||
if (!socket || !phone_num) return;
|
if (!socket || !phone_num) return;
|
||||||
|
|
||||||
const p = parsePhoneNumber(phone_num, "CA");
|
const p = parsePhoneNumber(phone_num, "CA");
|
||||||
const bodyshop = yield select(selectBodyshop);
|
const bodyshop = yield select(selectBodyshop);
|
||||||
|
|
||||||
try {
|
try {
|
||||||
|
// Fetch conversations including archived ones
|
||||||
const {
|
const {
|
||||||
data: { conversations }
|
data: { conversations }
|
||||||
} = yield client.query({
|
} = yield client.query({
|
||||||
query: CONVERSATION_ID_BY_PHONE,
|
query: CONVERSATION_ID_BY_PHONE,
|
||||||
variables: { phone: p.number },
|
variables: { phone: p.number },
|
||||||
// THIS NEEDS TO REMAIN NO CACHE, IT CHECKS FOR NEW MESSAGES FOR SYNC
|
fetchPolicy: "no-cache" // Ensure the query always gets the latest data
|
||||||
fetchPolicy: "no-cache"
|
|
||||||
});
|
});
|
||||||
|
|
||||||
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
|
// No conversation exists, create a new one
|
||||||
const {
|
const {
|
||||||
data: {
|
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", {
|
socket.emit("conversation-modified", {
|
||||||
bodyshopId: bodyshop.id,
|
bodyshopId: bodyshop.id,
|
||||||
type: "conversation-created",
|
type: "conversation-created",
|
||||||
@@ -78,45 +118,6 @@ export function* openChatByPhone({ payload }) {
|
|||||||
|
|
||||||
// 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 {
|
|
||||||
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) {
|
} catch (error) {
|
||||||
console.error("Error in openChatByPhone saga.", error);
|
console.error("Error in openChatByPhone saga.", error);
|
||||||
|
|||||||
@@ -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("conversation-modified", conversationModified);
|
||||||
socket.on("join-bodyshop-conversation", joinConversationRoom);
|
socket.on("join-bodyshop-conversation", joinConversationRoom);
|
||||||
socket.on("leave-bodyshop-conversation", leaveConversationRoom);
|
socket.on("leave-bodyshop-conversation", leaveConversationRoom);
|
||||||
|
|||||||
Reference in New Issue
Block a user