225 lines
6.1 KiB
JavaScript
225 lines
6.1 KiB
JavaScript
const express = require("express");
|
|
const router = express.Router();
|
|
const axios = require("axios");
|
|
const qs = require("querystring");
|
|
const util = require("util");
|
|
|
|
router.get("/", (req, res) => {
|
|
// token in session -> get user data and send it back to the vue app
|
|
if (req.session.token) {
|
|
query(req.query.threadId);
|
|
}
|
|
// no token -> send nothing
|
|
else {
|
|
axios
|
|
.post(
|
|
process.env.EO_API_ACCESS_TOKEN_URL,
|
|
qs.stringify({
|
|
grant_type: "password",
|
|
username: process.env.EO_API_USERNAME,
|
|
password: process.env.EO_API_PASSWORD,
|
|
scope: process.env.EO_API_SCOPE,
|
|
client_id: process.env.EO_API_CLIENT_ID,
|
|
client_secret: process.env.EO_API_SECRET,
|
|
}),
|
|
{
|
|
headers: {
|
|
"Content-Type": "application/x-www-form-urlencoded",
|
|
},
|
|
}
|
|
)
|
|
.then((result) => {
|
|
// save token to session
|
|
req.session.token = result.data.access_token;
|
|
console.log(result);
|
|
//redirect to Vue app
|
|
query(req.query.threadId);
|
|
})
|
|
.catch((err) => {
|
|
console.error(err);
|
|
});
|
|
}
|
|
|
|
function query(threadId) {
|
|
console.log("Executing Query");
|
|
|
|
var query = `query ($startTime: DateTime, $endTime: DateTime) {
|
|
findContactsCompletedBetween(startTime: $startTime, endTime: $endTime, filter: {interactionTypes : EMAIL}) {
|
|
|
|
totalCount
|
|
edges {
|
|
node {
|
|
systemId
|
|
startTime
|
|
endTime
|
|
direction
|
|
handledBy {
|
|
username
|
|
firstName
|
|
lastName
|
|
nickname
|
|
orgScope
|
|
}
|
|
activeDuration
|
|
notes {
|
|
totalCount
|
|
edges {
|
|
node {
|
|
text
|
|
}
|
|
}
|
|
}
|
|
interaction {
|
|
systemId
|
|
locale
|
|
__typename
|
|
... on Email {
|
|
messageId
|
|
threadId
|
|
sentDate
|
|
receivedDate
|
|
subject
|
|
fromAddress
|
|
toAddresses
|
|
ccAddresses
|
|
bccAddresses
|
|
detectedLanguage
|
|
mailboxName
|
|
attachmentCount
|
|
isDuplicate
|
|
}
|
|
}
|
|
outcome {
|
|
totalCount
|
|
edges {
|
|
node {
|
|
text
|
|
isActive
|
|
isVisible
|
|
}
|
|
}
|
|
}
|
|
customer {
|
|
totalCount
|
|
edges {
|
|
node {
|
|
ref
|
|
firstName
|
|
lastName
|
|
}
|
|
}
|
|
}
|
|
queue {
|
|
name
|
|
orgScope
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}`;
|
|
var startTime = new Date(
|
|
new Date().setFullYear(new Date().getFullYear() - 1)
|
|
);
|
|
var endTime = new Date(new Date().setHours(new Date().getHours() - 1));
|
|
|
|
axios
|
|
.post(
|
|
process.env.EO_API_UDG_URL,
|
|
JSON.stringify({
|
|
query,
|
|
variables: { startTime, endTime },
|
|
}),
|
|
{
|
|
headers: {
|
|
Authorization: `OIDC_id_token ${req.session.token}`,
|
|
"Content-Type": "application/json",
|
|
},
|
|
}
|
|
)
|
|
.then((result) => {
|
|
var contacts = result.data.data.findContactsCompletedBetween.edges;
|
|
var filteredContacts = [];
|
|
contacts.forEach(function (contact, i) {
|
|
if (contact.node.interaction.__typename === "Email") {
|
|
// threadId if Reference in Subject line will do
|
|
if (
|
|
(contact.node.interaction.threadId === threadId) |
|
|
contact.node.interaction.subject.includes(`<< Ref:${threadId} >>`)
|
|
) {
|
|
filteredContacts.push(contact);
|
|
}
|
|
}
|
|
});
|
|
|
|
result.data.data.findContactsCompletedBetween.edges = filteredContacts;
|
|
result.data.data.findContactsCompletedBetween.totalCount =
|
|
filteredContacts.length;
|
|
|
|
// Summary Values
|
|
var summary = {};
|
|
|
|
summary.totalCount = filteredContacts.length;
|
|
summary.totalInboundCount = 0;
|
|
summary.totalInboundActiveSeconds = 0;
|
|
|
|
summary.firstContactReceivedDate = new Date(
|
|
filteredContacts[0].node.interaction.receivedDate
|
|
);
|
|
|
|
filteredContacts.forEach(function (contact, i) {
|
|
if (contact.node.direction === "INBOUND") {
|
|
summary.totalInboundCount++;
|
|
if (!summary.firstInboundContactStartDate) {
|
|
summary.firstInboundContactStartDate = new Date(
|
|
contact.node.startTime
|
|
);
|
|
|
|
summary.firstContactReceivedDate = new Date(
|
|
contact.node.interaction.receivedDate
|
|
);
|
|
}
|
|
summary.totalInboundActiveSeconds += contact.node.activeDuration;
|
|
}
|
|
});
|
|
|
|
// TODO: Because of overlapping contacts, we may need to calculate max instead of last.
|
|
summary.lastContactEndTime = new Date(
|
|
filteredContacts[filteredContacts.length - 1].node.endTime
|
|
);
|
|
|
|
summary.totalHTDays =
|
|
(summary.lastContactEndTime.getTime() -
|
|
summary.firstContactReceivedDate.getTime()) /
|
|
(1000 * 3600);
|
|
summary.activeHTMinutes =
|
|
(summary.lastContactEndTime.getTime() -
|
|
summary.firstInboundContactStartDate.getTime()) /
|
|
(1000 * 60);
|
|
|
|
result.data.data.summary = summary;
|
|
|
|
if (result.data) {
|
|
console.log(
|
|
util.inspect(summary, {
|
|
showHidden: false,
|
|
depth: null,
|
|
colors: true,
|
|
})
|
|
);
|
|
|
|
res.send(result.data);
|
|
}
|
|
|
|
// expired token -> send nothing
|
|
else {
|
|
req.session.destroy();
|
|
res.send({});
|
|
}
|
|
})
|
|
.catch((err) => {
|
|
console.log(err);
|
|
});
|
|
}
|
|
});
|
|
module.exports = router;
|