Permalink
Browse files

Fix claim code normalization problem [closes #47]

  • Loading branch information...
1 parent 4aab819 commit 81eea594c6605c723adb89cd03ad8582864eada8 @brianloveswords brianloveswords committed Nov 11, 2012
Showing with 18 additions and 11 deletions.
  1. +2 −2 models/badge.js
  2. +1 −1 public/js/claim-award.js
  3. +15 −8 routes/badge.js
View
@@ -303,9 +303,10 @@ Badge.prototype.generateClaimCodes = function generateClaimCodes(options, callba
Badge.prototype.getClaimCode = function getClaimCode(code) {
const codes = this.claimCodes;
+ const normalizedCode = code.trim().replace(/ /g, '-').toLowerCase();
var idx = codes.length;
while (idx--) {
- if (codes[idx].code === code)
+ if (codes[idx].code === normalizedCode)
return codes[idx];
}
return null;
@@ -339,7 +340,6 @@ Badge.prototype.claimCodeIsClaimed = function claimCodeIsClaimed(code) {
Badge.prototype.redeemClaimCode = function redeemClaimCode(code, email) {
const claim = this.getClaimCode(code);
- console.dir(claim);
if (!claim)
return null;
if (claim.claimedBy && claim.claimedBy !== email)
@@ -22,7 +22,7 @@
var email = $emailInput.val().trim();
var url;
getAssertion(email, function (err, data) {
- if (err)
+ if (err || data.status === 'not-found')
return window.alert('There was an error trying to claim the badge');
if (data.status === 'already-claimed')
View
@@ -1,4 +1,5 @@
var fs = require('fs');
+var logger = require('../lib/logger');
var Badge = require('../models/badge');
var BadgeInstance = require('../models/badge-instance');
@@ -58,7 +59,6 @@ exports.update = function update(req, res, next) {
});
};
-
exports.addBehavior = function addBehavior(req, res) {
var form = req.body;
var behavior = {
@@ -146,19 +146,26 @@ exports.releaseClaimCode = function releaseClaimCode(req, res, next) {
})
};
+function reportError(err) {
+ return { status: 'error', error: err };
+}
exports.awardToUser = function awardToUser(req, res, next) {
var form = req.body
var email = (form.email || '').trim();
var code = (form.code || '').trim();
var badge = req.badge;
- var couldClaim = badge.redeemClaimCode(code, email);
- // if (!couldClaim)
- // return res.send({ status: 'already-claimed' })
+ var claimSuccess = badge.redeemClaimCode(code, email);
+
+ if (claimSuccess === false)
+ return res.send({ status: 'already-claimed' })
+ if (claimSuccess === null)
+ return res.send({ status: 'not-found' })
+
badge.awardOrFind(email, function (err, instance) {
- if (err) return res.send({ status: 'error', error: err });
+ if (err) return res.send(reportError(err));
badge.save(function (err) {
- if (err) return res.send({ status: 'error', error: err });
+ if (err) return res.send(reportError(err));
return res.send({
status: 'ok',
assertionUrl: instance.absoluteUrl('assertion')
@@ -170,13 +177,13 @@ exports.awardToUser = function awardToUser(req, res, next) {
exports.findByClaimCode = function findByClaimCode(options) {
return function (req, res, next) {
var code = req.body.code;
- var normalizedCode = code.trim().replace(/ /g, '-').toLowerCase();
+ var normalizedCode = code.trim().replace(/ +/g, '-').toLowerCase();
Badge.findByClaimCode(normalizedCode, function (err, badge) {
if (err) return next(err);
if (!badge)
return res.redirect('/claim?code=' + code + '&missing=true');
req.badge = badge;
- req.claim = badge.getClaimCode('normalizedCode');
+ req.claim = badge.getClaimCode(normalizedCode);
return next();
});
}

0 comments on commit 81eea59

Please sign in to comment.