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

Commit 5fc3d9b

Browse files
DanielBriertonWardormeur
authored andcommitted
Actually store firstName and lastName into DB (#241)
* Actually store firstName and lastName into DB * Edit first name/last name separately * Updated migration to migrate first/last name data * Fix registation and update e2e test data
1 parent 389f676 commit 5fc3d9b

File tree

7 files changed

+95
-45
lines changed

7 files changed

+95
-45
lines changed

config/perm/profiles.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,8 @@ module.exports = function(){
5151
role: 'cd-users',
5252
cmd: 'is_parent_of'
5353
}]
54-
}, { role: 'basic-user',
54+
}, {
55+
role: 'basic-user',
5556
customValidator: [{
5657
role: 'cd-users',
5758
cmd: 'is_self'

lib/users/is-parent-of.js

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,14 @@
22
var async = require('async');
33
var _ = require('lodash');
44

5-
65
function isParentOf (args, cb) {
76
var seneca = this;
8-
var plugin = args.role;
97
var userId = args.user.id;
108
var childrenId = args.params.userId;
119
var childrenProfileId;
12-
if(_.isUndefined(childrenId) && args.params.profile) childrenId = args.params.profile.userId;
13-
if(_.isUndefined(childrenId) && args.params.query) childrenId = args.params.query.userId;
14-
if(_.isUndefined(childrenId) && args.params.profileId) childrenProfileId = args.params.profileId;
10+
if (_.isUndefined(childrenId) && args.params.profile) childrenId = args.params.profile.userId;
11+
if (_.isUndefined(childrenId) && args.params.query) childrenId = args.params.query.userId;
12+
if (_.isUndefined(childrenId) && args.params.profileId) childrenProfileId = args.params.profileId;
1513
var getChildrenUserId = function (wfCb) {
1614
if (!childrenId && childrenProfileId) {
1715
seneca.act({role: 'cd-profiles', cmd: 'load', id: childrenProfileId}, function (err, profile) {
@@ -38,7 +36,7 @@ function isParentOf (args, cb) {
3836
} else {
3937
return done(null, false);
4038
}
41-
});
39+
});
4240
},
4341
childHasParent: function (done) {
4442
seneca.act({role: 'cd-profiles', cmd: 'load_user_profile', userId: childrenId},
@@ -52,14 +50,14 @@ function isParentOf (args, cb) {
5250
} else {
5351
return done(null, false);
5452
}
55-
});
53+
});
5654
}
5755
}, function (err, results) {
5856
if (err) {
5957
return cb(null, {'allowed': false});
6058
}
6159
return cb(null, {'allowed': results.parentHasChild && results.childHasParent});
62-
})
60+
});
6361
};
6462

