general setup
This commit is contained in:
@@ -9,6 +9,7 @@ function log(str) {
|
||||
/**
|
||||
* convert object to string
|
||||
* @param {any} obj object
|
||||
* @returns {Object | undefined}
|
||||
*/
|
||||
function obj2str(obj) {
|
||||
if (Array.isArray(obj)) {
|
||||
@@ -47,15 +48,22 @@ function clearSSRCache() {
|
||||
context.response.header("X-SSR-Cleared", info.removed)
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {{ [x: string]: any; }[]} dbObjs
|
||||
*/
|
||||
function calculateAverageDynamically(dbObjs) {
|
||||
const sumObj = {}
|
||||
let count = 0
|
||||
|
||||
dbObjs.forEach((obj) => {
|
||||
dbObjs.forEach((/** @type {{ [x: string]: any; }} */ obj) => {
|
||||
accumulate(obj, sumObj)
|
||||
count++
|
||||
})
|
||||
|
||||
/**
|
||||
* @param {{ [x: string]: any; }} sourceObj
|
||||
* @param {{ [x: string]: any; }} targetObj
|
||||
*/
|
||||
function accumulate(sourceObj, targetObj) {
|
||||
for (const key in sourceObj) {
|
||||
if (typeof sourceObj[key] === "number") {
|
||||
@@ -67,6 +75,9 @@ function calculateAverageDynamically(dbObjs) {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {{ [x: string]: any; }} targetObj
|
||||
*/
|
||||
function average(targetObj) {
|
||||
for (const key in targetObj) {
|
||||
if (typeof targetObj[key] === "number") {
|
||||
@@ -81,6 +92,9 @@ function calculateAverageDynamically(dbObjs) {
|
||||
return sumObj
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {string} url
|
||||
*/
|
||||
function run(url) {
|
||||
const response = context.http
|
||||
.fetch(url, {
|
||||
@@ -133,12 +147,103 @@ function setUpQuery(subPath = "/") {
|
||||
|
||||
let query = `${api}?`
|
||||
for (let key in parameters) {
|
||||
// @ts-ignore
|
||||
query += `${key}=${parameters[key]}&`
|
||||
}
|
||||
query += params // Append other parameters without URL encoding
|
||||
return query
|
||||
}
|
||||
|
||||
/**@param {LocalProduct} product */
|
||||
function recalcRatingToProduct(product) {
|
||||
let ratings = context.db.find("rating", {
|
||||
filter: {
|
||||
productId: product.id,
|
||||
status: "approved",
|
||||
},
|
||||
})
|
||||
|
||||
let ratingSum = ratings.map((e) => e.rating).reduce((accumulator, currentValue) => accumulator + currentValue, 0)
|
||||
let ratingAverage = (ratingSum || 0) / (ratings.length || 1)
|
||||
if (isNaN(ratingAverage)) return
|
||||
context.db.update("bigCommerceProducts", product.id, {
|
||||
amountOfRatings: ratings.length,
|
||||
averageRating: ratingAverage,
|
||||
})
|
||||
|
||||
// clear ssr cache because of product update
|
||||
clearSSRCache()
|
||||
}
|
||||
/**
|
||||
*
|
||||
* @param {HookContext} c
|
||||
* @param {string} filename
|
||||
* @param {string} locale
|
||||
* @returns {string}
|
||||
*/
|
||||
function tpl(c, filename, locale) {
|
||||
return c.tpl.execute(c.fs.readFile(filename), {
|
||||
context: c,
|
||||
config: config,
|
||||
})
|
||||
}
|
||||
var config = require("../config")
|
||||
function sendOperatorRatingMail() {
|
||||
if (!context.data) context.data = {}
|
||||
let locale = context.request().query("locale")
|
||||
locale = locale ? locale : "de-DE"
|
||||
let order = context.db.find("order", {
|
||||
filter: {
|
||||
_id: context.data.order,
|
||||
},
|
||||
})[0]
|
||||
context.data.order = order
|
||||
context.data.tibiLink = `project/${context.project().id}/collection/rating/edit/${context.data.id}` //projekt ID
|
||||
context.smtp.sendMail({
|
||||
to: config.operatorEmail,
|
||||
from: config.operatorEmail,
|
||||
fromName: config.operatorName,
|
||||
subject: tpl(context, "templates/operator_rating_subject.de-DE.txt", locale),
|
||||
html: tpl(context, "templates/operator_rating_body.de-DE.html", locale),
|
||||
})
|
||||
}
|
||||
|
||||
/**@param {ProductRating} currentRating */
|
||||
function validateAndModifyRating(currentRating) {
|
||||
// Fetch the rating object from the database
|
||||
|
||||
/** @type {ProductRating} */ // @ts-ignore
|
||||
let oldRating = context.db.find("rating", {
|
||||
filter: { orderId: currentRating.orderId, productId: currentRating.productId },
|
||||
})[0]
|
||||
|
||||
if (oldRating && (oldRating.status != "pending" || currentRating.status != "pending")) {
|
||||
//@ts-ignore
|
||||
oldRating.review_date = convertDateToUTCISO(oldRating.review_date)
|
||||
if (oldRating.status == "pending") {
|
||||
oldRating.status = currentRating.status
|
||||
if (currentRating.status == "approved") return currentRating
|
||||
}
|
||||
|
||||
return context?.user?.auth()?.id &&
|
||||
(currentRating?.status == "pending" || currentRating?.status == "rejected") &&
|
||||
(oldRating?.status == "approved" || oldRating?.status == "rejected")
|
||||
? currentRating
|
||||
: oldRating
|
||||
}
|
||||
|
||||
// If the status of the current rating is 'pending', or not provided
|
||||
if (currentRating.status == "pending" || !currentRating.status) {
|
||||
currentRating.status = "pending"
|
||||
if (!currentRating.review_date) {
|
||||
//@ts-ignore
|
||||
currentRating.review_date = new Date().toISOString()
|
||||
}
|
||||
}
|
||||
|
||||
return currentRating
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
log,
|
||||
clearSSRCache,
|
||||
@@ -146,4 +251,7 @@ module.exports = {
|
||||
run,
|
||||
setUpQuery,
|
||||
calculateAverageDynamically,
|
||||
recalcRatingToProduct,
|
||||
sendOperatorRatingMail,
|
||||
validateAndModifyRating,
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user