Skip to content
This repository was archived by the owner on Jan 14, 2020. It is now read-only.

Commit 4b20c57

Browse files
Merge pull request #34 from fielded/33
Wrong unknown stockStatusLevel
2 parents 9aacf1e + 40c5b40 commit 4b20c57

File tree

4 files changed

+44
-28
lines changed

4 files changed

+44
-28
lines changed

Diff for: dist/bundle.js

+19-13
Original file line numberDiff line numberDiff line change
@@ -42,16 +42,20 @@
4242
return undefined;
4343
};
4444

45-
var productsGroupedByStatus = function productsGroupedByStatus(stock) {
46-
return Object.keys(stock).reduce(function (grouped, product) {
47-
var status = stock[product].status;
45+
var productsGroupedByStatus = function productsGroupedByStatus(stock, products) {
46+
return Object.keys(stock).reduce(function (grouped, productId) {
47+
var isRelevant = !!find(products, function (product) {
48+
return product._id === productId;
49+
});
50+
var status = stock[productId].status;
51+
if (!isRelevant) {
52+
return grouped;
53+
}
4854
if (status) {
49-
grouped[status].push(product);
50-
} else {
51-
grouped['unknown'].push(product);
55+
grouped[status].push(productId);
5256
}
5357
return grouped;
54-
}, { understock: [], 're-stock': [], ok: [], overstock: [], unknown: [] });
58+
}, { understock: [], 're-stock': [], ok: [], overstock: [] });
5559
};
5660

5761
var sumAllocations = function sumAllocations(sum, stock) {
@@ -195,7 +199,7 @@
195199
};
196200

197201
if (stockCount.location && stockCount.location.lga) {
198-
var groupedByStatus = productsGroupedByStatus(stockCount.stock);
202+
var groupedByStatus = productsGroupedByStatus(stockCount.stock, products);
199203
stockCount.reStockNeeded = !!(groupedByStatus.understock.length + groupedByStatus['re-stock'].length);
200204
} else {
201205
// states and zones
@@ -208,17 +212,19 @@
208212
};
209213

210214
var addStockLevelStatusField = function addStockLevelStatusField(stockCount) {
211-
var unknownProducts = productsGroupedByStatus(stockCount.stock).unknown.length;
212-
var understockedProducts = productsGroupedByStatus(stockCount.stock).understock.length;
215+
var grouped = productsGroupedByStatus(stockCount.stock, products);
216+
var understockedProducts = grouped.understock.length;
217+
var totalGrouped = Object.keys(grouped).reduce(function (sum, group) {
218+
return sum + grouped[group].length;
219+
}, 0);
213220

221+
stockCount.stockLevelStatus = 'unknown';
214222
if (stockCount.location) {
215223
if (understockedProducts >= _this2.STOCK_STATUSES.alert.threshold) {
216224
stockCount.stockLevelStatus = _this2.STOCK_STATUSES.alert.id;
217225
} else if (understockedProducts >= _this2.STOCK_STATUSES.warning.threshold) {
218226
stockCount.stockLevelStatus = _this2.STOCK_STATUSES.warning.id;
219-
} else if (unknownProducts) {
220-
stockCount.stockLevelStatus = 'unknown';
221-
} else {
227+
} else if (totalGrouped > 0) {
222228
stockCount.stockLevelStatus = _this2.STOCK_STATUSES.ok.id;
223229
}
224230
}

Diff for: dist/bundle.min.js

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Diff for: src/state-indicators.service.js

+15-13
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,18 @@ const find = (list, match) => {
77
return undefined
88
}
99

10-
const productsGroupedByStatus = (stock) => {
11-
return Object.keys(stock).reduce((grouped, product) => {
12-
const status = stock[product].status
10+
const productsGroupedByStatus = (stock, products) => {
11+
return Object.keys(stock).reduce((grouped, productId) => {
12+
const isRelevant = !!find(products, (product) => product._id === productId)
13+
const status = stock[productId].status
14+
if (!isRelevant) {
15+
return grouped
16+
}
1317
if (status) {
14-
grouped[status].push(product)
15-
} else {
16-
grouped['unknown'].push(product)
18+
grouped[status].push(productId)
1719
}
1820
return grouped
19-
}, { understock: [], 're-stock': [], ok: [], overstock: [], unknown: [] })
21+
}, { understock: [], 're-stock': [], ok: [], overstock: [] })
2022
}
2123

2224
const sumAllocations = (sum, stock) => {
@@ -147,7 +149,7 @@ class StateIndicatorsService {
147149
}
148150

149151
if (stockCount.location && stockCount.location.lga) {
150-
const groupedByStatus = productsGroupedByStatus(stockCount.stock)
152+
const groupedByStatus = productsGroupedByStatus(stockCount.stock, products)
151153
stockCount.reStockNeeded = !!(groupedByStatus.understock.length + groupedByStatus['re-stock'].length)
152154
} else { // states and zones
153155
if (stockCount.stock) {
@@ -159,17 +161,17 @@ class StateIndicatorsService {
159161
}
160162

161163
const addStockLevelStatusField = (stockCount) => {
162-
const unknownProducts = productsGroupedByStatus(stockCount.stock).unknown.length
163-
const understockedProducts = productsGroupedByStatus(stockCount.stock).understock.length
164+
const grouped = productsGroupedByStatus(stockCount.stock, products)
165+
const understockedProducts = grouped.understock.length
166+
const totalGrouped = Object.keys(grouped).reduce((sum, group) => sum + grouped[group].length, 0)
164167

168+
stockCount.stockLevelStatus = 'unknown'
165169
if (stockCount.location) {
166170
if (understockedProducts >= this.STOCK_STATUSES.alert.threshold) {
167171
stockCount.stockLevelStatus = this.STOCK_STATUSES.alert.id
168172
} else if (understockedProducts >= this.STOCK_STATUSES.warning.threshold) {
169173
stockCount.stockLevelStatus = this.STOCK_STATUSES.warning.id
170-
} else if (unknownProducts) {
171-
stockCount.stockLevelStatus = 'unknown'
172-
} else {
174+
} else if (totalGrouped > 0) {
173175
stockCount.stockLevelStatus = this.STOCK_STATUSES.ok.id
174176
}
175177
}

Diff for: test/state-indicators.service.spec.js

+9-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@
33
describe('state indicators service', function () {
44
var stateIndicatorsService
55
var thresholdsService
6+
var productListService
67
var $rootScope
8+
var $q
79
var angularNavDataMock // eslint-disable-line
810
var testMod // eslint-disable-line
911

@@ -159,6 +161,7 @@ describe('state indicators service', function () {
159161
beforeEach(module('testMod'))
160162

161163
beforeEach(inject(function (
164+
_$q_,
162165
_$rootScope_,
163166
_lgasService_,
164167
_statesService_,
@@ -167,7 +170,9 @@ describe('state indicators service', function () {
167170
_productListService_
168171
) {
169172
$rootScope = _$rootScope_
173+
$q = _$q_
170174
thresholdsService = _thresholdsService_
175+
productListService = _productListService_
171176
stateIndicatorsService = _stateIndicatorsService_
172177

173178
spyOn(thresholdsService, 'calculateThresholds').and.callFake(function (location, stockCount, products, requiredStateAllocation) {
@@ -434,6 +439,9 @@ describe('state indicators service', function () {
434439
done()
435440
})
436441
it('uses no default stockLevelStatus, status, allocation or thresholds', function (done) {
442+
spyOn(productListService, 'relevant').and.callFake(function () {
443+
return $q.when([])
444+
})
437445
var unknownLgaStockCount = {
438446
location: { zone: 'nc', state: 'kogi', lga: 'unknown' },
439447
stock: { 'product:a': 2, 'product:b': 3, 'product:c': 10, 'product:d': 20 },
@@ -456,7 +464,7 @@ describe('state indicators service', function () {
456464

457465
stateIndicatorsService.decorateWithIndicators([unknownLgaStockCount])
458466
.then(function (decoratedStockCounts) {
459-
expect(thresholdsService.calculateThresholds).toHaveBeenCalledWith(undefined, unknownLgaStockCount, products)
467+
expect(thresholdsService.calculateThresholds).toHaveBeenCalledWith(undefined, unknownLgaStockCount, [])
460468
expect(decoratedStockCounts).toEqual(expected)
461469
})
462470
$rootScope.$digest()

0 commit comments

Comments
 (0)