Skip to content

Commit

Permalink
Handle channel as string for search queries
Browse files Browse the repository at this point in the history
  • Loading branch information
igorbrasileiro committed Mar 29, 2022
1 parent 735dced commit ccd0ee9
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 12 deletions.
6 changes: 4 additions & 2 deletions packages/api/src/platforms/vtex/clients/commerce/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import type {
SimulationOptions,
} from './types/Simulation'
import type { Session } from './types/Session'
import ChannelMarshal from '../../utils/channel'

const BASE_INIT = {
method: 'POST',
Expand Down Expand Up @@ -62,12 +63,13 @@ export const VtexCommerce = (
orderForm: ({
id,
refreshOutdatedData = true,
salesChannel = ctx.storage.channel,
channel = ctx.storage.channel,
}: {
id: string
refreshOutdatedData?: boolean
salesChannel?: string
channel?: string
}): Promise<OrderForm> => {
const { salesChannel } = ChannelMarshal.parse(channel)
const params = new URLSearchParams({
refreshOutdatedData: refreshOutdatedData.toString(),
sc: salesChannel,
Expand Down
1 change: 1 addition & 0 deletions packages/api/src/platforms/vtex/resolvers/product.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ export const StoreProduct: Record<string, Resolver<Root>> = {
} = ctx

const { id, policies } = product

const sellers = policies.find((policy) => policy.id === channel)?.sellers

if (sellers == null) {
Expand Down
20 changes: 14 additions & 6 deletions packages/api/src/platforms/vtex/resolvers/query.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,22 +11,26 @@ import type {
} from '../../../__generated__/schema'
import type { CategoryTree } from '../clients/commerce/types/CategoryTree'
import type { Context } from '../index'
import ChannelMarshal from '../utils/channel'

export const Query = {
product: async (_: unknown, { locator }: QueryProductArgs, ctx: Context) => {
// Insert channel in context for later usage
ctx.storage = {
...ctx.storage,
channel:
locator.find((facet) => facet.key === 'channel')?.value ??
ctx.storage.channel,
// TODO: Check if make sense move this parse a layer down or pass the whole parsed channel
// TODO: Put the channel from ctx inside the parse
ChannelMarshal.parse(
locator.find((facet) => facet.key === 'channel')?.value ?? '{}'
).salesChannel || ctx.storage.channel,
}

const {
loaders: { skuLoader },
} = ctx

return skuLoader.load(locator.map(transformSelectedFacet))
return skuLoader.load(locator.map(transformSelectedFacet).flat())
},
collection: (_: unknown, { slug }: QueryCollectionArgs, ctx: Context) => {
const {
Expand All @@ -44,8 +48,12 @@ export const Query = {
ctx.storage = {
...ctx.storage,
channel:
selectedFacets?.find((facet) => facet.key === 'channel')?.value ??
ctx.storage.channel,
// TODO: Check if make sense move this parse a layer down or pass the whole parsed channel
// TODO: Put the channel from ctx inside the parse
ChannelMarshal.parse(
selectedFacets?.find((facet) => facet.key === 'channel')?.value ??
'{}'
).salesChannel || ctx.storage.channel,
}

const after = maybeAfter ? Number(maybeAfter) : 0
Expand All @@ -54,7 +62,7 @@ export const Query = {
count: first,
query: term,
sort: SORT_MAP[sort ?? 'score_desc'],
selectedFacets: selectedFacets?.map(transformSelectedFacet) ?? [],
selectedFacets: selectedFacets?.map(transformSelectedFacet).flat() ?? [],
}

return searchArgs
Expand Down
2 changes: 1 addition & 1 deletion packages/api/src/platforms/vtex/utils/channel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ export interface Channel {
}

export default class ChannelMarshal {
public static parse(channelString: string): Channel {
public static parse(channelString: string): Required<Channel> {
try {
const parsedChannel = JSON.parse(channelString) as Channel

Expand Down
22 changes: 19 additions & 3 deletions packages/api/src/platforms/vtex/utils/facets.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,32 @@
import ChannelMarshal from './channel'

export interface SelectedFacet {
key: string
value: string
}

/**
* Transform facets from the store to VTEX platform facets.
* For instance, the channel in Store becomes trade-policy in VTEX's realm
* For instance, the channel in Store becomes trade-policy and regionId in VTEX's realm
* */
export const transformSelectedFacet = ({ key, value }: SelectedFacet) => {
switch (key) {
case 'channel':
return { key: 'trade-policy', value }
case 'channel': {
const channel = ChannelMarshal.parse(value)

const result = []

if (channel.salesChannel) {
result.push({ key: 'trade-policy', value: channel.salesChannel })
}

// TODO: Remove comment when the IS accept regionId facet.
// if (channel.regionId) {
// result.push({ key: 'regionId', value: channel.regionId })
// }

return result
}

default:
return { key, value }
Expand Down

0 comments on commit ccd0ee9

Please sign in to comment.