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,
|
2019-06-05 02:23:32 +00:00
|
|
|
errorCode: _.isFinite(err.code) ? 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) {
|
2018-06-25 06:44:40 +00:00
|
|
|
let orderParams = _.zip(...orderString.split(',').map(ord => _.trim(ord).split(' ').map(_.trim)))
|
2018-03-09 05:33:43 +00:00
|
|
|
return _.orderBy(arr, orderParams[0], orderParams[1])
|
|
|
|
}
|
|
|
|
}
|