2013-05-29 04:56:49 +04:00
|
|
|
const openbadger = require('../openbadger');
|
2013-05-30 04:12:01 +04:00
|
|
|
const db = require('../db');
|
|
|
|
const claim = db.model('Claim');
|
2013-05-30 22:00:19 +04:00
|
|
|
const loggedIn = require('../middleware').loggedIn;
|
2013-05-29 04:56:49 +04:00
|
|
|
|
2013-03-25 00:52:09 +04:00
|
|
|
module.exports = function (app) {
|
|
|
|
|
2013-05-30 22:00:19 +04:00
|
|
|
app.get('/claim', [loggedIn], function (req, res, next) {
|
2013-05-29 04:56:49 +04:00
|
|
|
var claimCode = req.query.code;
|
|
|
|
var user = res.locals.user;
|
|
|
|
|
|
|
|
if (!claimCode)
|
|
|
|
return res.render('claim.html');
|
2013-05-30 00:39:24 +04:00
|
|
|
claimCode = claimCode.trim();
|
2013-05-29 04:56:49 +04:00
|
|
|
|
2013-05-30 00:39:24 +04:00
|
|
|
openbadger.getBadgeFromCode({
|
|
|
|
code: claimCode,
|
2013-05-29 04:56:49 +04:00
|
|
|
email: user.email
|
|
|
|
}, function(err, data) {
|
2013-05-29 17:29:25 +04:00
|
|
|
if (err) {
|
2013-05-30 00:39:24 +04:00
|
|
|
if (err.code === 404)
|
2013-05-29 17:29:25 +04:00
|
|
|
req.flash('error', "That claim code appears to be invalid.");
|
|
|
|
else if (err.code === 409)
|
2013-05-30 00:39:24 +04:00
|
|
|
req.flash('warn', "You have already used that claim code.");
|
2013-05-29 17:29:25 +04:00
|
|
|
else
|
2013-05-30 00:39:24 +04:00
|
|
|
req.flash('error', "A problem was encountered.");
|
|
|
|
return res.render('claim.html', {
|
|
|
|
code: claimCode
|
|
|
|
});
|
2013-05-29 17:29:25 +04:00
|
|
|
}
|
|
|
|
else {
|
2013-05-30 00:39:24 +04:00
|
|
|
return res.render('claim.html', {
|
|
|
|
code: claimCode,
|
|
|
|
badge: data.badge
|
|
|
|
});
|
2013-05-29 17:29:25 +04:00
|
|
|
}
|
2013-05-30 00:39:24 +04:00
|
|
|
});
|
|
|
|
|
|
|
|
});
|
|
|
|
|
2013-05-30 22:00:19 +04:00
|
|
|
app.post('/claim', [loggedIn], function (req, res, next) {
|
2013-05-30 00:39:24 +04:00
|
|
|
var claimCode = req.query.code;
|
|
|
|
var user = res.locals.user;
|
|
|
|
|
2013-05-30 04:12:01 +04:00
|
|
|
claim.findOrCreate({
|
2013-05-30 00:39:24 +04:00
|
|
|
code: claimCode,
|
2013-05-30 04:12:01 +04:00
|
|
|
LearnerId: user.id
|
|
|
|
}).complete(function(err, claim) {
|
|
|
|
if (err) {
|
|
|
|
return next(err);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
claim.submit(function(err, claim){
|
|
|
|
if (err) {
|
|
|
|
if (err.code === 409)
|
|
|
|
req.flash('warn', "You already have that badge.");
|
|
|
|
else
|
|
|
|
req.flash('error', "There has been an error claiming your badge.");
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
if (claim.state === 'waiting')
|
|
|
|
req.flash('info', "Your badge claim is awaiting approval from your parent or guardian.");
|
|
|
|
else
|
|
|
|
req.flash('success', "You've claimed a new badge!");
|
|
|
|
}
|
2013-05-30 22:00:19 +04:00
|
|
|
return res.redirect('/mybadges');
|
2013-05-30 04:12:01 +04:00
|
|
|
});
|
|
|
|
}
|
2013-05-29 04:56:49 +04:00
|
|
|
});
|
2013-03-25 00:52:09 +04:00
|
|
|
});
|
|
|
|
|
2013-05-31 20:21:00 +04:00
|
|
|
app.get('/mybadges', [
|
2013-06-02 02:32:45 +04:00
|
|
|
loggedIn,
|
2013-05-31 20:21:00 +04:00
|
|
|
openbadger.middleware('getUserBadges')
|
|
|
|
], function (req, res, next) {
|
2013-05-30 22:00:19 +04:00
|
|
|
var data = req.remote;
|
2013-03-28 17:14:16 +04:00
|
|
|
|
|
|
|
res.render('user/backpack.html', {
|
2013-05-30 22:00:19 +04:00
|
|
|
items: data.badges
|
2013-03-28 17:14:16 +04:00
|
|
|
});
|
2013-03-25 00:52:09 +04:00
|
|
|
});
|
|
|
|
|
2013-05-31 20:21:00 +04:00
|
|
|
app.get('/mybadges/:id', [
|
|
|
|
loggedIn,
|
|
|
|
openbadger.middleware('getUserBadge')
|
|
|
|
], function (req, res, next) {
|
|
|
|
var data = req.remote;
|
|
|
|
|
|
|
|
console.log(data.badge);
|
|
|
|
res.render('user/badge.html', {
|
|
|
|
badge: data.badge
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2013-06-02 23:10:26 +04:00
|
|
|
app.get('/myapplications', [
|
|
|
|
loggedIn,
|
|
|
|
openbadger.middleware('getUserBadge')
|
|
|
|
], function (req, res, next) {
|
|
|
|
var data = req.remote;
|
|
|
|
|
|
|
|
res.render('user/backpack.html', {
|
|
|
|
items: data.badges
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2013-03-28 20:42:32 +04:00
|
|
|
app.get('/favorites/:view?', function (req, res, next) {
|
|
|
|
var badge = {
|
|
|
|
thumbnail: '/media/images/badge.png',
|
|
|
|
description: 'Badge blah in voluptate velit...',
|
|
|
|
url: '/badges/ae784f'
|
|
|
|
};
|
2013-03-28 17:14:16 +04:00
|
|
|
|
2013-03-28 20:42:32 +04:00
|
|
|
var org = {
|
|
|
|
thumbnail: '/media/images/org.png',
|
2013-06-02 02:32:45 +04:00
|
|
|
description: 'Organization blah irure...',
|
|
|
|
url: '/orgs/some-organization'
|
2013-03-28 20:42:32 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
var program = {
|
|
|
|
thumbnail: '/media/images/program.png',
|
|
|
|
description: 'Program blah sed eiusmod...',
|
|
|
|
url: '/programs/ae784f'
|
|
|
|
};
|
|
|
|
|
|
|
|
var view = req.params.view,
|
|
|
|
favorites = [badge, org, badge, program];
|
|
|
|
|
|
|
|
switch (view) {
|
|
|
|
case 'badges':
|
|
|
|
favorites = [badge, badge]; break;
|
|
|
|
case 'programs':
|
|
|
|
favorites = [program];
|
|
|
|
case 'orgs':
|
|
|
|
favorites = [org]; break;
|
|
|
|
default:
|
|
|
|
view = null;
|
2013-03-28 17:14:16 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
res.render('user/bookmarks.html', {
|
2013-03-28 20:42:32 +04:00
|
|
|
items: favorites,
|
|
|
|
view: view
|
2013-03-28 17:14:16 +04:00
|
|
|
})
|
2013-03-27 22:00:30 +04:00
|
|
|
})
|
|
|
|
|
2013-03-25 00:52:09 +04:00
|
|
|
};
|