-
Notifications
You must be signed in to change notification settings - Fork 9
/
available.js
53 lines (48 loc) · 1.55 KB
/
available.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
export default async (req, res) => {
const ids = req.query.ids
const arrOfIds = ids.split(',')
const filterArrValue = arrOfIds.map(i => ({ id : { eq: i }}))
// [{id:{eq:"<idNumber>"}},{id:{eq:"<idNumber>"}},{id:{eq:"<idNumber>"}}] ↓
const filterStringValue = JSON.stringify(filterArrValue).replaceAll('"id"', 'id').replaceAll('"eq"', 'eq')
// fetch values of available amounts of items in cart based on passed ids of items in cart
await fetch(`${
process.env.NODE_ENV === "production"
? process.env.PROD_CMS_URL
: process.env.DEV_CMS_URL
}/graphql`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({
query: `
query {
products(filters: { or: ${filterStringValue} }) {
data {
id
attributes {
available
}
}
}
}
`
})
}
)
.then(r => {
if (r.status >= 400) {
return r.json().then(errResData => {
const err = new Error('Error in: api/available.js, .then statement, if (r.status >= 400) condition')
err.data = errResData
throw err
})
}
return r.json()
})
.then(data => {
const items = data.data.products.data
res.status(200).json(items)
})
.catch(err => res.status(404).json({ message: `Error in api/available.js, .catch statement, status 404, err object: ${err}` }))
}