2014-02-04 17:08:20 -05:00
log = require ' winston '
2014-01-22 17:57:41 -05:00
errors = require ' ../commons/errors '
2014-02-04 16:29:13 -05:00
handlers = require ( ' ../commons/mapping ' ) . handlers
2015-11-11 22:29:56 -05:00
handlerUrlOverrides = require ( ' ../commons/mapping ' ) . handlerUrlOverrides
2014-03-21 19:22:47 -04:00
mongoose = require ' mongoose '
2016-03-18 20:05:21 -04:00
slack = require ' ../slack '
2014-01-03 13:32:13 -05:00
2014-02-04 16:29: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-12-15 13:02:05 -05:00
return errors . unauthorized ( res , ' You must be an admin to view that information ' ) unless req . user ? . isAdmin ( ) or ( ' github ' in ( req . user ? . get ( ' permissions ' ) ? [ ] ) )
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
2014-03-21 20:20:47 -04:00
unless req . user ? . isAdmin ( )
delete doc . email for doc in docs
2014-03-21 19:22:47 -04:00
res . send docs
res . end
2015-11-11 22:29:56 -05:00
bindHandler = (name, module) ->
routeHandler = (req, res) ->
res . setHeader ( ' Content-Type ' , ' application/json ' )
parts = req . path [ 4 . . ] . split ( ' / ' )
2014-01-03 13:32:13 -05:00
2015-11-12 15:01:33 -05:00
2015-11-11 22:29:56 -05:00
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
2015-11-11 22:29:56 -05:00
try
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 '
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 . . ] . . . )
catch error
errorMessage = " Error trying db method #{ req ? . route ? . method } route #{ parts } from #{ name } : #{ error } "
if req . user ?
userInfo = req . user . getUserInfo ( )
errorMessage += " \n -- User Info Id: #{ userInfo . id } #{ if req . user . isAnonymous ( ) then ' ' else ' Email: ' } #{ userInfo . email } "
log . error ( errorMessage )
log . error ( error )
log . error ( error . stack )
# TODO: Generally ignore this error: error: Error trying db method get route analytics.log.event from undefined: Error: Cannot find module '../undefined'
unless " #{ parts } " in [ ' analytics.users.active ' ]
2016-03-21 08:36:44 -04:00
slack . sendSlackMessage errorMessage , [ ' ops ' ] , papertrail: true
2015-11-11 22:29:56 -05:00
errors . notFound ( res , " Route #{ req ? . path } not found. " )
app . all ' /db/ ' + moduleName + ' /* ' , routeHandler
app . all ' /db/ ' + moduleName , routeHandler
2015-11-12 15:01:33 -05:00
app . get ' /db/:module/schema ' , (req, res) ->
res . setHeader ( ' Content-Type ' , ' application/json ' )
getSchema req , res , req . params . module
2015-11-11 22:29:56 -05:00
for moduleNameUnder of handlers
name = handlers [ moduleNameUnder ]
moduleName = handlerUrlOverrides [ moduleNameUnder ] or moduleNameUnder
bindHandler name , moduleName
# Fall back for URLs that start with /db/ but dont map to a handler.
app . all ' /db/* ' , (req, res) ->
res . setHeader ( ' Content-Type ' , ' text/plain ' )
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 ' . ' , ' _ '
2014-04-12 13:51:02 -04:00
schema = require ( ' ../../app/schemas/models/ ' + name )
2014-01-23 17:06:12 -05:00
2014-04-08 17:10:50 -04:00
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 } " )
2014-01-14 17:13:47 -05:00
errors . notFound ( res , " Schema #{ moduleName } not found. " )