feature/IO-3000-Migrate-MSG-to-Sockets - Progress Checkpoint
Signed-off-by: Dave Richer <dave@imexsystems.ca>
This commit is contained in:
@@ -87,6 +87,21 @@ mutation RECEIVE_MESSAGE($msg: [messages_insert_input!]!) {
|
||||
updated_at
|
||||
unreadcnt
|
||||
phone_num
|
||||
label
|
||||
job_conversations {
|
||||
job {
|
||||
id
|
||||
ro_number
|
||||
ownr_fn
|
||||
ownr_ln
|
||||
ownr_co_nm
|
||||
}
|
||||
}
|
||||
messages_aggregate {
|
||||
aggregate {
|
||||
count
|
||||
}
|
||||
}
|
||||
}
|
||||
conversationid
|
||||
created_at
|
||||
|
||||
@@ -63,18 +63,28 @@ exports.receive = async (req, res) => {
|
||||
// Insert new conversation and message
|
||||
const insertresp = await client.request(queries.RECEIVE_MESSAGE, { msg: newMessage });
|
||||
|
||||
const createdConversation = insertresp.insert_conversations.returning[0];
|
||||
const message = insertresp.insert_messages.returning[0];
|
||||
// Safely access conversation and message
|
||||
const createdConversation = insertresp?.insert_messages?.returning?.[0]?.conversation || null;
|
||||
const message = insertresp?.insert_messages?.returning?.[0] || {
|
||||
text: "",
|
||||
image: false,
|
||||
image_path: []
|
||||
};
|
||||
|
||||
if (!createdConversation) {
|
||||
throw new Error("Conversation data is missing from the response.");
|
||||
}
|
||||
|
||||
// Emit new conversation event
|
||||
ioRedis.to(getBodyshopRoom(response.bodyshops[0].id)).emit("new-conversation", {
|
||||
conversation: createdConversation,
|
||||
message: {
|
||||
...message,
|
||||
text: message.text || "",
|
||||
image: message.image || false,
|
||||
image_path: message.image_path || []
|
||||
}
|
||||
conversation: {
|
||||
...createdConversation,
|
||||
messages_aggregate: {
|
||||
aggregate: {
|
||||
count: 1 // Adjust dynamically based on your logic or default to 0
|
||||
}
|
||||
}
|
||||
},
|
||||
message
|
||||
});
|
||||
|
||||
logger.log("sms-inbound-success", "DEBUG", "api", null, {
|
||||
@@ -99,6 +109,7 @@ exports.receive = async (req, res) => {
|
||||
}
|
||||
} else if (response.bodyshops[0].conversations.length === 1) {
|
||||
// Add to the existing conversation
|
||||
// conversation UPDATED
|
||||
newMessage.conversationid = response.bodyshops[0].conversations[0].id;
|
||||
} else {
|
||||
// Duplicate phone error
|
||||
|
||||
@@ -140,11 +140,6 @@ const redisSocketEvents = ({
|
||||
};
|
||||
// Messaging Events
|
||||
const registerMessagingEvents = (socket) => {
|
||||
const broadcastNewMessage = async (message) => {
|
||||
const room = `conversation-${message.conversationId}`;
|
||||
io.to(room).emit("new-message", message);
|
||||
};
|
||||
|
||||
const openMessaging = async (bodyshopUUID) => {
|
||||
try {
|
||||
const conversations = await client.request(GET_CONVERSATIONS, { bodyshopId: bodyshopUUID });
|
||||
@@ -282,6 +277,19 @@ const redisSocketEvents = ({
|
||||
}
|
||||
};
|
||||
|
||||
const leaveConversation = (conversationId) => {
|
||||
try {
|
||||
const room = `conversation-${conversationId}`;
|
||||
socket.leave(room);
|
||||
|
||||
// Optionally notify the client
|
||||
socket.emit("conversation-left", { conversationId });
|
||||
} catch (error) {
|
||||
socket.emit("error", { message: "Failed to leave conversation" });
|
||||
}
|
||||
};
|
||||
|
||||
socket.on("leave-conversation", leaveConversation);
|
||||
socket.on("send-message", sendMessage);
|
||||
socket.on("mark-as-read", markAsRead);
|
||||
socket.on("join-conversation", joinConversation);
|
||||
|
||||
Reference in New Issue
Block a user