refactoring to a better nodejs template and adding
better filtering using sample data
This commit is contained in:
20
src/api/routes/config.js
Normal file
20
src/api/routes/config.js
Normal file
@@ -0,0 +1,20 @@
|
||||
import { Router } from "express";
|
||||
const router = Router();
|
||||
|
||||
router.get("/", (req, res) => {
|
||||
const config = {
|
||||
"auth:": {
|
||||
username: process.env.EO_API_USERNAME,
|
||||
password: process.env.EO_API_PASSWORD ? "*******" : undefined,
|
||||
scope: process.env.EO_API_SCOPE,
|
||||
client_id: process.env.EO_API_CLIENT_ID,
|
||||
client_secret: process.env.EO_API_SECRET ? "*******" : undefined,
|
||||
},
|
||||
endpoints: {
|
||||
token: process.env.EO_API_ACCESS_TOKEN_URL,
|
||||
udg: process.env.EO_API_UDG_URL,
|
||||
},
|
||||
};
|
||||
res.send(config);
|
||||
});
|
||||
export default router;
|
||||
11
src/api/routes/index.js
Normal file
11
src/api/routes/index.js
Normal file
@@ -0,0 +1,11 @@
|
||||
import { Router } from "express";
|
||||
import config from "./config.js";
|
||||
import interactionsFlows from "./interactions-flow.js";
|
||||
import udg from "./unified-data-gateway.js";
|
||||
const router = Router();
|
||||
|
||||
router.use("/config", config);
|
||||
router.use("/interactions-flow", interactionsFlows);
|
||||
router.use("/unified-data-gateway", udg);
|
||||
|
||||
export default router;
|
||||
157
src/api/routes/interactions-flow.js
Normal file
157
src/api/routes/interactions-flow.js
Normal file
@@ -0,0 +1,157 @@
|
||||
import { Router } from "express";
|
||||
import sampleFlow from "../../utils/sampleFlow.js";
|
||||
import sampleUDG from "../../utils/sampleUDGresponse.js";
|
||||
import { logger } from "../../utils/index.js";
|
||||
const router = Router();
|
||||
|
||||
router.get("/", (req, res) => {
|
||||
const filter = req.query.filter;
|
||||
|
||||
// nodes
|
||||
var channels = [];
|
||||
var queues = [];
|
||||
var outcomes = [];
|
||||
|
||||
// links
|
||||
var channel_queue_links = {};
|
||||
var channel_outcome_links = {};
|
||||
var queue_outcome_links = {};
|
||||
|
||||
sampleUDG.data.findContactsCompletedBetween.edges.forEach((value) => {
|
||||
const hasChannel = value.node.interaction.__typename ? true : false;
|
||||
const hasQueue = value.node.queue ? true : false;
|
||||
const hasOutcome = value.node.outcome ? true : false;
|
||||
|
||||
// NODES
|
||||
if (
|
||||
hasChannel &&
|
||||
channels.indexOf(value.node.interaction.__typename) == -1
|
||||
) {
|
||||
channels.push(value.node.interaction.__typename);
|
||||
}
|
||||
|
||||
if (hasQueue) {
|
||||
const queue = JSON.stringify(value.node.queue);
|
||||
if (queues.indexOf(queue) == -1) {
|
||||
queues.push(queue);
|
||||
}
|
||||
|
||||
if (hasChannel) {
|
||||
const key =
|
||||
value.node.interaction.__typename + JSON.stringify(value.node.queue);
|
||||
|
||||
if (channel_queue_links[key]) {
|
||||
channel_queue_links[key].value++;
|
||||
} else {
|
||||
channel_queue_links[key] = {
|
||||
source: value.node.interaction.__typename,
|
||||
target: JSON.stringify(value.node.queue),
|
||||
value: 1,
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (hasOutcome) {
|
||||
value.node.outcome.edges.forEach((element) => {
|
||||
var outcome = element.node.text;
|
||||
|
||||
if (outcomes.indexOf(outcome) == -1) {
|
||||
outcomes.push(outcome);
|
||||
}
|
||||
|
||||
if (hasQueue) {
|
||||
var key = outcome+JSON.stringify(value.node.queue);
|
||||
|
||||
if (queue_outcome_links[key]) {
|
||||
queue_outcome_links[key].value++;
|
||||
} else {
|
||||
queue_outcome_links[key] = {
|
||||
source: JSON.stringify(value.node.queue),
|
||||
target: outcome,
|
||||
value: 1,
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
if (hasChannel) {
|
||||
key = outcome+value.node.interaction.__typename;
|
||||
|
||||
if (channel_outcome_links[key]) {
|
||||
channel_outcome_links[key].value++;
|
||||
} else {
|
||||
channel_outcome_links[key] = {
|
||||
source: value.node.interaction.__typename,
|
||||
target: outcome,
|
||||
value: 1,
|
||||
};
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
logger.debug(`channels: ${JSON.stringify(channels)}`);
|
||||
logger.debug(`queues: ${JSON.stringify(queues)}`);
|
||||
logger.debug(`channel_queue_links: ${JSON.stringify(channel_queue_links)}`);
|
||||
logger.debug(`outcomes: ${JSON.stringify(outcomes)}`);
|
||||
|
||||
if (req.query.useSampleData) {
|
||||
const data = sampleFlow(filter);
|
||||
res.send(data);
|
||||
return;
|
||||
}
|
||||
|
||||
var data = {
|
||||
nodes: [],
|
||||
links: [],
|
||||
};
|
||||
|
||||
if (filter.includes("Channel")) {
|
||||
channels.forEach((value) => {
|
||||
data.nodes.push({
|
||||
name: value,
|
||||
category: "Channel",
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
if (filter.includes("Queue")) {
|
||||
queues.forEach((value) => {
|
||||
data.nodes.push({
|
||||
name: value,
|
||||
category: "Queue",
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
if (filter.includes("Outcome")) {
|
||||
outcomes.forEach((value) => {
|
||||
data.nodes.push({
|
||||
name: value,
|
||||
category: "Outcome",
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
if (filter.includes("Channel") && filter.includes("Queue")) {
|
||||
Object.values(channel_queue_links).forEach((element) => {
|
||||
data.links.push(element);
|
||||
});
|
||||
}
|
||||
|
||||
if (filter.includes("Queue") && filter.includes("Outcome")) {
|
||||
Object.values(queue_outcome_links).forEach((element) => {
|
||||
data.links.push(element);
|
||||
});
|
||||
}
|
||||
|
||||
if (filter.includes("Channel") && filter.includes("Outcome") && !filter.includes("Queue")) {
|
||||
Object.values(channel_outcome_links).forEach((element) => {
|
||||
data.links.push(element);
|
||||
});
|
||||
}
|
||||
|
||||
res.send(data);
|
||||
});
|
||||
export default router;
|
||||
253
src/api/routes/unified-data-gateway.js
Normal file
253
src/api/routes/unified-data-gateway.js
Normal file
@@ -0,0 +1,253 @@
|
||||
import { Router } from "express";
|
||||
import axios from "axios";
|
||||
import { URLSearchParams } from "url";
|
||||
import { inspect } from "util";
|
||||
|
||||
const router = Router();
|
||||
|
||||
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 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");
|
||||
|
||||
const 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
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}`;
|
||||
const startTime = new Date(
|
||||
new Date().setFullYear(new Date().getFullYear() - 2)
|
||||
);
|
||||
const 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) => {
|
||||
const contacts = result.data.data.findContactsCompletedBetween.edges;
|
||||
|
||||
// Log error to console
|
||||
if (result.data.errors && result.data.errors.length > 0) {
|
||||
result.data.errors.forEach(function (error) {
|
||||
console.log("ERROR: Errors in results - " + error.message);
|
||||
});
|
||||
|
||||
// TODO: Should keep errors for filteredContacts
|
||||
result.data.errors = [];
|
||||
}
|
||||
|
||||
const filteredContacts = [];
|
||||
contacts.forEach(function (contact) {
|
||||
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
|
||||
const summary = {};
|
||||
|
||||
summary.totalCount = filteredContacts.length;
|
||||
summary.totalInboundCount = 0;
|
||||
summary.totalInboundActiveSeconds = 0;
|
||||
|
||||
if (summary.totalCount > 0) {
|
||||
summary.firstContactReceivedDate = new Date(
|
||||
filteredContacts[0].node.interaction.receivedDate
|
||||
);
|
||||
|
||||
filteredContacts.forEach(function (contact) {
|
||||
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(
|
||||
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);
|
||||
});
|
||||
}
|
||||
});
|
||||
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,
|
||||
},
|
||||
],
|
||||
});
|
||||
}
|
||||
export default router;
|
||||
Reference in New Issue
Block a user