Initial commit
This commit is contained in:
15
api/hooks/rating/delete_delete.js
Normal file
15
api/hooks/rating/delete_delete.js
Normal file
@@ -0,0 +1,15 @@
|
||||
;(function () {
|
||||
const ratingId = context.request().param("id")
|
||||
let rating = context.db.find("rating", {
|
||||
filter: {
|
||||
_id: ratingId,
|
||||
},
|
||||
})[0]
|
||||
if (!rating.id)
|
||||
throw {
|
||||
status: 400,
|
||||
error: "No id specified.",
|
||||
}
|
||||
// @ts-ignore
|
||||
context["rating"] = rating
|
||||
})()
|
||||
7
api/hooks/rating/delete_return.js
Normal file
7
api/hooks/rating/delete_return.js
Normal file
@@ -0,0 +1,7 @@
|
||||
let { clearSSRCache } = require("../lib/utils")
|
||||
let { deleteRating } = require("../lib/bigcommerceRestAPI")
|
||||
;(function () {
|
||||
clearSSRCache()
|
||||
// @ts-ignore
|
||||
deleteRating(context["rating"].bigCommerceProductId, context["rating"].bigcommerceReviewId)
|
||||
})()
|
||||
46
api/hooks/rating/get_read.js
Normal file
46
api/hooks/rating/get_read.js
Normal file
@@ -0,0 +1,46 @@
|
||||
// @ts-check
|
||||
const { withAccount } = require("../lib/utils")
|
||||
;(function () {
|
||||
/** @type {HookResponse} */
|
||||
let hookResponse
|
||||
let request = context.request()
|
||||
if (context.user.auth()) return
|
||||
const orderId = Number(request.query("orderId"))
|
||||
if (orderId) {
|
||||
withAccount((login) => {
|
||||
let order = context.db.find("bigCommerceOrder", {
|
||||
filter: {
|
||||
bigCommerceId: orderId,
|
||||
customerBigCommerceId: login.bigCommerceId,
|
||||
},
|
||||
})[0]
|
||||
if (!order) {
|
||||
throw {
|
||||
status: 404,
|
||||
data: {
|
||||
message: "Order not found",
|
||||
},
|
||||
}
|
||||
}
|
||||
})
|
||||
hookResponse = {
|
||||
filter: {
|
||||
bigcommerceOrderId: orderId,
|
||||
},
|
||||
}
|
||||
|
||||
return hookResponse
|
||||
} else {
|
||||
hookResponse = {
|
||||
filter: context.filter,
|
||||
selector: {
|
||||
bigCommerceProductId: 1,
|
||||
rating: 1,
|
||||
comment: 1,
|
||||
title: 1,
|
||||
review_date: 1,
|
||||
},
|
||||
}
|
||||
return hookResponse
|
||||
}
|
||||
})()
|
||||
17
api/hooks/rating/post_create.js
Normal file
17
api/hooks/rating/post_create.js
Normal file
@@ -0,0 +1,17 @@
|
||||
const { productInsideOrder } = require("../lib/utils")
|
||||
|
||||
;(function () {
|
||||
if (!context?.user?.auth()?.id) {
|
||||
if (!context.data) throw { status: 400, error: "No data provided" }
|
||||
// @ts-ignore
|
||||
productInsideOrder(context.data)
|
||||
/** @type {ProductRating[]} */ // @ts-ignore
|
||||
let ratings = context.db.find("rating", {
|
||||
filter: {
|
||||
bigcommerceOrderId: context?.data?.bigcommerceOrderId,
|
||||
bigCommerceProductId: context?.data?.bigCommerceProductId,
|
||||
},
|
||||
})
|
||||
if (ratings.length) throw { status: 400, error: "Rating already existing" }
|
||||
}
|
||||
})()
|
||||
41
api/hooks/rating/post_return.js
Normal file
41
api/hooks/rating/post_return.js
Normal file
@@ -0,0 +1,41 @@
|
||||
const { bigcommerceApiOAuth, serverBaseURL, bigcommerceStoreHash } = require("../config.js")
|
||||
let { sendOperatorRatingMail, clearSSRCache, statusIsValid } = require("../lib/utils")
|
||||
;(function () {
|
||||
const response = context.http.fetch(
|
||||
`https://api.bigcommerce.com/stores/${bigcommerceStoreHash}/v3/catalog/products/${context?.data?.bigCommerceProductId}/reviews`,
|
||||
{
|
||||
method: "POST",
|
||||
headers: {
|
||||
"Content-Type": "application/json",
|
||||
Accept: "application/json",
|
||||
"X-Auth-Token": bigcommerceApiOAuth,
|
||||
},
|
||||
body: JSON.stringify({
|
||||
title: context?.data?.title,
|
||||
comment: context?.data?.comment,
|
||||
status:
|
||||
context?.data?.status === "approved"
|
||||
? "approved"
|
||||
: context?.data?.status == "pending"
|
||||
? "pending"
|
||||
: "disapproved",
|
||||
rating: context?.data?.rating?.overall,
|
||||
date_reviewed: context?.data?.review_date,
|
||||
}),
|
||||
}
|
||||
)
|
||||
|
||||
if (!statusIsValid(response.status)) {
|
||||
throw {
|
||||
status: response.status,
|
||||
error: response.statusText,
|
||||
}
|
||||
}
|
||||
const bigcommerceRating = response.body.json()
|
||||
context.db.update("rating", context?.data?.id || "", {
|
||||
bigcommerceReviewId: String(bigcommerceRating?.data?.id),
|
||||
})
|
||||
|
||||
clearSSRCache()
|
||||
sendOperatorRatingMail()
|
||||
})()
|
||||
5
api/hooks/rating/post_validate.js
Normal file
5
api/hooks/rating/post_validate.js
Normal file
@@ -0,0 +1,5 @@
|
||||
let { validateAndModifyRating } = require("../lib/utils")
|
||||
;(function () {
|
||||
// @ts-ignore
|
||||
return { data: validateAndModifyRating(context.data) }
|
||||
})()
|
||||
42
api/hooks/rating/put_return.js
Normal file
42
api/hooks/rating/put_return.js
Normal file
@@ -0,0 +1,42 @@
|
||||
let { sendOperatorRatingMail, clearSSRCache, statusIsValid } = require("../lib/utils")
|
||||
const { bigcommerceApiOAuth, bigcommerceStoreHash } = require("../config.js")
|
||||
;(function () {
|
||||
const response = context.http.fetch(
|
||||
// @ts-ignore
|
||||
`https://api.bigcommerce.com/stores/${bigcommerceStoreHash}/v3/catalog/products/${context?.data?.bigCommerceProductId}/reviews/${context["oldRating"]?.bigcommerceReviewId}`,
|
||||
{
|
||||
method: "PUT",
|
||||
headers: {
|
||||
"Content-Type": "application/json",
|
||||
Accept: "application/json",
|
||||
"X-Auth-Token": bigcommerceApiOAuth,
|
||||
},
|
||||
body: JSON.stringify({
|
||||
title: context?.data?.title,
|
||||
comment: context?.data?.comment,
|
||||
status:
|
||||
context?.data?.status === "approved"
|
||||
? "approved"
|
||||
: context?.data?.status == "pending"
|
||||
? "pending"
|
||||
: "disapproved",
|
||||
rating: context?.data?.rating?.overall,
|
||||
date_reviewed: context?.data?.review_date,
|
||||
}),
|
||||
}
|
||||
)
|
||||
if (!statusIsValid(response.status)) {
|
||||
throw {
|
||||
status: response.status,
|
||||
error: response.statusText,
|
||||
}
|
||||
}
|
||||
let rating = context.data
|
||||
// @ts-ignore
|
||||
let oldRating = context["oldRating"]
|
||||
// @ts-ignore
|
||||
if (!oldRating || JSON.stringify(rating) != JSON.stringify(oldRating)) {
|
||||
sendOperatorRatingMail()
|
||||
clearSSRCache()
|
||||
}
|
||||
})()
|
||||
16
api/hooks/rating/put_update.js
Normal file
16
api/hooks/rating/put_update.js
Normal file
@@ -0,0 +1,16 @@
|
||||
const { productInsideOrder } = require("../lib/utils")
|
||||
|
||||
;(function () {
|
||||
if (!context?.user?.auth()?.id) {
|
||||
// @ts-ignore
|
||||
productInsideOrder(context?.data)
|
||||
}
|
||||
/** @type {ProductRating} */ // @ts-ignore
|
||||
let ratingObj = context.db.find("rating", {
|
||||
filter: {
|
||||
_id: context?.data?.id,
|
||||
},
|
||||
})[0]
|
||||
// @ts-ignore
|
||||
context["oldRating"] = ratingObj
|
||||
})()
|
||||
5
api/hooks/rating/put_validate.js
Normal file
5
api/hooks/rating/put_validate.js
Normal file
@@ -0,0 +1,5 @@
|
||||
let { validateAndModifyRating } = require("../lib/utils")
|
||||
;(function () {
|
||||
// @ts-ignore
|
||||
return { data: validateAndModifyRating(context.data) }
|
||||
})()
|
||||
Reference in New Issue
Block a user