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

Update/es6 #73

Open
wants to merge 10 commits into
base: master
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
9 changes: 4 additions & 5 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ jobs:
build:
working_directory: ~/cp-badges-service
docker:
- image: node:8
- image: node:carbon
environment:
NODE_ENV: testing
steps:
Expand Down Expand Up @@ -33,12 +33,12 @@ jobs:
command: |
set -x
VER="17.03.0-ce"
curl -L -o /tmp/docker-$VER.tgz https://get.docker.com/builds/Linux/x86_64/docker-$VER.tgz
tar -xz -C /tmp -f /tmp/docker-$VER.tgz
curl -L -o /tmp/docker-"$VER".tgz https://get.docker.com/builds/Linux/x86_64/docker-"$VER".tgz
tar -xz -C /tmp -f /tmp/docker-"$VER".tgz
mv /tmp/docker/* /usr/bin
- run:
name: Deploy to K8
command: GIT_SHA1=$CIRCLE_SHA1 GIT_BRANCH=$CIRCLE_BRANCH ./.circleci/kube.sh
command: GIT_SHA1="$CIRCLE_SHA1" GIT_BRANCH="$CIRCLE_BRANCH" ./.circleci/kube.sh
workflows:
version: 2
build-test-and-deploy:
Expand All @@ -51,4 +51,3 @@ workflows:
branches:
only:
- master
- staging
21 changes: 9 additions & 12 deletions .circleci/kube.sh
Original file line number Diff line number Diff line change
Expand Up @@ -5,25 +5,22 @@ set -e
if [ "$GIT_BRANCH" = "master" ]; then
DEP_VER=latest
HOST=$PROD_HOST
echo "$PROD_CA_CERT" | base64 -i --decode > ca.pem
echo "$PROD_ADMIN_KEY" | base64 -i --decode > admin-key.pem
echo "$PROD_ADMIN_CERT" | base64 -i --decode > admin.pem
elif [ "$GIT_BRANCH" = "staging" ]; then
DEP_VER=staging
HOST=$STAGING_HOST
echo "$STAGING_CA_CERT" | base64 -i --decode > ca.pem
echo "$STAGING_ADMIN_KEY" | base64 -i --decode > admin-key.pem
echo "$STAGING_ADMIN_CERT" | base64 -i --decode > admin.pem
echo "$PROD_CA_CERT" | base64 -i --decode >ca.pem
echo "$PROD_ADMIN_KEY" | base64 -i --decode >admin-key.pem
echo "$PROD_ADMIN_CERT" | base64 -i --decode >admin.pem
else
exit 0
fi
docker build --rm=false --build-arg DEP_VERSION=$DEP_VER -t coderdojo/cp-badges-service:"$GIT_SHA1" .

TIMESTAMP=$(date +%s)

docker build --rm=false --build-arg DEP_VERSION=$DEP_VER -t coderdojo/cp-badges-service:"${GIT_SHA1}-${TIMESTAMP}" .
docker login -u "$DOCKER_USER" -p "$DOCKER_PASS" -e "$DOCKER_EMAIL"
docker push coderdojo/cp-badges-service:"$GIT_SHA1"
docker push coderdojo/cp-badges-service:"${GIT_SHA1}-${TIMESTAMP}"
curl -O https://storage.googleapis.com/kubernetes-release/release/v1.6.1/bin/linux/amd64/kubectl
chmod +x kubectl
./kubectl config set-cluster default-cluster --server=https://"$HOST" --certificate-authority=ca.pem
./kubectl config set-credentials default-admin --certificate-authority=ca.pem --client-key=admin-key.pem --client-certificate=admin.pem
./kubectl config set-context default-system --cluster=default-cluster --user=default-admin
./kubectl config use-context default-system
./kubectl patch deployment badges -p '{"spec":{"template":{"spec":{"containers":[{"name":"badges","image":"coderdojo/cp-badges-service:'"$GIT_SHA1"'"}]}}}}'
./kubectl patch deployment badges -p '{"spec":{"template":{"spec":{"containers":[{"name":"badges","image":"coderdojo/cp-badges-service:'"${GIT_SHA1}-${TIMESTAMP}"'"}]}}}}'
2 changes: 1 addition & 1 deletion .dockerignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
.git
node_modules
.gitignore
README.md
coverage
Expand All @@ -7,4 +8,3 @@ test
*.yml
*.md
*.sh
!docker-entrypoint.sh
1 change: 1 addition & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
node_modules/
3 changes: 3 additions & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module.exports = {
extends: 'coderdojo',
};
5 changes: 2 additions & 3 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
FROM node:8-alpine
MAINTAINER CoderDojo Foundation <webteam@coderdojo.org>
ARG DEP_VERSION=latest
FROM node:carbon-alpine
LABEL maintainer="CoderDojo Foundation <webteam@coderdojo.org>"
RUN apk add --update git make gcc g++ python && \
mkdir -p /usr/src/app
WORKDIR /usr/src/app
Expand Down
26 changes: 0 additions & 26 deletions appspec.yml

This file was deleted.

76 changes: 24 additions & 52 deletions config/permissions.js
Original file line number Diff line number Diff line change
@@ -1,60 +1,32 @@
'use strict';
module.exports = () => ({
'cd-badges': {
listBadges: [{ role: 'none' }],

module.exports = function () {
return {
'cd-badges': {
'listBadges': [{
role: 'none'
}],
// NOTE: Must be defined by visibility ?
getBadge: [{ role: 'none' }],

// NOTE: Must be defined by visibility ?
'getBadge': [{
role: 'none'
}],
sendBadgeApplication: [
{ role: 'basic-user', customValidator: [{ role: 'cd-dojos', cmd: 'can_award_badge' }] },
],

'sendBadgeApplication': [{
acceptBadge: [
{
role: 'basic-user',
customValidator: [{
role: 'cd-dojos',
cmd: 'can_award_badge'
}]
}],

'acceptBadge': [{
role: 'basic-user',
// TODO : this is buggy, it seems the userId in the badge is not set making the validation not working
// TODO : this is buggy,
// it seems the userId in the badge is not set making the validation not working
// customValidator: [{
// role: 'cd-badges',
// cmd: 'ownBadge'
// }]
}],
// NOTE: Must be defined by visibility ?
'loadUserBadges': [{
role: 'basic-user'
}],

'loadBadgeCategories': [{
role: 'none'
}],
'loadBadgeByCode': [{
role: 'none'
}],
'claimBadge': [{
role: 'basic-user'
}],
'exportBadges': [{
role: 'basic-user',
customValidator: [{
role: 'cd-users',
cmd: 'is_self'
}]
}],
'kpiNumberOfBadgesAwarded': [{
role: 'cdf-admin'
}],
'kpiNumberOfBadgesPublished' :[{
role: 'cdf-admin'
}]
}
};
};
},
],
// NOTE: Must be defined by visibility ?
loadUserBadges: [{ role: 'basic-user' }],
loadBadgeCategories: [{ role: 'none' }],
loadBadgeByCode: [{ role: 'none' }],
claimBadge: [{ role: 'basic-user' }],
exportBadges: [{ role: 'basic-user', customValidator: [{ role: 'cd-users', cmd: 'is_self' }] }],
kpiNumberOfBadgesAwarded: [{ role: 'cdf-admin' }],
kpiNumberOfBadgesPublished: [{ role: 'cdf-admin' }],
},
});
17 changes: 8 additions & 9 deletions config/seneca-options.js
Original file line number Diff line number Diff line change
@@ -1,21 +1,20 @@
'use strict';
var assert = require('assert');
var LogEntries = require('le_node');

var senecaOptions = {
const senecaOptions = {
transport: {
type: 'web',
web: {
timeout: 120000,
port: 10305
}
port: 10305,
},
},
apiBaseUrl: process.env.BADGEKIT_API_BASE_URL || 'http://localhost:8080',
apiSecret: process.env.BADGEKIT_API_SECRET || '',
claimCodePrefix: 'code-',
timeout: 120000,
strict: { add: false, result: false },
actcache: { active:false }
strict: {
add: false,
result: false,
},
actcache: { active: false },
};

module.exports = senecaOptions;
6 changes: 3 additions & 3 deletions dev.Dockerfile
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
FROM node:8-alpine
MAINTAINER butlerx <butlerx@notthe.cloud>
FROM node:carbon-alpine
LABEL maintainer="CoderDojo Foundation <webteam@coderdojo.org>"
WORKDIR /usr/src/app
ENV NODE_ENV development
RUN apk add --update git build-base python && \
RUN apk add --update git make gcc g++ python && \
mkdir -p /usr/src/app
COPY docker-entrypoint.sh /usr/src
EXPOSE 10305
Expand Down
4 changes: 3 additions & 1 deletion docker-entrypoint.sh
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
#! /usr/bin/env sh

set -e

cd /usr/src/app || exit
touch .yarn.sha1
OLD_SHA=$(cat .yarn.sha1)
NEW_SHA=$(sha1sum yarn.lock)
if [ "$OLD_SHA" != "$NEW_SHA" ]; then
echo "$NEW_SHA" > .yarn.sha1
echo "$NEW_SHA" >.yarn.sha1
yarn
fi
yarn dev
56 changes: 31 additions & 25 deletions lib/accept-badge.js
Original file line number Diff line number Diff line change
@@ -1,47 +1,53 @@
'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.' });
const { find } = require('lodash');

function acceptBadge(args, callback) {
const seneca = this;
const zenHostname = process.env.HOSTNAME || '127.0.0.1:8000';
const badgeData = args.badgeData;
const user = args.user;
const 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) {
seneca.act({
role: 'cd-profiles',
cmd: 'list',
query: { userId: badgeData.userId },
}, (err, response) => {
if (err) return callback(err);
var profile = response[0];
const profile = response[0];

var badgeFound = _.find(profile.badges, function (userBadge) {
return userBadge.slug === badgeData.badgeSlug;
});
const badgeFound = find(profile.badges, userBadge => 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.' });
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,
uid: `coderdojo-${user.id}${badgeFound.id}`,
recipient: {
identity: user.email,
type: 'email',
hashed: false
hashed: false,
},
badge: 'http://badgekit.coderdojo.com:8080/public/systems/coderdojo/badges/' + badgeFound.slug,
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'
url: `${protocol}://${zenHostname}/api/1.0/verify_badge/${user.id}/${badgeFound.id}/assertion`, // url should return badgeAssertion object.
type: 'hosted',
},
issuedOn: new Date()
issuedOn: new Date(),
};

seneca.act({role: 'cd-profiles', cmd: 'save', profile: profile}, callback);
seneca.act({ role: 'cd-profiles', cmd: 'save', profile }, callback);
});
}

Expand Down
18 changes: 7 additions & 11 deletions lib/add-badge-to-profile.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,14 @@
'use strict';
const _ = require('lodash');

var _ = require('lodash');

function addBadgeToProfile (args, callback) {
var seneca = this;
var profile = args.profile;
var badge = args.badge;
function addBadgeToProfile(args, callback) {
const seneca = this;
const profile = args.profile;
const badge = args.badge;
if (!profile.badges) profile.badges = [];
var badgeFound = _.find(profile.badges, function (userBadge) {
return userBadge.id === badge.id;
});
const badgeFound = _.find(profile.badges, userBadge => userBadge.id === badge.id);
if (badgeFound) return callback(null, { error: 'User has already received this badge.' });
profile.badges.push(badge);
seneca.act({ role: 'cd-profiles', cmd: 'save', profile: profile }, callback);
seneca.act({ role: 'cd-profiles', cmd: 'save', profile }, callback);
}

module.exports = addBadgeToProfile;
Loading