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

Commit 04371f4

Browse files
refactor: log missing param errors
Closes #16
1 parent 5acfb05 commit 04371f4

File tree

3 files changed

+43
-35
lines changed

3 files changed

+43
-35
lines changed

src/factor-extractor.js

Lines changed: 17 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,6 @@ import config from './config/config.json'
33
import { find } from './utils.js'
44
import calculateWeeklyLevels from './weekly-levels-calculator'
55

6-
// centralized for whenever we implement #16
7-
const somethingIsWrong = () => {}
8-
96
const isVersion = (date, version) => {
107
const momentDate = moment().isoWeekYear(date.year).isoWeek(date.week).isoWeekday(1).startOf('day')
118
const momentVersionStartDate = moment(version.date, config.versionDateFormat).startOf('isoWeek').startOf('day')
@@ -24,19 +21,26 @@ const getFactor = (versions, date) => {
2421

2522
const getCoefficients = (productCoefficients, date) => {
2623
if (!(productCoefficients && productCoefficients.versions && productCoefficients.versions.length)) {
27-
return somethingIsWrong()
24+
throw new Error('missing productCoefficients or productCoefficients.versions')
2825
}
26+
2927
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
3132
}
3233

3334
const getWeeksOfStock = (location, date) => {
3435
if (!(location.plans && location.plans.length)) {
35-
return somethingIsWrong()
36+
throw new Error(`missing plans on location ${location._id}`)
3637
}
3738

3839
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
4044
}
4145

4246
const getTargetPopulations = (location, date) => {
@@ -66,46 +70,33 @@ const getTargetPopulations = (location, date) => {
6670

6771
const getWeeklyLevels = (location, date) => {
6872
if (!(location.allocations && location.allocations.length)) {
69-
return somethingIsWrong()
73+
throw new Error(`missing allocations on location ${location._id}`)
7074
}
7175

7276
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
7481
}
7582

7683
export default (location, productCoefficients, date) => {
7784
const weeksOfStock = getWeeksOfStock(location, date)
78-
if (!weeksOfStock) {
79-
return somethingIsWrong()
80-
}
81-
8285
const { version, monthlyTargetPopulations } = getTargetPopulations(location, date)
8386

8487
// For backwards compatibility to version before introducing `targetPopulations`,
8588
// since for that version `weeklyAllocations` were not always calculated
8689
// based on target population
8790
if (version === 1) {
88-
const weeklyLevels = getWeeklyLevels(location, date)
89-
if (!weeklyLevels) {
90-
return somethingIsWrong()
91-
}
92-
9391
return {
94-
weeklyLevels,
92+
weeklyLevels: getWeeklyLevels(location, date),
9593
weeksOfStock,
9694
monthlyTargetPopulations
9795
}
9896
}
9997

10098
const coefficients = getCoefficients(productCoefficients, date)
101-
if (!(monthlyTargetPopulations && coefficients)) {
102-
return somethingIsWrong()
103-
}
104-
10599
const weeklyLevels = calculateWeeklyLevels(monthlyTargetPopulations, coefficients)
106-
if (!weeklyLevels) {
107-
return somethingIsWrong()
108-
}
109100

110101
return {
111102
weeklyLevels,

src/thresholds.service.js

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import defaultCoefficients from './config/coefficients.json'
2-
import { find } from './utils.js'
2+
import { find, somethingIsWrong } from './utils.js'
33
import getFactors from './factor-extractor.js'
44

55
class ThresholdsService {
@@ -16,21 +16,31 @@ class ThresholdsService {
1616
//
1717
// Passing the coefficientVersions as a param so that it can be adapted later to use the database doc
1818
calculateThresholds (location, stockCount, products, requiredStateStoresAllocation = {}, productCoefficients = defaultCoefficients) {
19-
if (!(stockCount && stockCount.date)) {
20-
return
19+
if (!stockCount) {
20+
const locationId = location && location._id ? location._id : 'with unknown id'
21+
return somethingIsWrong(`missing mandatory param stock count for location ${locationId}`)
22+
}
23+
if (!stockCount.date) {
24+
return somethingIsWrong(`missing date on stock count ${stockCount._id}`)
2125
}
2226

23-
if (!(location && location.level)) {
24-
return
27+
if (!location) {
28+
const stockCountId = stockCount && stockCount._id ? stockCount._id : 'with unknown id'
29+
return somethingIsWrong(`missing mandatory param location for stock count ${stockCountId}`)
30+
}
31+
if (!location.level) {
32+
return somethingIsWrong(`missing level on location ${location._id}`)
2533
}
2634

2735
if (!(products && products.length)) {
28-
return
36+
return somethingIsWrong('missing mandatory param products')
2937
}
3038

31-
const locationFactors = getFactors(location, productCoefficients, stockCount.date)
32-
33-
if (!locationFactors) {
39+
let locationFactors
40+
try {
41+
locationFactors = getFactors(location, productCoefficients, stockCount.date)
42+
} catch (e) {
43+
somethingIsWrong(e.message)
3444
return
3545
}
3646

src/utils.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,10 @@ export function find (list, match) {
77
}
88
return undefined
99
}
10+
11+
export function somethingIsWrong (msg) {
12+
if (console) {
13+
console.warn(`angular-nav-thresholds: ${msg}`)
14+
}
15+
// TODO: log it to Google Analytics (#28)
16+
}

0 commit comments

Comments
 (0)