From d906a0b3ac6c43e34c769d818ee51cd00fc8c2ac Mon Sep 17 00:00:00 2001 From: SushantJadhav Date: Thu, 26 Aug 2021 15:53:34 +0530 Subject: [PATCH] Customer Account Initial Commit --- .../api/endpoints/customer/customer.ts | 62 +++++++++++++++++++ .../api/endpoints/customer/index.ts | 19 +++++- .../api/queries/get-customer-account-query.ts | 12 ++++ framework/kibocommerce/auth/use-login.tsx | 2 +- framework/kibocommerce/auth/use-signup.tsx | 2 +- .../kibocommerce/customer/use-customer.tsx | 23 ++++--- framework/kibocommerce/schema.d.ts | 24 ++++++- framework/kibocommerce/types/customer.ts | 5 ++ framework/kibocommerce/types/logout.ts | 1 + 9 files changed, 139 insertions(+), 11 deletions(-) create mode 100644 framework/kibocommerce/api/endpoints/customer/customer.ts create mode 100644 framework/kibocommerce/api/queries/get-customer-account-query.ts create mode 100644 framework/kibocommerce/types/customer.ts create mode 100644 framework/kibocommerce/types/logout.ts diff --git a/framework/kibocommerce/api/endpoints/customer/customer.ts b/framework/kibocommerce/api/endpoints/customer/customer.ts new file mode 100644 index 000000000..97edb0947 --- /dev/null +++ b/framework/kibocommerce/api/endpoints/customer/customer.ts @@ -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 + +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 diff --git a/framework/kibocommerce/api/endpoints/customer/index.ts b/framework/kibocommerce/api/endpoints/customer/index.ts index 491bf0ac9..c32bcfa91 100644 --- a/framework/kibocommerce/api/endpoints/customer/index.ts +++ b/framework/kibocommerce/api/endpoints/customer/index.ts @@ -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 + +export type CustomerEndpoint = CustomerAPI['endpoint'] + +export const handlers: CustomerEndpoint['handlers'] = { getLoggedInCustomer } + +const customerApi = createEndpoint({ + handler: customerEndpoint, + handlers, +}) + +export default customerApi diff --git a/framework/kibocommerce/api/queries/get-customer-account-query.ts b/framework/kibocommerce/api/queries/get-customer-account-query.ts new file mode 100644 index 000000000..3d686ec38 --- /dev/null +++ b/framework/kibocommerce/api/queries/get-customer-account-query.ts @@ -0,0 +1,12 @@ +export const getCustomerAccountQuery = ` +query getUser { + customerAccount:getCurrentAccount { + id + firstName + lastName + emailAddress + userName + isAnonymous + } +} +` \ No newline at end of file diff --git a/framework/kibocommerce/auth/use-login.tsx b/framework/kibocommerce/auth/use-login.tsx index c0197e4c2..a23a444b5 100644 --- a/framework/kibocommerce/auth/use-login.tsx +++ b/framework/kibocommerce/auth/use-login.tsx @@ -31,7 +31,7 @@ export const handler: MutationHook = { return useCallback( async function login(input) { const data = await fetch({ input }) - // await revalidate() + await revalidate() return data }, [fetch, revalidate] diff --git a/framework/kibocommerce/auth/use-signup.tsx b/framework/kibocommerce/auth/use-signup.tsx index 28c144152..da06fd3eb 100644 --- a/framework/kibocommerce/auth/use-signup.tsx +++ b/framework/kibocommerce/auth/use-signup.tsx @@ -35,7 +35,7 @@ export const handler: MutationHook = { return useCallback( async function signup(input) { const data = await fetch({ input }) - // await revalidate() + await revalidate() return data }, [fetch, revalidate] diff --git a/framework/kibocommerce/customer/use-customer.tsx b/framework/kibocommerce/customer/use-customer.tsx index 41757cd0d..238b1229b 100644 --- a/framework/kibocommerce/customer/use-customer.tsx +++ b/framework/kibocommerce/customer/use-customer.tsx @@ -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 -export const handler: SWRHook = { + +export const handler: SWRHook = { 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, + }, + }) }, } diff --git a/framework/kibocommerce/schema.d.ts b/framework/kibocommerce/schema.d.ts index 430ce10b9..be8866857 100644 --- a/framework/kibocommerce/schema.d.ts +++ b/framework/kibocommerce/schema.d.ts @@ -11401,4 +11401,26 @@ export type WorkflowStateInput = { export type LoginMutationVariables = Exact<{ email: Scalars['String'] password: Scalars['String'] -}> \ No newline at end of file +}> + +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 + > + } + > +} \ No newline at end of file diff --git a/framework/kibocommerce/types/customer.ts b/framework/kibocommerce/types/customer.ts new file mode 100644 index 000000000..427bc0b03 --- /dev/null +++ b/framework/kibocommerce/types/customer.ts @@ -0,0 +1,5 @@ +import * as Core from '@commerce/types/customer' + +export * from '@commerce/types/customer' + +export type CustomerSchema = Core.CustomerSchema diff --git a/framework/kibocommerce/types/logout.ts b/framework/kibocommerce/types/logout.ts new file mode 100644 index 000000000..9f0a466af --- /dev/null +++ b/framework/kibocommerce/types/logout.ts @@ -0,0 +1 @@ +export * from '@commerce/types/logout'