This repository has been archived by the owner on Dec 14, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 29
/
profiles.js
842 lines (725 loc) · 30.9 KB
/
profiles.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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
'use strict';
var pg = require('pg');
var LargeObjectManager = require('pg-large-object').LargeObjectManager;
var shortid = require('shortid');
var moment = require('moment');
module.exports = function (options) {
var seneca = this;
var ENTITY_NS = 'cd/profiles';
var plugin = 'cd-profiles';
var _ = require('lodash');
var async = require('async');
var hiddenFields = require('./data/hidden-fields.js');
var syncedFields = [
'name',
'firstName',
'lastName',
'email',
'phone'
];
var immutableFields = ['userType', 'avatar'];
var requiredProfileFields = ['name', 'country'];
// var userTypes = ['champion', 'mentor', 'parent-guardian', 'attendee-o13', 'attendee-u13'];
// var userTypes = ['attendee-u13', 'attendee-o13', 'parent-guardian', 'mentor', 'champion'];
seneca.add({role: plugin, cmd: 'create'}, cmd_create);
seneca.add({role: plugin, cmd: 'user_profile_data'}, require('./lib/profiles/user-profile-data'));
seneca.add({role: plugin, cmd: 'load'}, cmd_load);
seneca.add({role: plugin, cmd: 'load_user_profile'}, cmd_load_user_profile);
seneca.add({role: plugin, cmd: 'save-youth-profile'}, cmd_save_youth_profile);
seneca.add({role: plugin, cmd: 'save'}, cmd_save);
seneca.add({role: plugin, cmd: 'update-youth-profile'}, cmd_update_youth);
seneca.add({role: plugin, cmd: 'invite-parent-guardian'}, cmd_invite_parent_guardian);
seneca.add({role: plugin, cmd: 'search'}, cmd_search);
seneca.add({role: plugin, cmd: 'accept-parent-invite'}, cmd_accept_parent_invite);
seneca.add({role: plugin, cmd: 'load_hidden_fields'}, cmd_load_hidden_fields);
seneca.add({role: plugin, cmd: 'list'}, cmd_list);
seneca.add({role: plugin, cmd: 'change_avatar'}, cmd_change_avatar);
seneca.add({role: plugin, cmd: 'get_avatar'}, cmd_get_avatar);
seneca.add({role: plugin, cmd: 'load_parents_for_user'}, require('./lib/profiles/load-parents-for-user'));
seneca.add({role: plugin, cmd: 'load_children_for_user'}, require('./lib/profiles/load-children-for-user'));
seneca.add({role: plugin, cmd: 'invite_ninja'}, cmd_invite_ninja);
seneca.add({role: plugin, cmd: 'approve_invite_ninja'}, cmd_approve_invite_ninja);
// Legacy call, replaced by load_children_for_user
seneca.add({role: plugin, cmd: 'ninjas_for_user'}, require('./lib/profiles/load-children-for-user'));
// Perms
seneca.add({role: plugin, cmd: 'is_own_profile'}, require('./lib/profiles/is-own-profile'));
// one-shot
seneca.add({role: plugin, cmd: '1202_syncSysUserName'}, require('./lib/fixes/1202-sync-sys-user-profile'));
function cmd_search (args, done) {
if (!args.query) {
return done(new Error('Empty query'));
}
seneca.make$(ENTITY_NS).list$(args.query, done);
}
function cmd_create (args, done) {
var profile = args.profile;
if (!args.user) return done(null, {ok: false, why: 'args.user is undefined'});
async.series([
validateRequest,
saveProfile
], function (err, res) {
if (err) return done(null, {ok: false, why: err.message});
return done(null, res);
});
function validateRequest (done) {
var profileEntity = seneca.make$(ENTITY_NS);
profileEntity.load$(profile.id, function (err, originalProfile) {
if (err) return done(err);
if (!originalProfile) return done();
if (originalProfile.email !== profile.email) {
seneca.act({role: 'cd-users', cmd: 'list', query: {nick: profile.email}}, function (err, users) {
if (err) return done(err);
if (!_.isEmpty(users)) return done(new Error('This email is already associated with an account.'));
return done();
});
} else {
return done();
}
});
}
function saveProfile (done) {
var userId = args.user ? args.user.id : null;
if (userId !== profile.userId) return done(null, new Error('Profiles can only be saved by the profile user.'));
var cleanedProfile = _.omit(profile, 'user');
// Fields accessible from saveProfile that needs to be passed down to the user entity
if (profile.id) {
cleanedProfile = _.omit(cleanedProfile, immutableFields);
}
seneca.act({role: plugin, cmd: 'save', profile: cleanedProfile}, function (err, retProfile) {
if (err) return done(err);
// TODO: use seneca-mesh to avoid coupling the integration to the user
if (args.user && !_.isEmpty(retProfile.email) && args.user.lmsId && args.user.email !== retProfile.email) {
seneca.act({role: 'cd-users', cmd: 'update_lms_user', lmsId: args.user.lmsId, userEmail: args.user.email, profileEmail: retProfile.email});
}
// save mutates the profile values, like "name" from "firstName" + "lastName" and need to be ported to the synced sys_user
var syncProfile = _.omit(retProfile, immutableFields);
syncProfile.user = profile.user;
syncUserObj(syncProfile, function (err, res) {
if (err) return done(err);
syncForumProfile(retProfile, function (err, res) {
if (err) seneca.log.error(err);
var query = {userId: profile.userId};
seneca.act({role: plugin, cmd: 'load'}, query, done);
});
});
});
}
}
function syncUserObj (profile, done) {
var updatedFields = {};
var userFields = ['mailingList', 'termsConditionsAccepted'];
updatedFields.id = profile.userId;
_.each(syncedFields, function (field) {
updatedFields[field] = profile[field];
});
_.extend(updatedFields, _.pick(profile.user, userFields));
if (updatedFields.email) updatedFields.nick = profile.email;
seneca.act({role: 'cd-users', cmd: 'update', user: updatedFields}, done);
}
function syncForumProfile (profile, done) {
var forumProfile = _.clone(profile);
forumProfile.username = forumProfile.name;
seneca.act({role: 'cd-nodebb-api', cmd: 'update', user: forumProfile, id: forumProfile.userId}, done);
}
function cmd_save_youth_profile (args, done) {
var profile = args.profile;
var userId = args.user ? args.user.id : null;
profile.parents = [];
profile.parents.push(userId);
if (profile.id) {
profile = _.omit(profile, immutableFields);
}
var initUserType = profile.userTypes[0];
var password = profile.password;
var nick = profile.alias;
profile.name = profile.firstName && profile.lastName ? profile.firstName + ' ' + profile.lastName : profile.name;
var user = {
name: profile.name,
firstName: profile.firstName,
lastName: profile.lastName,
nick: nick,
email: profile.email,
initUserType: {name: initUserType},
password: password,
mailingList: profile.user ? profile.user.mailingList : 0,
roles: ['basic-user']
};
// mutates user.nick & profile.alias
var aliasGenerator = require('./lib/profiles/alias-generator').bind(seneca);
function registerUser (youth, done) {
if (youth) {
delete user.email;
delete user.password;
delete profile.email;
}
seneca.act({role: 'user', cmd: 'register'}, user, function (err, data) {
if (err) return done(err);
if (!data.ok) return done(data.why);
profile.userId = data && data.user && data.user.id;
profile.userType = data && data.user && data.user.initUserType && data.user.initUserType.name;
profile = _.omit(profile, ['userTypes', 'password']);
_.defaults(profile, {private: true});
var userId = args.user ? args.user.id : null;
saveChild(profile, userId, done);
});
}
function addUserToParentsDojos (profile, done) {
var parentsDojos = [];
var userType = profile.userType;
async.each(profile.parents, function (parent, cb) {
// Load parents dojos
var query = {userId: parent};
seneca.act({role: 'cd-dojos', cmd: 'load_usersdojos', query: query}, function (err, usersDojos) {
if (err) return done(err);
_.each(usersDojos, function (userDojo) {
parentsDojos.push(userDojo.dojoId);
});
cb();
});
}, function (err) {
if (err) return done(err);
async.each(parentsDojos, function (parentDojo, cb) {
var userDojo = {
userId: profile.userId,
owner: 0,
dojoId: parentDojo,
userTypes: [userType]
};
seneca.act({role: 'cd-dojos', cmd: 'save_usersdojos', userDojo: userDojo, user: args.user}, cb);
}, function (err, res) {
if (err) return done(err);
return done(null, profile);
});
});
}
if (initUserType === 'attendee-o13') {
async.waterfall([
async.apply(aliasGenerator, user, profile),
async.apply(registerUser, false),
addUserToParentsDojos
], function (err, res) {
if (err) return done(null, {error: err});
return done(null, res);
});
} else if (initUserType === 'attendee-u13') {
async.waterfall([
async.apply(aliasGenerator, user, profile),
async.apply(registerUser, true),
addUserToParentsDojos
], function (err, res) {
if (err) return done(null, {error: err});
return done(null, res);
});
}
}
function cmd_update_youth (args, done) {
var profile = args.profile;
var derivedFields = ['password', 'userTypes', 'myChild', 'ownProfileFlag', 'dojos'];
var fieldsToBeRemoved = _.union(derivedFields, immutableFields);
profile = _.omit(profile, fieldsToBeRemoved);
var cleanedProfile = _.omit(profile, 'user');
seneca.act({ role: plugin, cmd: 'load', id: cleanedProfile.id }, function (err, requestedProfile) {
if (err) return done(err);
seneca.act({role: plugin, cmd: 'save', profile: cleanedProfile}, function (err, savedProfile) {
if (err) {
return done(err);
}
// save mutates the profile values, like "name" from "firstName" + "lastName" and need to be ported to the synced sys_user
savedProfile = _.omit(savedProfile, fieldsToBeRemoved);
savedProfile.user = profile.user;
savedProfile.userId = requestedProfile.userId;
syncUserObj(savedProfile, function (err, res) {
if (err) return done(err);
return done(null, profile);
});
});
});
}
// Note : tbdeleted
function saveChild (profile, parentId, done) {
if (_.includes(profile.parents, parentId)) {
delete profile.user;
seneca.make$(ENTITY_NS).save$(profile, function (err, profile) {
if (err) {
return done(err);
}
seneca.make$(ENTITY_NS).list$({userId: parentId}, function (err, results) {
var parent = results[0];
if (err) {
return done(err);
}
parent.children = parent.children ? parent.children : [];
parent.children.push(profile.userId);
parent.save$(function (err) {
if (err) {
return done(err);
}
return done(null, profile);
});
});
});
} else {
return done(new Error('Cannot save child'));
}
}
function cmd_save (args, done) {
var profile = args.profile;
if (!profile.raspberryId || !profile.name) {
// if it's a raspberry pi connected account, keep profile.name if present
profile.name = profile.firstName && profile.lastName ? profile.firstName + ' ' + profile.lastName : profile.name;
}
var profileKeys = _.keys(profile);
var missingKeys = _.difference(requiredProfileFields, profileKeys);
if (_.isEmpty(missingKeys)) profile.requiredFieldsComplete = true;
seneca.make$(ENTITY_NS).save$(profile, done);
}
function cmd_invite_parent_guardian (args, done) {
var data = args.data;
var invitedParentEmail = data.invitedParentEmail;
var childId = data.childId;
var emailSubject = data.emailSubject;
var zenHostname = process.env.HOSTNAME || '127.0.0.1:8000';
var childQuery = {
userId: childId
};
async.waterfall([
resolveParent,
resolveChild,
updateChildProfile,
sendEmail
], function (err, result) {
if (err) return done(null, {ok: false, why: err.message});
return done(null, result);
});
function resolveParent (done) {
seneca.act({role: plugin, cmd: 'search', query: {email: invitedParentEmail}}, function (err, results) {
if (err) return done(err);
return done(null, results[0]);
});
}
function resolveChild (parentProfile, done) {
if (!parentProfile) return done(new Error('Parent profile does not exist.'));
seneca.act({role: plugin, cmd: 'search'}, {query: childQuery}, function (err, results) {
if (err) return done(err);
if (_.isEmpty(results)) return done(new Error('Unable to find child profile'));
return done(null, parentProfile, results[0]);
});
}
function updateChildProfile (parentProfile, childProfile, done) {
var inviteToken = {
id: shortid.generate(),
childId: childId,
parentId: parentProfile.userId,
parentEmail: invitedParentEmail,
timestamp: new Date()
};
if (!childProfile.parentInvites) childProfile.parentInvites = [];
childProfile.parentInvites.push(inviteToken);
childProfile.parentInvites = _.chain(childProfile.parentInvites)
.sortBy(function (parentInvite) {
return parentInvite.timestamp;
})
.reverse()
.uniq(function (parentInvite) {
return parentInvite.parentEmail;
})
.value();
seneca.act({role: plugin, cmd: 'save', profile: childProfile}, function (err, result) {
if (err) return done(err);
return done(null, parentProfile, result, inviteToken);
});
}
function sendEmail (parentProfile, childProfile, inviteToken, done) {
if (!childProfile || !parentProfile) return done(new Error('An error has occured while sending email'));
var content = {
link: 'http://' + zenHostname + '/dashboard/accept_parent_guardian_request/' + childProfile.id + '/' + inviteToken.id,
childName: childProfile.name,
parentName: parentProfile.name,
year: moment(new Date()).format('YYYY')
};
var locality = args.locality || 'en_US';
var code = 'invite-parent-guardian-';
var to = invitedParentEmail;
seneca.act({role: 'email-notifications', cmd: 'send', to: to, content: content, code: code, locality: locality, subject: emailSubject}, done);
}
}
function cmd_accept_parent_invite (args, done) {
var data = args.data;
var inviteTokenId = data.inviteToken;
var childProfileId = data.childProfileId;
var requestingUserId = args.user ? args.user.id : null;
async.waterfall([
validateRequestingUserIsParent,
loadInvite,
updateParentProfile,
updateNinjaProfile,
removeInviteToken
], function (err, res) {
if (err) return done(null, {ok: false, why: err.message});
return done(null, res);
});
function validateRequestingUserIsParent (done) {
seneca.act({role: plugin, cmd: 'list', query: {userId: requestingUserId}}, function (err, requestingUserProfiles) {
if (err) return done(err);
var requestingUserProfile = requestingUserProfiles[0];
if (requestingUserProfile && requestingUserProfile.userType === 'parent-guardian') return done();
seneca.act({role: 'cd-dojos', cmd: 'load_usersdojos', query: {userId: requestingUserId}}, function (err, usersDojos) {
if (err) return done(err);
var parentTypeFound = _.find(usersDojos, function (userDojo) {
return _.includes(userDojo.userTypes, 'parent-guardian');
});
if (parentTypeFound) return done();
return done(new Error('You must have the parent/guardian user type to accept this invite'));
});
});
}
function loadInvite (done) {
seneca.act({role: plugin, cmd: 'load', id: childProfileId}, function (err, childProfile) {
if (err) return done(err);
var inviteTokenFound = _.find(childProfile.parentInvites, function (parentInvite) {
return parentInvite.id === inviteTokenId;
});
if (!inviteTokenFound) return done(new Error('Invite token not found'));
if (requestingUserId !== inviteTokenFound.parentId) return done(new Error('Only the invited parent can approve this request.'));
return done(null, inviteTokenFound);
});
}
function updateParentProfile (inviteToken, done) {
seneca.act({role: plugin, cmd: 'list', query: {userId: inviteToken.parentId}}, function (err, parentProfiles) {
if (err) return done(err);
var parentProfile = parentProfiles[0];
if (!parentProfile.children) parentProfile.children = [];
parentProfile.children.push(inviteToken.childId);
parentProfile.children = _.uniq(parentProfile.children);
seneca.act({role: plugin, cmd: 'save', profile: parentProfile}, function (err, parentProfile) {
if (err) return done(err);
return done(null, inviteToken);
});
});
}
function updateNinjaProfile (inviteToken, done) {
seneca.act({role: plugin, cmd: 'list', query: {userId: inviteToken.childId}}, function (err, ninjaProfiles) {
if (err) return done(err);
var ninjaProfile = ninjaProfiles[0];
if (!ninjaProfile.parents) ninjaProfile.parents = [];
ninjaProfile.parents.push(inviteToken.parentId);
ninjaProfile.parents = _.uniq(ninjaProfile.parents);
seneca.act({role: plugin, cmd: 'save', profile: ninjaProfile}, done);
});
}
function removeInviteToken (ninjaProfile, done) {
ninjaProfile.parentInvites = _.without(ninjaProfile.parentInvites, _.find(ninjaProfile.parentInvites, {id: inviteTokenId}));
seneca.act({role: plugin, cmd: 'save', profile: ninjaProfile}, done);
}
}
function cmd_load_hidden_fields (args, done) {
done(null, hiddenFields);
}
function cmd_change_avatar (args, done) {
var hostname = process.env.HOSTNAME || '127.0.0.1:8000';
var file = args.file;
if (!_.includes(args.fileType, 'image')) return done(null, {ok: false, why: 'Avatar upload: file must be an image.'});
if (file.length > 5242880) return done(null, {ok: false, why: 'Avatar upload: max file size of 5MB exceeded.'});
var buf = new Buffer(file.data, 'base64');
var type = buf.toString('hex', 0, 4);
var types = ['ffd8ffe0', '89504e47', '47494638'];
if (!_.includes(types, type)) return done(null, {ok: false, why: 'Avatar upload: file must be an image of type png, jpeg or gif.'});
// pg conf properties
options.postgresql.database = options.postgresql.name;
options.postgresql.user = options.postgresql.username;
pg.connect(options.postgresql, function (err, client) {
if (err) { return seneca.log.error('Could not connect to postgres', err); }
var man = new LargeObjectManager(client);
client.query('BEGIN', function (err) {
if (err) {
seneca.log.error('Unable to create transaction');
done(err);
return;
}
var bufferSize = 16384;
man.createAndWritableStream(bufferSize, function (err, oid, stream) {
var noop = function () {};
var avatarInfo = {
oid: oid.toString(),
sizeBytes: 0,
name: args.fileName,
type: args.fileType
};
if (err) {
seneca.log.error('Unable to create a new large object');
client.end();
done(err);
done = noop;
return;
}
stream.write(buf, 'base64', function () {
stream.end();
});
stream.on('data', function (chunk) {
seneca.log.info('got ' + chunk.length + ' bytes of data');
avatarInfo.sizeBytes += chunk.length;
});
stream.on('finish', function () {
seneca.log.info('Uploaded largeObject. committing...', oid);
client.query('COMMIT', function () {
client.end();
seneca.log.info('Saved LargeObject', oid);
// update profile record with avatarInfo
var profile = {
id: args.profileId,
avatar: avatarInfo
};
seneca.act({role: plugin, cmd: 'save'}, {profile: profile}, function (err, profile) {
if (err) {
return done(err);
}
seneca.make$(ENTITY_NS).load$(profile.id, function (err, profile) {
if (err) seneca.log.error(err);
var protocol = process.env.PROTOCOL || 'http';
var forumProfile = _.clone(profile);
forumProfile.username = forumProfile.name;
forumProfile.uploadedpicture = protocol + '://' + hostname + '/api/2.0/profiles/' + profile.id + '/avatar_img';
forumProfile.picture = protocol + '://' + hostname + '/api/2.0/profiles/' + profile.id + '/avatar_img';
seneca.act({role: 'cd-nodebb-api', cmd: 'update', user: forumProfile, id: forumProfile.userId}, function (err, res) {
if (err) seneca.log.error(err);
if (res.error) seneca.log.error('NodeBB Profile Sync Error: ' + res.error);
done(undefined, profile);
done = noop;
});
});
});
});
});
stream.on('error', function (err) {
seneca.log.error('postgresql filestore error', err);
done(err);
done = noop;
});
});
});
});
}
function cmd_get_avatar (args, done) {
var profileId = args.id;
// pg conf properties
options.postgresql.database = options.postgresql.name;
options.postgresql.user = options.postgresql.username;
seneca.act({role: plugin, cmd: 'load'}, {id: profileId}, function (err, profile) {
if (err) {
return done(err);
}
if (profile && profile.avatar) {
pg.connect(options.postgresql, function (err, client) {
if (err) {
seneca.log.error('Unable to connect to postgresql', err);
return done(err);
}
var man = new LargeObjectManager(client);
client.query('BEGIN', function (err) {
if (err) {
seneca.log.error('Unable to create transaction', err);
client.end();
return done(err);
}
// If you are on a high latency connection and working with
// large LargeObjects, you should increase the buffer size
var bufferSize = 16384;
man.openAndReadableStream(profile.avatar.oid, bufferSize, function (err, size, stream) {
if (err) {
seneca.log.error('Unable to open readable stream', err);
client.end();
return done(err);
}
var bufs = [];
stream.on('data', function (d) {
bufs.push(d);
});
stream.on('error', function (err) {
client.query('ROLLBACK', function () {
client.end();
done(err);
});
});
stream.on('end', function () {
client.query('COMMIT', function () {
client.end();
});
var buf = bufs.length > 1 ? Buffer.concat(bufs) : Buffer(bufs[0]);
done(null, {imageData: buf.toString('base64'), imageInfo: profile.avatar});
});
});
});
});
} else {
done();
}
});
}
function cmd_load_user_profile (args, done) {
seneca.make$(ENTITY_NS).load$({userId: args.userId}, done);
}
function cmd_load (args, done) {
seneca.make$(ENTITY_NS).load$(args.id, done);
}
function cmd_list (args, done) {
var query = args.query || {};
if (!query.limit$) query.limit$ = 'NULL';
var profilesEntity = seneca.make$(ENTITY_NS);
profilesEntity.list$(query, done);
}
function cmd_invite_ninja (args, done) {
var seneca = this;
var ninjaData = args.ninjaData;
var ninjaEmail = ninjaData.ninjaEmail;
var emailSubject = ninjaData.emailSubject;
var ninjaProfile;
var inviteToken;
async.waterfall([
validateInviteRequest,
loadParentProfile,
addTokenToParentProfile,
emailNinja
], function (err, res) {
if (err) return done(null, {ok: false, why: err.message});
return done(null, res);
});
function validateInviteRequest (done) {
// Requesting user should have parent-guardian user type.
// Ninja email should exist in cd_profiles.
// Ninja should have attendee-o13 user type.
async.series([
validateRequestingUserIsNotParentOfNinja,
validateNinjaEmailExists,
validateNinjaHasAttendeeO13UserType
], done);
function validateRequestingUserIsNotParentOfNinja (done) {
seneca.act({role: plugin, cmd: 'list', query: {email: ninjaEmail}}, function (err, ninjaProfiles) {
if (err) return done(err);
var ninjaProfile = ninjaProfiles[0];
var userId = args.user ? args.user.id : null;
if (ninjaProfile && _.includes(ninjaProfile.parents, userId)) return done(new Error('User is already a parent of this Ninja'));
return done();
});
}
function validateNinjaEmailExists (done) {
seneca.act({role: plugin, cmd: 'list', query: {email: ninjaEmail}}, function (err, ninjaProfiles) {
if (err) return done(err);
if (_.isEmpty(ninjaProfiles)) return done(new Error('Invalid invite request. Ninja email does not exist.'));
ninjaProfile = ninjaProfiles[0];
return done();
});
}
function validateNinjaHasAttendeeO13UserType (done) {
seneca.act({role: 'cd-dojos', cmd: 'load_usersdojos', query: {userId: ninjaProfile.userId}}, function (err, ninjaUsersDojos) {
if (err) return done(err);
var attendeeO13TypeFound = _.find(ninjaUsersDojos, function (ninjaUserDojo) {
return _.includes(ninjaUserDojo.userTypes, 'attendee-o13');
});
if (attendeeO13TypeFound || ninjaProfile.userType === 'attendee-o13') return done();
return done(new Error('Ninja must be an over 13 attendee'));
});
}
}
function loadParentProfile (validationResponse, done) {
var userId = args.user ? args.user.id : null;
seneca.act({role: plugin, cmd: 'list'}, {query: {userId: userId}}, done);
}
function addTokenToParentProfile (parentProfiles, done) {
var parentProfile = parentProfiles[0];
inviteToken = {
id: shortid.generate(),
ninjaEmail: ninjaEmail,
parentProfileId: parentProfile.id,
timestamp: new Date()
};
if (!parentProfile.ninjaInvites) parentProfile.ninjaInvites = [];
parentProfile.ninjaInvites.push(inviteToken);
parentProfile.ninjaInvites = _.chain(parentProfile.ninjaInvites)
.sortBy(function (ninjaInvite) {
return ninjaInvite.timestamp;
})
.reverse()
.uniq(function (ninjaInvite) {
return ninjaInvite.ninjaEmail;
})
.value();
seneca.act({role: plugin, cmd: 'save', profile: parentProfile}, done);
}
function emailNinja (parentProfile, done) {
var zenHostname = process.env.HOSTNAME || '127.0.0.1:8000';
var content = {
ninjaName: ninjaProfile.name,
parentName: parentProfile.name,
parentEmail: parentProfile.email,
link: 'http://' + zenHostname + '/dashboard/approve_invite_ninja/' + inviteToken.parentProfileId + '/' + inviteToken.id,
year: moment(new Date()).format('YYYY')
};
var locality = args.locality || 'en_US';
var code = 'invite-ninja-over-13-';
seneca.act({role: 'email-notifications', cmd: 'send', to: ninjaEmail, content: content, code: code, locality: locality, subject: emailSubject}, done);
}
}
function cmd_approve_invite_ninja (args, done) {
var seneca = this;
var inviteData = args.data;
var ninjaProfile;
var parentProfile;
async.series([
validateRequest,
updateNinjaAndParentProfiles,
addNinjaToParentsDojos
], done);
function validateRequest (done) {
seneca.act({role: plugin, cmd: 'load', id: inviteData.parentProfileId}, function (err, response) {
if (err) return done(err);
parentProfile = response;
if (!parentProfile.ninjaInvites) return done(new Error('No invite tokens exist for this profile'));
var inviteTokenFound = _.find(parentProfile.ninjaInvites, function (ninjaInvite) {
return ninjaInvite.id === inviteData.inviteTokenId;
});
if (!inviteTokenFound) return done(new Error('Invalid token'));
var userId = args.user ? args.user.id : null;
seneca.act({role: plugin, cmd: 'list', query: {userId: userId}}, function (err, ninjaProfiles) {
if (err) return done(err);
ninjaProfile = ninjaProfiles[0];
if (ninjaProfile.email !== inviteTokenFound.ninjaEmail) return done(new Error('You cannot approve invite Ninja requests for other users.'));
return done();
});
});
}
function updateNinjaAndParentProfiles (done) {
// Add parent user id to Ninja parents array
// Add ninja user id to Parent children array
if (!parentProfile.children) parentProfile.children = [];
parentProfile.children.push(ninjaProfile.userId);
parentProfile.ninjaInvites = _.without(parentProfile.ninjaInvites, _.find(parentProfile.ninjaInvites, {id: inviteData.inviteTokenId}));
if (!ninjaProfile.parents) ninjaProfile.parents = [];
ninjaProfile.parents.push(parentProfile.userId);
seneca.act({role: plugin, cmd: 'save', profile: parentProfile}, function (err, response) {
if (err) return done(err);
seneca.act({role: plugin, cmd: 'save', profile: ninjaProfile}, done);
});
}
function addNinjaToParentsDojos (done) {
seneca.act({role: 'cd-dojos', cmd: 'load_usersdojos', query: {userId: parentProfile.userId}}, function (err, parentUsersDojos) {
if (err) return done(err);
async.each(parentUsersDojos, function (parentUserDojo, cb) {
seneca.act({role: 'cd-dojos', cmd: 'load_usersdojos', query: {userId: ninjaProfile.userId, dojoId: parentUserDojo.dojoId}}, function (err, ninjaUsersDojos) {
if (err) return cb(err);
if (!_.isEmpty(ninjaUsersDojos)) return cb(); // Ninja is already a member of this Dojo.
var userDojo = {
owner: 0,
userId: ninjaProfile.userId,
dojoId: parentUserDojo.dojoId,
userTypes: ['attendee-o13']
};
seneca.act({role: 'cd-dojos', cmd: 'save_usersdojos', userDojo: userDojo, user: args.user}, cb);
});
}, done);
});
}
}
return {
name: plugin
};
};