mirror of
https://github.com/vercel/commerce.git
synced 2025-07-04 20:21:21 +00:00
Customer Account Initial Commit
This commit is contained in:
parent
07b22f5d4d
commit
d906a0b3ac
62
framework/kibocommerce/api/endpoints/customer/customer.ts
Normal file
62
framework/kibocommerce/api/endpoints/customer/customer.ts
Normal file
@ -0,0 +1,62 @@
|
||||
import type { GetLoggedInCustomerQuery } from '../../../schema'
|
||||
import type { CustomerEndpoint } from '.'
|
||||
import { getCustomerAccountQuery } from '../../queries/get-customer-account-query'
|
||||
|
||||
// export const getLoggedInCustomerQuery = /* GraphQL */ `
|
||||
// query getLoggedInCustomer {
|
||||
// customer {
|
||||
// entityId
|
||||
// firstName
|
||||
// lastName
|
||||
// email
|
||||
// company
|
||||
// customerGroupId
|
||||
// notes
|
||||
// phone
|
||||
// addressCount
|
||||
// attributeCount
|
||||
// storeCredit {
|
||||
// value
|
||||
// currencyCode
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
// `
|
||||
|
||||
export type Customer = NonNullable<GetLoggedInCustomerQuery['customer']>
|
||||
|
||||
const getLoggedInCustomer: CustomerEndpoint['handlers']['getLoggedInCustomer'] = async ({
|
||||
req,
|
||||
res,
|
||||
config,
|
||||
}) => {
|
||||
const token = req.cookies[config.customerCookie]
|
||||
const { accessToken } = JSON.parse(token);
|
||||
|
||||
if (accessToken) {
|
||||
const { data } = await config.fetch(
|
||||
getCustomerAccountQuery,
|
||||
undefined,
|
||||
{
|
||||
headers: {
|
||||
'x-vol-user-claims': accessToken
|
||||
},
|
||||
}
|
||||
)
|
||||
|
||||
const customer = data?.customerAccount;
|
||||
|
||||
if (!customer) {
|
||||
return res.status(400).json({
|
||||
data: null,
|
||||
errors: [{ message: 'Customer not found', code: 'not_found' }],
|
||||
})
|
||||
}
|
||||
|
||||
return res.status(200).json({ data: { customer } })
|
||||
}
|
||||
|
||||
res.status(200).json({ data: null })
|
||||
}
|
||||
|
||||
export default getLoggedInCustomer
|
@ -1 +1,18 @@
|
||||
export default function noopApi(...args: any[]): void {}
|
||||
import { GetAPISchema, createEndpoint } from '@commerce/api'
|
||||
import customerEndpoint from '@commerce/api/endpoints/customer'
|
||||
import type { CustomerSchema } from '../../../types/customer'
|
||||
import type { KiboCommerceAPI } from '../..'
|
||||
import getLoggedInCustomer from './customer'
|
||||
|
||||
export type CustomerAPI = GetAPISchema<KiboCommerceAPI, CustomerSchema>
|
||||
|
||||
export type CustomerEndpoint = CustomerAPI['endpoint']
|
||||
|
||||
export const handlers: CustomerEndpoint['handlers'] = { getLoggedInCustomer }
|
||||
|
||||
const customerApi = createEndpoint<CustomerAPI>({
|
||||
handler: customerEndpoint,
|
||||
handlers,
|
||||
})
|
||||
|
||||
export default customerApi
|
||||
|
@ -0,0 +1,12 @@
|
||||
export const getCustomerAccountQuery = `
|
||||
query getUser {
|
||||
customerAccount:getCurrentAccount {
|
||||
id
|
||||
firstName
|
||||
lastName
|
||||
emailAddress
|
||||
userName
|
||||
isAnonymous
|
||||
}
|
||||
}
|
||||
`
|
@ -31,7 +31,7 @@ export const handler: MutationHook<LoginHook> = {
|
||||
return useCallback(
|
||||
async function login(input) {
|
||||
const data = await fetch({ input })
|
||||
// await revalidate()
|
||||
await revalidate()
|
||||
return data
|
||||
},
|
||||
[fetch, revalidate]
|
||||
|
@ -35,7 +35,7 @@ export const handler: MutationHook<SignupHook> = {
|
||||
return useCallback(
|
||||
async function signup(input) {
|
||||
const data = await fetch({ input })
|
||||
// await revalidate()
|
||||
await revalidate()
|
||||
return data
|
||||
},
|
||||
[fetch, revalidate]
|
||||
|
@ -1,15 +1,24 @@
|
||||
import { SWRHook } from '@commerce/utils/types'
|
||||
import useCustomer, { UseCustomer } from '@commerce/customer/use-customer'
|
||||
import type { CustomerHook } from '../types/customer'
|
||||
|
||||
export default useCustomer as UseCustomer<typeof handler>
|
||||
export const handler: SWRHook<any> = {
|
||||
|
||||
export const handler: SWRHook<CustomerHook> = {
|
||||
fetchOptions: {
|
||||
query: '',
|
||||
url: '/api/customer',
|
||||
method: 'GET',
|
||||
},
|
||||
async fetcher({ input, options, fetch }) {},
|
||||
useHook: () => () => {
|
||||
return async function addItem() {
|
||||
return {}
|
||||
}
|
||||
async fetcher({ options, fetch }) {
|
||||
const data = await fetch(options)
|
||||
return data?.customer ?? null
|
||||
},
|
||||
useHook: ({ useData }) => (input) => {
|
||||
return useData({
|
||||
swrOptions: {
|
||||
revalidateOnFocus: false,
|
||||
...input?.swrOptions,
|
||||
},
|
||||
})
|
||||
},
|
||||
}
|
||||
|
24
framework/kibocommerce/schema.d.ts
vendored
24
framework/kibocommerce/schema.d.ts
vendored
@ -11401,4 +11401,26 @@ export type WorkflowStateInput = {
|
||||
export type LoginMutationVariables = Exact<{
|
||||
email: Scalars['String']
|
||||
password: Scalars['String']
|
||||
}>
|
||||
}>
|
||||
|
||||
export type GetLoggedInCustomerQuery = { __typename?: 'Query' } & {
|
||||
customer?: Maybe<
|
||||
{ __typename?: 'Customer' } & Pick<
|
||||
Customer,
|
||||
| 'entityId'
|
||||
| 'firstName'
|
||||
| 'lastName'
|
||||
| 'email'
|
||||
| 'company'
|
||||
| 'customerGroupId'
|
||||
| 'notes'
|
||||
| 'phone'
|
||||
| 'addressCount'
|
||||
| 'attributeCount'
|
||||
> & {
|
||||
storeCredit: Array<
|
||||
{ __typename?: 'Money' } & Pick<Money, 'value' | 'currencyCode'>
|
||||
>
|
||||
}
|
||||
>
|
||||
}
|
5
framework/kibocommerce/types/customer.ts
Normal file
5
framework/kibocommerce/types/customer.ts
Normal file
@ -0,0 +1,5 @@
|
||||
import * as Core from '@commerce/types/customer'
|
||||
|
||||
export * from '@commerce/types/customer'
|
||||
|
||||
export type CustomerSchema = Core.CustomerSchema
|
1
framework/kibocommerce/types/logout.ts
Normal file
1
framework/kibocommerce/types/logout.ts
Normal file
@ -0,0 +1 @@
|
||||
export * from '@commerce/types/logout'
|
Loading…
x
Reference in New Issue
Block a user