|
3 | 3 |
|
4 | 4 | angular$1 = 'default' in angular$1 ? angular$1['default'] : angular$1;
|
5 | 5 |
|
6 |
| - var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { |
7 |
| - return typeof obj; |
8 |
| - } : function (obj) { |
9 |
| - return obj && typeof Symbol === "function" && obj.constructor === Symbol ? "symbol" : typeof obj; |
10 |
| - }; |
11 |
| - |
12 |
| - var classCallCheck = function (instance, Constructor) { |
13 |
| - if (!(instance instanceof Constructor)) { |
14 |
| - throw new TypeError("Cannot call a class as a function"); |
15 |
| - } |
16 |
| - }; |
| 6 | + var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }(); |
17 | 7 |
|
18 |
| - var createClass = function () { |
19 |
| - function defineProperties(target, props) { |
20 |
| - for (var i = 0; i < props.length; i++) { |
21 |
| - var descriptor = props[i]; |
22 |
| - descriptor.enumerable = descriptor.enumerable || false; |
23 |
| - descriptor.configurable = true; |
24 |
| - if ("value" in descriptor) descriptor.writable = true; |
25 |
| - Object.defineProperty(target, descriptor.key, descriptor); |
26 |
| - } |
27 |
| - } |
28 |
| - |
29 |
| - return function (Constructor, protoProps, staticProps) { |
30 |
| - if (protoProps) defineProperties(Constructor.prototype, protoProps); |
31 |
| - if (staticProps) defineProperties(Constructor, staticProps); |
32 |
| - return Constructor; |
33 |
| - }; |
34 |
| - }(); |
| 8 | + function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } |
35 | 9 |
|
36 | 10 | // TODO: replace with Array#find ponyfill
|
37 | 11 | var find = function find(list, match) {
|
|
52 | 26 | };
|
53 | 27 |
|
54 | 28 | // Zones config
|
55 |
| - var zonesPlan = { |
56 |
| - min: 0, |
57 |
| - reOrder: 3, |
58 |
| - max: 6 |
| 29 | + var zonePlans = { |
| 30 | + weeksOfStock: { |
| 31 | + min: 0, |
| 32 | + reOrder: 3, |
| 33 | + max: 6 |
| 34 | + } |
| 35 | + }; |
| 36 | + |
| 37 | + var getFactorVersion = function getFactorVersion(stockCount, factor, options) { |
| 38 | + if (options.version === 'last') { |
| 39 | + return options.version; |
| 40 | + } |
| 41 | + if (!(stockCount[factor] && stockCount[factor].version)) { |
| 42 | + return 1; |
| 43 | + } |
| 44 | + return stockCount[factor].version; |
| 45 | + }; |
| 46 | + |
| 47 | + var getFactor = function getFactor(location, versions, version) { |
| 48 | + if (version === 'last') { |
| 49 | + return versions[versions.length - 1]; |
| 50 | + } |
| 51 | + |
| 52 | + return find(versions, isVersion.bind(null, version)); |
| 53 | + }; |
| 54 | + |
| 55 | + var getFactors = function getFactors(stockCount, location, options) { |
| 56 | + // centralized for whenever we implement #16 |
| 57 | + var somethingIsWrong = function somethingIsWrong() { |
| 58 | + return undefined; |
| 59 | + }; |
| 60 | + |
| 61 | + var getWeeklyLevels = function getWeeklyLevels() { |
| 62 | + if (!(location.allocations && location.allocations.length)) { |
| 63 | + somethingIsWrong(); |
| 64 | + } |
| 65 | + |
| 66 | + var allocationsVersion = getFactorVersion(stockCount, 'allocations', options); |
| 67 | + |
| 68 | + if (typeof allocationsVersion === 'undefined') { |
| 69 | + somethingIsWrong(); |
| 70 | + } |
| 71 | + |
| 72 | + var allocations = getFactor(location, location.allocations, allocationsVersion); |
| 73 | + return allocations && allocations.weeklyLevels; |
| 74 | + }; |
| 75 | + |
| 76 | + var getWeeksOfStock = function getWeeksOfStock() { |
| 77 | + if (location.level !== 'zone' && !(location.plans && location.plans.length)) { |
| 78 | + somethingIsWrong(); |
| 79 | + } |
| 80 | + |
| 81 | + var plansVersion = getFactorVersion(stockCount, 'plans', options); |
| 82 | + |
| 83 | + if (typeof plansVersion === 'undefined') { |
| 84 | + somethingIsWrong(); |
| 85 | + } |
| 86 | + |
| 87 | + var plans = zonePlans; |
| 88 | + if (location.level !== 'zone') { |
| 89 | + plans = getFactor(location, location.plans, plansVersion); |
| 90 | + } |
| 91 | + |
| 92 | + return plans && plans.weeksOfStock; |
| 93 | + }; |
| 94 | + |
| 95 | + var getMonthlyTargetPopulations = function getMonthlyTargetPopulations() { |
| 96 | + var monthlyTargetPopulations = void 0; |
| 97 | + if (location.targetPopulations) { |
| 98 | + if (!location.targetPopulations.length) { |
| 99 | + somethingIsWrong(); |
| 100 | + } |
| 101 | + var targetPopulationVersion = getFactorVersion(stockCount, 'targetPopulations', options); |
| 102 | + |
| 103 | + if (typeof targetPopulationVersion === 'undefined') { |
| 104 | + somethingIsWrong(); |
| 105 | + } |
| 106 | + |
| 107 | + var targetPopulations = getFactor(location, location.targetPopulations, targetPopulationVersion); |
| 108 | + monthlyTargetPopulations = targetPopulations && targetPopulations.monthlyTargetPopulations; |
| 109 | + } else { |
| 110 | + // For backwards compatibility with the old style location docs, |
| 111 | + // since we have no control about when the dashboards are going |
| 112 | + // to replicate the new location docs |
| 113 | + if (!(location.targetPopulation && location.targetPopulation.length)) { |
| 114 | + somethingIsWrong(); |
| 115 | + } |
| 116 | + monthlyTargetPopulations = location.targetPopulation; |
| 117 | + } |
| 118 | + return monthlyTargetPopulations; |
| 119 | + }; |
| 120 | + |
| 121 | + return { |
| 122 | + weeksOfStock: getWeeksOfStock(), |
| 123 | + weeklyLevels: getWeeklyLevels(), |
| 124 | + targetPopulations: getMonthlyTargetPopulations() |
| 125 | + }; |
59 | 126 | };
|
60 | 127 |
|
61 | 128 | var ThresholdsService = function () {
|
62 | 129 | function ThresholdsService($q, smartId, lgasService, statesService) {
|
63 |
| - classCallCheck(this, ThresholdsService); |
| 130 | + _classCallCheck(this, ThresholdsService); |
64 | 131 |
|
65 | 132 | this.$q = $q;
|
66 | 133 | this.smartId = smartId;
|
|
73 | 140 | // That param is only used for zones.
|
74 | 141 |
|
75 | 142 |
|
76 |
| - createClass(ThresholdsService, [{ |
| 143 | + _createClass(ThresholdsService, [{ |
77 | 144 | key: 'calculateThresholds',
|
78 | 145 | value: function calculateThresholds(location, stockCount, products) {
|
79 |
| - var requiredStateStoresAllocation = arguments.length <= 3 || arguments[3] === undefined ? {} : arguments[3]; |
80 |
| - var options = arguments.length <= 4 || arguments[4] === undefined ? {} : arguments[4]; |
81 |
| - |
82 |
| - if (!location || !location.allocations || !location.allocations.length || !location.plans || !location.plans.length || !location.level) { |
83 |
| - return; |
84 |
| - } |
| 146 | + var requiredStateStoresAllocation = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : {}; |
| 147 | + var options = arguments.length > 4 && arguments[4] !== undefined ? arguments[4] : {}; |
85 | 148 |
|
86 | 149 | if (!stockCount) {
|
87 | 150 | return;
|
88 | 151 | }
|
89 | 152 |
|
90 |
| - if (options.version !== 'last' && !(stockCount.allocations && _typeof(stockCount.allocations.version) !== undefined && stockCount.plans && _typeof(stockCount.plans.version) !== undefined)) { |
| 153 | + if (!location && location.level) { |
91 | 154 | return;
|
92 | 155 | }
|
93 | 156 |
|
94 | 157 | if (!products || !products.length) {
|
95 | 158 | return;
|
96 | 159 | }
|
97 | 160 |
|
98 |
| - var allocation = void 0; |
99 |
| - if (options.version === 'last') { |
100 |
| - allocation = location.allocations[location.allocations.length - 1]; |
101 |
| - } else { |
102 |
| - allocation = find(location.allocations, isVersion.bind(null, stockCount.allocations.version)); |
103 |
| - } |
| 161 | + var _getFactors = getFactors(stockCount, location, options), |
| 162 | + weeklyLevels = _getFactors.weeklyLevels, |
| 163 | + weeksOfStock = _getFactors.weeksOfStock, |
| 164 | + targetPopulations = _getFactors.targetPopulations; |
104 | 165 |
|
105 |
| - if (!(allocation && allocation.weeklyLevels)) { |
| 166 | + if (!(weeklyLevels && weeksOfStock && targetPopulations)) { |
106 | 167 | return;
|
107 | 168 | }
|
108 | 169 |
|
109 |
| - var weeklyLevels = allocation.weeklyLevels; |
110 |
| - |
111 |
| - var weeksOfStock = zonesPlan; |
112 |
| - |
113 |
| - if (location.level !== 'zone') { |
114 |
| - var plan = void 0; |
115 |
| - if (options.version === 'last') { |
116 |
| - plan = location.plans[location.plans.length - 1]; |
117 |
| - } else { |
118 |
| - plan = find(location.plans, isVersion.bind(null, stockCount.plans.version)); |
119 |
| - } |
120 |
| - |
121 |
| - if (!(plan && plan.weeksOfStock)) { |
122 |
| - return; |
123 |
| - } |
124 |
| - weeksOfStock = plan.weeksOfStock; |
125 |
| - } |
126 |
| - |
127 |
| - var thresholds = Object.keys(weeklyLevels).reduce(function (index, productId) { |
| 170 | + return Object.keys(weeklyLevels).reduce(function (index, productId) { |
128 | 171 | index[productId] = Object.keys(weeksOfStock).reduce(function (productThresholds, threshold) {
|
129 | 172 | var level = weeklyLevels[productId] * weeksOfStock[threshold];
|
130 | 173 | var product = find(products, isId.bind(null, productId));
|
|
147 | 190 | return productThresholds;
|
148 | 191 | }, {});
|
149 | 192 |
|
150 |
| - if (location.targetPopulation) { |
151 |
| - index[productId].targetPopulation = location.targetPopulation[productId]; |
152 |
| - } |
| 193 | + index[productId].targetPopulation = targetPopulations[productId]; |
153 | 194 |
|
154 | 195 | return index;
|
155 | 196 | }, {});
|
156 |
| - |
157 |
| - return thresholds; |
158 | 197 | }
|
159 | 198 | }, {
|
160 | 199 | key: 'getThresholdsFor',
|
|
177 | 216 | var id = _this.smartId.idify(scLocation, locationIdPattern);
|
178 | 217 | var allocations = stockCount.allocations || { version: 1 };
|
179 | 218 | var plans = stockCount.plans || { version: 1 };
|
180 |
| - index[id] = angular.merge({}, { allocations: allocations, plans: plans }); |
| 219 | + var targetPopulations = stockCount.targetPopulations || { version: 1 }; |
| 220 | + index[id] = angular.merge({}, { |
| 221 | + allocations: allocations, |
| 222 | + plans: plans, |
| 223 | + targetPopulations: targetPopulations |
| 224 | + }); |
181 | 225 |
|
182 | 226 | if (scLocation.lga) {
|
183 | 227 | if (!promises.lga) {
|
|
208 | 252 | return this.$q.all(promises).then(addThresholds);
|
209 | 253 | }
|
210 | 254 | }]);
|
| 255 | + |
211 | 256 | return ThresholdsService;
|
212 | 257 | }();
|
213 | 258 |
|
|
0 commit comments