@@ -30,30 +30,49 @@ class ThresholdsService {
30
30
// For zones the thresholds are based on the state store required allocation for
31
31
// the week, that information is passed as an optional param (`requiredStateStoresAllocation`).
32
32
// 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 ) {
35
36
return
36
37
}
37
38
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 ) ) {
40
46
return
41
47
}
42
48
43
49
if ( ! products || ! products . length ) {
44
50
return
45
51
}
46
52
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
+
48
60
if ( ! ( allocation && allocation . weeklyLevels ) ) {
49
61
return
50
62
}
51
63
52
64
const weeklyLevels = allocation . weeklyLevels
65
+
53
66
let weeksOfStock = zonesPlan
54
67
55
68
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
+
57
76
if ( ! ( plan && plan . weeksOfStock ) ) {
58
77
return
59
78
}
0 commit comments