general setup
This commit is contained in:
@@ -101,8 +101,8 @@
|
||||
else if (oldVal !== newVal) {
|
||||
updateLogs.push({
|
||||
field: newPath,
|
||||
previous: oldVal.toString(),
|
||||
current: newVal.toString(),
|
||||
previous: oldVal?.toString(),
|
||||
current: newVal?.toString(),
|
||||
})
|
||||
}
|
||||
})
|
||||
|
||||
@@ -8,4 +8,7 @@ if (release && typeof context !== "undefined") {
|
||||
module.exports = {
|
||||
release,
|
||||
apiClientBaseURL,
|
||||
frontendBase: "https://bkdf.code.testversion.online/",
|
||||
tibiUrl: "https://bkdf-tibiadmin.code.testversion.online/",
|
||||
bkdfApiUrl: "https://bkdf.code.testversion.online/api/",
|
||||
}
|
||||
|
||||
@@ -1,32 +1,92 @@
|
||||
const apiSsrBaseURL = "http://localhost:8080/api/v1/_/allkids_erfurt"
|
||||
|
||||
const apiSsrBaseURL = "http://localhost:8080/api/v1/_/tibi_starter"
|
||||
const { frontendBase, tibiUrl, bkdfApiUrl } = require("./config-client")
|
||||
module.exports = {
|
||||
operatorEmail: "binkrassdufass.clothing@gmail.com",
|
||||
operatorName: "BinKrassDuFass",
|
||||
contactEmail: "binkrassdufass.clothing@gmail.com",
|
||||
|
||||
frontendBase,
|
||||
apiBase: frontendBase + "/api/",
|
||||
tibiUrl,
|
||||
apiSsrBaseURL,
|
||||
ssrValidatePath: function (path) {
|
||||
// validate if path ssr rendering is ok, -1 = NOTFOUND, 0 = NO SSR, 1 = SSR
|
||||
// pe. use context.readCollection("product", {filter: {path: path}}) ... to validate dynamic urls
|
||||
|
||||
// // / is de home
|
||||
// if (path == "/") return 1
|
||||
|
||||
// // all other sites are in db
|
||||
//path = path?.replace(/^\//, "")
|
||||
console.log("PATH:", path)
|
||||
const resp = context.db.find("content", {
|
||||
filter: {
|
||||
$and: [{ path }],
|
||||
},
|
||||
|
||||
selector: { _id: 1 },
|
||||
})
|
||||
console.log("RESP:", resp?.length)
|
||||
if (resp && resp.length) {
|
||||
ssrValidatePath: function (/** @type {string} */ url) {
|
||||
//TODO: ANPASSEN!
|
||||
// -1 = NOTFOUND, 0 = NOSSR, 1 = SSR, "string" = PATH_FOR_CACHE
|
||||
if (url == "/") {
|
||||
return 1
|
||||
}
|
||||
|
||||
// not found
|
||||
return -1
|
||||
if (url.match(/^\/(checkout|order|search)/)) {
|
||||
// no ssr
|
||||
return 0
|
||||
}
|
||||
|
||||
if (url.match(/^\/service(\/.*)?$/)) {
|
||||
// static content
|
||||
return 1
|
||||
}
|
||||
|
||||
const categoryPath = url.replace(/\/[^_\/]+_[^\/]+$/, "") // also in app query
|
||||
const cat = context.db.find("category", {
|
||||
filter: {
|
||||
$or: [
|
||||
{ "path.de": categoryPath },
|
||||
{ "path.en": categoryPath },
|
||||
{ "path.fr": categoryPath },
|
||||
{ "path.se": categoryPath },
|
||||
{ "path.dk": categoryPath },
|
||||
],
|
||||
},
|
||||
selector: {
|
||||
insertTime: 1,
|
||||
},
|
||||
})
|
||||
|
||||
if (cat && cat.length) {
|
||||
// in category
|
||||
const matches = url.match(/^\/([^\/]+\/)*([^_\/]+)_/)
|
||||
if (matches && matches.length > 2) {
|
||||
// product url
|
||||
const prod = context.db.find("product", {
|
||||
filter: {
|
||||
code: matches[2],
|
||||
},
|
||||
selector: {
|
||||
insertTime: 1,
|
||||
},
|
||||
})
|
||||
|
||||
if (prod && prod.length) {
|
||||
// force one url for product code in cache
|
||||
return categoryPath + "/" + matches[2] + "_ssr-product"
|
||||
}
|
||||
|
||||
// product not found
|
||||
return -1
|
||||
}
|
||||
|
||||
// try to render category
|
||||
return 1
|
||||
}
|
||||
|
||||
// search for other content sites
|
||||
const c = context.db.find("content", {
|
||||
filter: {
|
||||
path: url,
|
||||
},
|
||||
selector: {
|
||||
insertTime: 1,
|
||||
},
|
||||
})
|
||||
if (c && c.length) {
|
||||
// found
|
||||
return 1
|
||||
} else {
|
||||
// not found
|
||||
return -1
|
||||
}
|
||||
},
|
||||
|
||||
ssrPublishCheckCollections: ["content"],
|
||||
LIGHTHOUSE_TOKEN: "AIzaSyC0UxHp3-MpJiDL3ws7pEV6lj57bfIc7GQ",
|
||||
}
|
||||
|
||||
@@ -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,
|
||||
}
|
||||
|
||||
5
api/hooks/products/post_validate.js
Normal file
5
api/hooks/products/post_validate.js
Normal file
@@ -0,0 +1,5 @@
|
||||
;(function () {
|
||||
// fetch all products from bigCommerce and save/update them in the database
|
||||
// pay attention to the avg. product rating, should also be inserted into BigCommerce with every update,
|
||||
// so the product requests dont have to be repeated -> Perfomance!
|
||||
})()
|
||||
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) }
|
||||
})()
|
||||
@@ -21,7 +21,7 @@ const { ssrRequest } = require("../lib/ssr-server.js")
|
||||
|
||||
if (url) {
|
||||
// comment will be printed to html later
|
||||
let comment = ""
|
||||
let comment = "yml"
|
||||
/** @type {Date} */ // @ts-ignore
|
||||
context.ssrCacheValidUntil = null
|
||||
|
||||
|
||||
Reference in New Issue
Block a user