Skip to content

Commit

Permalink
fix: Also consider shouldSplitItem from the mutation response
Browse files Browse the repository at this point in the history
  • Loading branch information
lucasfp13 committed Oct 25, 2024
1 parent 00b6f32 commit e0da991
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 9 deletions.
28 changes: 19 additions & 9 deletions packages/api/src/platforms/vtex/resolvers/validateCart.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,19 +110,24 @@ const equals = (storeOrder: IStoreOrder, orderForm: OrderForm) => {
}

function hasChildItem(items: OrderFormItem[], itemId: string) {
return items?.some(item => item.parentItemIndex && items[item.parentItemIndex].id === itemId)
return items?.some(
(item) => item.parentItemIndex && items[item.parentItemIndex].id === itemId
)
}

function hasParentItem(items: OrderFormItem[], itemId: string) {
return items?.some(item => item.id === itemId && item.parentItemIndex !== null)
return items?.some(
(item) => item.id === itemId && item.parentItemIndex !== null
)
}

const joinItems = (form: OrderForm) => {
const itemsById = form.items.reduce(
(acc, item, idx) => {
const id = hasParentItem(form.items, item.id) || hasChildItem(form.items, item.id) ?
`${getId(orderFormItemToOffer(item))}::${idx}` :
getId(orderFormItemToOffer(item))
const id =
hasParentItem(form.items, item.id) || hasChildItem(form.items, item.id)
? `${getId(orderFormItemToOffer(item))}::${idx}`
: getId(orderFormItemToOffer(item))

if (!acc[id]) {
acc[id] = []
Expand Down Expand Up @@ -159,7 +164,8 @@ const joinItems = (form: OrderForm) => {

const orderFormToCart = async (
form: OrderForm,
skuLoader: Context['loaders']['skuLoader']
skuLoader: Context['loaders']['skuLoader'],
shouldSplitItem?: boolean | null
) => {
return {
order: {
Expand All @@ -168,6 +174,7 @@ const orderFormToCart = async (
...item,
product: await skuLoader.load(`${item.id}-invisibleItems`),
})),
shouldSplitItem,
},
messages: form.messages.map(({ text, status }) => ({
text,
Expand Down Expand Up @@ -363,7 +370,7 @@ export const validateCart = async (
joinItems
)
if (orderNumber) {
return orderFormToCart(newOrderForm, skuLoader)
return orderFormToCart(newOrderForm, skuLoader, order.shouldSplitItem)
}
}

Expand All @@ -388,7 +395,10 @@ export const validateCart = async (
// Update existing items
const [head, ...tail] = maybeOriginItem

if(hasParentItem(orderForm.items, head.itemOffered.sku) || hasChildItem(orderForm.items, head.itemOffered.sku)) {
if (
hasParentItem(orderForm.items, head.itemOffered.sku) ||
hasChildItem(orderForm.items, head.itemOffered.sku)
) {
acc.itemsToUpdate.push(head)

return acc
Expand Down Expand Up @@ -449,5 +459,5 @@ export const validateCart = async (
}

// Step6: There were changes, convert orderForm to StoreCart
return orderFormToCart(updatedOrderForm, skuLoader)
return orderFormToCart(updatedOrderForm, skuLoader, order.shouldSplitItem)
}
1 change: 1 addition & 0 deletions packages/core/src/sdk/cart/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ export const ValidateCartMutation = gql(`
acceptedOffer {
...CartItem
}
shouldSplitItem
}
messages {
...CartMessage
Expand Down

0 comments on commit e0da991

Please sign in to comment.