Initial commit
This commit is contained in:
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",
|
||||
}
|
||||
})()
|
||||
Reference in New Issue
Block a user