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

Commit

Permalink
Merge pull request #34 from fielded/33
Browse files Browse the repository at this point in the history
Wrong unknown stockStatusLevel
  • Loading branch information
patriciagarcia authored Oct 26, 2016
2 parents 9aacf1e + 40c5b40 commit 4b20c57
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 28 deletions.
32 changes: 19 additions & 13 deletions dist/bundle.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,16 +42,20 @@
return undefined;
};

var productsGroupedByStatus = function productsGroupedByStatus(stock) {
return Object.keys(stock).reduce(function (grouped, product) {
var status = stock[product].status;
var productsGroupedByStatus = function productsGroupedByStatus(stock, products) {
return Object.keys(stock).reduce(function (grouped, productId) {
var isRelevant = !!find(products, function (product) {
return product._id === productId;
});
var status = stock[productId].status;
if (!isRelevant) {
return grouped;
}
if (status) {
grouped[status].push(product);
} else {
grouped['unknown'].push(product);
grouped[status].push(productId);
}
return grouped;
}, { understock: [], 're-stock': [], ok: [], overstock: [], unknown: [] });
}, { understock: [], 're-stock': [], ok: [], overstock: [] });
};

var sumAllocations = function sumAllocations(sum, stock) {
Expand Down Expand Up @@ -195,7 +199,7 @@
};

if (stockCount.location && stockCount.location.lga) {
var groupedByStatus = productsGroupedByStatus(stockCount.stock);
var groupedByStatus = productsGroupedByStatus(stockCount.stock, products);
stockCount.reStockNeeded = !!(groupedByStatus.understock.length + groupedByStatus['re-stock'].length);
} else {
// states and zones
Expand All @@ -208,17 +212,19 @@
};

var addStockLevelStatusField = function addStockLevelStatusField(stockCount) {
var unknownProducts = productsGroupedByStatus(stockCount.stock).unknown.length;
var understockedProducts = productsGroupedByStatus(stockCount.stock).understock.length;
var grouped = productsGroupedByStatus(stockCount.stock, products);
var understockedProducts = grouped.understock.length;
var totalGrouped = Object.keys(grouped).reduce(function (sum, group) {
return sum + grouped[group].length;
}, 0);

stockCount.stockLevelStatus = 'unknown';
if (stockCount.location) {
if (understockedProducts >= _this2.STOCK_STATUSES.alert.threshold) {
stockCount.stockLevelStatus = _this2.STOCK_STATUSES.alert.id;
} else if (understockedProducts >= _this2.STOCK_STATUSES.warning.threshold) {
stockCount.stockLevelStatus = _this2.STOCK_STATUSES.warning.id;
} else if (unknownProducts) {
stockCount.stockLevelStatus = 'unknown';
} else {
} else if (totalGrouped > 0) {
stockCount.stockLevelStatus = _this2.STOCK_STATUSES.ok.id;
}
}
Expand Down
2 changes: 1 addition & 1 deletion dist/bundle.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

28 changes: 15 additions & 13 deletions src/state-indicators.service.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,18 @@ const find = (list, match) => {
return undefined
}

const productsGroupedByStatus = (stock) => {
return Object.keys(stock).reduce((grouped, product) => {
const status = stock[product].status
const productsGroupedByStatus = (stock, products) => {
return Object.keys(stock).reduce((grouped, productId) => {
const isRelevant = !!find(products, (product) => product._id === productId)
const status = stock[productId].status
if (!isRelevant) {
return grouped
}
if (status) {
grouped[status].push(product)
} else {
grouped['unknown'].push(product)
grouped[status].push(productId)
}
return grouped
}, { understock: [], 're-stock': [], ok: [], overstock: [], unknown: [] })
}, { understock: [], 're-stock': [], ok: [], overstock: [] })
}

const sumAllocations = (sum, stock) => {
Expand Down Expand Up @@ -147,7 +149,7 @@ class StateIndicatorsService {
}

if (stockCount.location && stockCount.location.lga) {
const groupedByStatus = productsGroupedByStatus(stockCount.stock)
const groupedByStatus = productsGroupedByStatus(stockCount.stock, products)
stockCount.reStockNeeded = !!(groupedByStatus.understock.length + groupedByStatus['re-stock'].length)
} else { // states and zones
if (stockCount.stock) {
Expand All @@ -159,17 +161,17 @@ class StateIndicatorsService {
}

const addStockLevelStatusField = (stockCount) => {
const unknownProducts = productsGroupedByStatus(stockCount.stock).unknown.length
const understockedProducts = productsGroupedByStatus(stockCount.stock).understock.length
const grouped = productsGroupedByStatus(stockCount.stock, products)
const understockedProducts = grouped.understock.length
const totalGrouped = Object.keys(grouped).reduce((sum, group) => sum + grouped[group].length, 0)

stockCount.stockLevelStatus = 'unknown'
if (stockCount.location) {
if (understockedProducts >= this.STOCK_STATUSES.alert.threshold) {
stockCount.stockLevelStatus = this.STOCK_STATUSES.alert.id
} else if (understockedProducts >= this.STOCK_STATUSES.warning.threshold) {
stockCount.stockLevelStatus = this.STOCK_STATUSES.warning.id
} else if (unknownProducts) {
stockCount.stockLevelStatus = 'unknown'
} else {
} else if (totalGrouped > 0) {
stockCount.stockLevelStatus = this.STOCK_STATUSES.ok.id
}
}
Expand Down
10 changes: 9 additions & 1 deletion test/state-indicators.service.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
describe('state indicators service', function () {
var stateIndicatorsService
var thresholdsService
var productListService
var $rootScope
var $q
var angularNavDataMock // eslint-disable-line
var testMod // eslint-disable-line

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

beforeEach(inject(function (
_$q_,
_$rootScope_,
_lgasService_,
_statesService_,
Expand All @@ -167,7 +170,9 @@ describe('state indicators service', function () {
_productListService_
) {
$rootScope = _$rootScope_
$q = _$q_
thresholdsService = _thresholdsService_
productListService = _productListService_
stateIndicatorsService = _stateIndicatorsService_

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

stateIndicatorsService.decorateWithIndicators([unknownLgaStockCount])
.then(function (decoratedStockCounts) {
expect(thresholdsService.calculateThresholds).toHaveBeenCalledWith(undefined, unknownLgaStockCount, products)
expect(thresholdsService.calculateThresholds).toHaveBeenCalledWith(undefined, unknownLgaStockCount, [])
expect(decoratedStockCounts).toEqual(expected)
})
$rootScope.$digest()
Expand Down

0 comments on commit 4b20c57

Please sign in to comment.