BOD-14 Backend server work + sending of messages WIP for front end

This commit is contained in:
Patrick Fic
2020-03-25 15:50:46 -07:00
parent 5b5ffe21cd
commit 84a5820d8d
32 changed files with 892 additions and 274 deletions

View File

@@ -0,0 +1,9 @@
const GraphQLClient = require("graphql-request").GraphQLClient;
require("dotenv").config();
//TODO May need to use a different client that includes caching of resources.
exports.client = new GraphQLClient(process.env.GRAPHQL_ENDPOINT, {
headers: {
"x-hasura-admin-secret": process.env.HASURA_ADMIN_SECRET
}
});

View File

@@ -0,0 +1,23 @@
exports.FIND_BODYSHOP_BY_MESSAGING_SERVICE_SID = `
query FIND_BODYSHOP_BY_MESSAGING_SERVICE_SID(
$mssid: String!
$phone: String!
) {
bodyshops(where: { messagingservicesid: { _eq: $mssid } }) {
id
conversations(where: { phone_num: { _eq: $phone } }) {
id
}
}
}
`;
exports.INSERT_MESSAGE = `
mutation INSERT_MESSAGE($msg: [messages_insert_input!]!) {
insert_messages(objects: $msg) {
returning {
id
}
}
}
`;

82
server/sms/receive.js Normal file
View File

@@ -0,0 +1,82 @@
require("dotenv").config();
const client = require("../graphql-client/graphql-client").client;
const queries = require("../graphql-client/queries");
const phone = require("phone");
exports.receive = (req, res) => {
//Perform request validation
if (!req.body || !req.body.MessagingServiceSid || !req.body.SmsMessageSid) {
res.sendStatus(400);
} else {
client
.request(queries.FIND_BODYSHOP_BY_MESSAGING_SERVICE_SID, {
mssid: req.body.MessagingServiceSid,
phone: phone(req.body.From)[0]
})
.then(response => {
//TODO Add logic for handling MMS.
let newMessage = { msid: req.body.SmsMessageSid, text: req.body.Body };
if (response.bodyshops[0]) {
//Found a bodyshop - should always happen.
if (response.bodyshops[0].conversations.length === 0) {
//No conversation Found, create one.
console.log("[SMS Receive] No conversation found. Creating one.");
newMessage.conversation = {
data: {
bodyshopid: response.bodyshops[0].id,
phone_num: phone(req.body.From)[0]
}
};
} else if (response.bodyshops[0].conversations.length === 1) {
//Just add it to the conversation
console.log("[SMS Receive] Conversation found. Added ID.");
newMessage.conversationid =
response.bodyshops[0].conversations[0].id;
} else {
//We should never get here.
console.log(
"Massive Error: Duplicate Phone Numbers for MSSID: " +
req.body.MessagingServiceSid
);
}
client
.request(queries.INSERT_MESSAGE, { msg: newMessage })
.then(r2 => {
res.sendStatus(200);
})
.catch(e2 => {
console.log("e2", e2);
res.json(e2);
});
}
})
.catch(e1 => {
console.log("e1", e1);
res.json(e1);
});
}
};
// const sampleMessage: {
// "ToCountry": "CA",
// "ToState": "BC",
// "SmsMessageSid": "SMad7bddaf3454c0904999d6018b1e8f49",
// "NumMedia": "0",
// "ToCity": "Vancouver",
// "FromZip": "",
// "SmsSid": "SMad7bddaf3454c0904999d6018b1e8f49",
// "FromState": "BC",
// "SmsStatus": "received",
// "FromCity": "VANCOUVER",
// "Body": "Hi",
// "FromCountry": "CA",
// "To": "+16043301606",
// "MessagingServiceSid": "MG6e259e2add04ffa0d0aa355038670ee1",
// "ToZip": "",
// "NumSegments": "1",
// "MessageSid": "SMad7bddaf3454c0904999d6018b1e8f49",
// "AccountSid": "AC6c09d337d6b9c68ab6488c2052bd457c",
// "From": "+16049992002",
// "ApiVersion": "2010-04-01"
// }

41
server/sms/send.js Normal file
View File

@@ -0,0 +1,41 @@
require("dotenv").config();
const twilio = require("twilio");
const phone = require("phone");
const client = twilio(
process.env.TWILIO_AUTH_TOKEN,
process.env.TWILIO_AUTH_KEY
);
exports.send = (req, res) => {
console.log("Sending an SMS!");
const { to, messagingServiceSid, body, conversationid } = req.body;
if (!!to && !!messagingServiceSid && !!body && !!conversationid) {
client.messages
.create({
body: body,
messagingServiceSid: messagingServiceSid,
to: phone(to)[0]
})
.then(message => {
let newMessage = {
msid: message.sid,
text: body,
conversationid,
isoutbound: true
};
client
.request(queries.INSERT_MESSAGE, { msg: newMessage })
.then(r2 => {
res.sendStatus(200);
})
.catch(e2 => {
res.json({ success: false, message: e2 });
});
})
.catch(error => res.json({ success: false, message: error }));
} else {
res.json({ success: false, message: "Missing required parameter(s)." });
}
};

23
server/sms/status.js Normal file
View File

@@ -0,0 +1,23 @@
require("dotenv").config();
const client = require("../graphql-client/graphql-client").client;
const queries = require("../graphql-client/queries");
const phone = require("phone");
exports.status = (req, res) => {
//Perform request validation
console.log("Inbound Status Update: ", JSON.stringify(req.body));
res.sendStatus(200);
};
// Inbound Sample
// {
// "SmsSid": "SM5205ea340e06437799d9345e7283457c",
// "SmsStatus": "queued",
// "MessageStatus": "queued",
// "To": "+16049992002",
// "MessagingServiceSid": "MG6e259e2add04ffa0d0aa355038670ee1",
// "MessageSid": "SM5205ea340e06437799d9345e7283457c",
// "AccountSid": "AC6c09d337d6b9c68ab6488c2052bd457c",
// "From": "+16043301606",
// "ApiVersion": "2010-04-01"
// }