2018-03-09 05:33:43 +00:00
|
|
|
const _ = require('lodash')
|
|
|
|
const Filter = require('scim-query-filter-parser')
|
|
|
|
|
|
|
|
module.exports = {
|
2018-03-10 05:58:04 +00:00
|
|
|
generateSuccess (msg) {
|
|
|
|
return {
|
|
|
|
succeeded: true,
|
2018-03-25 02:35:47 +00:00
|
|
|
errorCode: 0,
|
2018-03-10 05:58:04 +00:00
|
|
|
slug: 'ok',
|
|
|
|
message: _.defaultTo(msg, 'Operation succeeded.')
|
|
|
|
}
|
|
|
|
},
|
|
|
|
generateError (err, complete = true) {
|
|
|
|
const error = {
|
|
|
|
succeeded: false,
|
2018-03-25 02:35:47 +00:00
|
|
|
errorCode: err.code || 1,
|
2018-03-10 05:58:04 +00:00
|
|
|
slug: err.name,
|
|
|
|
message: err.message || 'An unexpected error occured.'
|
|
|
|
}
|
2018-03-25 02:35:47 +00:00
|
|
|
return (complete) ? { responseResult: error } : error
|
2018-03-10 05:58:04 +00:00
|
|
|
},
|
2018-03-09 05:33:43 +00:00
|
|
|
filter (arr, filterString) {
|
|
|
|
const prvFilter = new Filter(_.toString(filterString).replace(/'/g, `"`))
|
|
|
|
return arr.filter(prvFilter.test)
|
|
|
|
},
|
|
|
|
orderBy (arr, orderString) {
|
|
|
|
let orderParams = _.zip(orderString.split(',').map(ord => _.trim(ord).split(' ').map(_.trim)))
|
|
|
|
return _.orderBy(arr, orderParams[0], orderParams[1])
|
|
|
|
}
|
|
|
|
}
|