diff --git a/index.js b/index.js index 6a3bb701..8844a9bb 100644 --- a/index.js +++ b/index.js @@ -8,8 +8,9 @@ exports.collection = Interface.collection; exports.ensureAuthenticated = Interface.ensureAuthenticated; exports.StatSerializer = Interface.StatSerializer; exports.ResourceSerializer = Interface.ResourceSerializer; +exports.Schemas = Interface.Schemas; -exports.init = function(opts) { +exports.init = function (opts) { exports.opts = opts; exports.getLianaName = function () { @@ -38,13 +39,13 @@ exports.init = function(opts) { exports.ResourcesGetter = require('./services/resources-getter'); exports.ResourceGetter = require('./services/resource-getter'); - exports.ResourceCreator = require('./services/resource-creator'); - exports.ResourceUpdater = require('./services/resource-updater'); - exports.ResourceRemover = require('./services/resource-remover'); + exports.ResourceCreator = opts.ResourceCreator || require('./services/resource-creator'); + exports.ResourceUpdater = opts.ResourceUpdater || require('./services/resource-updater'); + exports.ResourceRemover = opts.ResourceRemover || require('./services/resource-remover'); exports.HasManyGetter = require('./services/has-many-getter'); - exports.HasManyAssociator = require('./services/has-many-associator'); - exports.HasManyDissociator = require('./services/has-many-dissociator'); + exports.HasManyAssociator = opts.HasManyAssociator || require('./services/has-many-associator'); + exports.HasManyDissociator = opts.HasManyDissociator || require('./services/has-many-dissociator'); exports.BelongsToUpdater = require('./services/belongs-to-updater'); exports.ValueStatGetter = require('./services/value-stat-getter'); @@ -92,13 +93,13 @@ exports.init = function(opts) { return new P(function (resolve, reject) { if (customerId) { return userModel - .findById(customerId) - .lean() - .exec(function (err, customer) { - if (err) { return reject(err); } - if (!customer) { return reject(); } - resolve(customer); - }); + .findById(customerId) + .lean() + .exec(function (err, customer) { + if (err) { return reject(err); } + if (!customer) { return reject(); } + resolve(customer); + }); } else { resolve(); } diff --git a/package.json b/package.json index 39241cbf..d39bb14a 100644 --- a/package.json +++ b/package.json @@ -23,7 +23,7 @@ "dependencies": { "bluebird": "2.9.25", "flat": "1.6.0", - "forest-express": "1.1.10", + "forest-express": "kevin-frugier/forest-express#spk-1.1.10", "http-errors": "1.6.1", "lodash": "3.9.3", "moment": "2.10.6" diff --git a/services/line-stat-getter.js b/services/line-stat-getter.js index 4e27b185..4cb56aa5 100644 --- a/services/line-stat-getter.js +++ b/services/line-stat-getter.js @@ -193,7 +193,7 @@ function LineStatFinder(model, params, opts) { query = query.match(q); } - if (groupBy) { + if (!_.isEmpty(groupBy)) { var group = { _id: groupBy, count: { $sum: sum } @@ -206,8 +206,11 @@ function LineStatFinder(model, params, opts) { query = query.group(group); } - query.sort(sort) - .project({ + if (!_.isEmpty(sort)) { + query = query.sort(sort); + } + + query.project({ values: { key: '$_id.'+ params['group_by_field'], value: '$count' diff --git a/services/pie-stat-getter.js b/services/pie-stat-getter.js index 24771c8d..8a4d68a0 100644 --- a/services/pie-stat-getter.js +++ b/services/pie-stat-getter.js @@ -32,6 +32,10 @@ function PieStatGetter(model, params, opts) { } this.perform = function () { + if (!params['group_by_field']) { + return P.reject(new Error('Missing param `group_by_field`')); + } + var populateGroupByField = getReference(params['group_by_field']); return new P(function (resolve, reject) {