-
-
Notifications
You must be signed in to change notification settings - Fork 186
/
streets.js
509 lines (445 loc) · 13.3 KB
/
streets.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
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
const async = require('async')
const config = require('config')
const uuid = require('uuid')
const Street = require('../../models/street.js')
const User = require('../../models/user.js')
const Sequence = require('../../models/sequence.js')
const logger = require('../../../lib/logger.js')()
const { ERRORS, requestIp } = require('../../../lib/util')
exports.post = async function (req, res) {
const street = new Street()
let body
street.id = uuid.v1()
if (req.body && (req.body.length > 0)) {
try {
body = req.body
} catch (e) {
res.status(400).send('Could not parse body as JSON.')
return
}
// TODO: Validation
street.name = body.name
street.data = body.data
street.creator_ip = requestIp(req)
}
const makeNamespacedId = async function () {
let namespacedId
try {
if (street.creator_id) {
const row = await User.findByIdAndUpdate(street.creator_id,
{ $inc: { last_street_id: 1 } },
{ new: true, upsert: true })
namespacedId = (row) ? row.last_street_id : null
} else {
const row = await Sequence.findByIdAndUpdate('streets',
{ $inc: { seq: 1 } },
{ new: true, upsert: true })
namespacedId = (row) ? row.seq : null
}
} catch (err) {
logger.error(err)
throw new Error(ERRORS.CANNOT_CREATE_STREET)
}
if (!namespacedId) {
throw new Error(ERRORS.CANNOT_CREATE_STREET)
}
return namespacedId
} // END function - makeNamespacedId
const saveStreet = async function () {
if (body && body.originalStreetId) {
let origStreet
try {
origStreet = await Street.findOne({ id: body.originalStreetId })
} catch (err) {
logger.error(err)
throw new Error(ERRORS.STREET_NOT_FOUND)
}
if (!origStreet || origStreet.status === 'DELETED') {
throw new Error(ERRORS.STREET_NOT_FOUND)
}
street.original_street_id = origStreet
const namespacedId = await makeNamespacedId()
street.namespaced_id = namespacedId
return street.save()
}
const namespacedId = await makeNamespacedId()
street.namespaced_id = namespacedId
return street.save()
} // END function - saveStreet
const handleCreatedStreet = (s) => {
s.asJson((err, streetJson) => {
if (err) {
logger.error(err)
res.status(500).send('Could not render street JSON.')
return
}
logger.info({ street: streetJson }, 'New street created.')
res.header('Location', config.restapi.baseuri + '/v1/streets/' + s.id)
res.status(201).send(streetJson)
})
}
function handleErrors (error) {
switch (error) {
case ERRORS.USER_NOT_FOUND:
res.status(404).send('User not found.')
return
case ERRORS.STREET_NOT_FOUND:
res.status(404).send('Original street not found.')
return
case ERRORS.CANNOT_CREATE_STREET:
res.status(500).send('Could not create new street ID.')
return
case ERRORS.UNAUTHORISED_ACCESS:
res.status(401).send('User with that login token not found.')
return
default:
res.status(500).end()
}
}
if (req.loginToken) {
let user
try {
user = await User.findOne({ login_tokens: { $in: [req.loginToken] } })
} catch (err) {
logger.error(err)
handleErrors(ERRORS.USER_NOT_FOUND)
}
if (!user) {
handleErrors(ERRORS.UNAUTHORISED_ACCESS)
}
street.creator_id = user
saveStreet()
.then(handleCreatedStreet)
.catch(handleErrors)
} else {
saveStreet()
.then(handleCreatedStreet)
.catch(handleErrors)
}
} // END function - exports.post
exports.delete = async function (req, res) {
if (!req.loginToken) {
res.status(401).end()
return
}
if (!req.params.street_id) {
res.status(400).send('Please provide street ID.')
return
}
async function deleteStreet (street) {
let user
try {
user = await User.findOne({ login_tokens: { $in: [req.loginToken] } })
} catch (err) {
logger.error(err)
throw new Error(ERRORS.USER_NOT_FOUND)
}
if (!user) {
throw new Error(ERRORS.UNAUTHORISED_ACCESS)
}
if (!street.creator_id) {
throw new Error(ERRORS.FORBIDDEN_REQUEST)
}
if (street.creator_id.toString() !== user._id.toString()) {
throw new Error(ERRORS.FORBIDDEN_REQUEST)
}
street.status = 'DELETED'
return street.save()
}
function handleErrors (error) {
switch (error) {
case ERRORS.USER_NOT_FOUND:
res.status(404).send('User not found.')
return
case ERRORS.STREET_NOT_FOUND:
res.status(404).send('Could not find street.')
return
case ERRORS.UNAUTHORISED_ACCESS:
res.status(401).send('User is not signed-in.')
return
case ERRORS.FORBIDDEN_REQUEST:
res.status(403).send('Signed-in user cannot delete this street.')
return
default:
res.status(500).end()
}
}
let street
try {
street = await Street.findOne({ id: req.params.street_id })
} catch (err) {
logger.error(err)
handleErrors(ERRORS.STREET_NOT_FOUND)
}
if (!street) {
res.status(204).end()
return
}
deleteStreet(street)
.then(street => { res.status(204).end() })
.catch(handleErrors)
} // END function - exports.delete
exports.get = async function (req, res) {
if (!req.params.street_id) {
res.status(400).send('Please provide street ID.')
return
}
let street
try {
street = await Street.findOne({ id: req.params.street_id })
} catch (err) {
logger.error(err)
res.status(500).send('Could not find street.')
}
if (!street) {
res.status(404).send('Could not find street.')
return
}
if (street.status === 'DELETED') {
res.status(410).send('Could not find street.')
return
}
res.header('Last-Modified', street.updated_at)
if (req.method === 'HEAD') {
res.status(204).end()
return
}
street.asJson(function (err, streetJson) {
if (err) {
logger.error(err)
res.status(500).send('Could not render street JSON.')
return
}
res.set('Access-Control-Allow-Origin', '*')
res.set('Location', config.restapi.baseuri + '/v1/streets/' + street.id)
res.status(200).send(streetJson)
})
} // END function - exports.get
exports.find = function (req, res) {
const creatorId = req.query.creatorId
const namespacedId = req.query.namespacedId
const start = (req.query.start && parseInt(req.query.start, 10)) || 0
const count = (req.query.count && parseInt(req.query.count, 10)) || 20
const findStreetWithCreatorId = async function (creatorId) {
let user
try {
user = await User.findOne({ id: creatorId })
} catch (err) {
logger.error(err)
handleErrors(ERRORS.USER_NOT_FOUND)
}
if (!user) {
throw new Error(ERRORS.USER_NOT_FOUND)
}
return Street.findOne({ namespaced_id: namespacedId, creator_id: user._id })
} // END function - findStreetWithCreatorId
const findStreetWithNamespacedId = async function (namespacedId) {
return Street.findOne({ namespaced_id: namespacedId, creator_id: null })
}
const findStreets = async function (start, count) {
return Promise.all([
Street.count({ status: 'ACTIVE' }),
Street.find({ status: 'ACTIVE' })
.sort({ updated_at: 'descending' })
.skip(start)
.limit(count)
.exec()
])
} // END function - findStreets
const handleFindStreet = function (street) {
if (!street) {
handleErrors(ERRORS.STREET_NOT_FOUND)
}
if (street.status === 'DELETED') {
handleErrors(ERRORS.STREET_DELETED)
}
res.set('Access-Control-Allow-Origin', '*')
res.set('Location', config.restapi.baseuri + '/v1/streets/' + street.id)
res.set('Content-Length', 0)
res.status(307).end()
} // END function - handleFindStreet
const handleFindStreets = function (results) {
const totalNumStreets = results[0]
const streets = results[1]
const selfUri = config.restapi.baseuri + '/v1/streets?start=' + start + '&count=' + count
const json = {
meta: {
links: {
self: selfUri
}
},
streets: []
}
if (start > 0) {
let prevStart, prevCount
if (start >= count) {
prevStart = start - count
prevCount = count
} else {
prevStart = 0
prevCount = start
}
json.meta.links.prev = config.restapi.baseuri + '/v1/streets?start=' + prevStart + '&count=' + prevCount
}
if (start + streets.length < totalNumStreets) {
const nextStart = start + count
const nextCount = Math.min(count, totalNumStreets - start - streets.length)
json.meta.links.next = config.restapi.baseuri + '/v1/streets?start=' + nextStart + '&count=' + nextCount
}
async.map(
streets,
function (street, callback) { street.asJson(callback) },
function (err, results) {
if (err) {
logger.error(err)
res.status(500).send('Could not append street.')
return
}
json.streets = results
res.status(200).send(json)
}) // END - async.map
} // END function - handleFindStreets
function handleErrors (error) {
switch (error) {
case ERRORS.USER_NOT_FOUND:
res.status(404).send('Creator not found.')
return
case ERRORS.STREET_NOT_FOUND:
res.status(404).send('Could not find street.')
return
case ERRORS.STREET_DELETED:
res.status(410).send('Could not find street.')
return
case ERRORS.UNAUTHORISED_ACCESS:
res.status(401).send('User is not signed-in.')
return
case ERRORS.FORBIDDEN_REQUEST:
res.status(403).send('Signed-in user cannot delete this street.')
return
default:
res.status(500).end()
}
} // END function - handleErrors
if (creatorId) {
findStreetWithCreatorId(creatorId)
.then(handleFindStreet)
.catch(handleErrors)
} else if (namespacedId) {
findStreetWithNamespacedId(namespacedId)
.then(handleFindStreet)
.catch(handleErrors)
} else {
findStreets(start, count)
.then(handleFindStreets)
.catch(handleErrors)
}
} // END function - exports.find
exports.put = async function (req, res) {
let body
if (req.body) {
try {
body = req.body
} catch (e) {
res.status(400).send('Could not parse body as JSON.')
return
}
} else {
res.status(400).send('Street information not specified.')
return
}
if (!req.params.street_id) {
res.status(400).send('Please provide street ID.')
return
}
async function updateStreetData (street) {
street.name = body.name || street.name
street.data = body.data || street.data
if (body.originalStreetId) {
let origStreet
try {
origStreet = await Street.findOne({ id: body.originalStreetId })
} catch (err) {
logger.error(err)
handleErrors(ERRORS.CANNOT_UPDATE_STREET)
}
if (!origStreet) {
throw new Error(ERRORS.STREET_NOT_FOUND)
}
street.original_street_id = origStreet
return street.save()
} else {
return street.save()
}
} // END function - updateStreetData
async function updateStreetWithCreatorId (street) {
let user
try {
user = await User.findOne({ login_tokens: { $in: [req.loginToken] } })
} catch (err) {
logger.error(err)
handleErrors(ERRORS.CANNOT_UPDATE_STREET)
}
if (!user) {
throw new Error(ERRORS.UNAUTHORISED_ACCESS)
}
if (!street.creator_id) {
throw new Error(ERRORS.FORBIDDEN_REQUEST)
}
if (street.creator_id.toString() !== user._id.toString()) {
throw new Error(ERRORS.FORBIDDEN_REQUEST)
}
return updateStreetData(street)
} // END function - updateStreetWithCreatorId
function handleErrors (error) {
switch (error) {
case ERRORS.USER_NOT_FOUND:
res.status(404).send('Creator not found.')
return
case ERRORS.STREET_NOT_FOUND:
res.status(404).send('Original street not found.')
return
case ERRORS.STREET_DELETED:
res.status(410).send('Could not find street.')
return
case ERRORS.CANNOT_UPDATE_STREET:
res.status(500).send('Could not update street.')
return
case ERRORS.UNAUTHORISED_ACCESS:
res.status(401).send('User is not signed-in.')
return
case ERRORS.FORBIDDEN_REQUEST:
res.status(403).send('Signed-in user cannot update this street.')
return
default:
res.status(500).end()
}
} // END function - handleErrors
let street
try {
street = await Street.findOne({ id: req.params.street_id })
} catch (err) {
logger.error(err)
handleErrors(ERRORS.CANNOT_UPDATE_STREET)
}
if (!street) {
handleErrors(ERRORS.STREET_NOT_FOUND)
return
}
if (street.status === 'DELETED') {
handleErrors(ERRORS.STREET_DELETED)
return
}
if (!street.creator_id) {
updateStreetData(street)
.then(street => { res.status(204).end() })
.catch(handleErrors)
} else {
if (!req.loginToken) {
res.status(401).end()
return
}
updateStreetWithCreatorId(street)
.then(street => { res.status(204).end() })
.catch(handleErrors)
}
} // END function - exports.put