Skip to content

Commit

Permalink
Merge branch 'v1.x' into feat/add-param-to-crossselling
Browse files Browse the repository at this point in the history
  • Loading branch information
digital-forge-mn authored Feb 26, 2024
2 parents 1b34690 + 28e837a commit 1debd4e
Show file tree
Hide file tree
Showing 9 changed files with 178 additions and 56 deletions.
16 changes: 16 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,22 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
- `groupByProduct` param to `productRecommendations` query.
- `quantity` param to `Recommendation.similars` resolver.

## [1.67.1] - 2023-10-18

## [1.67.0] - 2023-08-22

### Added
- `sponsoredProducts` query.

## [1.66.1] - 2023-08-18

### Fixed
- Retrieve translated text-based product specification values ("properties") when using the `productSearch` and `products` queries.

## [1.66.0] - 2023-08-01

### Fixed
- Version bump to reset circuit breaker
## [1.65.1] - 2023-04-24

### Fixed
Expand Down
2 changes: 1 addition & 1 deletion manifest.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"vendor": "vtex",
"name": "search-resolver",
"version": "1.65.1",
"version": "1.67.1",
"title": "GraphQL resolver for the VTEX store APIs",
"description": "GraphQL resolvers for the VTEX API for the catalog and orders.",
"credentialType": "absolute",
Expand Down
21 changes: 21 additions & 0 deletions node/clients/intelligent-search-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -127,4 +127,25 @@ export class IntelligentSearchApi extends ExternalClient {
},
})
}

public async sponsoredProducts(params: SearchResultArgs, path: string, shippingHeader?: string[]) {
const {query, leap, searchState} = params
if (isPathTraversal(path)) {
throw new Error("Malformed URL")
}

return this.http.get(`/sponsored_products/${path}`, {
params: {
query: query && decodeQuery(query),
locale: this.locale,
bgy_leap: leap ? true : undefined,
...parseState(searchState),
...params,
},
metric: 'product-search',
headers: {
'x-vtex-shipping-options': shippingHeader ?? '',
},
})
}
}
8 changes: 4 additions & 4 deletions node/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
},
"dependencies": {
"@gocommerce/utils": "^0.6.11",
"@vtex/vtexis-compatibility-layer": "^0.2.5",
"@vtex/vtexis-compatibility-layer": "^1.2.13",
"atob": "^2.1.2",
"axios": "^0.21.2",
"camelcase": "^5.0.0",
Expand All @@ -33,7 +33,7 @@
"slugify": "^1.6.4",
"typescript": "3.9.7",
"unescape": "^1.0.1",
"vtex.search-graphql": "http://vtex.vtexassets.com/_v/public/typings/v1/vtex.search-graphql@0.46.0/public"
"vtex.search-graphql": "http://vtex.vtexassets.com/_v/public/typings/v1/vtex.search-graphql@0.54.0-beta.1/public"
},
"devDependencies": {
"@types/atob": "^2.1.2",
Expand All @@ -46,7 +46,7 @@
"@types/node": "^12.0.0",
"@types/qs": "^6.5.1",
"@types/ramda": "^0.26.21",
"@vtex/api": "6.45.15",
"@vtex/api": "6.45.22",
"@vtex/tsconfig": "^0.6.0",
"eslint": "^5.15.3",
"eslint-config-vtex": "^10.1.0",
Expand All @@ -59,5 +59,5 @@
"vtex.rewriter": "http://vtex.vtexassets.com/_v/public/typings/v1/[email protected]/public/@types/vtex.rewriter",
"vtex.search-graphql": "http://vtex.vtexassets.com/_v/public/typings/v1/[email protected]/public/@types/vtex.search-graphql"
},
"version": "1.65.1"
"version": "1.67.1"
}
46 changes: 45 additions & 1 deletion node/resolvers/search/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ import {
convertOrderBy,
} from '../../commons/compatibility-layer'
import { getWorkspaceSearchParamsFromStorage } from '../../routes/workspaceSearchParams'

interface ProductIndentifier {
field: 'id' | 'slug' | 'ean' | 'reference' | 'sku'
value: string
Expand Down Expand Up @@ -540,6 +539,51 @@ export const queries = {
}
},

