Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Grant Badges #11

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .env
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,7 @@ APP_ORIGIN=http://localhost:3000

# time in seconds
SHARE_EXPIRES_IN=2592000

BADGES_ENDPOINT=https://api.v2.mybadges.org
BADGES_USERNAME=badges_username
BADGES_PASSWORD=badges_password
2 changes: 2 additions & 0 deletions api.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,15 @@ var galleryRouter = require('./routes/gallery/index');
var projectRouter = require('./routes/project/index');
var userRouter = require('./routes/user/index');
var uploadRouter = require('./routes/upload/index');
var badgeRouter = require('./routes/badge/index');

api.use('/tutorial', tutorialRouter);
api.use('/share', shareRouter);
api.use('/gallery', galleryRouter);
api.use('/project', projectRouter);
api.use('/user', userRouter);
api.use('/upload', uploadRouter);
api.use('/badge', badgeRouter);

// catch 404 and forward to error handler
api.use(function(req, res, next) {
Expand Down
8 changes: 8 additions & 0 deletions models/tutorial.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,14 @@ const TutorialSchema = new mongoose.Schema(
type: String,
required: true,
},
badgeId: {
type: String,
required: false,
},
issuerId: {
type: String,
required: false,
},
steps: [
{
type: StepSchema,
Expand Down
4 changes: 4 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
"docs": "apidoc -i . -e \"docs\" \"(node_modules)\" -o docs"
},
"dependencies": {
"@geobadges/badgr-api-client": "^0.12.0",
"chalk": "^4.1.0",
"cookie-parser": "~1.4.4",
"cors": "^2.8.5",
Expand All @@ -25,5 +26,8 @@
"request": "^2.88.2",
"uuid": "^8.3.2",
"uuidv4": "^6.2.12"
},
"devDependencies": {
"nodemon": "^2.0.15"
}
}
59 changes: 59 additions & 0 deletions routes/badge/grantBadge.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
// jshint esversion: 8
// jshint node: true
"use strict";

const API = require("@geobadges/badgr-api-client");

const badgesClient = new API({
endpoint: process.env.BADGES_ENDPOINT,
username: process.env.BADGES_USERNAME,
password: process.env.BADGES_PASSWORD,
});

/**
* @api {post} /grant Grant a badge
* @apiName grandBadge
* @apiDescription Grant a badge to a user.
* @apiGroup Badge
*
* @apiParam {String} email Users Email
* @apiParam {String} badgeClassEntityId ID of the badge class
* @apiParam {boolean} createNotification User receives an email notification
* @apiParam {String} issuerEntityId ID of the issuer
*
* @apiSuccess (Success 200) {String} message `Badge granted successfully.`
* @apiError (On error) {Obejct} 500 Complications when granting badge
*/
const grantBadge = async (req, res) => {
try {
const { email, badgeClassEntityId, createNotification, issuerEntityId } = req.body;

if (!email) {
return res.status(400).send({
message: "email missing",
});
}

const status = await badgesClient.grant({
badgeClassEntityId,
createNotification,
email,
issuerEntityId,
});

if (!status) {
throw new Error("Could not grant badge");
}

res.status(200).send({
message: "Badge granted successfully.",
});
} catch (err) {
console.log(err);
return res.status(500).send(err);
}
};

module.exports = {
grantBadge,
};
10 changes: 10 additions & 0 deletions routes/badge/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
// jshint esversion: 8
// jshint node: true
"use strict";

var express = require("express");
var BadgeRouter = express.Router();

BadgeRouter.route("/grant").post(require("./grantBadge").grantBadge);

module.exports = BadgeRouter;
2 changes: 2 additions & 0 deletions routes/tutorial/postTutorial.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,8 @@ const postTutorial = async function (req, res) {
creator: req.user.email,
title: req.body.title,
steps: req.body.steps,
badgeId: req.body.badgeId,
issuerId: req.body.issuerId,
};
// storing existing images in mongoDB
req.files &&
Expand Down
4 changes: 4 additions & 0 deletions routes/tutorial/putTutorial.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ const path = require("path");

const Tutorial = require("../../models/tutorial");
const User = require("../../models/user");
const { update } = require("../../models/tutorial");

/**
* @api {put} /tutorial/:tutorialId Update tutorial
Expand Down Expand Up @@ -67,7 +68,10 @@ const putTutorial = async function (req, res) {
if (owner === oldTutorial.creator) {
var updatedTutorial = {};
updatedTutorial.title = req.body.title || oldTutorial.title;
updatedTutorial.badgeId = req.body.badgeId || oldTutorial.badgeId;
updatedTutorial.issuerId = req.body.issuerId || oldTutorial.issuerId;
updatedTutorial.steps = req.body.steps || oldTutorial.steps;

// ensure that the requirement is not related to the tutorial itself
if (updatedTutorial.steps[0].requirements) {
updatedTutorial.steps[0].requirements =
Expand Down