rewards test

v1.18.x
barisusakli 8 years ago
parent 9f67142786
commit 4bbf517af3

@ -1,19 +1,33 @@
"use strict"; "use strict";
var rewards = {}, var async = require('async');
async = require('async'), var plugins = require('../plugins');
plugins = require('../plugins'), var db = require('../database');
db = require('../database');
var rewards = module.exports;
rewards.save = function (data, callback) { rewards.save = function (data, callback) {
function save(data, next) {
function commit(err, id) { async.each(data, function save(data, next) {
if (err) {
return callback(err); if (!Object.keys(data.rewards).length) {
return next();
} }
data.id = id; var rewardsData = data.rewards;
delete data.rewards;
async.waterfall([
function (next) {
if (!parseInt(data.id, 10)) {
db.incrObjectField('global', 'rewards:id', next);
} else {
next(null, data.id);
}
},
function (rid, next) {
data.id = rid;
async.series([ async.series([
function (next) { function (next) {
@ -30,22 +44,9 @@ rewards.save = function (data, callback) {
} }
], next); ], next);
} }
], next);
if (!Object.keys(data.rewards).length) { }, function (err) {
return next();
}
var rewardsData = data.rewards;
delete data.rewards;
if (!parseInt(data.id, 10)) {
db.incrObjectField('global', 'rewards:id', commit);
} else {
commit(false, data.id);
}
}
async.each(data, save, function (err) {
if (err) { if (err) {
return callback(err); return callback(err);
} }
@ -84,13 +85,13 @@ rewards.get = function (callback) {
}; };
function saveConditions(data, callback) { function saveConditions(data, callback) {
db.delete('conditions:active', function (err) { var rewardsPerCondition = {};
if (err) { async.waterfall([
return callback(err); function (next) {
} db.delete('conditions:active', next);
},
var conditions = [], function (next) {
rewardsPerCondition = {}; var conditions = [];
data.forEach(function (reward) { data.forEach(function (reward) {
conditions.push(reward.condition); conditions.push(reward.condition);
@ -98,11 +99,15 @@ function saveConditions(data, callback) {
rewardsPerCondition[reward.condition].push(reward.id); rewardsPerCondition[reward.condition].push(reward.id);
}); });
db.setAdd('conditions:active', conditions, callback); db.setAdd('conditions:active', conditions, next);
},
function (next) {
async.each(Object.keys(rewardsPerCondition), function (condition, next) { async.each(Object.keys(rewardsPerCondition), function (condition, next) {
db.setAdd('condition:' + condition + ':rewards', rewardsPerCondition[condition], next); db.setAdd('condition:' + condition + ':rewards', rewardsPerCondition[condition], next);
}, callback); }, next);
}
], function (err) {
callback(err);
}); });
} }
@ -138,5 +143,3 @@ function getActiveRewards(callback) {
}); });
}); });
} }
module.exports = rewards;

@ -1,40 +1,36 @@
"use strict"; "use strict";
var rewards = {},
db = require('../database'),
plugins = require('../plugins'),
async = require('async');
var db = require('../database');
var plugins = require('../plugins');
var async = require('async');
var rewards = module.exports;
rewards.checkConditionAndRewardUser = function (uid, condition, method, callback) { rewards.checkConditionAndRewardUser = function (uid, condition, method, callback) {
callback = callback || function () {};
async.waterfall([ async.waterfall([
function (next) { function (next) {
isConditionActive(condition, function (err, isActive) { isConditionActive(condition, next);
},
function (isActive, next) {
if (!isActive) { if (!isActive) {
return back(err); return callback();
} }
getIDsByCondition(condition, next);
next(err);
});
},
function (next) {
getIDsByCondition(condition, function (err, ids) {
next(err, ids);
});
}, },
function (ids, next) { function (ids, next) {
getRewardDataByIDs(ids, next); getRewardDataByIDs(ids, next);
}, },
function (rewards, next) { function (rewards, next) {
filterCompletedRewards(uid, rewards, function (err, filtered) { filterCompletedRewards(uid, rewards, next);
if (!filtered || !filtered.length) {
return back(err);
}
next(err, filtered);
});
}, },
function (rewards, next) { function (rewards, next) {
if (!rewards || !rewards.length) {
return callback();
}
async.filter(rewards, function (reward, next) { async.filter(rewards, function (reward, next) {
if (!reward) { if (!reward) {
return next(false); return next(false);
@ -49,14 +45,7 @@ rewards.checkConditionAndRewardUser = function (uid, condition, method, callback
giveRewards(uid, eligible, next); giveRewards(uid, eligible, next);
}); });
} }
], back); ], callback);
function back(err) {
if (typeof callback === 'function') {
callback(err);
}
}
}; };
function isConditionActive(condition, callback) { function isConditionActive(condition, callback) {
@ -86,14 +75,10 @@ function filterCompletedRewards(uid, rewards, callback) {
var claimable = parseInt(reward.claimable, 10); var claimable = parseInt(reward.claimable, 10);
if (claimable === 0) { return claimable === 0 || (userRewards[reward.id] < reward.claimable);
return true;
}
return (userRewards[reward.id] >= reward.claimable) ? false : true;
}); });
callback(false, rewards); callback(null, rewards);
}); });
} }
@ -133,6 +118,3 @@ function giveRewards(uid, rewards, callback) {
}, callback); }, callback);
}); });
} }
module.exports = rewards;

