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