-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
67 lines (59 loc) · 2.09 KB
/
app.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
/*eslint-env node*/
//------------------------------------------------------------------------------
// node.js starter application
//------------------------------------------------------------------------------
// This application uses express as its web server
// for more info, see: http://expressjs.com
var express = require('express');
// create a new express server
var app = express();
// serve the files out of ./public as our main files
app.use(express.static(__dirname + '/public'));
app.get('/process', function(req, res) {
var fs = require('fs');
var ratingsText = fs.readFileSync('ratings.json', 'utf8');
var ratingsJson = JSON.parse(ratingsText);
var participants = [];
Object.keys(ratingsJson).forEach(function(key) {
var participant = ratingsJson[key];
if (participant.email) {
var generalSurveyCompleted = false;
var sessionSurveyCompleted = 0;
if (participant.sessions) {
var sessionKeys = Object.keys(participant.sessions);
var numSession = sessionKeys.length;
generalSurveyCompleted = sessionKeys.indexOf('137') >= 0;
sessionSurveyCompleted = generalSurveyCompleted ? numSession - 1 : numSession;
}
var part = {
'email': participant.email,
'name': participant.name || '',
'generalSurvey': generalSurveyCompleted,
'sessionSurvey': sessionSurveyCompleted
}
participants.push(part);
}
});
participants.sort(
function(a, b) {
var nameA = a.name.toLowerCase(), nameB = b.name.toLowerCase();
if (nameA < nameB) //sort string ascending
return -1;
if (nameA > nameB)
return 1;
return 0; //default return value (no sorting)
}
);
fs.writeFile('raffleFeed.jsonp', 'participants=' + JSON.stringify(participants, null, 2), function (err) {
if (err) {
res.send(err);
} else {
res.send('Processing finished');
}
});
});
// Start server on the specified port and binding host
app.listen(5858, '0.0.0.0', function() {
// Print a message when the server starts listening
console.log('server starting on 5858');
});