-
Notifications
You must be signed in to change notification settings - Fork 14
/
app.js
188 lines (167 loc) · 5.9 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
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
var fs = require('fs')
var path = require('path')
var express = require('express')
var app = express()
var json2csv = require('json2csv')
var _ = require('underscore')
var favicon = require('serve-favicon')
var hbs = require('hbs')
var report = require('./report')
var port = 5070
var admin = require('firebase-admin')
var serviceAccount = require('./serviceAccountKey.json')
admin.initializeApp({
credential: admin.credential.cert(serviceAccount),
databaseURL: 'https://whopays-522bd.firebaseio.com'
})
var db = admin.database()
var fields = ['invite_type', 'talk_type', 'event_name', 'event_location', 'travel_type', 'ticket_type', 'fee', 'currency', 'travel_assistance', 'travel_assistance_by_employer', 'time_off', 'speaking_slot', 'speaking_slot_unit', 'prep_time', 'prep_time_unit', 'experience', 'gender', 'expertise', 'speaking_years', 'event_type', 'additional_info']
var fieldNames = ['Invite Type', 'Talk Type', 'Event Name', 'Event Location', 'Travel Type', 'Ticket Type', 'Fee', 'Currency', 'Travel Assistance', 'Travel Assistance by Employer', 'Time Off', 'Speaking Slot', 'Speaking Slot Unit', 'Prep Time', 'Prep Time Unit', 'Experience', 'Gender', 'Expertise', 'Speaking Years', 'Event Type', 'Additional Info']
var postsPerPage = 5
function submissionDate () {
var monthNames = [ 'January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December' ]
var now = new Date()
var date = now.getDate() < 10 ? 'early' : now.getDate() > 20 ? 'late' : 'mid'
var month = monthNames[now.getMonth()]
var year = now.getFullYear()
return date + ' ' + month + ' ' + year
}
function escapeHtml (unsafe) {
return unsafe
.replace(/&/g, '&')
.replace(/</g, '<')
.replace(/>/g, '>')
}
function updateIndex () {
db.ref('posts').once('value', function (snapshot) {
var data = _.toArray(snapshot.val())
db.ref('events').set(_.uniq(_.pluck(data, 'event_name')))
db.ref('locations').set(_.uniq(_.pluck(data, 'event_location')))
})
}
/*
* express server setup
*/
app.set('view engine', 'html')
app.engine('html', hbs.__express)
app.use(favicon(path.join(__dirname, '/public/img/favicon.ico')))
hbs.registerHelper('join', function (els, options) {
return new hbs.SafeString(
els.join(options.hash['delimiter'])
)
})
hbs.registerPartial('ga', fs.readFileSync(path.join(__dirname, '/views/ga.html'), 'utf8'))
hbs.registerPartial('footer', fs.readFileSync(path.join(__dirname, '/views/footer.html'), 'utf8'))
app.use(express.json())
app.use(express.urlencoded())
app.use(express.multipart())
app.use(express.static(path.join(__dirname, '/public')))
app.listen(port)
app.get('/', function (req, res) {
db.ref('posts').once('value', function (snapshot) {
var data = _.toArray(snapshot.val())
res.render('index', {posts: data.slice(0, postsPerPage), length: data.length})
}, function () {
res.render('index', {})
})
})
app.post('/submit', function (req, res) {
var ref = db.ref('posts').push()
req.body._id = ref.key
for (var i in req.body) {
req.body[i] = escapeHtml(req.body[i])
}
req.body.report = report.generate(req.body)
req.body.submitted = submissionDate()
ref.set(req.body).then(function () {
updateIndex()
res.send({id: ref.key})
}, function() {
notfound(req, res)
})
})
app.get('/data.json', function (req, res) {
db.ref('posts').once('value', function (snapshot) {
var data = snapshot.val()
res.attachment('data.json')
res.set('Content-type', 'application/json')
res.send(_.shuffle(data))
}, function () {
notfound(req, res)
})
})
app.get('/reports/page/:pageNum(\\d+)', function (req, res) {
db.ref('posts').once('value', function (snapshot) {
var data = _.toArray(snapshot.val())
var start = postsPerPage * (req.params.pageNum - 1)
var end = start + postsPerPage
var json = {
reports: data.slice(start, end),
count: data.length,
more: req.params.pageNum < Math.ceil(data.length / postsPerPage)
}
res.json(json)
}, function () {
notfound(req, res)
})
})
app.get('/autoCompleteData', function (req, res) {
db.ref('events').once('value', function (snapshot) {
var events = Object.keys(snapshot.val())
db.ref('locations').once('value', function (snapshot) {
var locations = Object.keys(snapshot.val())
res.json({
eventNames: events,
eventLocations: locations
})
})
})
})
app.get('/data.csv', function (req, res) {
db.ref('posts').once('value', function (snapshot) {
var data = snapshot.val()
json2csv({data: _.shuffle(data), fields: fields, fieldNames: fieldNames}, function (err, csv) {
if (err) { console.log(err) }
res.attachment('data.csv')
res.set('Content-type', 'text/csv')
res.send(csv)
})
}, function () {
notfound(req, res)
})
})
app.get('/lab/datatable', function (req, res) {
db.ref('posts').once('value', function (snapshot) {
var data = snapshot.val()
res.render('datatable', {data: data})
}, function () {
notfound(req, res)
})
})
app.get('/:id', function (req, res, next) {
db.ref('posts/' + req.params.id).once('value', function (snapshot) {
var data = snapshot.val()
if (!data || data.length === 0) { return notfound(req, res) }
var text = data.report.join(' ')
res.render('submission', {
_id: data._id,
submitted: data.submitted,
report: text,
edited_by_admin: data.edited_by_admin,
edit_request_issues: data.edit_request_issues,
tweet: '"' + text.substr(0, 56) + '..." - Who pays conference speakers?'
})
}, function () {
notfound(req, res)
})
})
app.use(notfound)
function notfound (req, res, next) {
res.status(404)
// respond with html page
if (req.accepts('html')) return res.render('404')
// respond with json
if (req.accepts('json')) return res.send({ error: 'Not found' })
// default to plain-text. send()
return res.type('txt').send('Page Not found')
}