general setup
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["product"] = rating.productId
|
||||
})()
|
||||
17
api/hooks/rating/delete_return.js
Normal file
17
api/hooks/rating/delete_return.js
Normal file
@@ -0,0 +1,17 @@
|
||||
let { recalcRatingToProduct } = require("../lib/utils")
|
||||
;(function () {
|
||||
let product = context.db.find("bigCommerceProducts", {
|
||||
filter: {
|
||||
// @ts-ignore
|
||||
_id: context["product"],
|
||||
},
|
||||
})[0]
|
||||
if (!product)
|
||||
throw {
|
||||
status: 400,
|
||||
error: "Could not resolve rating product",
|
||||
}
|
||||
//@ts-ignore
|
||||
recalcRatingToProduct(product)
|
||||
//TODO: delete rating from bigCommerce
|
||||
})()
|
||||
44
api/hooks/rating/get_read.js
Normal file
44
api/hooks/rating/get_read.js
Normal file
@@ -0,0 +1,44 @@
|
||||
// @ts-check
|
||||
;(function () {
|
||||
/** @type {HookResponse} */
|
||||
let hookResponse
|
||||
let request = context.request()
|
||||
if (request.query("rateIt")) {
|
||||
let orderNumber
|
||||
orderNumber = Number(request.query("orderNumber"))
|
||||
|
||||
if (isNaN(orderNumber))
|
||||
throw {
|
||||
status: 400,
|
||||
message: "Invalid order number.",
|
||||
}
|
||||
|
||||
/*
|
||||
TODO: reprogram to bigcommerce
|
||||
let order = context.db.find("order", {
|
||||
filter: {
|
||||
sequence: orderNumber,
|
||||
},
|
||||
})[0]
|
||||
|
||||
if (!order)
|
||||
throw {
|
||||
status: 400,
|
||||
message: "No entry with this order number.",
|
||||
}
|
||||
|
||||
if (order.deliveryAddress.postcode != request.query("postalcode"))
|
||||
throw {
|
||||
status: 403,
|
||||
message: "Error",
|
||||
}
|
||||
|
||||
hookResponse = {
|
||||
filter: {
|
||||
orderId: order.id,
|
||||
},
|
||||
}*/
|
||||
|
||||
return hookResponse
|
||||
}
|
||||
})()
|
||||
35
api/hooks/rating/post_create.js
Normal file
35
api/hooks/rating/post_create.js
Normal file
@@ -0,0 +1,35 @@
|
||||
/*
|
||||
TODO: reprogram to bigcommerce
|
||||
function productInsideOrder(orderRating) {
|
||||
|
||||
let order = context.db.find("order", {
|
||||
filter: { _id: orderRating.orderId },
|
||||
})[0]
|
||||
|
||||
if (!order) throw { error: "No Order object with given ID.", status: 400 }
|
||||
|
||||
let productsInOrder = order.cart.map((entry) => entry.product.id)
|
||||
if (productsInOrder.length == 0) throw { error: "No products inside the Order.", status: 400 }
|
||||
|
||||
let productInRating = orderRating.productId
|
||||
let productInsideOrder = productsInOrder.includes(productInRating)
|
||||
|
||||
if (!productInsideOrder) throw { error: "Rated products are not inside the Order.", status: 400 }
|
||||
}
|
||||
|
||||
*/
|
||||
;(function () {
|
||||
if (!context?.user?.auth()?.id) {
|
||||
console.log(context?.user?.auth()?.id, "=IDD")
|
||||
//productInsideOrder(context.data)
|
||||
/** @type {ProductRating[]} */ // @ts-ignore
|
||||
let ratings = context.db.find("rating", {
|
||||
filter: {
|
||||
bigCommerceOrderId: context?.data?.orderId,
|
||||
productId: context?.data?.productId,
|
||||
},
|
||||
})
|
||||
|
||||
if (ratings.length) throw { status: 400, error: "Rating already existing" }
|
||||
}
|
||||
})()
|
||||
6
api/hooks/rating/post_return.js
Normal file
6
api/hooks/rating/post_return.js
Normal file
@@ -0,0 +1,6 @@
|
||||
// @ts-check
|
||||
let { sendOperatorRatingMail } = require("../lib/utils")
|
||||
;(function () {
|
||||
//TODO: has to be installed for this project,j ust copied
|
||||
sendOperatorRatingMail()
|
||||
})()
|
||||
4
api/hooks/rating/post_validate.js
Normal file
4
api/hooks/rating/post_validate.js
Normal file
@@ -0,0 +1,4 @@
|
||||
let { validateAndModifyRating } = require("../lib/utils")
|
||||
;(function () {
|
||||
return { data: validateAndModifyRating(context.data) }
|
||||
})()
|
||||
25
api/hooks/rating/put_return.js
Normal file
25
api/hooks/rating/put_return.js
Normal file
@@ -0,0 +1,25 @@
|
||||
let { sendOperatorRatingMail, recalcRatingToProduct } = require("../lib/utils")
|
||||
|
||||
;(function () {
|
||||
/** @type {ProductRating} */
|
||||
let rating = context.data
|
||||
/** @type {LocalProduct} */ // @ts-ignore
|
||||
let product = context.db.find("bigCommerceProducts", {
|
||||
filter: {
|
||||
_id: rating.productId,
|
||||
},
|
||||
})[0]
|
||||
|
||||
if (!product)
|
||||
throw {
|
||||
status: 400,
|
||||
error: "Product not found.",
|
||||
}
|
||||
recalcRatingToProduct(product)
|
||||
|
||||
/**@type {any} */
|
||||
let oldRating = context["oldRating"]
|
||||
if (!oldRating || JSON.stringify(rating.rating) != JSON.stringify(oldRating)) {
|
||||
sendOperatorRatingMail()
|
||||
}
|
||||
})()
|
||||
14
api/hooks/rating/put_update.js
Normal file
14
api/hooks/rating/put_update.js
Normal file
@@ -0,0 +1,14 @@
|
||||
;(function () {
|
||||
if (!context?.user?.auth()?.id) {
|
||||
console.log(context?.user?.auth()?.id)
|
||||
//TODO: productInsideOrder(context.data) look in post
|
||||
}
|
||||
/** @type {ProductRating} */ // @ts-ignore
|
||||
let ratingObj = context.db.find("rating", {
|
||||
filter: {
|
||||
_id: context.data.id,
|
||||
},
|
||||
})[0]
|
||||
|
||||
context["oldRating"] = ratingObj.rating
|
||||
})()
|
||||
4
api/hooks/rating/put_validate.js
Normal file
4
api/hooks/rating/put_validate.js
Normal file
@@ -0,0 +1,4 @@
|
||||
let { validateAndModifyRating } = require("../lib/utils")
|
||||
;(function () {
|
||||
return { data: validateAndModifyRating(context.data) }
|
||||
})()
|
||||
Reference in New Issue
Block a user