forked from sfbrigade/San-Francisco-in-Progress
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
322 lines (280 loc) · 9.09 KB
/
server.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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
// SETUP
// ================================================
var express = require('express')
, bodyParser = require('body-parser')
, mongoose = require('mongoose')
, path = require('path')
, request = require('request')
, geocoder = require('node-geocoder')('google','https', {apiKey:'AIzaSyBtoo72MlIo8Dwq0QPIAmAXIZWyrAAr9CQ'})
, request = require('request')
, createEmail = require('./example-email.js')
// , mandrill = require('node-mandrill')('7ZUL0_LO1EUZVpEF0FbzGw')
// create server
var app = express()
// configure app to use bodyParser()
app.use(bodyParser.urlencoded({ extended: true }))
app.use(bodyParser.json())
// where to serve static content
app.use( express.static('assets') )
// set our port
var port = process.env.PORT || 5000
// DATABASE
// ================================================
// connect to database
mongoose.connect('mongodb://jmcelroy:[email protected]:61731/sf-in-progress')
// SCHEMAS
// ================================================
// The SF Planning Commission holds a weekly meeting where they rule on various
// items concerning development projects.
//
// Example Data:
// projectId: ...
// location:
// Commission Chambers
// Room 400, City Hall
// 1 Dr. Carlton B. Goodlett Place
// date: 12:00 PM, June 11, 2015
// documents: ""
// type: "regular"
// id: "2013.1238CV"
// packetUrl: "http://commissions.sfplanning.org/cpcpackets/2013.1238CV.pdf"
// staffContact: {
// name: "S. VELLVE"
// phone: "(415) 558-6263"
// }
// description:
// 1238 SUTTER STREET - north side between Polk Street and Van Ness Avenue;
// Lot 011 in Assessor’s block 0670 - Request for Conditional Use
// Authorization...
// preliminaryRecommendation:
// Approve with Conditions
// action:
// Approve with Conditions as amended by staff, incorporating the desing
// comments from Commissioners; with a minimum 13’ setback on Sutter Street
var projectHearingSchema = new mongoose.Schema({
projectId: mongoose.Schema.Types.ObjectId
, location: String
, date: Date
, documents: String
, type: {
type: String
, enum: ['continuance', 'consent', 'regular', 'review']
}
, id: String
, packetUrl: String
, staffContact: {
name: String
, phone: String
}
, description: String
, preliminaryRecommendation: String
, action: String
})
var projectSchema = new mongoose.Schema({
address: String
, city: String
, neighborhood: String
, description: String
, benefits: String
, zoning: String
, units: Number
, status: String
, supervisor: String
, statusCategory: String
, picture: String
, coordinates: Array
, featured: Boolean
, sponsorFirm: String
, hearings: [projectHearingSchema]
})
// models
var Project = mongoose.model('Project', projectSchema)
var ProjectHearing = mongoose.model('ProjectHearing', projectHearingSchema)
// ROUTES
// ================================================
// landing page
app.get('/', function (req,resp){
resp.sendFile(path.join(__dirname, '/assets', '/index.html'))
})
// interactive map page
app.get('/map', function (req,resp){
resp.sendFile(path.join(__dirname, '/assets', '/map.html'))
})
// form for admins to add new projects
app.get('/projects/new', function (req, resp) {
resp.sendFile(path.join(__dirname, '/assets', '/new-project-form.html'))
})
// form for everyone to add new hearings
app.get('/hearings/new/:project_id', function (req, resp) {
resp.sendFile(path.join(__dirname, '/assets', '/new-hearing-form.html'))
})
// form for admins to update a project
app.get('/admin/projects/:project_id', function (req, resp) {
resp.sendFile(path.join(__dirname, '/assets', '/admin-form.html'))
})
// return a list of all the projects
app.get('/projects', function (req, resp){
return Project.find(function (err, projects){
if (err){
resp.send(err)
}
resp.json(projects)
})
})
// save a new project
app.post('/projects', function (req, resp) {
var saveProject = function(coords) {
var project = new Project({
name: req.body.name || ''
, address: req.body.address || ''
, city: req.body.city
, neighborhood: req.body.neighborhood || ''
, description: req.body.description || ''
, zoning: req.body.zoning || ''
, units: req.body.units || null
, status: req.body.status || ''
, website: req.body.website || ''
, picture: req.body.picture || ''
, statusCategory: determineStatusCategory(req.body.status) || ''
, coordinates: [(coords.latitude).toString(), (coords.longitude).toString()] || []
, featured: req.body.featured || false
, sponsorFirm: req.body.sponsorFirm || ''
, hearings: []
})
project.save(function(err){
if(err){
resp.send(err)
}
resp.json({message: project.address + ' saved'})
})
}
var address = req.body.address + ' ' + req.body.city
geocode(address, function(latitude,longitude) {
saveProject({'latitude': (latitude).toString(), 'longitude': (longitude).toString()})
}.bind(this))
})
app.get('/projects/featured', function (req, resp) {
return Project.find({featured: true}, function (err, projects) {
if (err) resp.send(err)
resp.json(projects)
})
})
// get a particular project
app.get('/projects/:project_id', function (req, resp){
var id = mongoose.Types.ObjectId(req.params.project_id)
return Project.findById(id, function (err, project){
if(err) resp.send(err)
resp.json(project)
})
})
// update / replace a particular project
app.post('/projects/:project_id', function (req, resp) {
var id = mongoose.Types.ObjectId(req.params.project_id)
var newDoc = req.body
newDoc.featured = req.body.featured == 'true'
var options = null
Project.findOneAndUpdate({_id: id}, newDoc, options, function (err, project) {
project.save()
resp.json({message: "project updated", project: project})
})
})
app.delete('/projects/:project_id', function (req, resp) {
var id = mongoose.Types.ObjectId(req.params.project_id)
Project.findOne({_id: id}, function (err, project) {
project.remove()
resp.sendStatus(200)
})
})
// email subscribe to a project
app.post('/subscribe/:project_id', function (req, resp) {
var projectId = mongoose.Types.ObjectId(req.params.project_id)
, email = req.body.email
, options = {}
Project.findByIdAndUpdate(
projectId
, { '$push' : {emails: email} }
, {}
, function (err, project) {
var message = createEmail(email, project.address, null)
// send email via mandrill
request.post('https://mandrillapp.com/api/1.0/messages/send.json', {form: message.confirmation}, function (err, resp) {
if (err) console.log('Mandrill error: ', err)
else {
console.log('Mandrill resp', resp.body)
}
})
resp.json({message: req.body.email + ' subscribed to project ' + projectId, project: project})
}
)
})
// project hearing form submission
app.post('/hearings/:project_id', function (req, resp) {
var projectId = mongoose.Types.ObjectId(req.params.project_id)
var hearing = new ProjectHearing({
projectId: projectId
, location: req.body.location
// TODO: make sure this is getting saved as timestamp. The result will be
// NaN if the date is not an ISO string format or numeric value.
, date: req.body.date
, packetUrl: req.body.documents
, documents: req.body.documents // any documents in addition to the pdf url
, type: req.body.hearing_type // continuance, consent, regular, review
, description: req.body.description
, staffContact: {
name: req.body.staffContactName
, phone: req.body.staffContactPhone
}
, preliminaryRecommendation: req.body.preliminaryRecommendation
// This is the outcome of the meeting. This is published in the minutes
// about 1 month after the meeting.
// , action: req.body.action
})
var options = {};
Project.findByIdAndUpdate(
projectId
, { $push : {hearings: hearing} }
, options
, function (err, project) {
// send email to subscribers to this project
var subscribers = project.emails
console.log('SUBSCRIBERS ', subscribers)
// email is null for now (defaults to [email protected])
var message = createEmail(null, project.address)
request.post('https://mandrillapp.com/api/1.0/messages/send.json', {form: message.announcement}, function (err, resp) {
if (err) console.log('Mandrill error: ', err)
else {
console.log('Mandrill resp', resp.body)
}
})
resp.sendStatus(201)
})
})
app.all('*', function(req, res){
res.sendStatus(404);
})
// START THE SERVER
// ================================================
app.listen(port)
console.log('SF in Progress is running. \n Open your browser and navigate to localhost:' + port + '/map')
// UTILITY FUNCTIONS
// ================================================
var determineStatusCategory = function determineStatusCategory(status) {
var statusCategory
if (status === "Construction") {
statusCategory = "construction"
}
else if (status.substring(0,2) === "PL" || status.substring(0,2) === "Pl") {
statusCategory = "planning"
}
else if (status.substring(0,2) === "BP") {
statusCategory = "building"
}
return statusCategory
}
var geocode = function geocode(address, callback) {
geocoder.geocode(address, function(err, resp) {
if (!err && resp.length) {
callback(resp[0].longitude, resp[0].latitude)
}
})
}