This repository has been archived by the owner on Jun 12, 2024. It is now read-only.
forked from kobotoolbox/enketo-express
-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
changed: OC: relinted all files to prepare for merge from enketo/enke…
…to-express with code style changes
- Loading branch information
Showing
86 changed files
with
11,122 additions
and
8,418 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,125 +1,133 @@ | ||
const account = require( '../models/account-model' ); | ||
const auth = require( 'basic-auth' ); | ||
const express = require( 'express' ); | ||
const auth = require('basic-auth'); | ||
const express = require('express'); | ||
const account = require('../models/account-model'); | ||
|
||
const router = express.Router(); | ||
//const debug = require( 'debug' )( 'account-controller' ); | ||
// const debug = require( 'debug' )( 'account-controller' ); | ||
|
||
module.exports = app => { | ||
app.use( '/accounts/api/v1', router ); | ||
module.exports = (app) => { | ||
app.use('/accounts/api/v1', router); | ||
}; | ||
|
||
router | ||
.all( '*', ( req, res, next ) => { | ||
.all('*', (req, res, next) => { | ||
// set content-type to json to provide appropriate json Error responses | ||
res.set( 'Content-Type', 'application/json' ); | ||
res.set('Content-Type', 'application/json'); | ||
next(); | ||
} ) | ||
.all( '*', authCheck ) | ||
.get( '/account', getExistingAccount ) | ||
.post( '/account', getNewOrExistingAccount ) | ||
.put( '/account', updateExistingAccount ) | ||
.delete( '/account', removeAccount ) | ||
.get( '/list', getList ) | ||
.post( '/list', getList ) | ||
.all( '*', ( req, res, next ) => { | ||
const error = new Error( 'Not allowed' ); | ||
}) | ||
.all('*', authCheck) | ||
.get('/account', getExistingAccount) | ||
.post('/account', getNewOrExistingAccount) | ||
.put('/account', updateExistingAccount) | ||
.delete('/account', removeAccount) | ||
.get('/list', getList) | ||
.post('/list', getList) | ||
.all('*', (req, res, next) => { | ||
const error = new Error('Not allowed'); | ||
error.status = 405; | ||
next( error ); | ||
} ); | ||
next(error); | ||
}); | ||
|
||
function authCheck( req, res, next ) { | ||
function authCheck(req, res, next) { | ||
// check authentication and account | ||
let error; | ||
|
||
const creds = auth( req ); | ||
const key = ( creds ) ? creds.name : undefined; | ||
const creds = auth(req); | ||
const key = creds ? creds.name : undefined; | ||
|
||
if ( !key || ( key !== req.app.get( 'account manager api key' ) ) ) { | ||
error = new Error( 'Not Allowed. Invalid API key.' ); | ||
if (!key || key !== req.app.get('account manager api key')) { | ||
error = new Error('Not Allowed. Invalid API key.'); | ||
error.status = 401; | ||
res | ||
.status( error.status ) | ||
.set( 'WWW-Authenticate', 'Basic realm="Enter valid API key as user name"' ); | ||
next( error ); | ||
res.status(error.status).set( | ||
'WWW-Authenticate', | ||
'Basic realm="Enter valid API key as user name"' | ||
); | ||
next(error); | ||
} else { | ||
next(); | ||
} | ||
} | ||
|
||
function getExistingAccount( req, res, next ) { | ||
return account.get( { | ||
linkedServer: req.query.server_url, | ||
key: req.query.api_key | ||
} ) | ||
.then( account => { | ||
_render( 200, account, res ); | ||
} ) | ||
.catch( next ); | ||
function getExistingAccount(req, res, next) { | ||
return account | ||
.get({ | ||
linkedServer: req.query.server_url, | ||
key: req.query.api_key, | ||
}) | ||
.then((account) => { | ||
_render(200, account, res); | ||
}) | ||
.catch(next); | ||
} | ||
|
||
function getNewOrExistingAccount( req, res, next ) { | ||
return account.set( { | ||
linkedServer: req.body.server_url || req.query.server_url, | ||
key: req.body.api_key || req.query.api_key | ||
} ) | ||
.then( account => { | ||
_render( account.status || 201, account, res ); | ||
} ) | ||
.catch( next ); | ||
function getNewOrExistingAccount(req, res, next) { | ||
return account | ||
.set({ | ||
linkedServer: req.body.server_url || req.query.server_url, | ||
key: req.body.api_key || req.query.api_key, | ||
}) | ||
.then((account) => { | ||
_render(account.status || 201, account, res); | ||
}) | ||
.catch(next); | ||
} | ||
|
||
function updateExistingAccount( req, res, next ) { | ||
return account.update( { | ||
linkedServer: req.body.server_url || req.query.server_url, | ||
key: req.body.api_key || req.query.api_key | ||
} ) | ||
.then( account => { | ||
_render( account.status || 201, account, res ); | ||
} ) | ||
.catch( next ); | ||
function updateExistingAccount(req, res, next) { | ||
return account | ||
.update({ | ||
linkedServer: req.body.server_url || req.query.server_url, | ||
key: req.body.api_key || req.query.api_key, | ||
}) | ||
.then((account) => { | ||
_render(account.status || 201, account, res); | ||
}) | ||
.catch(next); | ||
} | ||
|
||
function removeAccount( req, res, next ) { | ||
return account.remove( { | ||
linkedServer: req.body.server_url || req.query.server_url, | ||
key: req.body.api_key || req.query.api_key | ||
} ).then( () => { | ||
_render( 204, null, res ); | ||
} ) | ||
.catch( next ); | ||
function removeAccount(req, res, next) { | ||
return account | ||
.remove({ | ||
linkedServer: req.body.server_url || req.query.server_url, | ||
key: req.body.api_key || req.query.api_key, | ||
}) | ||
.then(() => { | ||
_render(204, null, res); | ||
}) | ||
.catch(next); | ||
} | ||
|
||
function getList( req, res, next ) { | ||
return account.getList() | ||
.then( list => { | ||
_render( 200, list, res ); | ||
} ) | ||
.catch( next ); | ||
function getList(req, res, next) { | ||
return account | ||
.getList() | ||
.then((list) => { | ||
_render(200, list, res); | ||
}) | ||
.catch(next); | ||
} | ||
|
||
function _render( status, body, res ) { | ||
if ( status === 204 ) { | ||
function _render(status, body, res) { | ||
if (status === 204) { | ||
// send 204 response without a body | ||
res.status( status ).end(); | ||
res.status(status).end(); | ||
} else { | ||
body = body || {}; | ||
if ( typeof body === 'string' ) { | ||
if (typeof body === 'string') { | ||
body = { | ||
message: body | ||
message: body, | ||
}; | ||
} else if ( Array.isArray( body ) ) { | ||
body = body.map( account => _renameProps( account ) ); | ||
} else if ( typeof body === 'object' ) { | ||
body = _renameProps( body ); | ||
} else if (Array.isArray(body)) { | ||
body = body.map((account) => _renameProps(account)); | ||
} else if (typeof body === 'object') { | ||
body = _renameProps(body); | ||
} | ||
body.code = status; | ||
res.status( status ).json( body ); | ||
res.status(status).json(body); | ||
} | ||
} | ||
|
||
function _renameProps( account ) { | ||
function _renameProps(account) { | ||
return { | ||
server_url: account.linkedServer, | ||
api_key: account.key | ||
api_key: account.key, | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.