feature/IO-3000-messaging-sockets-migrations2 - Everything but tagging and labels works
Signed-off-by: Dave Richer <dave@imexsystems.ca>
This commit is contained in:
@@ -5,29 +5,31 @@ require("dotenv").config({
|
||||
|
||||
const client = require("../graphql-client/graphql-client").client;
|
||||
const queries = require("../graphql-client/queries");
|
||||
const { phone } = require("phone");
|
||||
const logger = require("../utils/logger");
|
||||
const { admin } = require("../firebase/firebase-handler");
|
||||
|
||||
exports.status = (req, res) => {
|
||||
exports.status = async (req, res) => {
|
||||
const { SmsSid, SmsStatus } = req.body;
|
||||
const {
|
||||
ioRedis,
|
||||
ioHelpers: { getBodyshopRoom, getBodyshopConversationRoom }
|
||||
} = req;
|
||||
client
|
||||
.request(queries.UPDATE_MESSAGE_STATUS, {
|
||||
|
||||
try {
|
||||
// Update message status in the database
|
||||
const response = await client.request(queries.UPDATE_MESSAGE_STATUS, {
|
||||
msid: SmsSid,
|
||||
fields: { status: SmsStatus }
|
||||
})
|
||||
.then((response) => {
|
||||
});
|
||||
|
||||
const message = response.update_messages.returning[0];
|
||||
|
||||
if (message) {
|
||||
logger.log("sms-status-update", "DEBUG", "api", null, {
|
||||
msid: SmsSid,
|
||||
fields: { status: SmsStatus }
|
||||
});
|
||||
// TODO Verify
|
||||
const message = response.update_messages.returning[0];
|
||||
|
||||
// Emit WebSocket event to notify the change in message status
|
||||
const conversationRoom = getBodyshopConversationRoom({
|
||||
bodyshopId: message.conversation.bodyshopid,
|
||||
conversationId: message.conversationid
|
||||
@@ -36,69 +38,70 @@ exports.status = (req, res) => {
|
||||
ioRedis.to(conversationRoom).emit("message-changed", {
|
||||
message
|
||||
});
|
||||
})
|
||||
.catch((error) => {
|
||||
logger.log("sms-status-update-error", "ERROR", "api", null, {
|
||||
} else {
|
||||
logger.log("sms-status-update-warning", "WARN", "api", null, {
|
||||
msid: SmsSid,
|
||||
fields: { status: SmsStatus },
|
||||
error
|
||||
warning: "No message returned from the database update."
|
||||
});
|
||||
}
|
||||
res.sendStatus(200);
|
||||
} catch (error) {
|
||||
logger.log("sms-status-update-error", "ERROR", "api", null, {
|
||||
msid: SmsSid,
|
||||
fields: { status: SmsStatus },
|
||||
error
|
||||
});
|
||||
res.sendStatus(200);
|
||||
res.status(500).json({ error: "Failed to update message status." });
|
||||
}
|
||||
};
|
||||
|
||||
exports.markConversationRead = async (req, res) => {
|
||||
const { conversationid, imexshopid } = req.body;
|
||||
const { conversationid, imexshopid, bodyshopid } = req.body;
|
||||
const {
|
||||
ioRedis,
|
||||
ioHelpers: { getBodyshopRoom, getBodyshopConversationRoom }
|
||||
} = req;
|
||||
|
||||
//Server side, mark the conversation as read
|
||||
try {
|
||||
// Mark messages in the conversation as read
|
||||
const response = await client.request(queries.MARK_MESSAGES_AS_READ, {
|
||||
conversationId: conversationid
|
||||
});
|
||||
|
||||
//TODO: Convert this to run on server side. Stolen from chat-conversation.container.jsx
|
||||
// const [markConversationRead] = useMutation(MARK_MESSAGES_AS_READ_BY_CONVERSATION, {
|
||||
// variables: { conversationId: selectedConversation },
|
||||
// refetchQueries: ["UNREAD_CONVERSATION_COUNT"],
|
||||
//
|
||||
// update(cache) {
|
||||
// cache.modify({
|
||||
// id: cache.identify({
|
||||
// __typename: "conversations",
|
||||
// id: selectedConversation
|
||||
// }),
|
||||
// fields: {
|
||||
// messages_aggregate(cached) {
|
||||
// return { aggregate: { count: 0 } };
|
||||
// }
|
||||
// }
|
||||
// });
|
||||
// }
|
||||
// });
|
||||
const updatedMessages = response.update_messages.affected_rows;
|
||||
|
||||
const broadcastRoom = getBodyshopRoom(r2.insert_messages.returning[0].conversation.bodyshop.id);
|
||||
logger.log("conversation-mark-read", "DEBUG", "api", null, {
|
||||
conversationid,
|
||||
imexshopid,
|
||||
bodyshopid,
|
||||
updatedMessages
|
||||
});
|
||||
|
||||
ioRedis.to(broadcastRoom).emit("conversation-changed", {
|
||||
//type: "conversation-marked-unread" //TODO: Flush out what this looks like.
|
||||
// isoutbound: true,
|
||||
// conversationId: conversationid,
|
||||
// updated_at: r2.insert_messages.returning[0].updated_at,
|
||||
// msid: message.sid,
|
||||
// summary: true
|
||||
});
|
||||
const broadcastRoom = getBodyshopRoom(bodyshopid);
|
||||
|
||||
res.send(200);
|
||||
const conversationRoom = getBodyshopConversationRoom({
|
||||
bodyshopId: bodyshopid,
|
||||
conversationId: conversationid
|
||||
});
|
||||
|
||||
ioRedis.to(broadcastRoom).emit("conversation-changed", {
|
||||
type: "conversation-marked-read",
|
||||
conversationId: conversationid
|
||||
});
|
||||
|
||||
ioRedis.to(conversationRoom).emit("message-changed", {
|
||||
type: "all-messages-marked-read",
|
||||
conversationId: conversationid
|
||||
});
|
||||
|
||||
res.status(200).json({ success: true, message: "Conversation marked as read." });
|
||||
} catch (error) {
|
||||
logger.log("conversation-mark-read-error", "ERROR", "api", null, {
|
||||
conversationid,
|
||||
imexshopid,
|
||||
error
|
||||
});
|
||||
res.status(500).json({ error: "Failed to mark conversation as read." });
|
||||
}
|
||||
};
|
||||
|
||||
// Inbound Sample
|
||||
// {
|
||||
// "SmsSid": "SM5205ea340e06437799d9345e7283457c",
|
||||
// "SmsStatus": "queued",
|
||||
// "MessageStatus": "queued",
|
||||
// "To": "+16049992002",
|
||||
// "MessagingServiceSid": "MG6e259e2add04ffa0d0aa355038670ee1",
|
||||
// "MessageSid": "SM5205ea340e06437799d9345e7283457c",
|
||||
// "AccountSid": "AC6c09d337d6b9c68ab6488c2052bd457c",
|
||||
// "From": "+16043301606",
|
||||
// "ApiVersion": "2010-04-01"
|
||||
// }
|
||||
|
||||
Reference in New Issue
Block a user