feature/IO-3000-Migrate-MSG-to-Sockets - Progress Checkpoint

Signed-off-by: Dave Richer <dave@imexsystems.ca>
This commit is contained in:
Dave Richer
2024-11-18 21:06:56 -08:00
parent 6e6c44f2b9
commit 03ae7bb160
6 changed files with 64 additions and 30 deletions

View File

@@ -1,6 +1,5 @@
const { admin } = require("../firebase/firebase-handler");
const { MARK_MESSAGES_AS_READ, GET_CONVERSATIONS, GET_CONVERSATION_DETAILS } = require("../graphql-client/queries");
const logger = require("../utils/logger");
const { phone } = require("phone");
const { client: gqlClient } = require("../graphql-client/graphql-client");
const queries = require("../graphql-client/queries");
@@ -151,7 +150,6 @@ const redisSocketEvents = ({
const conversations = await client.request(GET_CONVERSATIONS, { bodyshopId: bodyshopUUID });
socket.emit("messaging-list", { conversations });
} catch (error) {
console.dir(error);
logger.log("error", "Failed to fetch conversations", error);
socket.emit("error", { message: "Failed to fetch conversations" });
}
@@ -171,19 +169,35 @@ const redisSocketEvents = ({
}
};
const markAsRead = async ({ conversationId, userId }) => {
const markAsRead = async ({ conversationId, userId, imexshopid, bodyshopId }) => {
try {
await client.request(MARK_MESSAGES_AS_READ, { conversationId, userId });
io.to(`conversation-${conversationId}`).emit("read-updated", { conversationId });
// Fetch the updated unread count for this conversation
const conversations = await client.request(GET_CONVERSATIONS, { bodyshopId });
// Emit the updated unread count to all clients
const room = `conversation-${conversationId}`;
io.to(room).emit("messaging-list", { conversations });
admin.messaging().send({
topic: `${imexshopid}-messaging`,
data: {
type: "messaging-mark-conversation-read",
conversationid: conversationId || ""
}
});
} catch (error) {
logger.log("error", "Failed to mark messages as read", error);
logger.log("Failed to mark messages as read", "error", null, null, {
message: error.message,
stack: error.stack
});
socket.emit("error", { message: "Failed to mark messages as read" });
}
};
const sendMessage = (data) => {
const { to, messagingServiceSid, body, conversationid, selectedMedia, imexshopid, user } = data;
console.dir({ data });
logger.log("sms-outbound", "DEBUG", user.email, null, {
messagingServiceSid: messagingServiceSid,
to: phone(to).phoneNumber,
@@ -217,7 +231,6 @@ const redisSocketEvents = ({
gqlClient
.request(queries.INSERT_MESSAGE, { msg: newMessage, conversationid })
.then((r2) => {
//console.log("Responding GQL Message ID", JSON.stringify(r2));
logger.log("sms-outbound-success", "DEBUG", user.email, null, {
msid: message.sid,
conversationid