Skip to content

Commit ab0d7da

Browse files
Added categoryTree param to mount href with dataplane data
1 parent ccfd78d commit ab0d7da

File tree

3 files changed

+40
-26
lines changed

3 files changed

+40
-26
lines changed

node/resolvers/search/category.ts

Lines changed: 24 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
import { compose, last, prop, split } from 'ramda'
22

3-
import { getCategoryInfo, logDegradedSearchError } from './utils'
43
import { formatTranslatableProp, shouldTranslateToBinding } from '../../utils/i18n'
54
import { Slugify } from '../../utils/slug'
65
import { APP_NAME } from './constants'
6+
import { getCategoryInfo, logDegradedSearchError } from './utils'
77

88
const lastSegment = compose<string, string[], string>(
99
last,
@@ -14,6 +14,28 @@ function cleanUrl(url: string) {
1414
return url.replace(/https:\/\/[A-z0-9]+\.vtexcommercestable\.com\.br/, '').toLowerCase()
1515
}
1616

17+
export async function mountHrefRewritter({ url, id }: any, _: unknown, ctx: Context) {
18+
const settings: AppSettings = await ctx.clients.apps.getAppSettings(APP_NAME)
19+
20+
if (shouldTranslateToBinding(ctx)) {
21+
try {
22+
const rewriterUrl = await ctx.clients.rewriter.getRoute(id.toString(), 'anyCategoryEntity', ctx.vtex.binding!.id!)
23+
if (rewriterUrl) {
24+
url = rewriterUrl
25+
}
26+
} catch (e) {
27+
logDegradedSearchError(ctx.vtex.logger, {
28+
service: 'Rewriter getRoute',
29+
error: `Rewriter getRoute query returned an error for category ${id}. Category href may be incorrect.`,
30+
errorStack: e,
31+
})
32+
}
33+
}
34+
const pathname = cleanUrl(url)
35+
36+
return settings.slugifyLinks ? Slugify(pathname) : pathname
37+
}
38+
1739
/** This type has to be created because the Catlog API to get category by ID does not return the url or children for now.
1840
* These fields only come if you get the category from the categroy tree api.
1941
*/
@@ -29,27 +51,7 @@ export const resolvers = {
2951

3052
cacheId: prop('id'),
3153

32-
href: async ({ url, id }: SafeCategory, _: unknown, ctx: Context) => {
33-
const settings: AppSettings = await ctx.clients.apps.getAppSettings(APP_NAME)
34-
35-
if (shouldTranslateToBinding(ctx)) {
36-
try {
37-
const rewriterUrl = await ctx.clients.rewriter.getRoute(id.toString(), 'anyCategoryEntity', ctx.vtex.binding!.id!)
38-
if (rewriterUrl) {
39-
url = rewriterUrl
40-
}
41-
} catch (e) {
42-
logDegradedSearchError(ctx.vtex.logger, {
43-
service: 'Rewriter getRoute',
44-
error: `Rewriter getRoute query returned an error for category ${id}. Category href may be incorrect.`,
45-
errorStack: e,
46-
})
47-
}
48-
}
49-
const pathname = cleanUrl(url)
50-
51-
return settings.slugifyLinks ? Slugify(pathname) : pathname
52-
},
54+
href: mountHrefRewritter,
5355

5456
metaTagDescription: formatTranslatableProp<SafeCategory, 'MetaTagDescription', 'id'>(
5557
'MetaTagDescription',

node/resolvers/search/product.ts

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
1-
import { compose, last, omit, pathOr, split, flatten } from 'ramda'
1+
import { compose, flatten, last, omit, pathOr, split } from 'ramda'
22

3+
import { fetchAppSettings } from '../../services/settings'
34
import {
45
addContextToTranslatableString,
56
formatTranslatableProp,
67
shouldTranslateToBinding,
78
shouldTranslateToUserLocale,
89
} from '../../utils/i18n'
910
import { getBenefits } from '../benefits'
11+
import { mountHrefRewritter } from './category'
1012
import { buildCategoryMap, logDegradedSearchError } from './utils'
1113

1214
type DynamicKey<T> = Record<string, T>
@@ -121,18 +123,27 @@ const findMainTree = (categoriesIds: string[], prodCategoryId: string) => {
121123
}
122124

123125
const productCategoriesToCategoryTree = async (
124-
{ categories, categoriesIds, categoryId: prodCategoryId }: SearchProduct,
126+
// TODO categoryTree will be added
127+
{ categories, categoriesIds, categoryId: prodCategoryId, categoryTree }: SearchProduct & { categoryTree: Array<{ id: number, name: string, href: string }> },
125128
_: any,
126-
{ clients: { search }, vtex: { platform } }: Context
129+
ctx: Context
127130
) => {
131+
const { clients: { search }, vtex: { platform } } = ctx
132+
const { shouldUseNewPDPEndpoint } = await fetchAppSettings(ctx)
128133
if (!categories || !categoriesIds) {
129134
return []
130135
}
131136

132137
const mainTreeIds = findMainTree(categoriesIds, prodCategoryId)
133138

134139
if (platform === 'vtex') {
135-
return mainTreeIds.map(categoryId => search.category(Number(categoryId)))
140+
if (shouldUseNewPDPEndpoint) {
141+
return categoryTree.map(category => ({ ...category, href: mountHrefRewritter(category, _, ctx) }))
142+
}
143+
return mainTreeIds.map(async categoryId => {
144+
const category = await search.category(Number(categoryId))
145+
return {...category,}
146+
})
136147
}
137148
const categoriesTree = await search.categories(mainTreeIds.length)
138149
const categoryMap = buildCategoryMap(categoriesTree)

node/utils/i18n.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ export const addContextToTranslatableString = (message: Message, ctx: Context) =
4747

4848
const context = (originalContext || message.context)?.toString()
4949
const from = originalFrom || message.from || locale
50+
// @ts-expect-error
5051
return formatTranslatableStringV2({ content, context, from, state })
5152
} catch (e) {
5253
logDegradedSearchError(ctx.vtex.logger, {

0 commit comments

Comments
 (0)