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

Commit

Permalink
fix: wrong unknown stockStatusLevel
Browse files Browse the repository at this point in the history
Occurs when there are no understocked products but there is an
unknown one (ex: tt)

Closes #33
  • Loading branch information
patriciagarcia committed Oct 21, 2016
1 parent 2816d42 commit d0850e6
Showing 1 changed file with 15 additions and 13 deletions.
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

0 comments on commit d0850e6

Please sign in to comment.