Skip to content

Commit

Permalink
add sales channel support
Browse files Browse the repository at this point in the history
  • Loading branch information
tlgimenes committed May 31, 2022
1 parent d8a0f58 commit 5b67df2
Show file tree
Hide file tree
Showing 20 changed files with 452 additions and 225 deletions.
59 changes: 49 additions & 10 deletions packages/api/src/__generated__/schema.ts

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

29 changes: 17 additions & 12 deletions packages/api/src/platforms/vtex/clients/commerce/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -135,17 +135,22 @@ export const VtexCommerce = (
)
},
},
session: (): Promise<Session> =>
fetchAPI(
`${base}/api/sessions?items=profile.id,profile.email,profile.firstName,profile.lastName`,
{
method: 'POST',
headers: {
'content-type': 'application/json',
cookie: ctx.headers.cookie,
},
body: '{}',
}
),
session: (search: string): Promise<Session> => {
const params = new URLSearchParams(search)

params.set(
'items',
'profile.id,profile.email,profile.firstName,profile.lastName,store.channel,store.countryCode,store.cultureInfo,store.currencyCode,store.currencySymbol'
)

return fetchAPI(`${base}/api/sessions?${params.toString()}`, {
method: 'POST',
headers: {
'content-type': 'application/json',
cookie: ctx.headers.cookie,
},
body: '{}',
})
},
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,21 @@ export interface Session {

export interface Namespaces {
profile?: Profile
store?: Store
}

export interface Value {
value: string
}

export interface Store {
channel: Value
countryCode: Value
cultureInfo: Value
currencyCode: Value
currencySymbol: Value
}

export interface Profile {
id?: Value
email?: Value
Expand Down
4 changes: 2 additions & 2 deletions packages/api/src/platforms/vtex/resolvers/mutation.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { validateCart } from './validateCart'
import { updateSession } from './updateSession'
import { validateSession } from './validateSession'

export const Mutation = {
validateCart,
updateSession,
validateSession,
}
18 changes: 0 additions & 18 deletions packages/api/src/platforms/vtex/resolvers/query.ts
Original file line number Diff line number Diff line change
Expand Up @@ -165,22 +165,4 @@ export const Query = {
})),
}
},
person: async (_: unknown, __: unknown, ctx: Context) => {
const {
clients: { commerce },
} = ctx

const {
namespaces: { profile = null },
} = await commerce.session()

return (
profile && {
id: profile.id?.value ?? '',
email: profile.email?.value ?? '',
givenName: profile.firstName?.value ?? '',
familyName: profile.lastName?.value ?? '',
}
)
},
}
26 changes: 0 additions & 26 deletions packages/api/src/platforms/vtex/resolvers/updateSession.ts

This file was deleted.

59 changes: 59 additions & 0 deletions packages/api/src/platforms/vtex/resolvers/validateSession.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
import deepEquals from 'fast-deep-equal'

import ChannelMarshal from '../utils/channel'
import type { Context } from '..'
import type {
MutationValidateSessionArgs,
StoreSession,
} from '../../../__generated__/schema'

export const validateSession = async (
_: any,
{ session: oldSession, search }: MutationValidateSessionArgs,
{ clients }: Context
): Promise<StoreSession | null> => {
const channel = ChannelMarshal.parse(oldSession.channel ?? '')
const postalCode = String(oldSession.postalCode ?? '').replace(/\D/g, '')
const country = oldSession.country ?? ''

const params = new URLSearchParams(search)

params.set('sc', params.get('sc') ?? channel.salesChannel)

const [regionData, sessionData] = await Promise.all([
postalCode
? clients.commerce.checkout.region({ postalCode, country })
: Promise.resolve(null),
clients.commerce.session(params.toString()).catch(() => null),
])

const profile = sessionData?.namespaces.profile ?? null
const store = sessionData?.namespaces.store ?? null

const newSession = {
...oldSession,
currency: {
code: store?.currencyCode.value ?? oldSession.currency.code,
symbol: store?.currencySymbol.value ?? oldSession.currency.symbol,
},
country: store?.countryCode.value ?? oldSession.country,
channel: ChannelMarshal.stringify({
salesChannel: store?.channel?.value ?? channel.salesChannel,
regionId: regionData?.[0]?.id ?? channel.regionId,
}),
person: profile?.id
? {
id: profile.id?.value ?? '',
email: profile.email?.value ?? '',
givenName: profile.firstName?.value ?? '',
familyName: profile.lastName?.value ?? '',
}
: null,
}

if (deepEquals(oldSession, newSession)) {
return null
}

return newSession
}
2 changes: 2 additions & 0 deletions packages/api/src/typeDefs/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import Status from './status.graphql'
import PropertyValue from './propertyValue.graphql'
import Person from './person.graphql'
import ObjectOrString from './objectOrString.graphql'
import Session from './session.graphql'

export const typeDefs = [
Query,
Expand All @@ -48,6 +49,7 @@ export const typeDefs = [
PropertyValue,
Person,
ObjectOrString,
Session,
]
.map(print)
.join('\n')
40 changes: 2 additions & 38 deletions packages/api/src/typeDefs/mutation.graphql
Original file line number Diff line number Diff line change
@@ -1,46 +1,10 @@
"""
Session information.
"""
type StoreSession {
"""
Session channel.
"""
channel: String
"""
Session country.
"""
country: String
"""
Session postal code.
"""
postalCode: String
}

"""
Session input.
"""
input IStoreSession {
"""
Session input channel.
"""
channel: String
"""
Session input country.
"""
country: String
"""
Session input postal code.
"""
postalCode: String
}

type Mutation {
"""
Returns the order if anything has changed in it, or `null` if the order is valid.
"""
validateCart(cart: IStoreCart!): StoreCart
"""
Update session information.
Validate session information.
"""
updateSession(session: IStoreSession!): StoreSession!
validateSession(session: IStoreSession!, search: String!): StoreSession
}
22 changes: 22 additions & 0 deletions packages/api/src/typeDefs/person.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,25 @@ type StorePerson {
"""
familyName: String!
}

"""
Client profile data.
"""
input IStorePerson {
"""
Client ID.
"""
id: String!
"""
Client email.
"""
email: String!
"""
Client first name.
"""
givenName: String!
"""
Client last name.
"""
familyName: String!
}
5 changes: 0 additions & 5 deletions packages/api/src/typeDefs/query.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -204,9 +204,4 @@ type Query {
"""
after: String
): StoreCollectionConnection!

"""
Person query.
"""
person: StorePerson
}
Loading

0 comments on commit 5b67df2

Please sign in to comment.