Merge branch 'master-AIO' into feature/IO-3020-IO-3036-imex-lite-rome-lite

This commit is contained in:
Patrick Fic
2024-12-04 12:58:23 -08:00
81 changed files with 4206 additions and 3939 deletions

View File

@@ -2,7 +2,11 @@ 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,
CREATE_CONVERSATION,
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 +31,73 @@ export function* onOpenChatByPhone() {
export function* openChatByPhone({ payload }) {
logImEXEvent("messaging_open_by_phone");
const { phone_num, jobid } = payload;
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 },
fetchPolicy: 'no-cache'
fetchPolicy: "no-cache" // Ensure the query always gets the latest data
});
if (conversations.length === 0) {
// Sort conversations by `updated_at` or `created_at` and pick the last one for the given phone number
const sortedConversations = conversations
?.filter((c) => c.phone_num === p.number) // Filter to match the phone number
.sort((a, b) => new Date(a.created_at) - new Date(b.created_at)); // Sort by `updated_at`
const existingConversation = sortedConversations?.[sortedConversations.length - 1] || null;
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: {
insert_conversations: { returning: newConversationsId }
insert_conversations: { returning: newConversations }
}
} = yield client.mutate({
mutation: CREATE_CONVERSATION,
@@ -57,26 +111,21 @@ 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))
yield client.mutate({
mutation: INSERT_CONVERSATION_TAG,
variables: {
conversationId: conversations[0].id,
jobId: jobid
}
});
} else {
console.log("ERROR: Multiple conversations found. ");
yield put(setSelectedConversation(null));
const createdConversation = newConversations[0];
// Emit event for the new conversation with full details
socket.emit("conversation-modified", {
bodyshopId: bodyshop.id,
type: "conversation-created",
...createdConversation
});
// Set the newly created conversation as selected
yield put(setSelectedConversation(createdConversation.id));
}
} catch (error) {
console.log("Error in sendMessage saga.", error);
console.error("Error in openChatByPhone saga.", error);
}
}