Skip to content

Commit

Permalink
fix build errors
Browse files Browse the repository at this point in the history
  • Loading branch information
tlgimenes committed Oct 4, 2021
1 parent 814f5b9 commit 8257f41
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 29 deletions.
45 changes: 17 additions & 28 deletions packages/store-api/src/platforms/vtex/resolvers/query.ts
Original file line number Diff line number Diff line change
@@ -1,32 +1,17 @@
import { transformSelectedFacet } from '../utils/facets'
import { enhanceSku } from '../utils/enhanceSku'
import { transformSelectedFacet } from '../utils/facets'
import { SORT_MAP } from '../utils/sort'
import type { SelectedFacet } from '../utils/facets'
import type {
QueryProductArgs,
QueryAllCollectionsArgs,
QueryAllProductsArgs,
QuerySearchArgs,
} from '../../../__generated__/schema'
import type { CategoryTree } from '../clients/commerce/types/CategoryTree'
import type { Context } from '../index'

export interface SearchArgs {
term?: string
first: number
after?: string
sort:
| 'price_desc'
| 'price_asc'
| 'orders_desc'
| 'name_desc'
| 'name_asc'
| 'release_desc'
| 'discount_desc'
| 'score_desc'
selectedFacets: SelectedFacet[]
}

export const Query = {
product: async (
_: unknown,
{ locator }: { locator: SelectedFacet[] },
ctx: Context
) => {
product: async (_: unknown, { locator }: QueryProductArgs, ctx: Context) => {
const {
loaders: { skuLoader },
} = ctx
Expand All @@ -35,22 +20,22 @@ export const Query = {
},
search: async (
_: unknown,
{ first, after: maybeAfter, sort, term, selectedFacets }: SearchArgs
{ first, after: maybeAfter, sort, term, selectedFacets }: QuerySearchArgs
) => {
const after = maybeAfter ? Number(maybeAfter) : 0
const searchArgs = {
page: Math.ceil(after / first),
count: first,
query: term,
sort: SORT_MAP[sort],
selectedFacets: selectedFacets.map(transformSelectedFacet),
sort: SORT_MAP[sort ?? 'score_desc'],
selectedFacets: selectedFacets?.map(transformSelectedFacet) ?? [],
}

return searchArgs
},
allProducts: async (
_: unknown,
{ first, after: maybeAfter }: { first: number; after: string | null },
{ first, after: maybeAfter }: QueryAllProductsArgs,
ctx: Context
) => {
const {
Expand Down Expand Up @@ -82,7 +67,11 @@ export const Query = {
})),
}
},
allCollections: async (_: unknown, __: unknown, ctx: Context) => {
allCollections: async (
_: unknown,
__: QueryAllCollectionsArgs,
ctx: Context
) => {
const {
clients: { commerce },
} = ctx
Expand Down
12 changes: 11 additions & 1 deletion packages/store-api/src/platforms/vtex/utils/facets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,16 @@ export interface SelectedFacet {
value: string
}

const getIdFromSlug = (slug: string) => {
const id = slug.split('-').pop()

if (id == null) {
throw new Error('Error while extracting sku id from product slug')
}

return id
}

/**
* Transform facets from the store to VTEX platform facets.
* For instance, the channel in Store becames trade-policy in VTEX's realm
Expand All @@ -13,7 +23,7 @@ export const transformSelectedFacet = ({ key, value }: SelectedFacet) => {
return { key: 'trade-policy', value }

case 'slug':
return { key: 'id', value: value.split('-').pop() }
return { key: 'id', value: getIdFromSlug(key) }

default:
return { key, value }
Expand Down

0 comments on commit 8257f41

Please sign in to comment.