(function (Database) {
    'use strict';

    var async       = require('async'),

        nodebb      = require('./nodebb'),
        db          = nodebb.db,
        constants   = require('./constants'),
        namespace   = constants.NAMESPACE,
        nextAwardId = constants.GLOBAL_AWARD_COUNTER,
        nextGrantId = constants.GLOBAL_GRANT_COUNTER;

    Database.createAward = function (name, description, image, done) {
        async.waterfall([
            async.apply(db.incrObjectField, 'global', nextAwardId),
            function (id, next) {
                //Where score as id will work as index position value for sorting
                db.sortedSetAdd(namespace, id, id, function (error) {
                    if (error) {
                        return next(error);
                    }
                    next(null, id);
                });
            }, function (id, next) {
                var awardModel = {
                    aid  : id,
                    name : name,
                    desc : description,
                    image: image
                };
                db.setObject(namespace + ':' + id, awardModel, function (error) {
                    if (error) {
                        return next(error);
                    }

                    next(null, awardModel);
                });
            }
        ], done);
    };

    Database.createGrant = function (uid, aid, reason, initiatorUid, done) {
        async.waterfall([
            async.apply(db.incrObjectField, 'global', nextGrantId),
            function (gid, next) {

                var grant = {
                    uid       : uid,
                    fromuid   : initiatorUid,
                    aid       : aid,
                    gid       : gid,
                    createtime: Date.now(),
                    reason    : reason
                };

                async.parallel([
                    async.apply(db.sortedSetAdd, namespace + ':award:' + aid, uid, gid),
                    async.apply(db.sortedSetAdd, namespace + ':user:' + uid, aid, gid),
                    async.apply(db.setObject, namespace + ':grant:' + gid, grant)
                ], function (error) {
                    if (error) {
                        return next(error);
                    }
                    next(null, grant);
                });
            }
        ], done);
    };

    Database.deleteAward = function (id, done) {
        async.parallel([
            async.apply(db.delete, namespace + ':' + id),
            async.apply(db.sortedSetRemove, namespace, id)
        ], done);
    };

    Database.deleteGrant = function (gid, done) {
        var grantKey = namespace + ':grant:' + gid;

        db.getObject(grantKey, function (error, grant) {
            if (error) {
                return done(error);
            } else if (!grant) {
                return done(new Error('Grant Object can not be found'));
            }

            async.parallel([
                async.apply(db.delete, grantKey),
                async.apply(db.sortedSetRemove, namespace + ':award:' + grant.aid, gid),
                async.apply(db.sortedSetRemove, namespace + ':user:' + grant.uid, gid)
            ], done);
        });
    };

    Database.getAllAwards = function (done) {
        async.waterfall([
            async.apply(db.getSortedSetRange, namespace, 0, -1),
            function (ids, next) {
                if (!ids.length) {
                    return next(null, ids);
                }
                db.getObjects(ids.map(function (id) {
                    return namespace + ':' + id;
                }), next);
            }
        ], done);
    };

    Database.getAward = function (id, done) {
        db.getObject(namespace + ':' + id, done);
    };

    Database.getGrantIdsByAward = function (aid, done) {
        db.getSortedSetRevRange(namespace + ':award:' + aid, 0, -1, done);
    };

    Database.getGrantIdsByUser = function (uid, done) {
        db.getSortedSetRevRange(namespace + ':user:' + uid, 0, -1, done);
    };


})(module.exports);
@@ -1577,16 +1577,61 @@
 ext) %7B%0A%0A
+                var createTime = Date.now();%0A
         
@@ -1648,24 +1648,24 @@
 r grant = %7B%0A
-
             
@@ -1841,26 +1841,26 @@
 tetime: 
-Date.now()
+createTime
 ,%0A      
@@ -2023,19 +2023,26 @@
  + aid, 
-uid
+createTime
 , gid),%0A
@@ -2118,19 +2118,26 @@
  + uid, 
-aid
+createTime
 , gid),%0A

Set Grant objects with timestamp scores