Skip to content
This repository has been archived by the owner on Jan 14, 2020. It is now read-only.

Commit

Permalink
Method for testing whether a user has enough credits to earn a badge.
Browse files Browse the repository at this point in the history
  • Loading branch information
brianloveswords committed Oct 16, 2012
1 parent 0622c04 commit 8ed3109
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 0 deletions.
18 changes: 18 additions & 0 deletions models/badge.js
Expand Up @@ -95,6 +95,24 @@ Badge.findByBehavior = function findByBehavior(shortnames, callback) {
return Badge.find(searchTerms, callback);
};


/**
* Check if the credits are enough to earn the badge
*
* @param {User} user An object resembling a User object.
* @return {Boolean} whether or not the badge is earned by the credits
*/

Badge.prototype.hasEnoughCredit = function hasEnoughCredit(user) {
return this.behaviors.map(function (behavior) {
var name = behavior.shortname;
var minimum = behavior.count;
return user.credit[name] >= minimum;
}).reduce(function (result, value) {
return result && value;
}, true);
};

/**
* Remove a behavior from the list of required behaviors for the badge
*
Expand Down
17 changes: 17 additions & 0 deletions test/badge-model.test.js
Expand Up @@ -160,6 +160,23 @@ test.applyFixtures(fixtures, function () {
});
});

test('Badge#hasEnoughCredit: should have enough', function (t) {
var badge = fixtures['link-comment'];
var expect = true;
var result = badge.hasEnoughCredit({ credit: { link: 10, comment: 10 }});
t.same(expect, result);
t.end();
});

test('Badge#hasEnoughCredit: not enough', function (t) {
var badge = fixtures['link-comment'];
var expect = false;
var result = badge.hasEnoughCredit({ credit: { link: 10 }});
t.same(expect, result);
t.end();
});


test('Badge default: shortname', function (t) {
var badge = new Badge({
name: 'An awesome badge!',
Expand Down

0 comments on commit 8ed3109

Please sign in to comment.