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

Commit

Permalink
Fix claim code normalization problem [closes #47]
Browse files Browse the repository at this point in the history
  • Loading branch information
brianloveswords committed Nov 11, 2012
1 parent 4aab819 commit 81eea59
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 11 deletions.
4 changes: 2 additions & 2 deletions models/badge.js
Expand Up @@ -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;
Expand Down Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion public/js/claim-award.js
Expand Up @@ -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')
Expand Down
23 changes: 15 additions & 8 deletions routes/badge.js
@@ -1,4 +1,5 @@
var fs = require('fs');
var logger = require('../lib/logger');
var Badge = require('../models/badge');
var BadgeInstance = require('../models/badge-instance');

Expand Down Expand Up @@ -58,7 +59,6 @@ exports.update = function update(req, res, next) {
});
};


exports.addBehavior = function addBehavior(req, res) {
var form = req.body;
var behavior = {
Expand Down Expand Up @@ -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')
Expand All @@ -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();
});
}
Expand Down

0 comments on commit 81eea59

Please sign in to comment.