-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
187 lines (146 loc) · 5.47 KB
/
index.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
var Firebase = require('firebase');
var settings = require('./config');
var md5 = require('MD5');
var nodemailer = require('nodemailer');
var fs = require('fs');
var request = require("request");
var fb = new Firebase('https://hackthenorth.firebaseio.com/');
fb.auth(settings.fbToken, function(e, r) {});
var ref = fb.child('signup');
var testObject = {linkedin : "http://linkedin.com/in/kartiktalwar",
grad_year : "2015",
school : "University of Waterloo",
first_hackathon : "false",
proud_project : "I don't know about you, but I'm kind of a big deal.",
name : "Kartik Talwar",
comments : "When do I find out if I'm in?",
timestamp : 1402269531,
email : "[email protected]",
student_status : "undergraduate",
is_hardware : "true",
travel : "false",
portfolio : "https://github.com/kartiktalwar"}
var sanitizeData = function(obj) {
obj.is_hardware = obj.is_hardware === 'true';
obj.travel = obj.travel === 'true';
obj.first_hackathon = obj.first_hackathon === 'true';
return obj;
}
var makeUserObject = function(obj) {
var user = {};
var salt = settings.salt;
var sanitized = sanitizeData(obj);
user[md5(obj.email+salt)] = sanitized;
return user;
}
var createUser = function(userObj) {
var hash = Object.keys(userObj)[0]
var users = fb.child('users').child(hash);
var map = fb.child('map').child('users').child(hash);
map.set(userObj[hash].email);
if(userObj[hash].comments.trim().length > 1) {
fb.child('questions').push(userObj[hash].comments);
}
users.set(userObj[hash]);
doMath(userObj);
var html = fs.readFileSync('./emails/applicant-submission.html').toString();
var url = "https://hackthenorth.firebaseio.com/users/" + hash + "/flags/registration_email.json?auth="+settings.fbToken;
request(url, function(error, response, body) {
if(body === null || body === 'null') {
sendMail(userObj[hash].email, 'Thanks for applying to Hack the North!', html);
}
});
}
var doMath = function(user) {
var hash = Object.keys(user)[0];
var data = user[hash];
var stamp = new Date(data.timestamp*1000);
var stats = fb.child('stats');
var months = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sept', 'Oct', 'Nov', 'Dec'];
stats.child('total').child('signups').transaction(function(current) {
return current + 1;
});
var emailKey = data.email.split('@')[1].replace(/\./g, '-'); // fb doesn't allow periods in keys
stats.child('emails').child(emailKey).transaction(function(current) {
return current+1;
});
stats.child('total').child('hardware').transaction(function(current) {
if(data.is_hardware) {
return current + 1;
} else {
stats.child('total').child('software').transaction(function(curr) {
return curr+1;
});
}
});
stats.child('total').child('first_hackathon').transaction(function(current) {
if(data.first_hackathon) {
return current+1;
} else {
return current;
}
});
stats.child('graduating').child(data.grad_year).transaction(function(current) {
return current+1;
});
stats.child('total').child('travel').transaction(function(current) {
return current+1;
});
stats.child('student_status').child(data.student_status).transaction(function(current) {
return current+1;
});
stats.child('schools').transaction(function(current) {
if(current === null) {
return [data.school];
} else {
current.push(data.school);
// the schools list appends the school name to the current list
// and make the array unique yielding total registered schools
return current.reduce(function(p, c) {
if(p.indexOf(c) < 0) {
p.push(c);
}
return p;
}, []);
}
});
stats.child('signups').child((months[stamp.getMonth()]+'-'+stamp.getDate())).transaction(function(current) {
return current+1;
});
stats.child('breakdown').child((months[stamp.getMonth()]+'-'+stamp.getDate()+'-'+stamp.getHours()+'h')).transaction(function(current) {
return current+1;
});
}
var sendMail = function(to, subject, body) {
var smtpTransport = nodemailer.createTransport("SMTP",{
service: "Mailgun", // sets automatically host, port and connection security settings
auth: {
user: "[email protected]",
pass: settings.mailgunPassword //fill in actual SMTP password in prod
}
});
var mailOptions = {
headers: {
'X-Mailgun-Campaign-Id': 'registration',
'X-Mailgun-Track': 'yes',
'X-Mailgun-Track-Clicks': 'yes',
'X-Mailgun-Track-Opens': 'yes'
},
from: '"Hack the North" <[email protected]>',
to: to,
subject: subject,
html: body
}
smtpTransport.sendMail(mailOptions, function(err, res) {
if(!err) {
fb.child('users').child(md5(mailOptions.to+settings.salt)).child('flags').child('registration_email').set(res.messageId);
} else {
fb.child('errors').child(md5(mailOptions.to+settings.salt)).child('emails').child('registration_email').set(res);
}
});
}
ref.on('child_added', function(i) {
var data = makeUserObject(i.val());
createUser(data);
console.log(data);
});