angular-nav-thresholds
Advanced tools
Comparing version 1.0.1 to 1.1.0
@@ -1,1 +0,1 @@ | ||
!function(n){"use strict";n="default"in n?n.default:n;var e=function(n,e){if(!(n instanceof e))throw new TypeError("Cannot call a class as a function")},r=function(){function n(n,e){for(var r=0;r<e.length;r++){var t=e[r];t.enumerable=t.enumerable||!1,t.configurable=!0,"value"in t&&(t.writable=!0),Object.defineProperty(n,t.key,t)}}return function(e,r,t){return r&&n(e.prototype,r),t&&n(e,t),e}}(),t=function(n,e){for(var r=0;r<n.length;r++)if(e(n[r]))return n[r]},a=function(n,e){return e.version===n},o=function(){function n(){e(this,n)}return r(n,[{key:"calculateThresholds",value:function(n,e){if(n&&n.allocations&&n.plans&&e&&e.allocations&&e.allocations.version&&e.plans&&e.plans.version){var r=t(n.allocations,a.bind(null,e.allocations.version));if(r&&r.weeklyLevels){var o=t(n.plans,a.bind(null,e.plans.version));if(o&&o.weeksOfStock){var l=r.weeklyLevels,i=o.weeksOfStock,u=Object.keys(l).reduce(function(n,e){return n[e]=Object.keys(i).reduce(function(n,r){return n[r]=Math.round(l[e]*i[r]),n},{}),n},{});return u}}}}}]),n}();n.module("angularNavThresholds",[]).service("thresholdsService",o)}(angular); | ||
!function(e){"use strict";e="default"in e?e.default:e;var t=function(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")},n=function(){function e(e,t){for(var n=0;n<t.length;n++){var a=t[n];a.enumerable=a.enumerable||!1,a.configurable=!0,"value"in a&&(a.writable=!0),Object.defineProperty(e,a.key,a)}}return function(t,n,a){return n&&e(t.prototype,n),a&&e(t,a),t}}(),a=function(e,t){for(var n=0;n<e.length;n++)if(t(e[n]))return e[n]},r=function(e,t){return t.version===e},l=function(e,t){return t._id===e},s=function(){function e(n,a,r,l){t(this,e),this.$q=n,this.smartId=a,this.lgasService=r,this.statesService=l}return n(e,[{key:"calculateThresholds",value:function(e,t){if(e&&e.allocations&&e.plans&&t&&t.allocations&&t.allocations.version&&t.plans&&t.plans.version){var n=a(e.allocations,r.bind(null,t.allocations.version));if(n&&n.weeklyLevels){var l=a(e.plans,r.bind(null,t.plans.version));if(l&&l.weeksOfStock){var s=n.weeklyLevels,i=l.weeksOfStock,o=Object.keys(s).reduce(function(e,t){return e[t]=Object.keys(i).reduce(function(e,n){return e[n]=Math.round(s[t]*i[n]),e},{}),e},{});return o}}}}},{key:"getThresholdsFor",value:function(e){var t=this,n="zone:?state:?lga",r={},s={};r=e.reduce(function(e,a){var r=a.location;if(!r)return e;var l=t.smartId.idify(r,n),i=a.allocations||{version:1},o=a.plans||{version:1};return e[l]=angular.merge({},{allocations:i,plans:o}),r.lga&&!s.lga?(s.lga=t.lgasService.list(),e[l].type="lga"):r.state&&!s.state&&(s.state=t.statesService.list(),e[l].type="state"),e},{});var i=function(e){return Object.keys(r).forEach(function(n){var s=r[n],i=a(e[s.type],l.bind(null,n));s.thresholds=t.calculateThresholds(i,s),delete s.type}),r};return this.$q.all(s).then(i)}}]),e}();s.$inject=["$q","smartId","lgasService","statesService"],e.module("angularNavThresholds",["angularNavData","ngSmartId"]).service("thresholdsService",s)}(angular); |
@@ -5,2 +5,3 @@ module.exports = function (config) { | ||
'node_modules/angular/angular.js', | ||
'node_modules/ng-smart-id/dist/bundle.js', | ||
'dist/bundle.js', | ||
@@ -7,0 +8,0 @@ 'node_modules/angular-mocks/angular-mocks.js', |
{ | ||
"name": "angular-nav-thresholds", | ||
"version": "1.0.1", | ||
"version": "1.1.0", | ||
"description": "", | ||
@@ -25,2 +25,3 @@ "main": "dist/bundle.js", | ||
"karma-phantomjs-launcher": "^1.0.0", | ||
"ng-smart-id": "github:fielded/ng-smart-id", | ||
"phantomjs-prebuilt": "^2.1.7", | ||
@@ -27,0 +28,0 @@ "rollup": "^0.26.3", |
@@ -6,3 +6,6 @@ import angular from 'angular' | ||
angular | ||
.module('angularNavThresholds', []) | ||
.module('angularNavThresholds', [ | ||
'angularNavData', | ||
'ngSmartId' | ||
]) | ||
.service('thresholdsService', ThresholdsService) |
@@ -12,3 +12,12 @@ const find = (list, match) => { | ||
const isId = (id, item) => item._id === id | ||
class ThresholdsService { | ||
constructor ($q, smartId, lgasService, statesService) { | ||
this.$q = $q | ||
this.smartId = smartId | ||
this.lgasService = lgasService | ||
this.statesService = statesService | ||
} | ||
calculateThresholds (location, stockCount) { | ||
@@ -45,4 +54,49 @@ if (!location || !location.allocations || !location.plans) { | ||
} | ||
getThresholdsFor (stockCounts) { | ||
// TODO: make it work for zones too | ||
const locationIdPattern = 'zone:?state:?lga' | ||
let index = {} | ||
let promises = {} | ||
index = stockCounts.reduce((index, stockCount) => { | ||
let scLocation = stockCount.location | ||
if (!scLocation) { | ||
return index | ||
} | ||
const id = this.smartId.idify(scLocation, locationIdPattern) | ||
const allocations = stockCount.allocations || { version: 1 } | ||
const plans = stockCount.plans || { version: 1 } | ||
index[id] = angular.merge({}, { allocations: allocations, plans: plans }) | ||
if (scLocation.lga && !promises.lga) { | ||
promises.lga = this.lgasService.list() | ||
index[id].type = 'lga' | ||
} else if (scLocation.state && !promises.state) { | ||
promises.state = this.statesService.list() | ||
index[id].type = 'state' | ||
} | ||
return index | ||
}, {}) | ||
const addThresholds = (promisesRes) => { | ||
Object.keys(index).forEach((key) => { | ||
const item = index[key] | ||
const location = find(promisesRes[item.type], isId.bind(null, key)) | ||
item.thresholds = this.calculateThresholds(location, item) | ||
delete item.type | ||
}) | ||
return index | ||
} | ||
return this.$q.all(promises) | ||
.then(addThresholds) | ||
} | ||
} | ||
ThresholdsService.$inject = ['$q', 'smartId', 'lgasService', 'statesService'] | ||
export default ThresholdsService |
'use strict' | ||
describe('thresholds service', function () { | ||
var $rootScope | ||
var thresholdsService | ||
var angularNavDataMock // eslint-disable-line | ||
var testMod // eslint-disable-line | ||
beforeEach(module('angularNavThresholds')) | ||
beforeEach(function () { | ||
angularNavDataMock = angular.module('angularNavData', []) | ||
.service('lgasService', function ($q) { | ||
this.list = function () { | ||
var lga = angular.merge({ _id: 'zone:nc:state:kogi:lga:adavi' }, location) | ||
return $q.when([lga]) | ||
} | ||
}) | ||
.service('statesService', function ($q) { | ||
this.list = function () { | ||
var state = angular.merge({ _id: 'zone:nc:state:kogi' }, location) | ||
return $q.when([state]) | ||
} | ||
}) | ||
testMod = angular.module('testMod', ['angularNavData', 'angularNavThresholds']) | ||
}) | ||
beforeEach(inject(function (_thresholdsService_) { | ||
beforeEach(module('testMod')) | ||
beforeEach(inject(function (_$rootScope_, _thresholdsService_) { | ||
thresholdsService = _thresholdsService_ | ||
$rootScope = _$rootScope_ | ||
})) | ||
@@ -14,3 +35,8 @@ | ||
allocations: [ | ||
{ version: 1 }, | ||
{ version: 1, | ||
weeklyLevels: { | ||
'product:a': 50, | ||
'product:b': 100 | ||
} | ||
}, | ||
{ | ||
@@ -41,3 +67,9 @@ version: 2, | ||
describe('calculate thesholds', function () { | ||
var stockCounts = [ | ||
// { location: { zone: 'nc' }, allocations: { version: 2 }, plans: { version: 1 } }, | ||
{ location: { zone: 'nc', state: 'kogi' }, allocations: { version: 2 }, plans: { version: 1 } }, | ||
{ location: { zone: 'nc', state: 'kogi', lga: 'adavi' } } | ||
] | ||
describe('calculateThresholds', function () { | ||
it('takes a location and a stockCount and returns the min, reOrder, max thresholds', function () { | ||
@@ -60,2 +92,38 @@ var expected = { | ||
}) | ||
describe('getThresholdsFor', function () { | ||
it('takes an array of objects with location, allocations and plans fields and returns an object of location thresholds', function (done) { | ||
var expected = { | ||
// 'zone:nc': { | ||
// thresholds: { | ||
// 'product:a': { min: 100, reOrder: 200, max: 500 }, | ||
// 'product:b': { min: 200, reOrder: 400, max: 1000 } | ||
// } | ||
// }, | ||
'zone:nc:state:kogi': { | ||
allocations: { version: 2 }, | ||
plans: { version: 1 }, | ||
thresholds: { | ||
'product:a': { min: 100, reOrder: 200, max: 500 }, | ||
'product:b': { min: 200, reOrder: 400, max: 1000 } | ||
} | ||
}, | ||
'zone:nc:state:kogi:lga:adavi': { | ||
allocations: { version: 1 }, | ||
plans: { version: 1 }, | ||
thresholds: { | ||
'product:a': { min: 50, reOrder: 100, max: 250 }, | ||
'product:b': { min: 100, reOrder: 200, max: 500 } | ||
} | ||
} | ||
} | ||
thresholdsService.getThresholdsFor(stockCounts) | ||
.then(function (thresholds) { | ||
expect(expected).toEqual(thresholds) | ||
}) | ||
$rootScope.$digest() | ||
done() | ||
}) | ||
}) | ||
}) |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
23705
282
15