Skip to content

Commit

Permalink
initial sales channel support
Browse files Browse the repository at this point in the history
  • Loading branch information
tlgimenes committed May 25, 2022
1 parent bc16810 commit c54c725
Show file tree
Hide file tree
Showing 18 changed files with 1,695 additions and 1,133 deletions.
58 changes: 48 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.

2 changes: 1 addition & 1 deletion packages/api/src/platforms/vtex/clients/commerce/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ export const VtexCommerce = (
},
session: (): Promise<Session> =>
fetchAPI(
`${base}/api/sessions?items=profile.id,profile.email,profile.firstName,profile.lastName`,
`${base}/api/sessions?items=profile.id,profile.email,profile.firstName,profile.lastName,store.channel`,
{
method: 'POST',
headers: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,17 @@ export interface Session {

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

export interface Value {
value: string
}

export interface Store {
channel: 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 @@ -154,22 +154,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.

47 changes: 47 additions & 0 deletions packages/api/src/platforms/vtex/resolvers/validateSession.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
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 }: 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 [regionData, sessionData] = await Promise.all([
postalCode
? clients.commerce.checkout.region({ postalCode, country })
: Promise.resolve(null),
clients.commerce.session(),
])

const { profile = null, store = null } = sessionData.namespaces

const newSession = {
...oldSession,
channel: ChannelMarshal.stringify({
salesChannel: store?.channel?.value ?? channel.salesChannel,
regionId: regionData?.[0]?.id ?? channel.regionId,
}),
person: profile && {
id: profile.id?.value ?? '',
email: profile.email?.value ?? '',
givenName: profile.firstName?.value ?? '',
familyName: profile.lastName?.value ?? '',
},
}

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!): 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 @@ -190,9 +190,4 @@ type Query {
"""
after: String
): StoreCollectionConnection!

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

0 comments on commit c54c725

Please sign in to comment.