diff --git a/lib/bigcommerce/api/customers.ts b/lib/bigcommerce/api/customers.ts deleted file mode 100644 index c4734c6e5..000000000 --- a/lib/bigcommerce/api/customers.ts +++ /dev/null @@ -1,56 +0,0 @@ -import createApiHandler, { - BigcommerceApiHandler, - BigcommerceHandler, -} from './utils/create-api-handler' -import isAllowedMethod from './utils/is-allowed-method' -import { BigcommerceApiError } from './utils/errors' - -type Body = Partial | undefined - -export type Customer = any - -export type AddCustomerBody = { item: any } - -export type CartHandlers = { - addItem: BigcommerceHandler> -} - -const METHODS = ['POST'] - -const customersApi: BigcommerceApiHandler = async ( - req, - res, - config -) => { - if (!isAllowedMethod(req, res, METHODS)) return - - try { - if (req.method === 'POST') { - // let result = {} as any - // const - // result = await config.storeApiFetch('/v3/customers') - } - } catch (error) { - console.error(error) - - const message = - error instanceof BigcommerceApiError - ? 'An unexpected error ocurred with the Bigcommerce API' - : 'An unexpected error ocurred' - - res.status(500).json({ data: null, errors: [{ message }] }) - } -} - -const createCustomer: BigcommerceHandler = ({ - req, - res, - body, - config, -}) => {} - -const handlers = { - createCustomer, -} - -export default createApiHandler(customersApi, handlers, {}) diff --git a/lib/bigcommerce/api/customers/handlers/create-customer.ts b/lib/bigcommerce/api/customers/handlers/create-customer.ts new file mode 100644 index 000000000..73f3738cd --- /dev/null +++ b/lib/bigcommerce/api/customers/handlers/create-customer.ts @@ -0,0 +1,36 @@ +import { 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. + // Passwords must be at least 7 characters and contain both alphabetic + // and numeric characters. + + const { data } = await config.storeApiFetch('/v3/customers', { + method: 'POST', + body: JSON.stringify([ + { + first_name: firstName, + last_name: lastName, + email, + authentication: { + new_password: password, + }, + }, + ]), + }) + + res.status(200).json({ data }) +} + +export default createCustomer diff --git a/lib/bigcommerce/api/customers/index.ts b/lib/bigcommerce/api/customers/index.ts new file mode 100644 index 000000000..25b2fd270 --- /dev/null +++ b/lib/bigcommerce/api/customers/index.ts @@ -0,0 +1,59 @@ +import createApiHandler, { + BigcommerceApiHandler, + BigcommerceHandler, +} from '../utils/create-api-handler' +import isAllowedMethod from '../utils/is-allowed-method' +import { BigcommerceApiError } from '../utils/errors' +import createCustomer from './handlers/create-customer' + +type Body = Partial | undefined + +export type Customer = any + +export type CreateCustomerBody = { + firstName: string + lastName: string + email: string + password: string +} + +export type CustomersHandlers = { + createCustomer: BigcommerceHandler< + Customer, + { cartId?: string } & Body + > +} + +const METHODS = ['POST'] + +const customersApi: BigcommerceApiHandler = async ( + req, + res, + config +) => { + if (!isAllowedMethod(req, res, METHODS)) return + + const { cookies } = req + const cartId = cookies[config.cartCookie] + + try { + if (req.method === 'POST') { + console.log('BODY', req.body) + const body = { cartId, ...req.body } + return await handlers['createCustomer']({ req, res, config, body }) + } + } catch (error) { + console.error(error) + + const message = + error instanceof BigcommerceApiError + ? 'An unexpected error ocurred with the Bigcommerce API' + : 'An unexpected error ocurred' + + res.status(500).json({ data: null, errors: [{ message }] }) + } +} + +const handlers = { createCustomer } + +export default createApiHandler(customersApi, handlers, {}) diff --git a/pages/api/bigcommerce/customers.ts b/pages/api/bigcommerce/customers.ts new file mode 100644 index 000000000..67e342604 --- /dev/null +++ b/pages/api/bigcommerce/customers.ts @@ -0,0 +1,3 @@ +import customersApi from '@lib/bigcommerce/api/customers' + +export default customersApi()