Initial commit
This commit is contained in:
46
api/hooks/cart/get_read.js
Normal file
46
api/hooks/cart/get_read.js
Normal file
@@ -0,0 +1,46 @@
|
||||
const { getCart, getRedirectUrl } = require("../lib/bigcommerceRestAPI")
|
||||
const { bigcommerceStoreHash, bigcommerceBaseURL, bigCommerceCliendId, bigCommerceClientSecret } = require("../config")
|
||||
const { getCustomerById, getLoginUrl } = require("../lib/bigcommerceRestAPI")
|
||||
const { withAccount } = require("../lib/utils")
|
||||
const { mapRestApiCartToGraphQL } = require("./helper")
|
||||
;(function () {
|
||||
if (context.user.auth()) return {}
|
||||
const checkout = context.request().query("checkout")
|
||||
const loggedInCheckout = context.request().query("loggedInCheckout")
|
||||
if (checkout) {
|
||||
if (loggedInCheckout) {
|
||||
withAccount((loginClaims) => {
|
||||
const cartId = context.request().param("id")
|
||||
const cart = getCart(cartId)
|
||||
const { checkoutURL } = getRedirectUrl(cartId)
|
||||
const loginUrl = getLoginUrl(
|
||||
loginClaims.bigCommerceId,
|
||||
bigcommerceStoreHash,
|
||||
bigcommerceBaseURL,
|
||||
bigCommerceCliendId,
|
||||
bigCommerceClientSecret,
|
||||
checkoutURL.split(bigcommerceBaseURL).pop()
|
||||
)
|
||||
throw {
|
||||
status: 200,
|
||||
data: cart && {
|
||||
cart: mapRestApiCartToGraphQL(cart),
|
||||
checkoutURL: loginUrl,
|
||||
},
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
const cartId = context.request().param("id")
|
||||
const cart = getCart(cartId)
|
||||
const { checkoutURL, embeddedCheckoutURL } = getRedirectUrl(cartId)
|
||||
|
||||
throw {
|
||||
status: 200,
|
||||
data: cart && {
|
||||
cart: mapRestApiCartToGraphQL(cart),
|
||||
checkoutURL: checkoutURL,
|
||||
embeddedCheckoutURL: embeddedCheckoutURL,
|
||||
},
|
||||
}
|
||||
})()
|
||||
53
api/hooks/cart/helper.js
Normal file
53
api/hooks/cart/helper.js
Normal file
@@ -0,0 +1,53 @@
|
||||
/**
|
||||
*
|
||||
* @param {RestApiCart} cart
|
||||
* @returns {BigCommerceCart}
|
||||
*/
|
||||
|
||||
function mapRestApiCartToGraphQL(cart) {
|
||||
return {
|
||||
entityId: cart.id,
|
||||
currencyCode: cart.currency.code,
|
||||
isTaxIncluded: cart.tax_included,
|
||||
baseAmount: { value: cart.base_amount, currencyCode: cart.currency.code },
|
||||
discountedAmount: { value: cart.discount_amount, currencyCode: cart.currency.code },
|
||||
amount: { value: cart.cart_amount, currencyCode: cart.currency.code },
|
||||
discounts: cart.discounts.map((d) => ({
|
||||
entityId: d.id,
|
||||
discountedAmount: { value: d.discounted_amount, currencyCode: cart.currency.code },
|
||||
})),
|
||||
lineItems: {
|
||||
physicalItems: cart.line_items.physical_items.map((item) => ({
|
||||
entityId: item.id,
|
||||
parentEntityId: cart.parent_id,
|
||||
productEntityId: item.product_id,
|
||||
variantEntityId: item.variant_id,
|
||||
sku: item.sku,
|
||||
name: item.name,
|
||||
url: item.url,
|
||||
imageUrl: item.image_url,
|
||||
brand: "BKDF", // REST API does not provide brand directly
|
||||
quantity: item.quantity,
|
||||
isTaxable: item.is_taxable,
|
||||
listPrice: { value: item.list_price, currencyCode: cart.currency.code },
|
||||
extendedListPrice: { value: item.extended_list_price, currencyCode: cart.currency.code },
|
||||
selectedOptions: item?.options?.map((option) => ({
|
||||
// Map each option to the GraphQL format
|
||||
// Assuming a simplified structure here for the example
|
||||
})),
|
||||
isShippingRequired: true, // Example assumption
|
||||
})),
|
||||
digitalItems: [],
|
||||
customItems: [],
|
||||
giftCertificates: [],
|
||||
totalQuantity: cart.line_items.physical_items.reduce((total, item) => total + item.quantity, 0),
|
||||
},
|
||||
createdAt: { utc: new Date(cart.created_time) },
|
||||
updatedAt: { utc: new Date(cart.updated_time) },
|
||||
locale: cart.locale,
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
mapRestApiCartToGraphQL,
|
||||
}
|
||||
25
api/hooks/cart/post_validate.js
Normal file
25
api/hooks/cart/post_validate.js
Normal file
@@ -0,0 +1,25 @@
|
||||
const { addCartItem, updateCartItem, deleteCartItem, getCart } = require("../lib/bigcommerceRestAPI")
|
||||
const { postAddToCart } = require("../lib/facebookRestAPI")
|
||||
|
||||
;(function () {
|
||||
const operation = context?.data?.operation
|
||||
|
||||
if (operation == "add") {
|
||||
addCartItem(context?.data?.cartId, context?.data?.lineItems)
|
||||
try {
|
||||
postAddToCart()
|
||||
} catch (e) {}
|
||||
} else if (operation == "update") {
|
||||
updateCartItem(context?.data?.cartId, context?.data?.lineItem, context?.data?.entityId)
|
||||
try {
|
||||
postAddToCart()
|
||||
} catch (e) {}
|
||||
} else if (operation == "delete") {
|
||||
deleteCartItem(context?.data?.cartId, context?.data?.entityId)
|
||||
}
|
||||
|
||||
throw {
|
||||
status: 200,
|
||||
message: "success",
|
||||
}
|
||||
})()
|
||||
Reference in New Issue
Block a user