Skip to content

Instantly share code, notes, and snippets.

@gje4
Created September 25, 2023 19:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gje4/0c1f6269532a10d5fe5709683176fec2 to your computer and use it in GitHub Desktop.
Save gje4/0c1f6269532a10d5fe5709683176fec2 to your computer and use it in GitHub Desktop.
Bloomreach
'use strict';
const BigCommerce = require("node-bigcommerce");
const request = require("request-promise");
const { BLOOMREACH_ACCOUNT_ID, BLOOMREACH_AUTH_KEY, BLOOMREACH_DOMAIN_KEY } = process.env;
async function getProductData(productId) {
const options = {
method: "GET",
uri: `https://api.bigcommerce.com/stores/${process.env.STORE_HASH}/v3/catalog/products/${productId}?include=primary_image`,
headers: {
accept: "application/json",
"X-Auth-Client": process.env.BC_CLIENT,
"X-Auth-Token": process.env.BC_TOKEN
}
};
return await request(options);
}
async function sendProductBloomreach(product) {
const bloomBody =
[{
"op": "add",
"path": `/products/${product.id}`,
"value": {
"attributes": {
"title": product?.name,
"price": product?.price,
"description": product?.description,
"url": product?.custom_url?.url,
"availability": true,
"thumb_image": product?.primary_image?.url_standard
}
}
}]
const options = {
method: "PUT",
uri: `https://api.connect.bloomreach.com/dataconnect/api/v1/accounts/${BLOOMREACH_ACCOUNT_ID}/catalogs/trialspace_sales01/products`,
headers: {
accept: "application/json-patch+json",
"Content-Type": "application/json-patch+json",
"Authorization": BLOOMREACH_AUTH_KEY
},
body: JSON.stringify(bloomBody)
};
return await request(options)
}
async function publishIndex() {
const options = {
method: "POST",
uri: `https://api.connect.bloomreach.com/dataconnect/api/v1/accounts/${BLOOMREACH_ACCOUNT_ID}/catalogs/trialspace_sales01/indexes`,
headers: {
accept: "application/json",
"Authorization": BLOOMREACH_AUTH_KEY
}
};
return await request(options);
}
module.exports.bcProducts = async event => {
try {
if (trigger === "deleted") {
//Delete Product in BloomReach
} else {
const productDataRaw = await getProductData(catalogData.data.id);
const product = JSON.parse(productDataRaw);
//update/add
if (trigger === "updated") {
//Update product in BloomReach
const bloomReachCreate = await sendProductBloomreach(product?.data)
} else if (trigger === "created") {
//Create product in BloomReach
const bloomReachCreate = await sendProductBloomreach( product?.data)
}
}
const indexed = await publishIndex()
return {
statusCode: 200,
headers: {
"Content-Type": "application/json"
},
body: JSON.stringify("Product Sent To BloomReach")
};
}
catch (error) {
console.log("error", error);
return {
statusCode: 200,
headers: {
"Content-Type": "application/json"
},
body: JSON.stringify("Issue Adding product", error)
};
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment