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

Commit 43fa7cd

Browse files
Merge pull request #269 from CoderDojo/staging
Register a Dojo & Find Dojo
2 parents 57d10ec + 654ed51 commit 43fa7cd

File tree

9 files changed

+412
-41
lines changed

9 files changed

+412
-41
lines changed

agreements.js

Lines changed: 48 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -8,46 +8,53 @@ module.exports = function (options) {
88
var ENTITY_NS = 'cd/agreements';
99

1010
seneca.add({role: plugin, cmd: 'save'}, cmd_save);
11-
seneca.add({role: plugin, cmd: 'list'}, cmd_list);
1211
seneca.add({role: plugin, cmd: 'count'}, cmd_count);
13-
seneca.add({role: plugin, cmd: 'load_user_agreement'}, cmd_load_user_agreement);
12+
seneca.add({role: plugin, cmd: 'list'}, cmd_list);
13+
seneca.add({role: plugin, cmd: 'load'}, cmd_load);
14+
seneca.add({role: plugin, cmd: 'getVersion'}, cmd_get_version);
15+
seneca.add({role: plugin, cmd: 'loadUserAgreement'}, cmd_load_user_agreement);
16+
17+
function cmd_get_version (args, done) {
18+
var version = 2;
19+
return done(null, {version: version});
20+
}
1421

1522
function cmd_save (args, done) {
1623
var agreementEntity = seneca.make$(ENTITY_NS);
1724
var agreement = args.agreement;
18-
1925
agreement.timestamp = new Date();
20-
21-
agreementEntity.load$({userId: agreement.userId}, function (err, response) {
22-
if (err) return done(err);
26+
var user = args.user;
27+
agreement.userId = user.id;
28+
async.waterfall([
29+
getCurrentVersion,
30+
loadCurrentAgreementsForUser,
31+
saveAgreement
32+
], done);
33+
function getCurrentVersion (wCb) {
34+
seneca.act({role: plugin, cmd: 'getVersion'}, function (err, response) {
35+
if (err) return done(err);
36+
agreement.agreementVersion = response.version;
37+
wCb(null, response.version);
38+
});
39+
}
40+
function loadCurrentAgreementsForUser (version, wCb) {
41+
agreementEntity.load$({userId: agreement.userId, agreementVersion: version},
42+
function (err, response) {
43+
if (err) return done(err);
44+
return wCb(null, response);
45+
});
46+
}
47+
function saveAgreement (response, wCb) {
2348
if (!response || !response.id) {
24-
agreementEntity.save$(agreement, done);
49+
agreementEntity.save$(agreement, wCb);
2550
} else {
26-
return done(null, {msg: 'Charter already signed.'});
51+
if (response && response.id) {
52+
return wCb(null, {msg: 'Charter already signed.'});
53+
} else {
54+
seneca.log.warn('Unexpected scenario when saving an agreement', response);
55+
}
2756
}
28-
});
29-
}
30-
31-
function cmd_list (args, done) {
32-
var seneca = this;
33-
34-
function get_user_agreements (userId, done) {
35-
seneca.make(ENTITY_NS).list$({userId: userId}, done);
3657
}
37-
38-
async.mapSeries(args.userIds, function (userId, done) {
39-
async.waterfall([
40-
function (done) {
41-
get_user_agreements(userId, done);
42-
},
43-
function (agreements, done) {
44-
return done(null, {
45-
userId: userId,
46-
agreements: agreements
47-
});
48-
}
49-
], done);
50-
}, done);
5158
}
5259

5360
function cmd_count (args, done) {
@@ -67,10 +74,20 @@ module.exports = function (options) {
6774
});
6875
}
6976

77+
function cmd_list (args, done) {
78+
var query = args.query;
79+
seneca.make(ENTITY_NS).list$(query, done);
80+
}
81+
82+
function cmd_load (args, done) {
83+
var id = args.id;
84+
seneca.make(ENTITY_NS).load$(id, done);
85+
}
86+
7087
function cmd_load_user_agreement (args, done) {
7188
var seneca = this;
7289

73-
seneca.make(ENTITY_NS).load$({userId: args.id}, done);
90+
seneca.make(ENTITY_NS).load$({userId: args.userId, agreementVersion: args.version}, done);
7491
}
7592

7693
return {

circle.yml

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,3 +25,16 @@ deployment:
2525
key_pattern: applications/cp-users-service-ci-{BUILD_NUM}.zip
2626
deployment_group: prod-zen
2727
deployment_config: CodeDeployDefault.OneAtATime
28+
staging:
29+
branch: staging
30+
codedeploy:
31+
cp-users-service-staging:
32+
application_root: /
33+
region: eu-west-1
34+
revision_location:
35+
revision_type: S3
36+
s3_location:
37+
bucket: zen-deployments
38+
key_pattern: staging/applications/cp-users-service-staging-{SHORT_COMMIT}-{BUILD_NUM}.zip
39+
deployment_group: staging-zen
40+
deployment_config: CodeDeployDefault.OneAtATime

lib/profiles/alias-generator.js

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
var async = require('async');
2+
3+
module.exports = function(user, profile, done) {
4+
var seneca = this;
5+
var lowerCaseFirstName = user.firstName.toLowerCase();
6+
if (user.nick) return done(null);
7+
8+
seneca.make('cd_first_name_counter').list$({
9+
firstName: lowerCaseFirstName
10+
}, function (err, results) {
11+
if (err) return done(err);
12+
var firstNameCount = results[0] || {
13+
firstName: lowerCaseFirstName,
14+
userCount: 0
15+
};
16+
firstNameCount.userCount++;
17+
var uniqNicknameFound = false;
18+
var nickname = user.firstName + firstNameCount.userCount;
19+
async.whilst(
20+
function () {
21+
return !uniqNicknameFound;
22+
},
23+
function (cb) {
24+
seneca.make('sys/user').list$({
25+
nick: nickname
26+
}, function (err, users) {
27+
if (err) return cb(err);
28+
if (users.length === 0) {
29+
uniqNicknameFound = true;
30+
} else {
31+
firstNameCount.userCount++;
32+
nickname = user.firstName + firstNameCount.userCount;
33+
}
34+
cb(null, nickname);
35+
});
36+
},
37+
function (err) {
38+
if (err) return done(err);
39+
user.nick = nickname;
40+
profile.alias = nickname;
41+
seneca.make('cd_first_name_counter').save$(firstNameCount, function (err) {
42+
if (err) return done(err);
43+
done(null);
44+
});
45+
}
46+
);
47+
});
48+
};

package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,8 @@
5959
"lab": "5.15.1",
6060
"lab-quieter-reporter": "1.0.1",
6161
"pre-commit": "1.1.2",
62-
"semistandard": "7.0.3"
62+
"semistandard": "7.0.3",
63+
"sinon": "^2.3.4",
64+
"sinon-chai": "^2.11.0"
6365
}
6466
}

profiles.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ module.exports = function (options) {
153153
var initUserType = profile.userTypes[0];
154154
var password = profile.password;
155155

156-
var nick = profile.alias || profile.name;
156+
var nick = profile.alias;
157157
profile.name = profile.firstName && profile.lastName ? profile.firstName + ' ' + profile.lastName : profile.name;
158158

159159
var user = {
@@ -168,6 +168,9 @@ module.exports = function (options) {
168168
roles: ['basic-user']
169169
};
170170

171+
// mutates user.nick & profile.alias
172+
var aliasGenerator = require('./lib/profiles/alias-generator').bind(seneca);
173+
171174
function registerUser (youth, done) {
172175
if (youth) {
173176
delete user.email;
@@ -221,6 +224,7 @@ module.exports = function (options) {
221224

222225
if (initUserType === 'attendee-o13') {
223226
async.waterfall([
227+
async.apply(aliasGenerator, user, profile),
224228
async.apply(registerUser, false),
225229
addUserToParentsDojos
226230
], function (err, res) {
@@ -229,6 +233,7 @@ module.exports = function (options) {
229233
});
230234
} else if (initUserType === 'attendee-u13') {
231235
async.waterfall([
236+
async.apply(aliasGenerator, user, profile),
232237
async.apply(registerUser, true),
233238
addUserToParentsDojos
234239
], function (err, res) {
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
CREATE TABLE IF NOT EXISTS cd_first_name_counter
2+
(
3+
id character varying NOT NULL,
4+
first_name character varying UNIQUE NOT NULL,
5+
user_count int NOT NULL,
6+
CONSTRAINT pk_cd_nick_names_id PRIMARY KEY (id)
7+
)
8+
WITH (
9+
OIDS=FALSE
10+
);

0 commit comments

Comments
 (0)