forked from ionic-team/ionic-site
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtools.js
222 lines (206 loc) · 6.32 KB
/
tools.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
const config = require('./config');
const request = require('request');
const sg = require('sendgrid')(config.SENDGRID_APIKEY);
const twitter = require('twitter')({
consumer_key: config.TWITTER_CONSUMER_KEY,
consumer_secret: config.TWITTER_CONSUMER_SECRET,
access_token_key: config.TWITTER_ACCESS_TOKEN_KEY,
access_token_secret: config.TWITTER_ACCESS_TOKEN_SECRET
});
const jsforce = require('jsforce');
var sfConn = new jsforce.Connection()
module.exports = {
// CloudFlare
bustCloudflareCache: () => {
if (!config.CF_EMAIL || !config.CF_TOKEN || !config.CF_ZONE) {
// return console.log('Insufficient credentials to bust CloudFlare cache');
return; // silently fail to bust cache
}
let CFAPI = 'https://api.cloudflare.com';
let options = {
url: CFAPI + '/client/v4/zones/' + config.CF_ZONE + '/purge_cache',
method: 'DELETE',
headers: {
'X-Auth-Email': config.CF_EMAIL,
'X-Auth-Key': config.CF_TOKEN,
'Content-Type': 'application/json'
},
body: '{"purge_everything":true}'
};
request(options, (err, res, body) => {
if (err) {
return console.error(
new Error({
error: err,
response: res,
body: JSON.parse(body)
})
);
}
console.log('CloudFlare Cache busted', JSON.parse(body));
});
},
// Twitter
getTwitterProfile: () => {
if (!config.TWITTER_CONSUMER_KEY ||
!config.TWITTER_CONSUMER_SECRET ||
!config.TWITTER_ACCESS_TOKEN_KEY ||
!config.TWITTER_ACCESS_TOKEN_SECRET) {
console.warn('Twitter credentials not found. Ignoring profile request.');
return Promise.resolve(null);
}
return new Promise((resolve, reject) => {
twitter.get('users/show.json',{screen_name: 'ionicframework'}, (error, ionicframework) => {
if(error) return reject( error );
resolve(ionicframework);
});
});
},
// Sendgrid
email: (toEmails, from, fromName, subject, text) => {
if (!config.SENDGRID_APIKEY) {
console.warn('Sendgrid API keys not found, ignoring email request');
return Promise.resolve()
}
var emailData = {
method: 'POST',
path: '/v3/mail/send',
body: {
personalizations: [{
to: toEmails.map((email)=>{return {email: email}}),
subject: subject
}],
from: {
email: from,
name: fromName
},
content: [{
type: 'text/plain',
value: text
}]
}
}
return sg.API(emailData)
},
addContactList: (name) => {
if (!config.SENDGRID_APIKEY) {
console.warn('Sendgrid API keys not found, ignoring add contact request');
return Promise.resolve()
}
var requestParams = {
method: 'POST',
path: '/v3/contactdb/lists',
body: [{
name: name
}]
}
return sg.API(requestParams)
},
saveEmail: (opts) => {
if (!config.SENDGRID_APIKEY) {
console.warn('Sendgrid API keys not found, ignoring add contact request');
return Promise.resolve()
}
var requestParams = {
method: 'POST',
path: '/v3/contactdb/recipients',
body: [{
email: opts.email,
first_name: opts.first_name || null,
last_name: opts.last_name || null
}]
}
return sg.API(requestParams)
},
addEmailToList: (opts) => {
var requestParams = {
method: 'POST',
path: `/v3/contactdb/lists/${opts.list_id}/recipients/${opts.user}`,
body: {}
}
return sg.API(requestParams)
},
sendThankYouForContacting: (email) => {
return new Promise((resolve, reject) => {
// server doesn't have API keys in local env, ignore
if(!config.SENDGRID_APIKEY) {
console.warn('Sendgrid API key not found. Ignoring email request.');
return resolve(null);
}
var thankYouEmail = {
method: 'POST',
path: '/v3/mail/send',
body: {
personalizations: [{
to: [{email:email}],
subject: 'Thanks for reaching out!'
}],
from: {
email: '[email protected]',
name: 'Ionic'
},
content: [{
type: 'text/html',
value: `We’ve received your info and a member of our sales team will get in touch soon.
Until then, feel free to visit our Resource Center for additional information on our business and enterprise offerings, including customer stories and product info.
Cheers,
The Ionic Team`
}],
template_id: 'd8f22fb4-d88b-4e82-8ffa-025cb8039447',
}
}
sg.API(thankYouEmail, (error, response) => {
if (error) {
reject(error)
return console.error(error, response.body);
}
resolve(response)
})
})
},
// SalesForce
addSalesForceLead: (form) => {
return new Promise((resolve, reject) => {
if(!config.SALESFORCE_USER || !config.SALESFORCE_PASSWORD_TOKEN) {
console.warn('Salesforce API credentials not found. Ignoring CRM request.');
return reject(null);
}
sfConn.login(config.SALESFORCE_USER, config.SALESFORCE_PASSWORD_TOKEN, function(err, userInfo) {
console.log(err)
if (err) { return reject(err); }
sfConn.sobject("Lead").create({
email: form.email,
firstname: form.first_name,
lastname: form.last_name,
title: form.title,
company: form.company,
leadsource: 'Ionicframework.com',
Webpage__c: form.page,
Lead_Capture_Message__c: form.message,
NumberOfEmployees: form.Employees,
Phone: form.phone,
Lead_Life_Stage__c: 'Marketing Qualified Lead'
}).then((ret, err) => {
if (err || !ret.success) {
reject(err)
return console.error(err, ret);
}
sfConn.sobject("campaignMember").create({
LeadId: ret.id,
Status: 'Responded',
CampaignId: '701f40000008UYD'
}).then((ret, err) => {
if (err || !ret.success) {
reject(err)
return console.error(err, ret);
}
resolve(ret)
})
}).catch((err,ret)=> {
reject(err)
return console.error(err, ret);
})
})
})
}
};