Sunday, 9 April 2017

to write a function to calculate working hours in a week fetched from mongodb database

i need to calculate working hours worked by an employee in a week. it should be fetched from mongodb database. i have tried with this code but i am not getting the answer.it should fetch the hours worked by an employee from mongodb. how do i write the function to calculate?

`Week: function(callback) {
    entries.find({
        $and: [{
            $or: [{
                "times.0.date": {
                    $gt: firstday,
                    $lt: lastday
                }
            }, {
                "times.date": {
                    $gt: firstday,
                    $lt: lastday
                }
            }]
        }, {
            "user_id": parseInt(data.user_id),
            "company_id": parseInt(data.company_id),
            "entry_type": "task_entry"
        }]
    }).toArray().then(function(timer) {
        var week_hrs = 0;
        timer.map(function(tim) {
           if (tim.times.length > 0) {
                var ms = 0;
                var start = null;
                tim.times.map(function(item) {
                    if ((item.state == 'start' || item.state == 'resume') && !start) {
                        start = item;
                    } else if ((item.state == 'pause' || item.state == 'stop') && start) {
                        ms = ms + moment(item.date) - moment(start.date);
                        start = null;
                    }
                })
                tim.total_hours = moment.utc(ms).format("HH:mm");
                week_hrs = week_hrs + ms;
                week_hrs.push(tim);
            }
        })
        return callback(null, {
     week_hrs: moment.utc(week_hrs).format("HH:mm")

        })
    }).catch(function(err) {
        return callback(new Error(err));
    })    
},

`



via rashmi r

No comments:

Post a Comment