From 6a719cbf5c45800f2e5fc4b0f135e16303dd86fc Mon Sep 17 00:00:00 2001 From: Emanuel Ramos <78380713+Emanuel-Ramos@users.noreply.github.com> Date: Thu, 24 Oct 2024 13:15:47 -0300 Subject: [PATCH] fix: has child items logic on validate cart --- .../platforms/vtex/resolvers/validateCart.ts | 23 ++++++++++++++----- 1 file changed, 17 insertions(+), 6 deletions(-) diff --git a/packages/api/src/platforms/vtex/resolvers/validateCart.ts b/packages/api/src/platforms/vtex/resolvers/validateCart.ts index 4b5a96d0c6..6198e42dc8 100644 --- a/packages/api/src/platforms/vtex/resolvers/validateCart.ts +++ b/packages/api/src/platforms/vtex/resolvers/validateCart.ts @@ -110,19 +110,27 @@ 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 !== null && + item.parentItemIndex !== undefined && + 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] = [] @@ -388,7 +396,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