This repository was archived by the owner on Dec 14, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathaccept-badge.js
48 lines (38 loc) · 1.67 KB
/
accept-badge.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
'use strict';
var _ = require('lodash');
function acceptBadge (args, callback) {
var seneca = this;
var zenHostname = process.env.HOSTNAME || '127.0.0.1:8000';
var badgeData = args.badgeData;
var user = args.user;
var protocol = process.env.PROTOCOL || 'http';
if (badgeData.userId !== user.id && !args.badgeData.parent) return callback(null, { error: 'Only the nominated user or their parent can accept this badge.' });
seneca.act({role: 'cd-profiles', cmd: 'list', query: {userId: badgeData.userId}}, function (err, response) {
if (err) return callback(err);
var profile = response[0];
var badgeFound = _.find(profile.badges, function (userBadge) {
return userBadge.slug === badgeData.badgeSlug;
});
if (!badgeFound) return callback(null, { error: 'Badge not found.' });
if (badgeFound.status === 'accepted') return callback(null, { error: 'This badge has already been accepted.' });
badgeFound.status = 'accepted';
badgeFound.dateAccepted = new Date();
// Add assertion for badge baking.
badgeFound.assertion = {
uid: 'coderdojo-' + user.id + badgeFound.id,
recipient: {
identity: user.email,
type: 'email',
hashed: false
},
badge: 'http://badgekit.coderdojo.com:8080/public/systems/coderdojo/badges/' + badgeFound.slug,
verify: {
url: protocol + '://' + zenHostname + '/api/1.0/verify_badge/' + user.id + '/' + badgeFound.id + '/assertion', // url should return badgeAssertion object.
type: 'hosted'
},
issuedOn: new Date()
};
seneca.act({role: 'cd-profiles', cmd: 'save', profile: profile}, callback);
});
}
module.exports = acceptBadge;