feature/IO-3000-messaging-sockets-migration2 - Final Modifications
Signed-off-by: Dave Richer <dave@imexsystems.ca>
This commit is contained in:
@@ -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);
|
||||
|
||||
Reference in New Issue
Block a user