Skip to content

Commit

Permalink
fix validateCart logic
Browse files Browse the repository at this point in the history
  • Loading branch information
tlgimenes committed May 30, 2022
1 parent cb2f0b1 commit 00fef03
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 6 deletions.
2 changes: 2 additions & 0 deletions packages/api/src/__generated__/schema.ts

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 10 additions & 6 deletions packages/api/src/platforms/vtex/resolvers/validateCart.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import type {
IStoreCart,
IStoreOffer,
IStoreOrder,
IStorePropertyValue,
} from '../../../__generated__/schema'
import type {
OrderForm,
Expand All @@ -20,17 +21,18 @@ import {

type Indexed<T> = T & { index?: number }

const getAttachments = (item: IStoreOffer) =>
item.itemOffered.additionalProperty?.filter(
(i) => i.valueReference === VALUE_REFERENCES.attachment
)
const isAttachment = (value: IStorePropertyValue) =>
value.valueReference === VALUE_REFERENCES.attachment

const getId = (item: IStoreOffer) =>
[
item.itemOffered.sku,
item.seller.identifier,
item.price,
item.itemOffered.additionalProperty?.map(getPropertyId).join('-'),
item.itemOffered.additionalProperty
?.filter(isAttachment)
.map(getPropertyId)
.join('-'),
]
.filter(Boolean)
.join('::')
Expand Down Expand Up @@ -59,7 +61,9 @@ const offerToOrderItemInput = (
seller: offer.seller.identifier,
id: offer.itemOffered.sku,
index: offer.index,
attachments: (getAttachments(offer) ?? []).map((attachment) => ({
attachments: (
offer.itemOffered.additionalProperty?.filter(isAttachment) ?? []
).map((attachment) => ({
name: attachment.name,
content: attachment.value,
})),
Expand Down

0 comments on commit 00fef03

Please sign in to comment.