rewards test

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

@ -1,35 +1,14 @@
"use strict";
var rewards = {},
async = require('async'),
plugins = require('../plugins'),
db = require('../database');
var async = require('async');
var plugins = require('../plugins');
var db = require('../database');
var rewards = module.exports;
rewards.save = function (data, callback) {
function save(data, next) {
function commit(err, id) {
if (err) {
return callback(err);
}
data.id = id;
async.series([
function (next) {
rewards.delete(data, next);
},
function (next) {
db.setAdd('rewards:list', data.id, next);
},
function (next) {
db.setObject('rewards:id:' + data.id, data, next);
},
function (next) {
db.setObject('rewards:id:' + data.id + ':rewards', rewardsData, next);
}
], next);
}
async.each(data, function save(data, next) {
if (!Object.keys(data.rewards).length) {
return next();
@ -38,14 +17,36 @@ rewards.save = function (data, callback) {
var rewardsData = data.rewards;
delete data.rewards;
if (!parseInt(data.id, 10)) {
db.incrObjectField('global', 'rewards:id', commit);
} else {
commit(false, data.id);
}
}
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([
function (next) {
rewards.delete(data, next);
},
function (next) {
db.setAdd('rewards:list', data.id, next);
},
function (next) {
db.setObject('rewards:id:' + data.id, data, next);
},
function (next) {
db.setObject('rewards:id:' + data.id + ':rewards', rewardsData, next);
}
], next);
}
], next);
async.each(data, save, function (err) {
}, function (err) {
if (err) {
return callback(err);
}
@ -84,25 +85,29 @@ rewards.get = function (callback) {
};
function saveConditions(data, callback) {
db.delete('conditions:active', function (err) {
if (err) {
return callback(err);
}
var conditions = [],
rewardsPerCondition = {};
data.forEach(function (reward) {
conditions.push(reward.condition);
rewardsPerCondition[reward.condition] = rewardsPerCondition[reward.condition] || [];
rewardsPerCondition[reward.condition].push(reward.id);
});
var rewardsPerCondition = {};
async.waterfall([
function (next) {
db.delete('conditions:active', next);
},
function (next) {
var conditions = [];
db.setAdd('conditions:active', conditions, callback);
data.forEach(function (reward) {
conditions.push(reward.condition);
rewardsPerCondition[reward.condition] = rewardsPerCondition[reward.condition] || [];
rewardsPerCondition[reward.condition].push(reward.id);
});
async.each(Object.keys(rewardsPerCondition), function (condition, next) {
db.setAdd('condition:' + condition + ':rewards', rewardsPerCondition[condition], next);
}, callback);
db.setAdd('conditions:active', conditions, next);
},
function (next) {
async.each(Object.keys(rewardsPerCondition), function (condition, next) {
db.setAdd('condition:' + condition + ':rewards', rewardsPerCondition[condition], next);
}, next);
}
], function (err) {
callback(err);
});
}
@ -138,5 +143,3 @@ function getActiveRewards(callback) {
});
});
}
module.exports = rewards;

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

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

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