forked from rafaeljesus/newww
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathenterprise-trial-signup.js
69 lines (55 loc) · 2.14 KB
/
enterprise-trial-signup.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
var utils = require('../lib/utils');
var VError = require('verror');
var sendEmail = require('../adapters/send-email');
var CustomerAgent = require('../agents/customer');
// if they agree to the ULA, notify hubspot, create a trial and send verification link
module.exports = function trialSignup(request, reply) {
var postToHubspot = request.server.methods.npme.sendData;
var data = {
hs_context: {
pageName: "enterprise-trial-signup",
ipAddress: utils.getUserIP(request)
},
// we can trust the email is fine because we've verified it in the show-ula handler
email: request.payload.customer_email,
};
postToHubspot(process.env.HUBSPOT_FORM_NPME_AGREED_ULA, data, function(err) {
if (err) {
return reply(new VError(err, "Could not hit ULA notification form on Hubspot"));
}
new CustomerAgent().getById(data.email, function(err, customer) {
if (err) {
return reply(new VError(err, "Unknown problem with customer record"));
} else if (!customer) {
return reply(new VError("Unable to locate customer '%s'", data.email));
} else if (customer && String(customer.id) === String(request.payload.customer_id)) {
return createTrialAccount(request, reply, customer);
} else {
return reply(new VError("Unable to verify customer record '%s'", data.email));
}
});
});
};
function createTrialAccount(request, reply, customer) {
new CustomerAgent().createOnSiteTrial(customer, function(err, trial) {
if (err) {
return reply(new VError(err, "There was an error with creating a trial for %j", customer.id));
}
return sendVerificationEmail(request, reply, customer, trial);
});
}
function sendVerificationEmail(request, reply, customer, trial) {
var opts = {};
var user = {
name: customer.name,
email: customer.email,
verification_key: trial.verification_key
};
sendEmail('npme-trial-verification', user, request.redis)
.then(function() {
return reply.view('enterprise/thanks', opts);
})
.catch(function(err) {
return reply(new VError(err, "unable to send verification email to %j", customer));
});
}