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

Commit f363633

Browse files
Merge pull request #18 from fielded/17-version-last
Pass { version: 'last' } to use last version of plans and allocations
2 parents 37d6c38 + 42288bc commit f363633

File tree

2 files changed

+51
-6
lines changed

2 files changed

+51
-6
lines changed

src/thresholds.service.js

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,30 +30,49 @@ class ThresholdsService {
3030
// For zones the thresholds are based on the state store required allocation for
3131
// the week, that information is passed as an optional param (`requiredStateStoresAllocation`).
3232
// That param is only used for zones.
33-
calculateThresholds (location, stockCount, products, requiredStateStoresAllocation = {}) {
34-
if (!location || !location.allocations || !location.plans || !location.level) {
33+
calculateThresholds (location, stockCount, products, requiredStateStoresAllocation = {}, options = {}) {
34+
if (!location || !location.allocations || !location.allocations.length ||
35+
!location.plans || !location.plans.length || !location.level) {
3536
return
3637
}
3738

38-
if (!stockCount || !stockCount.allocations || !stockCount.allocations.version ||
39-
!stockCount.plans || !stockCount.plans.version) {
39+
if (!stockCount) {
40+
return
41+
}
42+
43+
if (options.version !== 'last' &&
44+
!(stockCount.allocations && typeof stockCount.allocations.version !== undefined &&
45+
stockCount.plans && typeof stockCount.plans.version !== undefined)) {
4046
return
4147
}
4248

4349
if (!products || !products.length) {
4450
return
4551
}
4652

47-
const allocation = find(location.allocations, isVersion.bind(null, stockCount.allocations.version))
53+
let allocation
54+
if (options.version === 'last') {
55+
allocation = location.allocations[location.allocations.length - 1]
56+
} else {
57+
allocation = find(location.allocations, isVersion.bind(null, stockCount.allocations.version))
58+
}
59+
4860
if (!(allocation && allocation.weeklyLevels)) {
4961
return
5062
}
5163

5264
const weeklyLevels = allocation.weeklyLevels
65+
5366
let weeksOfStock = zonesPlan
5467

5568
if (location.level !== 'zone') {
56-
const plan = find(location.plans, isVersion.bind(null, stockCount.plans.version))
69+
let plan
70+
if (options.version === 'last') {
71+
plan = location.plans[location.plans.length - 1]
72+
} else {
73+
plan = find(location.plans, isVersion.bind(null, stockCount.plans.version))
74+
}
75+
5776
if (!(plan && plan.weeksOfStock)) {
5877
return
5978
}

test/thresholds.service.spec.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,14 @@ describe('thresholds service', function () {
5959
reOrder: 2,
6060
min: 1
6161
}
62+
},
63+
{
64+
version: 2,
65+
weeksOfStock: {
66+
max: 10,
67+
reOrder: 4,
68+
min: 2
69+
}
6270
}
6371
]
6472
}
@@ -171,6 +179,24 @@ describe('thresholds service', function () {
171179
var actual = thresholdsService.calculateThresholds(unroundedLocation, stockCount, products)
172180
expect(actual).toEqual(expected)
173181
})
182+
it('uses the last version of plans and allocations if { version: "last"} is passed as option', function () {
183+
var expected = {
184+
'product:a': {
185+
min: 200,
186+
reOrder: 400,
187+
max: 1000,
188+
targetPopulation: 1000
189+
},
190+
'product:b': {
191+
min: 400,
192+
reOrder: 800,
193+
max: 2000,
194+
targetPopulation: 2000
195+
}
196+
}
197+
var actual = thresholdsService.calculateThresholds(location, stockCount, products, null, { version: 'last' })
198+
expect(actual).toEqual(expected)
199+
})
174200
})
175201

176202
describe('getThresholdsFor', function () {

0 commit comments

Comments
 (0)