From fd1be18b09e8e5141a2a899e786e7caf08b3ff62 Mon Sep 17 00:00:00 2001 From: Tiago Gimenes Date: Fri, 25 Feb 2022 15:33:35 -0300 Subject: [PATCH] fix: Cart item availability (#1160) * validate cart * Update packages/api/src/platforms/vtex/resolvers/validateCart.ts Co-authored-by: Eduardo Formiga Co-authored-by: Eduardo Formiga --- .../api/src/platforms/vtex/clients/fetch.ts | 1 + .../platforms/vtex/resolvers/validateCart.ts | 44 +++++++++---------- 2 files changed, 23 insertions(+), 22 deletions(-) diff --git a/packages/api/src/platforms/vtex/clients/fetch.ts b/packages/api/src/platforms/vtex/clients/fetch.ts index 7020c11dba..458637fed4 100644 --- a/packages/api/src/platforms/vtex/clients/fetch.ts +++ b/packages/api/src/platforms/vtex/clients/fetch.ts @@ -7,6 +7,7 @@ export const fetchAPI = async (info: RequestInfo, init?: RequestInit) => { return response.json() } + console.error(info, init, response) const text = await response.text() throw new Error(text) diff --git a/packages/api/src/platforms/vtex/resolvers/validateCart.ts b/packages/api/src/platforms/vtex/resolvers/validateCart.ts index b654e9532a..56a0a404b8 100644 --- a/packages/api/src/platforms/vtex/resolvers/validateCart.ts +++ b/packages/api/src/platforms/vtex/resolvers/validateCart.ts @@ -1,6 +1,10 @@ import deepEquals from 'fast-deep-equal' -import type { IStoreCart, IStoreOffer } from '../../../__generated__/schema' +import type { + IStoreOrder, + IStoreCart, + IStoreOffer, +} from '../../../__generated__/schema' import type { OrderForm, OrderFormItem, @@ -47,23 +51,22 @@ const groupById = (offers: IStoreOffer[]): Map => return acc }, new Map()) -const equals = (of1: OrderForm, of2: OrderForm) => { - const pick = ({ orderFormId, messages, items, salesChannel }: OrderForm) => ({ - orderFormId, - messages, - salesChannel, - items: items.map( - ({ uniqueId, quantity, seller, sellingPrice, availability }) => ({ - uniqueId, - quantity, - seller, - sellingPrice, - availability, - }) - ), +const equals = (storeOrder: IStoreOrder, orderForm: OrderForm) => { + const pick = (item: Indexed, index: number) => ({ + ...item, + itemOffered: { + sku: item.itemOffered.sku, + }, + index, }) - return deepEquals(pick(of1), pick(of2)) + const orderFormItems = orderForm.items.map(orderFormItemToOffer).map(pick) + const storeOrderItems = storeOrder.acceptedOffer.map(pick) + + const isSameOrder = storeOrder.orderNumber === orderForm.orderFormId + const orderItensAreSync = deepEquals(orderFormItems, storeOrderItems) + + return isSameOrder && orderItensAreSync } /** @@ -81,13 +84,10 @@ const equals = (of1: OrderForm, of2: OrderForm) => { */ export const validateCart = async ( _: unknown, - { - cart: { - order: { orderNumber, acceptedOffer }, - }, - }: { cart: IStoreCart }, + { cart: { order } }: { cart: IStoreCart }, ctx: Context ) => { + const { orderNumber, acceptedOffer } = order const { clients: { commerce }, loaders: { skuLoader }, @@ -145,7 +145,7 @@ export const validateCart = async ( }) // Step5: If no changes detected before/after updating orderForm, the order is validated - if (equals(orderForm, updatedOrderForm)) { + if (equals(order, updatedOrderForm)) { return null }