-
Notifications
You must be signed in to change notification settings - Fork 9
/
postReview.js
66 lines (62 loc) · 2.01 KB
/
postReview.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
import DOMPurify from 'isomorphic-dompurify'
export default async (req, res) => {
const query = req.query
const productId = query.productId
const name = DOMPurify.sanitize(query.name.trim())
const email = DOMPurify.sanitize(query.email.trim().toLowerCase())
const reviewText = DOMPurify.sanitize(query.reviewText.trim())
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: `
mutation postReview($productId: String, $name: String, $email: String, $reviewText: String) {
createReview(data: { productId: $productId, name: $name, email: $email, reviewText: $reviewText }) {
data {
id
attributes {
productId
name
email
reviewText
createdAt
}
}
}
}
`,
variables: {
productId: productId,
name: name,
email: email,
reviewText: reviewText
}
})
})
.then(r => {
if (r.status >= 400) {
return r.json().then(errResData => {
const err = new Error('Error in api/postReview.js, when mutating query to post review, if (r.status >= 400) condition')
err.data = errResData
throw err
})
}
return r.json()
})
.then(data => {
// Strapi will attach errors obj to data in case such email is already present
if (data.errors) {
res.statusMessage = 'The review with such email has already been published. Please, choose another email.'
res.status(400).end()
} else {
res.status(200).json(data)
}
})
.catch(err => res.status(404).json({ message: `Error in api/postReview.js, nested .catch statement, err object: ${err}` }))
}