Skip to content

Commit

Permalink
Merge pull request #66 from tidepool-org/krystophv/in-clinic
Browse files Browse the repository at this point in the history
creation and confirmation of custodial accounts
  • Loading branch information
gniezen committed Jun 2, 2016
2 parents e4581b4 + 7a7182b commit 8115778
Show file tree
Hide file tree
Showing 2 changed files with 87 additions and 0 deletions.
1 change: 1 addition & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -783,6 +783,7 @@ module.exports = function (config, deps) {
* User
*/
acceptTerms: user.acceptTerms,
createCustodialAccount: user.createCustodialAccount,
destroySession: user.destroySession,
getCurrentUser: user.getCurrentUser,
getUserId: user.getUserId,
Expand Down
86 changes: 86 additions & 0 deletions user.js
Original file line number Diff line number Diff line change
Expand Up @@ -310,6 +310,91 @@ module.exports = function (common, config, deps) {
return cb(null,res.body);
});
}
/**
* Create a custodial account for the logged in user
*
* @param profile {Object} profile for account that is being created for
* @param cb
* @returns {cb} cb(err, response)
*/
function createCustodialAccount(profile, cb) {

if (_.isEmpty(profile.fullName)) {
return cb({ status : common.STATUS_BAD_REQUEST, message: 'Must specify a fullName' });
}

var custodialUser = {};
// create an custodial account to attach to ours
function createAccount(next){
superagent
.post(common.makeAPIUrl('/auth/user/' + getUserId() + '/user'))
.set(common.SESSION_TOKEN_HEADER, getUserToken())
.send({})
.end(
function (err, res) {
if (err != null) {
return next(err);
}
if(res.status === 201){
custodialUser.id = res.body.userid;
return next(null,{userid:res.body.userid});
}
return next({status:res.status,message:res.error});
});
}
//add a profile name to the child account
function createProfile(next){
superagent
.put(common.makeAPIUrl('/metadata/'+ custodialUser.id + '/profile'))
.send(profile)
.set(common.SESSION_TOKEN_HEADER, getUserToken())
.end(
function (err, res) {
if (err != null) {
return next(err);
}
if(res.status === 200){
return next(null,res.body);
}
return next({status:res.status,message:res.error});
});
}
// optionally send a confirmation email if email was provided
function sendEmailConfirmation(next){
if(_.isEmpty(profile.emails)){
return next(null);
}
superagent
.post(common.makeAPIUrl('/confirm/send/signup/'+custodialUser.id))
.set(common.SESSION_TOKEN_HEADER, getUserToken())
.send({})
.end(
function (err, res) {
if (err != null) {
return next(err);
}
if(res.status === 200){
return next(null,res.body);
}
return next({status:res.status,message:res.error});
});
}

async.series([
createAccount,
createProfile,
sendEmailConfirmation
], function(err, results) {
if(_.isEmpty(err)){
var acct = {
userid: results[0].userid,
profile: results[1]
};
return cb(null,acct);
}
return cb(err);
});
}
/**
* Update current user account info
*
Expand Down Expand Up @@ -356,6 +441,7 @@ module.exports = function (common, config, deps) {
}
return {
acceptTerms : acceptTerms,
createCustodialAccount : createCustodialAccount,
destroySession: destroySession,
getCurrentUser : getCurrentUser,
getUserId : getUserId,
Expand Down

0 comments on commit 8115778

Please sign in to comment.