@@ -13,10 +13,101 @@ const isVersion = (version, item) => item.version === version
13
13
const isId = ( id , item ) => item . _id === id
14
14
15
15
// Zones config
16
- const zonesPlan = {
17
- min : 0 ,
18
- reOrder : 3 ,
19
- max : 6
16
+ const zonePlans = {
17
+ weeksOfStock : {
18
+ min : 0 ,
19
+ reOrder : 3 ,
20
+ max : 6
21
+ }
22
+ }
23
+
24
+ const getFactorVersion = ( stockCount , factor , options ) => {
25
+ if ( options . version === 'last' ) {
26
+ return options . version
27
+ }
28
+ if ( ! ( stockCount [ factor ] && stockCount [ factor ] . version ) ) {
29
+ return 1
30
+ }
31
+ return stockCount [ factor ] . version
32
+ }
33
+
34
+ const getFactor = ( location , versions , version ) => {
35
+ if ( version === 'last' ) {
36
+ return versions [ versions . length - 1 ]
37
+ }
38
+
39
+ return find ( versions , isVersion . bind ( null , version ) )
40
+ }
41
+
42
+ const getFactors = ( stockCount , location , options ) => {
43
+ // centralized for whenever we implement #16
44
+ const somethingIsWrong = ( ) => undefined
45
+
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
+ }
56
+
57
+ const allocations = getFactor ( location , location . allocations , allocationsVersion )
58
+ return allocations && allocations . weeklyLevels
59
+ }
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
78
+ }
79
+
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
+ }
91
+
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
104
+ }
105
+
106
+ return {
107
+ weeksOfStock : getWeeksOfStock ( ) ,
108
+ weeklyLevels : getWeeklyLevels ( ) ,
109
+ targetPopulations : getMonthlyTargetPopulations ( )
110
+ }
20
111
}
21
112
22
113
class ThresholdsService {
@@ -31,55 +122,25 @@ class ThresholdsService {
31
122
// the week, that information is passed as an optional param (`requiredStateStoresAllocation`).
32
123
// That param is only used for zones.
33
124
calculateThresholds ( location , stockCount , products , requiredStateStoresAllocation = { } , options = { } ) {
34
- if ( ! location || ! location . allocations || ! location . allocations . length ||
35
- ! location . plans || ! location . plans . length || ! location . level ) {
36
- return
37
- }
38
-
39
125
if ( ! stockCount ) {
40
126
return
41
127
}
42
128
43
- if ( options . version !== 'last' &&
44
- ! ( stockCount . allocations && typeof stockCount . allocations . version !== undefined &&
45
- stockCount . plans && typeof stockCount . plans . version !== undefined ) ) {
129
+ if ( ! location && location . level ) {
46
130
return
47
131
}
48
132
49
133
if ( ! products || ! products . length ) {
50
134
return
51
135
}
52
136
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
- }
137
+ const { weeklyLevels, weeksOfStock, targetPopulations } = getFactors ( stockCount , location , options )
59
138
60
- if ( ! ( allocation && allocation . weeklyLevels ) ) {
139
+ if ( ! ( weeklyLevels && weeksOfStock && targetPopulations ) ) {
61
140
return
62
141
}
63
142
64
- const weeklyLevels = allocation . weeklyLevels
65
-
66
- let weeksOfStock = zonesPlan
67
-
68
- if ( location . level !== 'zone' ) {
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
-
76
- if ( ! ( plan && plan . weeksOfStock ) ) {
77
- return
78
- }
79
- weeksOfStock = plan . weeksOfStock
80
- }
81
-
82
- let thresholds = Object . keys ( weeklyLevels ) . reduce ( ( index , productId ) => {
143
+ return Object . keys ( weeklyLevels ) . reduce ( ( index , productId ) => {
83
144
index [ productId ] = Object . keys ( weeksOfStock ) . reduce ( ( productThresholds , threshold ) => {
84
145
const level = weeklyLevels [ productId ] * weeksOfStock [ threshold ]
85
146
const product = find ( products , isId . bind ( null , productId ) )
@@ -102,14 +163,10 @@ class ThresholdsService {
102
163
return productThresholds
103
164
} , { } )
104
165
105
- if ( location . targetPopulation ) {
106
- index [ productId ] . targetPopulation = location . targetPopulation [ productId ]
107
- }
166
+ index [ productId ] . targetPopulation = targetPopulations [ productId ]
108
167
109
168
return index
110
169
} , { } )
111
-
112
- return thresholds
113
170
}
114
171
115
172
getThresholdsFor ( stockCounts , products ) {
@@ -129,7 +186,12 @@ class ThresholdsService {
129
186
const id = this . smartId . idify ( scLocation , locationIdPattern )
130
187
const allocations = stockCount . allocations || { version : 1 }
131
188
const plans = stockCount . plans || { version : 1 }
132
- index [ id ] = angular . merge ( { } , { allocations : allocations , plans : plans } )
189
+ const targetPopulations = stockCount . targetPopulations || { version : 1 }
190
+ index [ id ] = angular . merge ( { } , {
191
+ allocations,
192
+ plans,
193
+ targetPopulations
194
+ } )
133
195
134
196
if ( scLocation . lga ) {
135
197
if ( ! promises . lga ) {
0 commit comments