6563
async.waterfall([

profiles.js

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ module.exports = function (options) {
2222

2323
var immutableFields = ['userType', 'avatar'];
2424

25-
var requiredProfileFields = ['name', 'alias', 'dob', 'country', 'place', 'address'];
25+
var requiredProfileFields = ['name', 'alias', 'dob', 'country', 'place'];
2626

2727
// var userTypes = ['champion', 'mentor', 'parent-guardian', 'attendee-o13', 'attendee-u13'];
2828
// var userTypes = ['attendee-u13', 'attendee-o13', 'parent-guardian', 'mentor', 'champion'];
@@ -88,16 +88,13 @@ module.exports = function (options) {
8888
}
8989

9090
function saveProfile (done) {
91-
var profileKeys = _.keys(profile);
92-
var missingKeys = _.difference(requiredProfileFields, profileKeys);
9391
var userId = args.user ? args.user.id : null;
94-
if (_.isEmpty(missingKeys)) profile.requiredFieldsComplete = true;
9592
if (userId !== profile.userId) return done(null, new Error('Profiles can only be saved by the profile user.'));
9693
if (profile.id) {
9794
profile = _.omit(profile, immutableFields);
9895
}
9996

100-
seneca.make$(ENTITY_NS).save$(profile, function (err, profile) {
97+
seneca.act({role: plugin, cmd: 'save', profile: profile}, function (err, profile) {
10198
if (err) return done(err);
10299
if (process.env.SALESFORCE_ENABLED === 'true') {
103100
seneca.act({ role: 'cd-profiles', cmd: 'load', id: profile.id }, function (err, fullProfile) {
@@ -154,9 +151,12 @@ module.exports = function (options) {
154151
var password = profile.password;
155152

156153
var nick = profile.alias || profile.name;
154+
profile.name = profile.firstName && profile.lastName ? profile.firstName + ' ' + profile.lastName : profile.name;
157155

158156
var user = {
159157
name: profile.name,
158+
firstName: profile.firstName,
159+
lastName: profile.lastName,
160160
nick: nick,
161161
email: profile.email,
162162
initUserType: {name: initUserType},
@@ -241,7 +241,7 @@ module.exports = function (options) {
241241
var fieldsToBeRemoved = _.union(derivedFields, immutableFields);
242242

243243
profile = _.omit(profile, fieldsToBeRemoved);
244-
seneca.make$(ENTITY_NS).save$(profile, function (err, profile) {
244+
seneca.act({role: plugin, cmd: 'save', profile: profile}, function (err, profile) {
245245
if (err) {
246246
return done(err);
247247
}
@@ -291,6 +291,7 @@ module.exports = function (options) {
291291
var profileKeys = _.keys(profile);
292292
var missingKeys = _.difference(requiredProfileFields, profileKeys);
293293
if (_.isEmpty(missingKeys)) profile.requiredFieldsComplete = true;
294+
profile.name = profile.firstName && profile.lastName ? profile.firstName + ' ' + profile.lastName : profile.name;
294295

295296
seneca.make$(ENTITY_NS).save$(profile, done);
296297
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
DO $$
2+
BEGIN
3+
BEGIN
4+
ALTER TABLE cd_profiles ADD COLUMN first_name character varying, ADD COLUMN last_name character varying;
5+
UPDATE sys_user SET first_name = name WHERE last_name IS NULL;
6+
UPDATE sys_user SET first_name = substring(name, 0, char_length(name) - char_length(last_name)) WHERE name LIKE '% ' || last_name AND last_name IS NOT NULL;
7+
UPDATE sys_user SET first_name = name WHERE first_name IS NULL;
8+
UPDATE sys_user SET name = first_name || ' ' || last_name WHERE last_name IS NOT NULL;
9+
UPDATE sys_user SET name = first_name WHERE last_name IS NULL;
10+
UPDATE cd_profiles p SET name = (SELECT name FROM sys_user s WHERE p.user_id=s.id), first_name = (SELECT first_name FROM sys_user s WHERE p.user_id=s.id), last_name = (SELECT last_name FROM sys_user s WHERE p.user_id=s.id);
11+
EXCEPTION
12+
WHEN duplicate_column THEN RAISE NOTICE 'columns first_name, last_name already exist in sys_user.';
13+
END;
14+
END;
15+
$$

test/fixtures/e2e/children.json

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,34 @@
11
[{
22
"parentEmail": "[email protected]",
33
"data": {
4-
"name": "child1",
4+
"firstName": "child1",
5+
"lastName": "of parent1",
56
"alias": "nick1",
67
"dob": "2010-01-31T22:00:00.000Z",
78
"email": "",
89
"userTypes": ["attendee-u13"],
9-
"requiredFieldsComplete": false,
10+
"requiredFieldsComplete": true,
1011
"password": ""
1112
}
1213
},
1314
{
1415
"parentEmail": "[email protected]",
1516
"data": {
16-
"name": "child2",
17+
"firstName": "child2",
18+
"lastName": "of parent1",
1719
"alias": "nick2",
1820
"dob": "2002-01-31T22:00:00.000Z",
1921
"email": "[email protected]",
2022
"userTypes": ["attendee-o13"],
21-
"requiredFieldsComplete": false,
23+
"requiredFieldsComplete": true,
2224
"password": "testchild2"
2325
}
2426
},
2527
{
2628
"parentEmail": "[email protected]",
2729
"data": {
28-
"name": "child2-1",
30+
"firstName": "child2-1",
31+
"lastName": "of parent1",
2932
"alias": "nick2-1",
3033
"dob": "2002-01-31T22:00:00.000Z",
3134
"email": "[email protected]",
@@ -37,7 +40,8 @@
3740
{
3841
"parentEmail": "[email protected]",
3942
"data": {
40-
"name": "child3",
43+
"firstName": "child3",
44+
"lastName": "of parent2",
4145
"alias": "nick3",
4246
"dob": "2010-01-31T22:00:00.000Z",
4347
"email": "[email protected]",
@@ -48,12 +52,13 @@
4852
{
4953
"parentEmail": "[email protected]",
5054
"data": {
51-
"name": "child3-1",
55+
"firstName": "child3-1",
56+
"lastName": "of parent3",
5257
"alias": "nick3-1",
5358
"dob": "2002-01-31T22:00:00.000Z",
5459
"email": "[email protected]",
5560
"userTypes": ["attendee-o13"],
56-
"requiredFieldsComplete": false,
61+
"requiredFieldsComplete": true,
5762
"private": false,
5863
"password": "testchild3-1"
5964
}

0 commit comments

Comments
 (0)