@@ -9,79 +9,85 @@ module.exports = {
99 options || ( options = { } )
1010
1111 if ( ! options . param ) {
12+ // internal code exception. api will not start
1213 throw new Error ( 'param name is required' )
1314 }
1415
1516 const paramName = options . param
1617 const entityName = ( options . entity || options . param )
1718 const targetName = ( options . into || paramName )
1819
19- return function ( req , res , next ) {
20- try {
21- const customer = req . customer
22- let _id = (
23- req . params [ paramName ] ||
24- ( req . body && req . body [ paramName ] ) ||
25- req . query [ paramName ]
26- )
27-
28- if ( isObject ( _id ) ) {
29- _id = ( _id . _id || _id . id || undefined )
30- } else if ( typeof _id !== 'string' ) {
31- _id = undefined
20+ const getRequestedEntityById = async ( req ) => {
21+ const customer = req . customer
22+ let _id = (
23+ req . params [ paramName ] ||
24+ ( req . body && req . body [ paramName ] ) ||
25+ req . query [ paramName ]
26+ )
27+
28+ if ( isObject ( _id ) ) {
29+ _id = ( _id . _id || _id . id || undefined )
30+ } else if ( typeof _id !== 'string' ) {
31+ _id = undefined
32+ }
33+
34+ // not set
35+ if ( ! _id ) {
36+ if ( options . required ) {
37+ throw new ClientError ( `${ options . param } is required` )
3238 }
39+ return null
40+ }
3341
34- if ( ! _id ) {
35- if ( options . required ) {
36- throw new ClientError ( `${ options . param } is required` )
37- }
42+ // invalid format
43+ if ( ! isMongoId ( _id ) ) {
44+ if ( options . required ) {
45+ throw new ClientError ( `${ options . param } invalid value` )
46+ }
47+ return null
48+ }
3849
39- req [ targetName ] = null
40- return next ( )
41- } else if ( ! isMongoId ( _id ) ) {
42- if ( options . required ) {
43- throw new ClientError ( `${ options . param } invalid value` )
44- }
50+ logger . debug ( 'resolving "%s" with id "%s"' , targetName , _id )
4551
46- req [ targetName ] = null
47- return next ( )
48- } else {
49- logger . debug ( 'resolving "%s" with id "%s"' , targetName , _id )
50-
51- const EntityModule = require ( '../entity/' + entityName )
52- const Entity = (
53- EntityModule . Entity ||
54- EntityModule [ firstToUpper ( entityName ) ] ||
55- EntityModule
56- )
57-
58- // filter by customer
59- Entity . findOne ( {
60- _id,
61- $or : [
62- { customer : customer . _id } ,
63- { customer_id : customer . _id . toString ( ) } ,
64- { customer_id : customer . _id } ,
65- { customer_name : customer . name }
66- ]
67- } ) . then ( dbDoc => {
68- if ( ! dbDoc ) {
69- if ( options . required ) {
70- throw new ClientError ( `${ options . param } not found` , { statusCode : 404 } )
71- }
72-
73- req [ targetName ] = null
74- return next ( )
75- }
52+ const EntityModule = require ( '../entity/' + entityName )
53+ const Entity = (
54+ EntityModule . Entity ||
55+ EntityModule [ firstToUpper ( entityName ) ] ||
56+ EntityModule
57+ )
7658
77- logger . debug ( 'instances of "%s" found' , options . param )
78- req [ targetName ] = dbDoc
79- return next ( )
80- } )
59+ // filter by customer
60+ const dbDoc = await Entity . findOne ( {
61+ _id,
62+ $or : [
63+ { customer : customer . _id } ,
64+ { customer_id : customer . _id . toString ( ) } ,
65+ { customer_id : customer . _id } ,
66+ { customer_name : customer . name }
67+ ]
68+ } )
69+
70+ // entity not found
71+ if ( ! dbDoc ) {
72+ if ( options . required ) {
73+ throw new ClientError ( `${ options . param } not found` , { statusCode : 404 } )
8174 }
82- } catch ( err ) {
83- res . sendError ( err )
75+ return null
8476 }
77+
78+ logger . debug ( 'entity instance of "%s" found' , options . param )
79+ return dbDoc
80+ }
81+
82+ // should return a callback form function.
83+ // async fn breaks restify middlewares
84+ return function ( req , res , next ) {
85+ getRequestedEntityById ( req , res )
86+ . then ( entity => {
87+ req [ targetName ] = entity
88+ next ( )
89+ } )
90+ . catch ( res . sendError )
8591 }
8692 } ,
8793 idToEntity ( options ) {
0 commit comments