eHealthAfrica/direct-delivery-dashboard

View on GitHub
couchdb/app/deliveries/_design/dashboard-delivery-rounds/views/report-by-id/map.js

Summary

Maintainability
A
0 mins
Test Coverage
function(doc) {

    var successTag = 'success';
    var cceTag = 'cce';
    var failedTag = 'failed';
    var cancelTag = 'cancel';

    var DELIVERY_STATUSES = {
        UPCOMING_FIRST: 'Upcoming: 1st Attempt',
        UPCOMING_SECOND: 'Upcoming: 2st Attempt',
        SUCCESS_FIRST: 'Success: 1st Attempt',
        SUCCESS_SECOND: 'Success: 2nd Attempt',
        CANCELED_CCE: 'Canceled: CCE',
        CANCELED_OTHER: 'Canceled: Other',
        CANCELED_STAFF: 'Canceled: Staff availability',
        FAILED_CCE: 'Failed: CCE',
        FAILED_STAFF: 'Failed: Staff availability',
        FAILED_OTHER: 'Failed: other'
    };

    function isValidStatus(status) {
        status = status.toLowerCase();
        var option;
        for (var k in DELIVERY_STATUSES) {
            option = DELIVERY_STATUSES[k].toLowerCase();
            if (status === option) {
                return true;
            }
        }
        return false; //default value
    }

    function isInvalidDate(date) {
        return (!date || date === null || new Date(date).toString() === 'Invalid Date');
    }

    function isOnTime(targetDate, deliveryDate) {
        var TWO_DAYS = 172800000; //milli-seconds

        //assume that target date is same as delivery date if not set, hence on time
        if (isInvalidDate(targetDate)) {
            return true;
        }

        var timeDiff = new Date(deliveryDate) - new Date(targetDate);
        return timeDiff <= TWO_DAYS;
    }

    function isBillable(status) {
        return (status.toLowerCase().indexOf(successTag) !== -1 || status.toLowerCase().indexOf(failedTag) !== -1);
    }

    function hasWorkingCCE(status) {
        status = status.toLowerCase();
        var isFailedOrCanceled = (status.indexOf(cancelTag) !== -1 || status.indexOf(failedTag) !== -1);
        if(isFailedOrCanceled){
            return (status.indexOf(cceTag) === -1)
        }
        return true;
    }

    function isDelivered(status) {
        return status.toLowerCase().indexOf(successTag) !== -1;
    }

    function genReport(targetDate, deliveryDate, status) {
        //default facility report object to be passed to reduce function.
        var facRndReport = {
            onTime: 0,
            billable: 0,
            workingCCE: 0,
            delivered: 0
        };
        facRndReport.onTime = isOnTime(targetDate, deliveryDate) ? 1 : 0;
        facRndReport.billable = isBillable(status) ? 1 : 0;
        facRndReport.workingCCE = hasWorkingCCE(status) ? 1 : 0;
        facRndReport.delivered = isDelivered(status) ? 1 : 0;
        return facRndReport
    }

    if (doc.doc_type === 'dailyDelivery' && !isInvalidDate(doc.date)) {

        var facRnd;
        var facRndReport;

        //old bundled delivery document
        if (doc.facilityRounds) {
            for (var i in doc.facilityRounds) {
                facRnd = doc.facilityRounds[i];
                facRndReport = genReport(facRnd.targetDate, doc.date, facRnd.status);
                emit(doc.deliveryRoundID, facRndReport);
            }
        } else {
            //newer single facility round document
            facRnd = doc;
            facRndReport = genReport(facRnd.targetDate, facRnd.date, facRnd.status);
            emit(facRnd.deliveryRoundID, facRndReport);
        }
    }

}