Skip to content

Commit

Permalink
chore(build): bundle library for testing
Browse files Browse the repository at this point in the history
  • Loading branch information
BenMaruchu committed Sep 12, 2019
1 parent 8e7abbb commit 1c32fbf
Show file tree
Hide file tree
Showing 2 changed files with 333 additions and 89 deletions.
212 changes: 167 additions & 45 deletions es/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { pkg } from '@lykmapipo/common';
import { head, map, isNumber, merge } from 'lodash';
import { head, map, upperFirst, merge, isNumber } from 'lodash';
import { Router } from '@lykmapipo/express-common';
import { getString } from '@lykmapipo/env';
import { model } from '@lykmapipo/mongoose-common';
Expand Down Expand Up @@ -32,47 +32,6 @@ const getBaseAggregation = criteria => {
const ServiceRequest = model('ServiceRequest');

return ServiceRequest.lookup(criteria)
.addFields({
/**
* Time difference between expected time to resolve the service request
* and today.
*
* This time will indicate if the service request is late or not base on
* the SLA(Service Level Agreement) time set per service request nature
*/
lateTime: { $subtract: ['$expectedAt', new Date()] },

/**
* This is the time for a confirmed service request to be assigned to
* a responsible party
*/
assignTime: { $subtract: ['$assignedAt', '$confirmedAt'] },

/**
* This is the time for a assigned service request to be attended
*/
attendTime: { $subtract: ['$attendedAt', '$assignedAt'] },

/**
* This is the time for a attended service request to be completed
*/
completeTime: { $subtract: ['$completedAt', '$attendedAt'] },

/**
* This is the time for a completed service request to be verified
*/
verifyTime: { $subtract: ['$verifiedAt', '$completedAt'] },

/**
* This is the time for a verified service request to be approved
*/
approveTime: { $subtract: ['$approvedAt', '$verifiedAt'] },

/**
* This is the time for an approved service request to be marked as resolved
*/
resolveTime: { $subtract: ['$resolvedAt', '$createdAt'] },
})
.addFields({
/**
* Flag for unconfirmed service request. This shows all service requests
Expand Down Expand Up @@ -234,7 +193,7 @@ const getBaseAggregation = criteria => {
},

/**
* Flag for resovled service request i.e service request which is resolved
* Flag for resolved service request i.e service request which is resolved
*
* A service request is flagged as resolved service request when it
* has been resolved.
Expand All @@ -247,7 +206,7 @@ const getBaseAggregation = criteria => {
* Flag for reopened service request i.e service request which have been
* reopened after been resolved
*
* A service request is flagged as reopend service request when it
* A service request is flagged as reopened service request when it
* has been confirmed and reopened.
*/
reopened: {
Expand All @@ -257,6 +216,98 @@ const getBaseAggregation = criteria => {
else: 0,
},
},

/**
* Flag for late service request i.e service request passed it's SLA without
* being resolved
*
* Service request is flagged as late service request when it has been either
* resolved pass it's SLA or not resolved and it's pass it's SLA
*/
late: {
$cond: {
if: {
$or: [
{
$and: [
{ $not: '$resolvedAt' },
'$expectedAt',
{ $gt: [new Date(), '$expectedAt'] },
],
},
{
$and: ['$expectedAt', { $gt: ['$resolvedAt', '$expectedAt'] }],
},
],
},
then: 1,
else: 0,
},
},
})
.addFields({
/**
* Time difference between when service request was reported and when it was
* confirmed by an operator or responsible party.
*
* This metric calculate how much time does it take for an organization
* to confirm/respond to issues which have been reported via channels
* which doesn't involve operator intervention. i.e USSD, Mobile App, Bot
* and e.t.c
*/
confirmTime: { $subtract: ['$confirmedAt', '$createdAt'] },

/**
* Time difference between expected time to resolve the service request
* and current date if not resolved or resolvedAt if resolved pass it SLA.
*
* This time will indicate if the service request is late or not base on
* the SLA(Service Level Agreement) time set per service request nature
*/
lateTime: {
$cond: {
if: { $eq: ['$late', 1] },
then: {
$cond: {
if: '$resolvedAt',
then: { $subtract: ['$resolvedAt', '$expectedAt'] },
else: { $subtract: [new Date(), '$expectedAt'] },
},
},
else: null,
},
},

/**
* This is the time for a confirmed service request to be assigned to
* a responsible party
*/
assignTime: { $subtract: ['$assignedAt', '$confirmedAt'] },

/**
* This is the time for a assigned service request to be attended
*/
attendTime: { $subtract: ['$attendedAt', '$assignedAt'] },

/**
* This is the time for a attended service request to be completed
*/
completeTime: { $subtract: ['$completedAt', '$attendedAt'] },

/**
* This is the time for a completed service request to be verified
*/
verifyTime: { $subtract: ['$verifiedAt', '$completedAt'] },

/**
* This is the time for a verified service request to be approved
*/
approveTime: { $subtract: ['$approvedAt', '$verifiedAt'] },

/**
* This is the time for an approved service request to be marked as resolved
*/
resolveTime: { $subtract: ['$resolvedAt', '$createdAt'] },
});
};

