@@ -3,9 +3,6 @@ import config from './config/config.json'
3
3
import { find } from './utils.js'
4
4
import calculateWeeklyLevels from './weekly-levels-calculator'
5
5
6
- // centralized for whenever we implement #16
7
- const somethingIsWrong = ( ) => { }
8
-
9
6
const isVersion = ( date , version ) => {
10
7
const momentDate = moment ( ) . isoWeekYear ( date . year ) . isoWeek ( date . week ) . isoWeekday ( 1 ) . startOf ( 'day' )
11
8
const momentVersionStartDate = moment ( version . date , config . versionDateFormat ) . startOf ( 'isoWeek' ) . startOf ( 'day' )
@@ -24,19 +21,26 @@ const getFactor = (versions, date) => {
24
21
25
22
const getCoefficients = ( productCoefficients , date ) => {
26
23
if ( ! ( productCoefficients && productCoefficients . versions && productCoefficients . versions . length ) ) {
27
- return somethingIsWrong ( )
24
+ throw new Error ( 'missing productCoefficients or productCoefficients.versions' )
28
25
}
26
+
29
27
const version = getFactor ( productCoefficients . versions , date )
30
- return version && version . coefficients
28
+ if ( ! ( version && version . coefficients ) ) {
29
+ throw new Error ( `cannot find version of coefficients for date ${ date } ` )
30
+ }
31
+ return version . coefficients
31
32
}
32
33
33
34
const getWeeksOfStock = ( location , date ) => {
34
35
if ( ! ( location . plans && location . plans . length ) ) {
35
- return somethingIsWrong ( )
36
+ throw new Error ( `missing plans on location ${ location . _id } ` )
36
37
}
37
38
38
39
const plans = getFactor ( location . plans , date )
39
- return plans && plans . weeksOfStock
40
+ if ( ! ( plans && plans . weeksOfStock ) ) {
41
+ throw new Error ( `cannot find version of weeksOfStock for location ${ location . _id } and date ${ date } ` )
42
+ }
43
+ return plans . weeksOfStock
40
44
}
41
45
42
46
const getTargetPopulations = ( location , date ) => {
@@ -66,46 +70,33 @@ const getTargetPopulations = (location, date) => {
66
70
67
71
const getWeeklyLevels = ( location , date ) => {
68
72
if ( ! ( location . allocations && location . allocations . length ) ) {
69
- return somethingIsWrong ( )
73
+ throw new Error ( `missing allocations on location ${ location . _id } ` )
70
74
}
71
75
72
76
const allocations = getFactor ( location . allocations , date )
73
- return allocations && allocations . weeklyLevels
77
+ if ( ! ( allocations && allocations . weeklyLevels ) ) {
78
+ throw new Error ( `cannot find version of weeklyLevels for location ${ location . _id } and date ${ date } ` )
79
+ }
80
+ return allocations . weeklyLevels
74
81
}
75
82
76
83
export default ( location , productCoefficients , date ) => {
77
84
const weeksOfStock = getWeeksOfStock ( location , date )
78
- if ( ! weeksOfStock ) {
79
- return somethingIsWrong ( )
80
- }
81
-
82
85
const { version, monthlyTargetPopulations } = getTargetPopulations ( location , date )
83
86
84
87
// For backwards compatibility to version before introducing `targetPopulations`,
85
88
// since for that version `weeklyAllocations` were not always calculated
86
89
// based on target population
87
90
if ( version === 1 ) {
88
- const weeklyLevels = getWeeklyLevels ( location , date )
89
- if ( ! weeklyLevels ) {
90
- return somethingIsWrong ( )
91
- }
92
-
93
91
return {
94
- weeklyLevels,
92
+ weeklyLevels : getWeeklyLevels ( location , date ) ,
95
93
weeksOfStock,
96
94
monthlyTargetPopulations
97
95
}
98
96
}
99
97
100
98
const coefficients = getCoefficients ( productCoefficients , date )
101
- if ( ! ( monthlyTargetPopulations && coefficients ) ) {
102
- return somethingIsWrong ( )
103
- }
104
-
105
99
const weeklyLevels = calculateWeeklyLevels ( monthlyTargetPopulations , coefficients )
106
- if ( ! weeklyLevels ) {
107
- return somethingIsWrong ( )
108
- }
109
100
110
101
return {
111
102
weeklyLevels,
0 commit comments