getDayViewData.js

var async = require("async");
var mongo = require('mongodb');
var ObjectId = mongo.ObjectID;

function getDayViewData(options) {
    var asyncTasks = [];
    var dates = options.data.dates;

    // get mark items query
    var getTemplateCllnSnippets = [];
    getTemplateCllnSnippets.push({
        $match: {
            createdAt: {
                $gte:  new Date(dates.startDate)
            }
        }
    });
    getTemplateCllnSnippets.push({
        $project: {
            _id: 0,
            document: "$$ROOT"
        }
    });

    // get mark items from TemplateClln
    options.DB.TemplateClln.aggregate(getTemplateCllnSnippets, onComplete);

    function onComplete(err, snippets) {
        if (err) {
            console.log(err);
        } else {
            var data = {};
            data.snippets = [];

            //console.log('Received snippets from TemplateClln');
            for (var index in snippets) {
                var snippet = snippets[index];
                //console.log(JSON.stringify(snippet));
                (function (snippet) {
                    if (snippet.hasOwnProperty('document')) {
                        var templateCode = snippet.document.templateCode;
                        if(templateCode === 'WHOM') {
                            asyncTasks.push(function(_callback){
                                options.DB.NameClln.findOne({'_id': new ObjectId(snippet.document.fields[0].whom)},
                                    { _id: 0, name: 1 }, function (err, docs) {
                                        if(docs !== null) {
                                            snippet.document.fields[0].whom = docs.name;
                                            data.snippets.push(snippet.document);
                                        }
                                        _callback();
                                    });
                                });
                        }
                        else if(templateCode === 'LIST') {
                            if(snippet.document.latest) {
                                asyncTasks.push(function(_callback){
                                    data.snippets.push(snippet.document);
                                    _callback();
                                });
                            }
                        }
                        else if(templateCode === 'CKIN' || templateCode === 'CKOT') {
                            asyncTasks.push(function(_callback){
                                options.DB.PlaceClln.findOne({'_id': new ObjectId(snippet.document.fields[0].name)},
                                    { _id: 0, name: 1 }, function (err, docs) {
                                        if(docs !== null) {
                                            //console.log(docs);
                                            snippet.document.fields[0].name = docs.name;
                                            data.snippets.push(snippet.document);
                                        }
                                        _callback();
                                    });
                                });
                        }
                        else {
                            asyncTasks.push(function(_callback){
                                data.snippets.push(snippet.document);
                                _callback();
                            });
                        }
                    }
                })(snippet);
            }

            async.parallel(asyncTasks, function(){
                console.log('Data for dashboard');
                console.log(JSON.stringify(data.snippets));
                options.res.json(data);
            });
        }
    }
}

module.exports = getDayViewData;