241 lines
6.5 KiB
JavaScript
241 lines
6.5 KiB
JavaScript
const express = require("express");
|
|
const router = express.Router();
|
|
const axios = require("axios");
|
|
const url = require("url");
|
|
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.referenceId);
|
|
}
|
|
// no token -> send nothing
|
|
else {
|
|
const params = new url.URLSearchParams({
|
|
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,
|
|
});
|
|
|
|
axios
|
|
.post(process.env.EO_API_ACCESS_TOKEN_URL, params.toString(), {
|
|
headers: {
|
|
"Content-Type": "application/x-www-form-urlencoded",
|
|
},
|
|
})
|
|
.then((result) => {
|
|
// save token to session
|
|
req.session.token = result.data.access_token;
|
|
console.log(result);
|
|
|
|
query(req.query.referenceId);
|
|
})
|
|
.catch((err) => {
|
|
sendError(err, res);
|
|
});
|
|
}
|
|
|
|
function query(referenceId) {
|
|
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 === referenceId) |
|
|
contact.node.interaction.subject.includes(
|
|
`<< Ref:${referenceId} >>`
|
|
)
|
|
) {
|
|
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.totalHTHours =
|
|
(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) => {
|
|
// bin the token on error
|
|
req.session.destroy();
|
|
sendError(err, res);
|
|
});
|
|
}
|
|
});
|
|
module.exports = router;
|
|
function sendError(err, res) {
|
|
console.error(err);
|
|
let errStatus = 500;
|
|
if (err.response) errStatus = err.response.status;
|
|
res.status(errStatus).send({
|
|
errors: [
|
|
{
|
|
status: errStatus,
|
|
title: err.code,
|
|
detail: err.message,
|
|
},
|
|
],
|
|
});
|
|
}
|