Skip to content

Commit

Permalink
feat: validate cart with attachments (#1301)
Browse files Browse the repository at this point in the history
* feat: validate cart with attachments

* feat: improve get id

* Add attachment type

Co-authored-by: Ícaro Azevedo <icazevedo10@gmail.com>
  • Loading branch information
emersonlaurentino and icazevedo authored May 17, 2022
1 parent 2626da7 commit 495675a
Show file tree
Hide file tree
Showing 5 changed files with 84 additions and 1 deletion.
22 changes: 22 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.

Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,12 @@ export interface OrderFormInputItem {
quantity: number
seller: string
index?: number
attachments?: Attachment[]
}

export interface Attachment {
name: string
content: Record<string, string>
}

export interface OrderFormItem {
Expand Down
33 changes: 32 additions & 1 deletion packages/api/src/platforms/vtex/resolvers/validateCart.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,38 @@ import type {
OrderFormItem,
} from '../clients/commerce/types/OrderForm'
import type { Context } from '..'
import { VALUE_REFERENCES } from '../utils/propertyValue'

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

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

const serializeAttachment = (item: IStoreOffer) => {
const attachments = getAttachments(item)

if (attachments?.length === 0) {
return null
}

return attachments
?.map(
(attachment) => `${attachment.name}:${JSON.stringify(attachment.value)}`
)
.join('-')
}

const getId = (item: IStoreOffer) =>
[item.itemOffered.sku, item.seller.identifier, item.price].join('::')
[
item.itemOffered.sku,
item.seller.identifier,
item.price,
serializeAttachment(item),
]
.filter(Boolean)
.join('::')

const orderFormItemToOffer = (
item: OrderFormItem,
Expand All @@ -41,6 +68,10 @@ const offerToOrderItemInput = (
seller: offer.seller.identifier,
id: offer.itemOffered.sku,
index: offer.index,
attachments: getAttachments(offer)?.map((attachment) => ({
name: attachment.name,
content: attachment.value,
})),
})

const groupById = (offers: IStoreOffer[]): Map<string, IStoreOffer> =>
Expand Down
4 changes: 4 additions & 0 deletions packages/api/src/typeDefs/product.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -80,4 +80,8 @@ input IStoreProduct {
Array of product images.
"""
image: [IStoreImage!]!
"""
Custom Product Additional Properties.
"""
additionalProperty: [IStorePropertyValue!]
}
20 changes: 20 additions & 0 deletions packages/api/src/typeDefs/propertyValue.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ type StorePropertyValue {
Property name.
"""
name: String!
"""
Property value reference.
"""
valueReference: ValueReference!
}

Expand All @@ -18,3 +21,20 @@ enum ValueReference {
ATTACHMENT
PROPERTY
}

scalar Object

input IStorePropertyValue {
"""
Property value.
"""
value: Object!
"""
Property name.
"""
name: String!
"""
Property value reference.
"""
valueReference: ValueReference!
}

0 comments on commit 495675a

Please sign in to comment.