Expand Down Expand Up @@ -291,6 +342,47 @@ const OVERALL_FACET = {
],
};

/**
* @namespace TIME_FACET
* @description Facet for service request time calculation summary
*
* @version 0.1.0
* @since 0.5.0
*/
const TIME_FACET = {
time: [
{
$group: {
_id: null,
maximumAssignTime: { $max: '$assignTime' },
minimumAssignTime: { $min: '$assignTime' },
averageAssignTime: { $avg: '$assignTime' },
maximumAttendTime: { $max: '$attendTime' },
minimumAttendTime: { $min: '$attendTime' },
averageAttendTime: { $avg: '$attendTime' },
maximumCompleteTime: { $max: '$completeTime' },
minimumCompleteTime: { $min: '$completeTime' },
averageCompleteTime: { $avg: '$completeTime' },
maximumVerifyTime: { $max: '$verifyTime' },
minimumVerifyTime: { $min: '$verifyTime' },
averageVerifyTime: { $avg: '$verifyTime' },
maximumApproveTime: { $max: '$approveTime' },
minimumApproveTime: { $min: '$approveTime' },
averageApproveTime: { $avg: '$approveTime' },
maximumResolveTime: { $max: '$resolveTime' },
minimumResolveTime: { $min: '$resolveTime' },
averageResolveTime: { $avg: '$resolveTime' },
maximumLateTime: { $max: '$lateTime' },
minimumLateTime: { $min: '$lateTime' },
averageLateTime: { $avg: '$lateTime' },
maximumConfirmTime: { $max: '$confirmTime' },
minimumConfirmTime: { $min: '$confirmTime' },
averageConfirmTime: { $avg: '$confirmTime' },
},
},
],
};

/**
* @namespace PRIORITY_FACET
* @description Facet for service requests breakdown based on their priorities
Expand Down Expand Up @@ -667,6 +759,7 @@ const OPERATOR_LEADERSBOARD_FACET = {

const OVERVIEW_FACET = {
...OVERALL_FACET,
...TIME_FACET,
...JURISDICTION_FACET,
...STATUS_FACET,
...PRIORITY_FACET,
Expand Down Expand Up @@ -847,7 +940,7 @@ const normalizeObjectTimes = item => {
* @param {object} results Aggregation results
* @returns {object} Normalized response object
*
* @version 0.1.0
* @version 0.2.0
* @since 0.2.0
*/
const prepareReportResponse = results => {
Expand All @@ -859,10 +952,39 @@ const prepareReportResponse = results => {

data.overall = head(data.overall);

data.time = head(data.time);

if (data.overall) {
data.overall = normalizeObjectTimes(data.overall);
}

if (data.time) {
// const times = {};

const keys = [
'confirmTime',
'assignTime',
'attendTime',
'completeTime',
'verifyTime',
'approveTime',
'resolveTime',
'lateTime',
];

const times = map(keys, key => ({
[key]: {
minimum: normalizeTime(data.time[`minimum${upperFirst(key)}`]),
maximum: normalizeTime(data.time[`maximum${upperFirst(key)}`]),
average: normalizeTime(data.time[`average${upperFirst(key)}`]),
},
}));

data.overall = merge({}, data.overall, ...times);

delete data.time;
}

if (data.jurisdictions) {
data.jurisdictions = map(data.jurisdictions, normalizeObjectTimes);
}
Expand Down
Loading

0 comments on commit 1c32fbf

Please sign in to comment.