IO-1438 Resolve inbound conversation creation.
This commit is contained in:
@@ -20,6 +20,28 @@ mutation UNARCHIVE_CONVERSATION($id: uuid!) {
|
|||||||
}
|
}
|
||||||
`;
|
`;
|
||||||
|
|
||||||
|
exports.RECEIVE_MESSAGE = `
|
||||||
|
mutation RECEIVE_MESSAGE($msg: [messages_insert_input!]!) {
|
||||||
|
|
||||||
|
insert_messages(objects: $msg) {
|
||||||
|
returning {
|
||||||
|
conversation {
|
||||||
|
id
|
||||||
|
archived
|
||||||
|
bodyshop {
|
||||||
|
associations(where: {active: {_eq: true}}) {
|
||||||
|
user {
|
||||||
|
fcmtokens
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
`;
|
||||||
|
|
||||||
exports.INSERT_MESSAGE = `
|
exports.INSERT_MESSAGE = `
|
||||||
mutation INSERT_MESSAGE($msg: [messages_insert_input!]!, $conversationid: uuid!) {
|
mutation INSERT_MESSAGE($msg: [messages_insert_input!]!, $conversationid: uuid!) {
|
||||||
update_conversations_by_pk(pk_columns: {id: $conversationid}, _set: {archived: false}) {
|
update_conversations_by_pk(pk_columns: {id: $conversationid}, _set: {archived: false}) {
|
||||||
|
|||||||
@@ -11,7 +11,7 @@ const queries = require("../graphql-client/queries");
|
|||||||
const { phone } = require("phone");
|
const { phone } = require("phone");
|
||||||
const admin = require("../firebase/firebase-handler").admin;
|
const admin = require("../firebase/firebase-handler").admin;
|
||||||
const logger = require("../utils/logger");
|
const logger = require("../utils/logger");
|
||||||
exports.receive = (req, res) => {
|
exports.receive = async (req, res) => {
|
||||||
//Perform request validation
|
//Perform request validation
|
||||||
|
|
||||||
logger.log("sms-inbound", "DEBUG", "api", null, {
|
logger.log("sms-inbound", "DEBUG", "api", null, {
|
||||||
@@ -36,114 +36,80 @@ exports.receive = (req, res) => {
|
|||||||
res.status(400);
|
res.status(400);
|
||||||
res.json({ success: false, error: "Malformed Request" });
|
res.json({ success: false, error: "Malformed Request" });
|
||||||
} else {
|
} else {
|
||||||
client
|
try {
|
||||||
.request(queries.FIND_BODYSHOP_BY_MESSAGING_SERVICE_SID, {
|
const response = await client.request(
|
||||||
mssid: req.body.MessagingServiceSid,
|
queries.FIND_BODYSHOP_BY_MESSAGING_SERVICE_SID,
|
||||||
phone: phone(req.body.From).phoneNumber,
|
{
|
||||||
})
|
mssid: req.body.MessagingServiceSid,
|
||||||
.then((response) => {
|
phone: phone(req.body.From).phoneNumber,
|
||||||
let newMessage = {
|
}
|
||||||
msid: req.body.SmsMessageSid,
|
);
|
||||||
text: req.body.Body,
|
|
||||||
image: !!req.body.MediaUrl0,
|
let newMessage = {
|
||||||
image_path: generateMediaArray(req.body),
|
msid: req.body.SmsMessageSid,
|
||||||
};
|
text: req.body.Body,
|
||||||
if (response.bodyshops[0]) {
|
image: !!req.body.MediaUrl0,
|
||||||
//Found a bodyshop - should always happen.
|
image_path: generateMediaArray(req.body),
|
||||||
if (response.bodyshops[0].conversations.length === 0) {
|
};
|
||||||
//No conversation Found, create one.
|
if (response.bodyshops[0]) {
|
||||||
console.log("[SMS Receive] No conversation found. Creating one.");
|
//Found a bodyshop - should always happen.
|
||||||
newMessage.conversation = {
|
if (response.bodyshops[0].conversations.length === 0) {
|
||||||
data: {
|
//No conversation Found, create one.
|
||||||
bodyshopid: response.bodyshops[0].id,
|
console.log("[SMS Receive] No conversation found. Creating one.");
|
||||||
phone_num: phone(req.body.From).phoneNumber,
|
newMessage.conversation = {
|
||||||
},
|
data: {
|
||||||
};
|
bodyshopid: response.bodyshops[0].id,
|
||||||
} else if (response.bodyshops[0].conversations.length === 1) {
|
phone_num: phone(req.body.From).phoneNumber,
|
||||||
//Just add it to the conversation
|
},
|
||||||
console.log("[SMS Receive] Conversation found. Added ID.");
|
};
|
||||||
newMessage.conversationid =
|
} else if (response.bodyshops[0].conversations.length === 1) {
|
||||||
response.bodyshops[0].conversations[0].id;
|
//Just add it to the conversation
|
||||||
} else {
|
console.log("[SMS Receive] Conversation found. Added ID.");
|
||||||
//We should never get here.
|
newMessage.conversationid = response.bodyshops[0].conversations[0].id;
|
||||||
logger.log("sms-inbound-error", "ERROR", "api", null, {
|
} else {
|
||||||
msid: req.body.SmsMessageSid,
|
//We should never get here.
|
||||||
text: req.body.Body,
|
logger.log("sms-inbound-error", "ERROR", "api", null, {
|
||||||
image: !!req.body.MediaUrl0,
|
msid: req.body.SmsMessageSid,
|
||||||
image_path: generateMediaArray(req.body),
|
text: req.body.Body,
|
||||||
messagingServiceSid: req.body.MessagingServiceSid,
|
image: !!req.body.MediaUrl0,
|
||||||
type: "duplicate-phone",
|
image_path: generateMediaArray(req.body),
|
||||||
|
messagingServiceSid: req.body.MessagingServiceSid,
|
||||||
|
type: "duplicate-phone",
|
||||||
|
});
|
||||||
|
}
|
||||||
|
try {
|
||||||
|
const r2 = await client.request(queries.RECEIVE_MESSAGE, {
|
||||||
|
msg: newMessage,
|
||||||
|
});
|
||||||
|
if (response.bodyshops[0].conversations[0]) {
|
||||||
|
const r3 = await client.request(queries.INSERT_MESSAGE, {
|
||||||
|
id:
|
||||||
|
response.bodyshops[0].conversations[0] &&
|
||||||
|
response.bodyshops[0].conversations[0].id,
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
client
|
logger.log("sms-inbound-success", "DEBUG", "api", null, {
|
||||||
.request(queries.INSERT_MESSAGE, {
|
newMessage,
|
||||||
msg: newMessage,
|
});
|
||||||
conversationid: response.bodyshops[0].conversations[0].id,
|
res.status(200).send("");
|
||||||
})
|
} catch (e2) {
|
||||||
.then((r2) => {
|
logger.log("sms-inbound-error", "ERROR", "api", null, {
|
||||||
logger.log("sms-inbound-success", "DEBUG", "api", null, {
|
msid: req.body.SmsMessageSid,
|
||||||
newMessage,
|
text: req.body.Body,
|
||||||
});
|
image: !!req.body.MediaUrl0,
|
||||||
res.status(200).send("");
|
image_path: generateMediaArray(req.body),
|
||||||
|
messagingServiceSid: req.body.MessagingServiceSid,
|
||||||
|
error: e2,
|
||||||
|
});
|
||||||
|
|
||||||
const arrayOfAllUserFcmTokens =
|
res.sendStatus(500).json(e2);
|
||||||
r2.insert_messages.returning[0].conversation.bodyshop.associations.map(
|
|
||||||
(a) => a.user.fcmtokens
|
|
||||||
);
|
|
||||||
const allTokens = [];
|
|
||||||
arrayOfAllUserFcmTokens.map((i) =>
|
|
||||||
Object.keys(i).map((k) => allTokens.push(k))
|
|
||||||
);
|
|
||||||
const uniqueTokens = [...new Set(allTokens)];
|
|
||||||
var message = {
|
|
||||||
notification: {
|
|
||||||
title: `SMS - ${phone(req.body.From).phoneNumber}`,
|
|
||||||
body: req.body.Body,
|
|
||||||
click_action: "TEST CLICK ACTION",
|
|
||||||
},
|
|
||||||
data: {
|
|
||||||
jobid: "1234",
|
|
||||||
title: `New SMS From ${phone(req.body.From).phoneNumber}`,
|
|
||||||
body: req.body.Body,
|
|
||||||
},
|
|
||||||
tokens: uniqueTokens,
|
|
||||||
};
|
|
||||||
|
|
||||||
// Send a message to the device corresponding to the provided
|
|
||||||
// registration token.
|
|
||||||
// admin
|
|
||||||
// .messaging()
|
|
||||||
// .sendMulticast(message)
|
|
||||||
// .then((response) => {
|
|
||||||
// // Response is a message ID string.
|
|
||||||
// console.log(
|
|
||||||
// "[SMS Receive] Successfully sent FCM Broadcast.:"
|
|
||||||
// //JSON.stringify(response)
|
|
||||||
// );
|
|
||||||
// })
|
|
||||||
// .catch((error) => {
|
|
||||||
// console.log("Error sending message:", error);
|
|
||||||
// });
|
|
||||||
})
|
|
||||||
.catch((e2) => {
|
|
||||||
logger.log("sms-inbound-error", "ERROR", "api", null, {
|
|
||||||
msid: req.body.SmsMessageSid,
|
|
||||||
text: req.body.Body,
|
|
||||||
image: !!req.body.MediaUrl0,
|
|
||||||
image_path: generateMediaArray(req.body),
|
|
||||||
messagingServiceSid: req.body.MessagingServiceSid,
|
|
||||||
error: e2,
|
|
||||||
});
|
|
||||||
|
|
||||||
res.sendStatus(500).json(e2);
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
})
|
}
|
||||||
.catch((e1) => {
|
} catch (e1) {
|
||||||
console.log("e1", e1);
|
console.log("e1", e1);
|
||||||
res.sendStatus(500).json(e1);
|
res.sendStatus(500).json(e1);
|
||||||
});
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user