Skip to content
Draft
Show file tree
Hide file tree
Changes from all 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
23 changes: 23 additions & 0 deletions packages/api/src/__generated__/schema.ts

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

35 changes: 28 additions & 7 deletions packages/api/src/platforms/vtex/clients/commerce/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import type {
IProcessOrderAuthorization,
IUserOrderCancel,
QueryListUserOrdersArgs,
QuerySearchShopperArgs,
StoreMarketingData,
UserOrder,
UserOrderCancel,
Expand All @@ -16,9 +17,11 @@ import type {
import type { Context, Options } from '../../index'
import type { Channel } from '../../utils/channel'
import {
getAuthCookie,
getStoreCookie,
getWithAutCookie,
getWithCookie,
parseJwt,
} from '../../utils/cookies'
import type { ContractResponse } from './Contract'
import type { Address, AddressInput } from './types/Address'
Expand Down Expand Up @@ -686,24 +689,42 @@ export const VtexCommerce = (
{}
)
},
getShopperNameById: ({
searchShopper: ({
userId,
}: { userId: string }): Promise<
name,
}: QuerySearchShopperArgs): Promise<
Array<{
firstName: string
lastName: string
fullName: string
userId: string
}>
> => {
if (!userId) {
throw new Error('Missing userId to fetch shopper name')
if (!userId && !name) {
throw new Error('You must provide userId or name to search shopper')
}

const userIdNormalized = userId.replace(/-/g, '') // Normalize userId by removing hyphens

const headers: HeadersInit = withAutCookie(forwardedHost, account)

// Normalize userId by removing hyphens if present
const userIdNormalized = userId ? userId.replace(/-/g, '') : undefined

const whereParts = []
if (userIdNormalized) {
whereParts.push(`userId=${userIdNormalized}`)
}
if (name) {
const jwt = parseJwt(getAuthCookie(headers?.cookie ?? '', account))
const customerId = jwt?.customerId
whereParts.push(`(fullName = *${name}*)`)

if (customerId) whereParts.push(`(contractIds=${customerId})`)
}

const where = whereParts.join(' AND ')

return fetchAPI(
`${base}/api/dataentities/shopper/search?_where=(userId=${userIdNormalized})&_fields=_all&_schema=v1`,
`${base}/api/dataentities/shopper/search?_where=(${encodeURIComponent(where)})&_fields=_all&_schema=v1`,
{
method: 'GET',
headers,
Expand Down
24 changes: 23 additions & 1 deletion packages/api/src/platforms/vtex/resolvers/query.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import type {
QueryProfileArgs,
QueryRedirectArgs,
QuerySearchArgs,
QuerySearchShopperArgs,
QuerySellersArgs,
QueryShippingArgs,
QueryUserOrderArgs,
Expand Down Expand Up @@ -418,7 +419,7 @@ export const Query = {
} catch (err: any) {}

const shopperSearch =
(await commerce.masterData.getShopperNameById({
(await commerce.masterData.searchShopper({
userId: order.purchaseAgentData?.purchaseAgents?.[0]?.userId ?? '',
})) ?? []
const shopper = shopperSearch[0] ?? {}
Expand All @@ -444,6 +445,7 @@ export const Query = {
shopperName: {
firstName: shopper?.firstName || '',
lastName: shopper?.lastName || '',
fullName: shopper?.fullName || '',
},
}
} catch (error) {
Expand Down Expand Up @@ -492,6 +494,26 @@ export const Query = {
paging: orders.paging,
}
},
searchShopper: async (
_: unknown,
filters: QuerySearchShopperArgs,
ctx: Context
) => {
const {
clients: { commerce },
} = ctx
const shopperSearch =
(await commerce.masterData.searchShopper(filters)) ?? []
return {
shoppers:
shopperSearch?.map((shopper) => ({
userId: shopper?.userId,
firstName: shopper?.firstName || '',
lastName: shopper?.lastName || '',
fullName: shopper?.fullName || '',
})) ?? [],
}
},
accountName: async (_: unknown, __: unknown, ctx: Context) => {
const {
account,
Expand Down
13 changes: 13 additions & 0 deletions packages/api/src/typeDefs/query.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -413,6 +413,19 @@ type Query {
clientEmail: String
): UserOrderListMinimalResult
@cacheControl(scope: "public", sMaxAge: 120, staleWhileRevalidate: 3600)
"""
Returns information about the Shoppers.
"""
searchShopper(
"""
Shopper's userId.
"""
userId: String
"""
Shopper's name.
"""
name: String
): SearchShopperResult

"""
Returns information about the current user details.
Expand Down
13 changes: 13 additions & 0 deletions packages/api/src/typeDefs/shopper.graphql
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
"""
SearchShopperResult information.
"""
type SearchShopperResult {
shoppers: [SearchShopper]
}

type SearchShopper {
userId: String
firstName: String
lastName: String
fullName: String
}
1 change: 1 addition & 0 deletions packages/api/src/typeDefs/userOrder.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ type UserOrderResult {
type UserOrderShopperName {
firstName: String
lastName: String
fullName: String
}

type UserOrderListResult {
Expand Down
1 change: 1 addition & 0 deletions packages/api/test/integration/schema.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ const QUERIES = [
'productCount',
'userOrder',
'listUserOrders',
'searchShopper',
'userDetails',
'accountProfile',
'accountName',
Expand Down
8 changes: 8 additions & 0 deletions packages/core/@generated/gql.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,8 @@ const documents = {
types.ClientProductGalleryQueryDocument,
'\n query ClientProductQuery($locator: [IStoreSelectedFacet!]!) {\n ...ClientProduct\n product(locator: $locator) {\n ...ProductDetailsFragment_product\n }\n }\n':
types.ClientProductQueryDocument,
'\n query ClientSearchShopperQuery($userId: String, $name: String) {\n searchShopper(userId: $userId, name: $name) {\n shoppers {\n userId\n firstName\n lastName\n fullName\n }\n }\n }\n':
types.ClientSearchShopperQueryDocument,
'\n query ClientProfileQuery($id: String!) {\n profile(id: $id) {\n addresses {\n country\n postalCode\n geoCoordinate\n city\n }\n }\n }\n':
types.ClientProfileQueryDocument,
'\n query ClientSearchSuggestionsQuery(\n $term: String!\n $selectedFacets: [IStoreSelectedFacet!]\n ) {\n ...ClientSearchSuggestions\n search(first: 5, term: $term, selectedFacets: $selectedFacets) {\n suggestions {\n terms {\n value\n }\n products {\n ...ProductSummary_product\n }\n }\n products {\n pageInfo {\n totalCount\n }\n }\n metadata {\n ...SearchEvent_metadata\n }\n }\n }\n':
Expand Down Expand Up @@ -282,6 +284,12 @@ export function gql(
export function gql(
source: '\n query ClientProductQuery($locator: [IStoreSelectedFacet!]!) {\n ...ClientProduct\n product(locator: $locator) {\n ...ProductDetailsFragment_product\n }\n }\n'
): typeof import('./graphql').ClientProductQueryDocument
/**
* The gql function is used to parse GraphQL queries into a document that can be used by GraphQL clients.
*/
export function gql(
source: '\n query ClientSearchShopperQuery($userId: String, $name: String) {\n searchShopper(userId: $userId, name: $name) {\n shoppers {\n userId\n firstName\n lastName\n fullName\n }\n }\n }\n'
): typeof import('./graphql').ClientSearchShopperQueryDocument
/**
* The gql function is used to parse GraphQL queries into a document that can be used by GraphQL clients.
*/
Expand Down
45 changes: 45 additions & 0 deletions packages/core/@generated/graphql.ts
Original file line number Diff line number Diff line change
Expand Up @@ -847,6 +847,8 @@ export type Query = {
redirect: Maybe<StoreRedirect>
/** Returns the result of a product, facet, or suggestion search. */
search: StoreSearchResult
/** Returns information about the Shoppers. */
searchShopper: Maybe<SearchShopperResult>
/** Returns a list of sellers available for a specific localization. */
sellers: Maybe<SellersData>
/** Returns information about shipping simulation. */
Expand Down Expand Up @@ -913,6 +915,11 @@ export type QuerySearchArgs = {
term?: InputMaybe<Scalars['String']['input']>
}

export type QuerySearchShopperArgs = {
name: InputMaybe<Scalars['String']['input']>
userId: InputMaybe<Scalars['String']['input']>
}

export type QuerySellersArgs = {
country: Scalars['String']['input']
geoCoordinates: InputMaybe<IGeoCoordinates>
Expand Down Expand Up @@ -940,6 +947,18 @@ export type SearchMetadata = {
logicalOperator: Scalars['String']['output']
}

export type SearchShopper = {
firstName: Maybe<Scalars['String']['output']>
fullName: Maybe<Scalars['String']['output']>
lastName: Maybe<Scalars['String']['output']>
userId: Maybe<Scalars['String']['output']>
}

/** SearchShopperResult information. */
export type SearchShopperResult = {
shoppers: Maybe<Array<Maybe<SearchShopper>>>
}

/** Information of sellers. */
export type SellerInfo = {
/** Identification of the seller */
Expand Down Expand Up @@ -2293,6 +2312,7 @@ export type UserOrderShippingData = {

export type UserOrderShopperName = {
firstName: Maybe<Scalars['String']['output']>
fullName: Maybe<Scalars['String']['output']>
lastName: Maybe<Scalars['String']['output']>
}

Expand Down Expand Up @@ -3274,6 +3294,22 @@ export type ClientProductQueryQuery = {
}
}

export type ClientSearchShopperQueryQueryVariables = Exact<{
userId: InputMaybe<Scalars['String']['input']>
name: InputMaybe<Scalars['String']['input']>
}>

export type ClientSearchShopperQueryQuery = {
searchShopper: {
shoppers: Array<{
userId: string | null
firstName: string | null
lastName: string | null
fullName: string | null
} | null> | null
} | null
}

export type ClientProfileQueryQueryVariables = Exact<{
id: Scalars['String']['input']
}>
Expand Down Expand Up @@ -4135,6 +4171,15 @@ export const ClientProductQueryDocument = {
ClientProductQueryQuery,
ClientProductQueryQueryVariables
>
export const ClientSearchShopperQueryDocument = {
__meta__: {
operationName: 'ClientSearchShopperQuery',
operationHash: '4dc1daf8ceb850279427dd59f35193ebe490b3c1',
},
} as unknown as TypedDocumentString<
ClientSearchShopperQueryQuery,
ClientSearchShopperQueryQueryVariables
>
export const ClientProfileQueryDocument = {
__meta__: {
operationName: 'ClientProfileQuery',
Expand Down
Loading
Loading