Skip to content

Commit

Permalink
fix(router): converted to promise
Browse files Browse the repository at this point in the history
  • Loading branch information
facugon committed Jan 4, 2025
1 parent f3c3c6c commit cf966e5
Showing 1 changed file with 65 additions and 59 deletions.
124 changes: 65 additions & 59 deletions core/router/param-resolver.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,79 +9,85 @@ module.exports = {
options||(options={})

if (!options.param) {
// internal code exception. api will not start
throw new Error('param name is required')
}

const paramName = options.param
const entityName = (options.entity||options.param)
const targetName = (options.into||paramName)

return function (req, res, next) {
try {
const customer = req.customer
let _id = (
req.params[paramName] ||
(req.body && req.body[paramName]) ||
req.query[paramName]
)

if (isObject(_id)) {
_id = ( _id._id || _id.id || undefined )
} else if (typeof _id !== 'string') {
_id = undefined
const getRequestedEntityById = async (req) => {
const customer = req.customer
let _id = (
req.params[paramName] ||
(req.body && req.body[paramName]) ||
req.query[paramName]
)

if (isObject(_id)) {
_id = ( _id._id || _id.id || undefined )
} else if (typeof _id !== 'string') {
_id = undefined
}

// not set
if (!_id) {
if (options.required) {
throw new ClientError(`${options.param} is required`)
}
return null
}

if (!_id) {
if (options.required) {
throw new ClientError(`${options.param} is required`)
}
// invalid format
if (!isMongoId(_id)) {
if (options.required) {
throw new ClientError(`${options.param} invalid value`)
}
return null
}

req[targetName] = null
return next()
} else if (!isMongoId(_id)) {
if (options.required) {
throw new ClientError(`${options.param} invalid value`)
}
logger.debug('resolving "%s" with id "%s"', targetName, _id)

req[targetName] = null
return next()
} else {
logger.debug('resolving "%s" with id "%s"', targetName, _id)

const EntityModule = require('../entity/' + entityName)
const Entity = (
EntityModule.Entity ||
EntityModule[ firstToUpper(entityName) ] ||
EntityModule
)

// filter by customer
Entity.findOne({
_id,
$or: [
{ customer: customer._id },
{ customer_id: customer._id.toString() },
{ customer_id: customer._id },
{ customer_name: customer.name }
]
}).then(dbDoc => {
if (!dbDoc) {
if (options.required) {
throw new ClientError(`${options.param } not found`, { statusCode: 404 })
}

req[targetName] = null
return next()
}
const EntityModule = require('../entity/' + entityName)
const Entity = (
EntityModule.Entity ||
EntityModule[ firstToUpper(entityName) ] ||
EntityModule
)

logger.debug('instances of "%s" found', options.param)
req[targetName] = dbDoc
return next()
})
// filter by customer
const dbDoc = await Entity.findOne({
_id,
$or: [
{ customer: customer._id },
{ customer_id: customer._id.toString() },
{ customer_id: customer._id },
{ customer_name: customer.name }
]
})

// entity not found
if (!dbDoc) {
if (options.required) {
throw new ClientError(`${options.param } not found`, { statusCode: 404 })
}
} catch (err) {
res.sendError(err)
return null
}

logger.debug('entity instance of "%s" found', options.param)
return dbDoc
}

// should return a callback form function.
// async fn breaks restify middlewares
return function (req, res, next) {
getRequestedEntityById(req, res)
.then(entity => {
req[targetName] = entity
next()
})
.catch(res.sendError)
}
},
idToEntity (options) {
Expand Down

0 comments on commit cf966e5

Please sign in to comment.