sponsoredProducts: async (_: any, args: ProductSearchInput, ctx: any) => {
const [shippingOptions, facets] = getShippingOptionsFromSelectedFacets(args.selectedFacets)
args.selectedFacets = facets

args = (await getCompatibilityArgsFromSelectedFacets(
ctx,
args
)) as ProductSearchInput

if (!validMapAndQuery(args.query, args.map)) {
ctx.vtex.logger.warn({
message: 'Invalid map or query',
query: args.query,
map: args.map
})
}

const { intelligentSearchApi } = ctx.clients
const {
selectedFacets,
fullText
} = args

const workspaceSearchParams = await getWorkspaceSearchParamsFromStorage(ctx)

const biggyArgs: {[key:string]: any} = {
...args,
query: fullText,
sort: convertOrderBy(args.orderBy),
...args.options,
...workspaceSearchParams,
}

// unnecessary field. It's is an object and breaks the @vtex/api cache
delete biggyArgs.selectedFacets

const result = await intelligentSearchApi.sponsoredProducts({...biggyArgs}, buildAttributePath(selectedFacets), shippingOptions)

if (ctx.vtex.tenant && !args.productOriginVtex) {
ctx.translated = result.translated
}

return result
},

productRecommendations: async (
_: any,
{ identifier, type, groupByProduct = true }: ProductRecommendationArg,
Expand Down
13 changes: 8 additions & 5 deletions node/resolvers/search/product.ts
Original file line number Diff line number Diff line change
Expand Up @@ -209,14 +209,17 @@ export const resolvers = {
},

properties: async (product: SearchProduct, _: unknown, ctx: Context) => {
let valuesUntranslated = []

if (product.origin === 'intelligent-search') {
return product.properties
valuesUntranslated = product.properties ?? []
} else {
valuesUntranslated = (product.allSpecifications ?? []).map((name: string) => {
const value = (product as unknown as DynamicKey<string[]>)[name]
return { name, originalName: name, values: value }
})
}

const valuesUntranslated = (product.allSpecifications ?? []).map((name: string) => {
const value = (product as unknown as DynamicKey<string[]>)[name]
return { name, originalName: name, values: value }
})
if (!shouldTranslateToUserLocale(ctx)) {
return valuesUntranslated
}
Expand Down
11 changes: 9 additions & 2 deletions node/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,14 @@
{
"extends": "@vtex/tsconfig",
"include": ["./typings/*.d.ts", "./**/*.tsx", "./**/*.ts"],
"include": [
"./typings/*.d.ts",
"./**/*.tsx",
"./**/*.ts"
],
"compilerOptions": {
"types": ["node", "jest"]
"types": [
"node",
"jest"
]
}
}
30 changes: 20 additions & 10 deletions node/utils/i18n.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import {
parseTranslatableStringV2,
createMessagesLoader,
} from '@vtex/api'
import { logDegradedSearchError } from '../resolvers/search/utils'

export const formatTranslatableProp = <R, P extends keyof R, I extends keyof R>(prop: P, idProp: I) =>
(root: R, _: unknown, ctx: Context) => addContextToTranslatableString(
Expand Down Expand Up @@ -34,16 +35,25 @@ export const addContextToTranslatableString = (message: Message, ctx: Context) =
return message.content
}

const {
content,
context: originalContext,
from: originalFrom
} = parseTranslatableStringV2(message.content)

const context = (originalContext || message.context)?.toString()
const from = originalFrom || message.from || locale

return formatTranslatableStringV2({ content, context, from })
try {
const {
content,
context: originalContext,
from: originalFrom
} = parseTranslatableStringV2(message.content)

const context = (originalContext || message.context)?.toString()
const from = originalFrom || message.from || locale
return formatTranslatableStringV2({ content, context, from })
} catch (e) {
logDegradedSearchError(ctx.vtex.logger, {
service: 'node-vtex-api translation',
error: 'Error when trying to add context to translatable string',
errorStack: e,
})
return message.content
}
}

export const translateToCurrentLanguage = (message: MessageWithContext, ctx: Context) => {
Expand Down Expand Up @@ -78,4 +88,4 @@ export const shouldTranslateToBinding = ({ translated, vtex: { binding, tenant }
binding && binding?.locale !== tenant?.locale && (!translated || ignoreIndexedTranslation)

export const shouldTranslateToTenantLocale = ({ vtex: { locale, tenant } }: Context) =>
Boolean(tenant?.locale && locale && tenant.locale !== locale)
Boolean(tenant?.locale && locale && tenant.locale !== locale)
Loading

0 comments on commit 1debd4e

Please sign in to comment.