Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(api): Handle channel as facet for search and product queries #1197

Merged
merged 7 commits into from
Mar 31, 2022
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
4 changes: 2 additions & 2 deletions packages/api/src/platforms/vtex/resolvers/query.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ export const Query = {
loaders: { skuLoader },
} = ctx

return skuLoader.load(locator.map(transformSelectedFacet))
return skuLoader.load(locator.flatMap(transformSelectedFacet))
},
collection: (_: unknown, { slug }: QueryCollectionArgs, ctx: Context) => {
const {
Expand Down Expand Up @@ -54,7 +54,7 @@ export const Query = {
count: first,
query: term,
sort: SORT_MAP[sort ?? 'score_desc'],
selectedFacets: selectedFacets?.map(transformSelectedFacet) ?? [],
selectedFacets: selectedFacets?.flatMap(transformSelectedFacet) ?? [],
}

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) {
igorbrasileiro marked this conversation as resolved.
Show resolved Hide resolved
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