Browse files

Award or claim method for badge.

  • Loading branch information...
1 parent 168dd8e commit f547bdad1ab08cc8b4cc6761c2713e779eaa9cdf @brianloveswords brianloveswords committed Nov 6, 2012
Showing with 27 additions and 0 deletions.
  1. +17 −0 models/badge.js
  2. +10 −0 tests/badge-model.test.js
View
17 models/badge.js
@@ -405,6 +405,23 @@ Badge.prototype.award = function award(email, callback) {
};
/**
+ * Award a badge or find the awarded badge
+ */
+
+Badge.prototype.awardOrFind = function awardOrFind(email, callback) {
+ var BadgeInstance = require('./badge-instance');
+ var query = { userBadgeKey: [email, this.shortname].join('.') };
+ this.award(email, function (err, instance) {
+ if (!instance) {
+ BadgeInstance.findOne(query, function (err, instance) {
+ if (err) return callback(err);
+ return callback(null, instance);
+ });
+ }
+ });
+};
+
+/**
* Get how many credits a user has to earn before the badge is earnable.
*
* @param {User} user A user-like object, containing `credits` property
View
10 tests/badge-model.test.js
@@ -345,6 +345,16 @@ test.applyFixtures(fixtures, function () {
t.end();
});
+ test('Badge#awardOrFind: award badge to a user', function (t) {
+ var badge = fixtures['link-comment'];
+ var email = fixtures['user'].user;
+ badge.awardOrFind(email, function (err, instance) {
+ t.notOk(err, 'should not have an error');
+ t.ok(instance, 'should have a badge instance');
+ t.same(instance.user, email, 'should be assigned to the right user');
+ t.end();
+ });
+ });
// necessary to stop the test runner
test('shutting down #', function (t) {

0 comments on commit f547bda

Please sign in to comment.