This repository was archived by the owner on Mar 18, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.js
More file actions
111 lines (93 loc) · 4.25 KB
/
utils.js
File metadata and controls
111 lines (93 loc) · 4.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
const getEanAndQuantityFromIngredient = require('./apiModules/getEanAndQuantityFromIngredient');
const getStoresFromEan = require('./apiModules/getStoresFromEan');
const getPriceFromEanAndStore = require('./apiModules/getPriceFromEanAndStore');
async function getPriceFromIngredient(ingredient) {
const { ean, quantity } = await getEanAndQuantityFromIngredient(ingredient);
let stores = await getStoresFromEan(ean);
let price = await getPriceFromEanAndStore(ean, stores);
let weightedPrice = quantity === 0 ? 0 : price * ingredient.amount / quantity
Number.isNaN(weightedPrice) ? weightedPrice = 0 : Math.abs(weightedPrice)
return weightedPrice
}
module.exports = {
getIngredients: function (recipe, requiredPortions) {
let ingredients = []
if (!recipe) return ingredients
let portions = recipe.Portions ? eval(recipe.Portions.Amount) : 1
Array.isArray(recipe.Ingredients) ? recipe.Ingredients.forEach(ingredient => {
Array.isArray(ingredient.SubSectionIngredients) ? ingredient.SubSectionIngredients.forEach(subSectionIngredient => {
let name = subSectionIngredient[0].Name
let ean = subSectionIngredient[0].Ean
let ingredientTypeName = subSectionIngredient[0].IngredientTypeName
let amount
if (subSectionIngredient[0] && subSectionIngredient[0].Amount) {
let amountNumber = eval(subSectionIngredient[0].Amount.match(/\d/g).join(""))
if (subSectionIngredient[0].Unit === "tl") { // tea spoon
amount = amountNumber * 4.2 / portions * requiredPortions // convert to grams
} else if (subSectionIngredient[0].Unit === "dl") { // deciliters
amount = amountNumber * 100 / portions * requiredPortions // convert to milliliters
} else if (subSectionIngredient[0].Unit === "rkl") { // table spoon
amount = amountNumber * 15 / portions * requiredPortions // convert to grams
} else if (subSectionIngredient[0].Unit === "ripaus") { // an insignificant amount, from 1/2 tea spoon to 1/16
amount = (1.5 / portions) * requiredPortions // convert to grams
} else {
if (subSectionIngredient[0].AmountInfo) {
let num = eval(subSectionIngredient[0].AmountInfo.match(/\d/g).join(""))
amount = (num / portions) * requiredPortions
} else {
amount = (amountNumber / portions) * requiredPortions
}
}
}
amount = amount ? amount : 0
let index = ingredients.findIndex(x => x.name === name)
if (index === -1) {
ingredients.push({
name,
ean: ean ? ean : null,
amount: Math.round(amount),
ingredientTypeName
})
}
else {
ingredients[index].amount = Math.round(amount + ingredients[index].amount)
}
}) : console.log('No sub-ingredients')
}) : console.log("No ingredients")
return ingredients
},
getVanillaIngredients: function (recipe, requiredPortions) {
let ingredients = []
if (!recipe) return ingredients
let portions = recipe.Portions ? eval(recipe.Portions.Amount) : 1
Array.isArray(recipe.Ingredients) ? recipe.Ingredients.forEach(ingredient => {
Array.isArray(ingredient.SubSectionIngredients) ? ingredient.SubSectionIngredients.forEach(subSectionIngredient => {
let name = subSectionIngredient[0].Name
let ean = subSectionIngredient[0].Ean
let ingredientTypeName = subSectionIngredient[0].IngredientTypeName
let amount
if (subSectionIngredient[0] && subSectionIngredient[0].Amount) {
ingredients.push({
name,
ean: ean ? ean : null,
amount: subSectionIngredient[0].Amount,
ingredientTypeName
})
}
}) : console.log('No sub-ingredients')
}) : console.log("No ingredients")
return ingredients
},
getRecipePrice: async function (ingredients) {
let finalPrice = 0
for (let i = 0; i < ingredients.length; i++) {
weightedPrice = await getPriceFromIngredient(ingredients[i])
finalPrice += weightedPrice
}
if (finalPrice > 3) {
return (finalPrice * 1.5).toFixed(2) + '€'
} else {
return (finalPrice * 2.5).toFixed(2) + '€'
}
}
}