diff --git a/routes/threadTracker.js b/routes/threadTracker.js index 7eda590..57438a4 100644 --- a/routes/threadTracker.js +++ b/routes/threadTracker.js @@ -139,52 +139,68 @@ router.get("/", (req, res) => { .then((result) => { var contacts = result.data.data.findContactsCompletedBetween.edges; var filteredContacts = []; - for (const contact of contacts) { - if ( - contact.node.interaction.__typename === "Email" && - contact.node.interaction.threadId === threadId - ) { - filteredContacts.push(contact); + 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; - // calculate elapsed - var contactReceived = new Date( + // Summary Values + var summary = {}; + + summary.totalCount = filteredContacts.length; + summary.totalInboundCount = 0; + summary.totalInboundActiveSeconds = 0; + + summary.firstContactReceivedDate = new Date( filteredContacts[0].node.interaction.receivedDate ); - var lastContactEndTime = new Date( + + 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 ); - var firstContactStartTime = new Date( - filteredContacts[0].node.startTime - ); - var totalHTDays = - (lastContactEndTime.getTime() - contactReceived.getTime()) / - (1000 * 3600 * 24); - var activeHTDays = - (lastContactEndTime.getTime() - firstContactStartTime.getTime()) / - (1000 * 3600 * 24); - // var totalATHours = (lastContactEndTime.getTime() - firstContactStartTime.getTime()) / (1000 * 3600); - var totalATSeconds = 0; - for (const contact of filteredContacts) { - totalATSeconds += contact.node.activeDuration; - } - var totalATHours = totalATSeconds / (60 * 60); + summary.totalHTDays = + (summary.lastContactEndTime.getTime() - + summary.firstContactReceivedDate.getTime()) / + (1000 * 3600); + summary.activeHTMinutes = + (summary.lastContactEndTime.getTime() - + summary.firstInboundContactStartDate.getTime()) / + (1000 * 60); - result.data.data.findContactsCompletedBetween.totalHTDays = - totalHTDays.toPrecision(5); - result.data.data.findContactsCompletedBetween.activeHTDays = - activeHTDays.toPrecision(5); - result.data.data.findContactsCompletedBetween.totalATHours = - totalATHours.toPrecision(5); + result.data.data.summary = summary; if (result.data) { console.log( - util.inspect(result.data, { + util.inspect(summary, { showHidden: false, depth: null, colors: true,