@ -382,8 +382,9 @@ function generateForTag(req, res, next) {
} }
var tag = validator.escape(String(req.params.tag)); var tag = validator.escape(String(req.params.tag));
var page = parseInt(req.query.page, 10) || 1; var page = parseInt(req.query.page, 10) || 1;
var start = Math.max(0, (page - 1) * meta.config.topicsPerPage); var topicsPerPage = meta.config.topicsPerPage || 20;
var stop = start + meta.config.topicsPerPage - 1; var start = Math.max(0, (page - 1) * topicsPerPage);
var stop = start + topicsPerPage - 1;
generateForTopics({ generateForTopics({
uid: req.uid, uid: req.uid,
title: 'Topics tagged with ' + tag, title: 'Topics tagged with ' + tag,

@ -1,7 +1,7 @@
"use strict"; "use strict";
var rewardsAdmin = require('../../rewards/admin'), var rewardsAdmin = require('../../rewards/admin');
SocketRewards = {}; var SocketRewards = module.exports;
SocketRewards.save = function (socket, data, callback) { SocketRewards.save = function (socket, data, callback) {
rewardsAdmin.save(data, callback); rewardsAdmin.save(data, callback);
@ -11,5 +11,3 @@ SocketRewards.delete = function (socket, data, callback) {
rewardsAdmin.delete(data, callback); rewardsAdmin.delete(data, callback);
}; };
module.exports = SocketRewards;

@ -0,0 +1,86 @@
'use strict';
var assert = require('assert');
var async = require('async');
var db = require('./mocks/databasemock');
var meta = require('../src/meta');
var User = require('../src/user');
var Groups = require('../src/groups');
describe('rewards', function () {
var adminUid;
var bazUid;
var herpUid;
before(function (done) {
Groups.resetCache();
// Create 3 users: 1 admin, 2 regular
async.series([
async.apply(User.create, { username: 'foo', password: 'barbar' }),
async.apply(User.create, { username: 'baz', password: 'quuxquux' }),
async.apply(User.create, { username: 'herp', password: 'derpderp' })
], function (err, uids) {
if (err) {
return done(err);
}
adminUid = uids[0];
bazUid = uids[1];
herpUid = uids[2];
async.series([
function (next) {
Groups.join('administrators', adminUid, done);
},
function (next) {
Groups.join('rewardGroup', adminUid, done);
}
], done);
});
});
describe('rewards create', function () {
var socketAdmin = require('../src/socket.io/admin');
var rewards = require('../src/rewards');
it('it should save a reward', function (done) {
var data = [
{
rewards: { groupname: 'Gamers' },
condition: 'essentials/user.postcount',
conditional: 'greaterthan',
value: '10',
rid: 'essentials/add-to-group',
claimable: '1',
id: '',
disabled: false
}
];
socketAdmin.rewards.save({uid: adminUid}, data, function (err) {
assert.ifError(err);
done();
});
});
it('should check condition', function (done) {
function method(next) {
next(null, 1);
}
rewards.checkConditionAndRewardUser(adminUid, 'essentials/user.postcount', method, function (err, data) {
assert.ifError(err);
done();
});
});
});
after(function (done) {
db.emptydb(done);
});
});
Loading…
Cancel
Save