@@ -40,40 +40,73 @@ const getFactor = (location, versions, version) => {
40
40
}
41
41
42
42
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
46
45
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
+ }
50
56
51
- if ( ! ( location . targetPopulations && location . targetPopulations . length ) ) {
52
- return { }
57
+ const allocations = getFactor ( location , location . allocations , allocationsVersion )
58
+ return allocations && allocations . weeklyLevels
53
59
}
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
63
78
}
64
79
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
+ }
67
91
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
71
104
}
72
105
73
106
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 ( )
77
110
}
78
111
}
79
112
@@ -107,7 +140,7 @@ class ThresholdsService {
107
140
return
108
141
}
109
142
110
- let thresholds = Object . keys ( weeklyLevels ) . reduce ( ( index , productId ) => {
143
+ return Object . keys ( weeklyLevels ) . reduce ( ( index , productId ) => {
111
144
index [ productId ] = Object . keys ( weeksOfStock ) . reduce ( ( productThresholds , threshold ) => {
112
145
const level = weeklyLevels [ productId ] * weeksOfStock [ threshold ]
113
146
const product = find ( products , isId . bind ( null , productId ) )
@@ -134,8 +167,6 @@ class ThresholdsService {
134
167
135
168
return index
136
169
} , { } )
137
-
138
- return thresholds
139
170
}
140
171
141
172
getThresholdsFor ( stockCounts , products ) {
0 commit comments