From 7fa61f03e22f7b95fbb316a8aa2d8794ab06aab5 Mon Sep 17 00:00:00 2001 From: psychobunny Date: Fri, 20 Feb 2015 12:16:13 -0500 Subject: [PATCH] saving conditions --- src/rewards/admin.js | 31 ++++++++++++++++++++++++++----- 1 file changed, 26 insertions(+), 5 deletions(-) diff --git a/src/rewards/admin.js b/src/rewards/admin.js index 185ad6b1e5..1f13ba7854 100644 --- a/src/rewards/admin.js +++ b/src/rewards/admin.js @@ -52,7 +52,9 @@ rewards.save = function(data, callback) { ], next); } - async.each(data, save, callback); + async.each(data, save, function(err) { + saveConditions(data, callback); + }); }; rewards.delete = function(data, callback) { @@ -76,15 +78,15 @@ rewards.get = function(callback) { plugins.fireHook('filter:rewards.conditions', [ { "name": "Reputation", - "condition": "reputation" + "condition": "core:user.reputation" }, { "name": "Post Count", - "condition": "postcount" + "condition": "core:user.postcount" }, { "name": "Last Logged in Time", - "condition": "lastLoggedIn" + "condition": "core:user.lastonline" } ], next); }, @@ -139,8 +141,27 @@ rewards.get = function(callback) { }, callback); }; -function getConditions() { +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.rid); + }); + + db.setAdd('conditions:active', conditions, callback); + + async.each(Object.keys(rewardsPerCondition), function(condition, next) { + db.setAdd('condition:' + condition + ':rewards', rewardsPerCondition[condition]); + }, callback); + }); } function getActiveRewards(callback) {