feature/IO-3000-messaging-sockets-migrations2 -

- Checkpoint,

Signed-off-by: Dave Richer <dave@imexsystems.ca>
This commit is contained in:
Dave Richer
2024-11-21 17:35:04 -08:00
parent cd592b671c
commit d2e1b32557
10 changed files with 272 additions and 73 deletions

View File

@@ -2,7 +2,13 @@ import axios from "axios";
import parsePhoneNumber from "libphonenumber-js";
import { all, call, put, select, takeLatest } from "redux-saga/effects";
import { logImEXEvent } from "../../firebase/firebase.utils";
import { CONVERSATION_ID_BY_PHONE, CREATE_CONVERSATION } from "../../graphql/conversations.queries";
import {
CONVERSATION_ID_BY_PHONE,
CONVERSATION_LIST_QUERY,
CREATE_CONVERSATION,
GET_CONVERSATION_DETAILS,
TOGGLE_CONVERSATION_ARCHIVE
} from "../../graphql/conversations.queries";
import { INSERT_CONVERSATION_TAG } from "../../graphql/job-conversations.queries";
import client from "../../utils/GraphQLClient";
import { selectBodyshop } from "../user/user.selectors";
@@ -27,23 +33,24 @@ export function* onOpenChatByPhone() {
export function* openChatByPhone({ payload }) {
logImEXEvent("messaging_open_by_phone");
const { phone_num, jobid } = payload;
const { socket, phone_num, jobid } = payload;
const p = parsePhoneNumber(phone_num, "CA");
const bodyshop = yield select(selectBodyshop);
try {
const {
data: { conversations }
} = yield client.query({
query: CONVERSATION_ID_BY_PHONE,
variables: { phone: p.number },
fetchPolicy: 'no-cache'
fetchPolicy: "no-cache"
});
if (conversations.length === 0) {
// No conversation exists, create a new one
const {
data: {
insert_conversations: { returning: newConversationsId }
insert_conversations: { returning: newConversations }
}
} = yield client.mutate({
mutation: CREATE_CONVERSATION,
@@ -57,26 +64,107 @@ export function* openChatByPhone({ payload }) {
]
}
});
yield put(setSelectedConversation(newConversationsId[0].id));
} else if (conversations.length === 1) {
//got the ID. Open it.
yield put(setSelectedConversation(conversations[0].id));
//Check to see if this job ID is already a child of it. If not add the tag.
if (jobid && !conversations[0].job_conversations.find((jc) => jc.jobid === jobid))
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
});
}
// Set the newly created conversation as selected
yield put(setSelectedConversation(createdConversation.id));
} else if (conversations.length === 1) {
const conversation = conversations[0];
if (conversation.archived) {
// Conversation is archived, unarchive it in the DB
const {
data: { update_conversations_by_pk: updatedConversation }
} = yield client.mutate({
mutation: TOGGLE_CONVERSATION_ARCHIVE,
variables: {
id: conversation.id,
archived: false
}
});
if (socket) {
socket.emit("conversation-modified", {
type: "conversation-unarchived",
conversationId: updatedConversation.id,
bodyshopId: bodyshop.id,
archived: false
});
}
// 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
});
}
// Open the conversation
yield put(setSelectedConversation(conversation.id));
// Check and add job tag if needed
if (jobid && !conversation.job_conversations.find((jc) => jc.jobid === jobid)) {
yield client.mutate({
mutation: INSERT_CONVERSATION_TAG,
variables: {
conversationId: conversations[0].id,
conversationId: conversation.id,
jobId: jobid
}
});
}
} else {
console.log("ERROR: Multiple conversations found. ");
// Multiple conversations found
console.error("ERROR: Multiple conversations found.");
yield put(setSelectedConversation(null));
}
} catch (error) {
console.log("Error in sendMessage saga.", error);
console.error("Error in openChatByPhone saga.", error);
}
}