Initial commit
This commit is contained in:
23
api/hooks/product/get_read.js
Normal file
23
api/hooks/product/get_read.js
Normal file
@@ -0,0 +1,23 @@
|
||||
const { attachRatingObjsToProduct } = require("./helper")
|
||||
;(function () {
|
||||
if (context.user.auth()) return
|
||||
const bigCommerceProductId = context.request().query("bigCommerceProductId")
|
||||
if (bigCommerceProductId) {
|
||||
/**@type {LocalProduct[]} */
|
||||
//@ts-ignore
|
||||
let products = context.db.find("bigCommerceProduct", {
|
||||
filter: {
|
||||
bigCommerceId: Number(bigCommerceProductId),
|
||||
},
|
||||
})
|
||||
products = attachRatingObjsToProduct(products)
|
||||
|
||||
if (products.length > 0) {
|
||||
throw {
|
||||
status: 200,
|
||||
data: products[0],
|
||||
}
|
||||
}
|
||||
}
|
||||
return
|
||||
})()
|
||||
12
api/hooks/product/get_return.js
Normal file
12
api/hooks/product/get_return.js
Normal file
@@ -0,0 +1,12 @@
|
||||
const { attachRatingObjsToProduct } = require("./helper")
|
||||
;(function () {
|
||||
if (context.user.auth()) return
|
||||
/**@type {LocalProduct[]} */
|
||||
//@ts-ignore
|
||||
let products = context.results()
|
||||
products = attachRatingObjsToProduct(products)
|
||||
let hookResponse = {
|
||||
results: products,
|
||||
}
|
||||
return hookResponse
|
||||
})()
|
||||
26
api/hooks/product/helper.js
Normal file
26
api/hooks/product/helper.js
Normal file
@@ -0,0 +1,26 @@
|
||||
/**
|
||||
* @param {LocalProduct[]} products
|
||||
* @returns {LocalProduct[]}
|
||||
*/
|
||||
function attachRatingObjsToProduct(products) {
|
||||
let productIds = products.map((product) => product.bigCommerceId)
|
||||
/**@type {ProductRating[]} */
|
||||
//@ts-ignore
|
||||
let allRatings = context.db.find("rating", {
|
||||
filter: {
|
||||
status: "approved",
|
||||
bigCommerceProductId: { $in: productIds },
|
||||
},
|
||||
})
|
||||
|
||||
products.forEach((product, i) => {
|
||||
let ratings = allRatings.filter((rating) => rating.bigCommerceProductId === product.bigCommerceId).reverse()
|
||||
products[i].ratings = ratings
|
||||
})
|
||||
|
||||
return products
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
attachRatingObjsToProduct,
|
||||
}
|
||||
36
api/hooks/product/post_create.js
Normal file
36
api/hooks/product/post_create.js
Normal file
@@ -0,0 +1,36 @@
|
||||
const { getAllProducts, getProductImages } = require("../lib/bigcommerceRestAPI")
|
||||
const { extractSizingChart } = require("../lib/printfulRestAPI")
|
||||
;(function () {
|
||||
const products = getAllProducts()
|
||||
const productIds = products.map((p) => p.id)
|
||||
const currentProducts = context.db.find("bigCommerceProduct", { filter: { bigCommerceId: { $in: productIds } } })
|
||||
const notFoundProductIds = productIds.filter((id) => !currentProducts.some((p) => p.bigCommerceId == id))
|
||||
|
||||
const newProducts = products.filter((p) => notFoundProductIds.includes(p.id))
|
||||
newProducts.forEach((p) => {
|
||||
const productImage = getProductImages(String(p.id))
|
||||
context.db.create("bigCommerceProduct", {
|
||||
forcedWarning: "",
|
||||
productName: p.name_customer || p.name,
|
||||
previewImage: productImage?.[0]?.url_thumbnail,
|
||||
bigCommerceId: p?.id,
|
||||
sizingChart: null,
|
||||
})
|
||||
})
|
||||
|
||||
currentProducts.forEach((p) => {
|
||||
context.db.update("bigCommerceProduct", p.id, {
|
||||
...p,
|
||||
productName:
|
||||
products.find((bp) => bp.id == p.bigCommerceId).name_customer ||
|
||||
products.find((bp) => bp.id == p.bigCommerceId).name,
|
||||
bigCommerceSKU: products.find((bp) => bp.id == p.bigCommerceId).sku,
|
||||
bigCommerceId: products.find((bp) => bp.id == p.bigCommerceId).id,
|
||||
sizingChart: extractSizingChart(p.printfulProductId, p.id),
|
||||
})
|
||||
})
|
||||
throw {
|
||||
status: 200,
|
||||
message: "Products created or updated successfully",
|
||||
}
|
||||
})()
|
||||
13
api/hooks/product/put_update.js
Normal file
13
api/hooks/product/put_update.js
Normal file
@@ -0,0 +1,13 @@
|
||||
const { extractSizingChart } = require("../lib/printfulRestAPI")
|
||||
let { clearSSRCache } = require("../lib/utils")
|
||||
|
||||
;(function () {
|
||||
clearSSRCache()
|
||||
if (context.data.printfulProductId) {
|
||||
const sizingChart = extractSizingChart(context.data.printfulProductId, context.data.id)
|
||||
if (sizingChart) {
|
||||
context.data.sizingChart = sizingChart
|
||||
return { data: context.data }
|
||||
}
|
||||
}
|
||||
})()
|
||||
Reference in New Issue
Block a user