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

Commit 1acaec1

Browse files
fix: stay backwards compatible with old location docs
1 parent f367a67 commit 1acaec1

File tree

2 files changed

+83
-28
lines changed

2 files changed

+83
-28
lines changed

src/thresholds.service.js

+59-28
Original file line numberDiff line numberDiff line change
@@ -40,40 +40,73 @@ const getFactor = (location, versions, version) => {
4040
}
4141

4242
const getFactors = (stockCount, location, options) => {
43-
if (!(location.allocations && location.allocations.length)) {
44-
return {}
45-
}
43+
// centralized for whenever we implement #16
44+
const somethingIsWrong = () => undefined
4645

47-
if (location.level !== 'zone' && !(location.plans && location.plans.length)) {
48-
return {}
49-
}
46+
const getWeeklyLevels = () => {
47+
if (!(location.allocations && location.allocations.length)) {
48+
somethingIsWrong()
49+
}
50+
51+
const allocationsVersion = getFactorVersion(stockCount, 'allocations', options)
52+
53+
if (typeof allocationsVersion === 'undefined') {
54+
somethingIsWrong()
55+
}
5056

51-
if (!(location.targetPopulations && location.targetPopulations.length)) {
52-
return {}
57+
const allocations = getFactor(location, location.allocations, allocationsVersion)
58+
return allocations && allocations.weeklyLevels
5359
}
54-
const allocationsVersion = getFactorVersion(stockCount, 'allocations', options)
55-
const plansVersion = getFactorVersion(stockCount, 'plans', options)
56-
const targetPopulationVersion = getFactorVersion(stockCount, 'targetPopulations', options)
57-
58-
if (typeof allocationsVersion === 'undefined' ||
59-
typeof plansVersion === 'undefined' ||
60-
typeof targetPopulationVersion === 'undefined'
61-
) {
62-
return {}
60+
61+
const getWeeksOfStock = () => {
62+
if (location.level !== 'zone' && !(location.plans && location.plans.length)) {
63+
somethingIsWrong()
64+
}
65+
66+
const plansVersion = getFactorVersion(stockCount, 'plans', options)
67+
68+
if (typeof plansVersion === 'undefined') {
69+
somethingIsWrong()
70+
}
71+
72+
let plans = zonePlans
73+
if (location.level !== 'zone') {
74+
plans = getFactor(location, location.plans, plansVersion)
75+
}
76+
77+
return plans && plans.weeksOfStock
6378
}
6479

65-
const allocations = getFactor(location, location.allocations, allocationsVersion)
66-
const targetPopulations = getFactor(location, location.targetPopulations, targetPopulationVersion)
80+
const getMonthlyTargetPopulations = () => {
81+
let monthlyTargetPopulations
82+
if (location.targetPopulations) {
83+
if (!location.targetPopulations.length) {
84+
somethingIsWrong()
85+
}
86+
const targetPopulationVersion = getFactorVersion(stockCount, 'targetPopulations', options)
87+
88+
if (typeof targetPopulationVersion === 'undefined') {
89+
somethingIsWrong()
90+
}
6791

68-
let plans = zonePlans
69-
if (location.level !== 'zone') {
70-
plans = getFactor(location, location.plans, plansVersion)
92+
const targetPopulations = getFactor(location, location.targetPopulations, targetPopulationVersion)
93+
monthlyTargetPopulations = targetPopulations && targetPopulations.monthlyTargetPopulations
94+
} else {
95+
// For backwards compatibility with the old style location docs,
96+
// since we have no control about when the dashboards are going
97+
// to replicate the new location docs
98+
if (!(location.targetPopulation && location.targetPopulation.length)) {
99+
somethingIsWrong()
100+
}
101+
monthlyTargetPopulations = location.targetPopulation
102+
}
103+
return monthlyTargetPopulations
71104
}
72105

73106
return {
74-
weeklyLevels: allocations && allocations.weeklyLevels,
75-
weeksOfStock: plans && plans.weeksOfStock,
76-
targetPopulations: targetPopulations && targetPopulations.monthlyTargetPopulations
107+
weeksOfStock: getWeeksOfStock(),
108+
weeklyLevels: getWeeklyLevels(),
109+
targetPopulations: getMonthlyTargetPopulations()
77110
}
78111
}
79112

@@ -107,7 +140,7 @@ class ThresholdsService {
107140
return
108141
}
109142

110-
let thresholds = Object.keys(weeklyLevels).reduce((index, productId) => {
143+
return Object.keys(weeklyLevels).reduce((index, productId) => {
111144
index[productId] = Object.keys(weeksOfStock).reduce((productThresholds, threshold) => {
112145
const level = weeklyLevels[productId] * weeksOfStock[threshold]
113146
const product = find(products, isId.bind(null, productId))
@@ -134,8 +167,6 @@ class ThresholdsService {
134167

135168
return index
136169
}, {})
137-
138-
return thresholds
139170
}
140171

141172
getThresholdsFor (stockCounts, products) {

test/thresholds.service.spec.js

+24
Original file line numberDiff line numberDiff line change
@@ -222,6 +222,30 @@ describe('thresholds service', function () {
222222
var actual = thresholdsService.calculateThresholds(location, {}, products)
223223
expect(actual).toEqual(expected)
224224
})
225+
it('still works if the location doc contains a non versioned targetPopulation field', function () {
226+
var oldStyleTargetPopulations = {
227+
'product:a': 500,
228+
'product:b': 1000
229+
}
230+
var oldStyleLocation = angular.extend({}, location, { targetPopulation: oldStyleTargetPopulations })
231+
delete oldStyleLocation.targetPopulations
232+
var expected = {
233+
'product:a': {
234+
min: 50,
235+
reOrder: 100,
236+
max: 250,
237+
targetPopulation: 500
238+
},
239+
'product:b': {
240+
min: 100,
241+
reOrder: 200,
242+
max: 500,
243+
targetPopulation: 1000
244+
}
245+
}
246+
var actual = thresholdsService.calculateThresholds(oldStyleLocation, {}, products)
247+
expect(actual).toEqual(expected)
248+
})
225249
})
226250

227251
describe('getThresholdsFor', function () {

0 commit comments

Comments
 (0)