-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontroller.js
78 lines (72 loc) · 2.31 KB
/
controller.js
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
import dotenv from 'dotenv';
import got from 'got';
dotenv.config();
// retrieve Edamam API credentials
const id = process.env.APP_ID;
const key = process.env.APP_KEY;
// check if UPC sent from clientside is vegan
const lookup = (req, res) => {
const { upc } = req.query;
let name = null;
let image = null;
// retrieve initial product details from Edamam's Parser
got.get(`https://api.edamam.com/api/food-database/v2/parser?upc=${upc}&app_id=${id}&app_key=${key}`, {
responseType: 'json',
})
.then((response) => ({
name: response.body.hints[0].food.label,
id: response.body.hints[0].food.foodId,
image: response.body.hints[0].food.image,
}))
// using retrieved product id, retrieve nutritional information from Edamam
.then((food) => {
name = food.name;
image = food.image;
return got.post(`https://api.edamam.com/api/food-database/v2/nutrients?app_id=${id}&app_key=${key}`, {
json: {
ingredients: [{
quantity: 1,
measureURI: 'http://www.edamam.com/ontologies/edamam.owl#Measure_serving',
foodId: food.id,
}],
},
responseType: 'json',
});
})
// check if product is vegan and send relevant product information back
.then((response) => {
/*
two-step vegan checking process, evaluates from left to right
check if product explicitly has "vegan" label; else, check if all other vegan labels are present
as long as one condition is satisfied, product is vegan
todo: add third vegan check as last safety check which reviews the product ingredients
*/
const isVegan = response.body.healthLabels.includes('VEGAN') || checkHealthLabels(response.body.healthLabels);
const food = {
name,
isVegan,
image,
};
res.send(food);
})
// if API returns with 404 error, product was not in database
.catch((err) => {
console.log(err);
res.send('Product not found.');
});
};
const checkHealthLabels = (labels) => {
const veganLabels = [
'VEGETARIAN',
'SHELLFISH_FREE',
'RED_MEAT_FREE',
'PORK_FREE',
'FISH_FREE',
'EGG_FREE',
'DAIRY_FREE',
'CRUSTACEAN_FREE',
'MOLLUSK_FREE',
];
return veganLabels.every((label) => labels.includes(label));
};
export default lookup;