Skip to content

Commit

Permalink
fix: typescript errors on enhanced commertial offers
Browse files Browse the repository at this point in the history
  • Loading branch information
icazevedo committed May 17, 2022
1 parent ccb1633 commit 2626da7
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 25 deletions.
3 changes: 1 addition & 2 deletions packages/api/src/platforms/vtex/resolvers/aggregateOffer.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
import { inStock } from '../utils/productStock'
import type { EnhancedCommercialOffer } from '../utils/enhanceCommercialOffer'
import type { StoreProduct } from './product'
import type { PromiseType } from '../../../typings'
import type { Resolver } from '..'

type Root = PromiseType<ReturnType<typeof StoreProduct.offers>>

export const StoreAggregateOffer: Record<string, Resolver<Root>> & {
offers: Resolver<Root, any, EnhancedCommercialOffer[]>
offers: Resolver<Root, any, Root>
} = {
highPrice: (offers) => {
const availableOffers = offers.filter(inStock)
Expand Down
32 changes: 21 additions & 11 deletions packages/api/src/platforms/vtex/resolvers/product.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,12 @@ const isAttachmentDefinition = (
): attachment is AttachmentDefinition => 'id' in attachment

export const StoreProduct: Record<string, Resolver<Root>> & {
offers: Resolver<Root, any, EnhancedCommercialOffer[]>
offers: Resolver<
Root,
any,
Array<EnhancedCommercialOffer<Root['sellers'][number], Root>>
>

isVariantOf: Resolver<Root, any, Root>
} = {
productID: ({ itemId }) => itemId,
Expand Down Expand Up @@ -98,17 +103,22 @@ export const StoreProduct: Record<string, Resolver<Root>> & {
}))
)

const propertyValueAttachments = attachments.map((attachment) => {
if (isAttachmentDefinition(attachment)) {
return
}
// Typescript don't understand (A[] | B[]) as an array, only Array<A | B>
const propertyValueAttachments = (attachments as Array<
Root['attachments'][number]
>)
.map((attachment: Root['attachments'][number]) => {
if (isAttachmentDefinition(attachment)) {
return
}

return {
name: attachment.name,
value: attachment.content,
valueReference: VALUE_REFERENCES.attachment,
}
})
return {
name: attachment.name,
value: attachment.content,
valueReference: VALUE_REFERENCES.attachment,
}
})
.filter(Boolean)

return [...propertyValueVariations, ...propertyValueAttachments]
},
Expand Down
20 changes: 8 additions & 12 deletions packages/api/src/platforms/vtex/utils/enhanceCommercialOffer.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,19 @@
import type {
CommertialOffer,
Seller,
} from '../clients/search/types/ProductSearchResult'
import type { EnhancedSku } from './enhanceSku'
import type { CommertialOffer } from '../clients/search/types/ProductSearchResult'

export type EnhancedCommercialOffer = CommertialOffer & {
seller: Seller
product: EnhancedSku
export type EnhancedCommercialOffer<S, P> = CommertialOffer & {
seller: S
product: P
}

export const enhanceCommercialOffer = ({
export const enhanceCommercialOffer = <S, P>({
offer,
seller,
product,
}: {
offer: CommertialOffer
seller: Seller
product: EnhancedSku
}): EnhancedCommercialOffer => ({
seller: S
product: P
}): EnhancedCommercialOffer<S, P> => ({
...offer,
product,
seller,
Expand Down

0 comments on commit 2626da7

Please sign in to comment.