-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
44 lines (35 loc) · 1.4 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
var express = require('express')
var bodyParser = require('body-parser')
var SantaSelector = require('./select-santa.js');
var Mandrill = require('./mandrill.js');
var app = express();
app.use(bodyParser.urlencoded({ extended: true }));
app.set('port', (process.env.PORT || 5000));
app.use(express.static(__dirname + '/public'));
app.engine('html', require('ejs').renderFile);
app.set('view engine', 'ejs');
app.get('/', function(request, response) {
response.render('index.html');
});
app.post('/submit', function(request, response) {
var emailSubject = request.body.email_subject;
var emailBody = request.body.email_body;
var namesEmailArray = request.body.peeps; // ["name1:email1", "name2:email2"]
var contactBook = {}; // {"name1":"email1", "name2":"email2"}
var namesOnly = []; // ["name1", "name2"]
for (person in namesEmailArray) {
var parts = namesEmailArray[person].split(":");
var name = parts[0];
var email = parts[1];
namesOnly.push(name);
contactBook[name] = email;
}
var ss = new SantaSelector(namesOnly);
var santaToChild = ss.validDraw();
var md = new Mandrill(santaToChild, contactBook, emailSubject, emailBody);
var mandrillResponse = md.sendEmails();
response.render('finish.html', { santaToChild: santaToChild });
});
app.listen(app.get('port'), function() {
console.log('Secret santa app is running at localhost:' + app.get('post'));
});