diff --git a/codegen.json b/codegen.json index cce0f4f8b..e08292091 100644 --- a/codegen.json +++ b/codegen.json @@ -8,7 +8,7 @@ }, "documents": [ { - "./lib/bigcommerce/api/{operations,fragments}/**/*.ts": { + "./lib/bigcommerce/api/**/*.ts": { "noRequire": true } } diff --git a/lib/bigcommerce/api/customers/handlers/create-customer.ts b/lib/bigcommerce/api/customers/handlers/create-customer.ts deleted file mode 100644 index 4ccbf31b4..000000000 --- a/lib/bigcommerce/api/customers/handlers/create-customer.ts +++ /dev/null @@ -1,68 +0,0 @@ -import { BigcommerceApiError } from '../../utils/errors' -import login from '../../operations/login' -import type { CustomersHandlers } from '..' - -const createCustomer: CustomersHandlers['createCustomer'] = async ({ - res, - body: { firstName, lastName, email, password }, - config, -}) => { - // TODO: Add proper validations with something like Ajv - if (!(firstName && lastName && email && password)) { - return res.status(400).json({ - data: null, - errors: [{ message: 'Invalid request' }], - }) - } - // TODO: validate the password and email - // Passwords must be at least 7 characters and contain both alphabetic - // and numeric characters. - - let result: { data?: any } = {} - - try { - result = await config.storeApiFetch('/v3/customers', { - method: 'POST', - body: JSON.stringify([ - { - first_name: firstName, - last_name: lastName, - email, - authentication: { - new_password: password, - }, - }, - ]), - }) - } catch (error) { - if (error instanceof BigcommerceApiError && error.status === 422) { - const hasEmailError = '0.email' in error.data?.errors - - // If there's an error with the email, it most likely means it's duplicated - if (hasEmailError) { - return res.status(400).json({ - data: null, - errors: [ - { - message: 'The email is already in use', - code: 'duplicated_email', - }, - ], - }) - } - } - - throw error - } - - console.log('DATA', result.data) - - // TODO: Currently not working, fix this asap. - const loginData = await login({ variables: { email, password }, config }) - - console.log('LOGIN DATA', loginData) - - res.status(200).json({ data: result.data ?? null }) -} - -export default createCustomer diff --git a/lib/bigcommerce/api/customers/handlers/get-logged-in-customer.ts b/lib/bigcommerce/api/customers/handlers/get-logged-in-customer.ts new file mode 100644 index 000000000..99e9191f5 --- /dev/null +++ b/lib/bigcommerce/api/customers/handlers/get-logged-in-customer.ts @@ -0,0 +1,44 @@ +import { GetLoggedInCustomerQuery } from '@lib/bigcommerce/schema' +import type { CustomersHandlers } from '..' + +export const getLoggedInCustomerQuery = /* GraphQL */ ` + query getLoggedInCustomer { + customer { + entityId + firstName + lastName + email + company + customerGroupId + notes + phone + addressCount + attributeCount + storeCredit { + value + currencyCode + } + } + } +` + +const getLoggedInCustomer: CustomersHandlers['getLoggedInCustomer'] = async ({ + res, + config, +}) => { + const data = await config.fetch( + getLoggedInCustomerQuery + ) + const { customer } = data + + if (!customer) { + return res.status(400).json({ + data: null, + errors: [{ message: 'Customer not found', code: 'not_found' }], + }) + } + + res.status(200).json({ data: { customer } }) +} + +export default getLoggedInCustomer diff --git a/lib/bigcommerce/api/customers/index.ts b/lib/bigcommerce/api/customers/index.ts index a626ef2c7..228e882e7 100644 --- a/lib/bigcommerce/api/customers/index.ts +++ b/lib/bigcommerce/api/customers/index.ts @@ -4,42 +4,30 @@ import createApiHandler, { } from '../utils/create-api-handler' import isAllowedMethod from '../utils/is-allowed-method' import { BigcommerceApiError } from '../utils/errors' -import createCustomer from './handlers/create-customer' +import getLoggedInCustomer from './handlers/get-logged-in-customer' -type Body = Partial | undefined +export type Customer = any -export type Customer = null - -export type CreateCustomerBody = { - firstName: string - lastName: string - email: string - password: string +export type CustomerData = { + customer: Customer } export type CustomersHandlers = { - createCustomer: BigcommerceHandler< - Customer, - { cartId?: string } & Body - > + getLoggedInCustomer: BigcommerceHandler } -const METHODS = ['POST'] +const METHODS = ['GET'] -const customersApi: BigcommerceApiHandler = async ( - req, - res, - config -) => { +const customersApi: BigcommerceApiHandler< + CustomerData, + CustomersHandlers +> = async (req, res, config, handlers) => { if (!isAllowedMethod(req, res, METHODS)) return - const { cookies } = req - const cartId = cookies[config.cartCookie] - try { - if (req.method === 'POST') { - const body = { ...req.body, cartId } - return await handlers['createCustomer']({ req, res, config, body }) + if (req.method === 'GET') { + const body = null + return await handlers['getLoggedInCustomer']({ req, res, config, body }) } } catch (error) { console.error(error) @@ -53,6 +41,6 @@ const customersApi: BigcommerceApiHandler = async ( } } -const handlers = { createCustomer } +const handlers = { getLoggedInCustomer } export default createApiHandler(customersApi, handlers, {}) diff --git a/lib/bigcommerce/schema.d.ts b/lib/bigcommerce/schema.d.ts index eae1c8187..361d45a30 100644 --- a/lib/bigcommerce/schema.d.ts +++ b/lib/bigcommerce/schema.d.ts @@ -1653,6 +1653,30 @@ export enum CurrencyCode { Zwr = 'ZWR', } +export type GetLoggedInCustomerQueryVariables = Exact<{ [key: string]: never }> + +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 + > + } + > +} + export type CategoryTreeItemFragment = { __typename?: 'CategoryTreeItem' } & Pick<