Skip to content

Commit

Permalink
fix: Cart item availability (#1160)
Browse files Browse the repository at this point in the history
* validate cart

* Update packages/api/src/platforms/vtex/resolvers/validateCart.ts

Co-authored-by: Eduardo Formiga <[email protected]>

Co-authored-by: Eduardo Formiga <[email protected]>
  • Loading branch information
tlgimenes and eduardoformiga authored Feb 25, 2022
1 parent 085a7f5 commit fd1be18
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 22 deletions.
1 change: 1 addition & 0 deletions packages/api/src/platforms/vtex/clients/fetch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
44 changes: 22 additions & 22 deletions packages/api/src/platforms/vtex/resolvers/validateCart.ts
Original file line number Diff line number Diff line change
@@ -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,
Expand Down Expand Up @@ -47,23 +51,22 @@ const groupById = (offers: IStoreOffer[]): Map<string, IStoreOffer> =>
return acc
}, new Map<string, IStoreOffer>())

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<IStoreOffer>, 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
}

/**
Expand All @@ -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 },
Expand Down Expand Up @@ -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
}

Expand Down

0 comments on commit fd1be18

Please sign in to comment.