/*
* auth.signupAccount()
* creates a new account + a new stripe customer
* processes the payment and creates a new recurring monthly subscription
* params are passed from the front-end signup form via router.js
*/
exports.signupAccount = function(name, email, password, referer, stripe_id, plan, session){
return new Promise(function(resolve, reject){
var accountId, planPrice;
// check if the email address is already registered with an account
account.doesExist(email, null).then(function(exists){
if (!exists)
return account.create(email, referer, stripe_id, plan);
else
throw ({ status: false, error: "emailError", message: "Account already exists" });
}).then(function(res){
// create a new stripe customer
accountId = res;
return account.createStripeCustomer(email, stripe_id);
}).then(function(customerId){
// subscribe the account to a new stipe subscription plan
return account.subscribeToPlan(accountId, customerId, plan);
}).then(function(price){
// create the user and add to account
planPrice = price;
return user.create(name, email, password, accountId, "owner");
}).then(function(userId){
// set permissions and autheticate user
session.user = userId;
session.account = accountId;
session.permission = "owner";
// send welcome email
mail.send(email, "Welcome to Gravity!", "welcome-account", { name: name, plan: plan, price: planPrice });
// redirect user to dashboard
resolve({ success: true });
}).catch(function(err){
reject(err);
});
});
}