From 4650a4d5a3794b412660e1354bd863f88a8cf36f Mon Sep 17 00:00:00 2001 From: Peter Morton Date: Sun, 25 Jun 2023 17:56:53 -0500 Subject: [PATCH] Error logging, go back 2 years and 0 contact handling --- routes/unified-data-gateway.js | 72 ++++++++++++++++++++-------------- 1 file changed, 42 insertions(+), 30 deletions(-) diff --git a/routes/unified-data-gateway.js b/routes/unified-data-gateway.js index 416e72a..79a0693 100644 --- a/routes/unified-data-gateway.js +++ b/routes/unified-data-gateway.js @@ -116,7 +116,7 @@ router.get("/", (req, res) => { } }`; var startTime = new Date( - new Date().setFullYear(new Date().getFullYear() - 1) + new Date().setFullYear(new Date().getFullYear() - 2) ); var endTime = new Date(new Date().setHours(new Date().getHours() - 1)); @@ -136,6 +136,17 @@ router.get("/", (req, res) => { ) .then((result) => { var 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, i) { + console.log("ERROR: Errors in results - " + error.message); + }); + + // TODO: Should keep errors for filteredContacts + result.data.errors = []; + } + var filteredContacts = []; contacts.forEach(function (contact, i) { if (contact.node.interaction.__typename === "Email") { @@ -162,40 +173,41 @@ router.get("/", (req, res) => { summary.totalInboundCount = 0; summary.totalInboundActiveSeconds = 0; - summary.firstContactReceivedDate = new Date( - filteredContacts[0].node.interaction.receivedDate - ); + if (summary.totalCount > 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 - ); + 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.firstContactReceivedDate = new Date( + contact.node.interaction.receivedDate + ); + } + summary.totalInboundActiveSeconds += contact.node.activeDuration; } - 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); + // 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) {