codecombat/server/routes/db.coffee

63 lines
2.8 KiB
CoffeeScript
Raw Normal View History

2014-02-04 17:08:20 -05:00
log = require 'winston'
errors = require '../commons/errors'
handlers = require('../commons/mapping').handlers
2014-03-21 19:22:47 -04:00
mongoose = require 'mongoose'
2014-11-28 11:37:31 -05:00
hipchat = require '../hipchat'
2014-01-03 13:32:13 -05:00
module.exports.setup = (app) ->
2014-03-21 19:22:47 -04:00
# This is hacky and should probably get moved somewhere else, I dunno
app.get '/db/cla.submissions', (req, res) ->
2014-06-30 22:16:26 -04:00
return errors.unauthorized(res, 'You must be an admin to view that information') unless req.user?.isAdmin()
2014-03-21 19:22:47 -04:00
res.setHeader('Content-Type', 'application/json')
collection = mongoose.connection.db.collection 'cla.submissions', (err, collection) ->
return log.error "Couldn't fetch CLA submissions because #{err}" if err
resultCursor = collection.find {}
resultCursor.toArray (err, docs) ->
return log.error "Couldn't fetch distinct CLA submissions because #{err}" if err
unless req.user?.isAdmin()
delete doc.email for doc in docs
2014-03-21 19:22:47 -04:00
res.send docs
res.end
2014-01-03 13:32:13 -05:00
app.all '/db/*', (req, res) ->
res.setHeader('Content-Type', 'application/json')
module = req.path[4..]
parts = module.split('/')
module = parts[0]
return getSchema(req, res, module) if parts[1] is 'schema'
if (not req.user) and req.route.method isnt 'get'
return errors.unauthorized(res, 'Must have an identity to do anything with the db. Do you have cookies enabled?')
2014-01-03 13:32:13 -05:00
try
2014-08-14 14:55:43 -04:00
moduleName = module.replace new RegExp('\\.', 'g'), '_'
name = handlers[moduleName]
2014-02-04 17:08:20 -05:00
handler = require('../' + name)
return handler.getLatestVersion(req, res, parts[1], parts[3]) if parts[2] is 'version'
return handler.versions(req, res, parts[1]) if parts[2] is 'versions'
return handler.files(req, res, parts[1]) if parts[2] is 'files'
return handler.getNamesByIDs(req, res) if req.route.method in ['get', 'post'] and parts[1] is 'names'
2014-02-04 17:08:20 -05:00
return handler.getByRelationship(req, res, parts[1..]...) if parts.length > 2
return handler.getById(req, res, parts[1]) if req.route.method is 'get' and parts[1]?
return handler.patch(req, res, parts[1]) if req.route.method is 'patch' and parts[1]?
handler[req.route.method](req, res, parts[1..]...)
2014-01-03 13:32:13 -05:00
catch error
2014-11-28 11:37:31 -05:00
errorMessage = "Error trying db method #{req?.route?.method} route #{parts} from #{name}: #{error}"
log.error(errorMessage)
2014-02-04 17:08:20 -05:00
log.error(error)
log.error(error.stack)
2014-11-28 11:37:31 -05:00
hipchat.sendTowerHipChatMessage errorMessage
errors.notFound(res, "Route #{req?.path} not found.")
2014-01-03 13:32:13 -05:00
getSchema = (req, res, moduleName) ->
try
2014-04-12 04:35:56 -04:00
name = moduleName.replace '.', '_'
schema = require('../../app/schemas/models/' + name)
res.send(JSON.stringify(schema, null, '\t'))
2014-01-03 13:32:13 -05:00
res.end()
catch error
2014-02-04 17:08:20 -05:00
log.error("Error trying to grab schema from #{name}: #{error}")
errors.notFound(res, "Schema #{moduleName} not found.")