forked from crowetic/commerce
Allow the API to create customers
This commit is contained in:
parent
8212cec939
commit
20c08fcbb4
@ -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<T> = Partial<T> | undefined
|
|
||||||
|
|
||||||
export type Customer = any
|
|
||||||
|
|
||||||
export type AddCustomerBody = { item: any }
|
|
||||||
|
|
||||||
export type CartHandlers = {
|
|
||||||
addItem: BigcommerceHandler<Customer, { cartId?: string } & Body<any>>
|
|
||||||
}
|
|
||||||
|
|
||||||
const METHODS = ['POST']
|
|
||||||
|
|
||||||
const customersApi: BigcommerceApiHandler<Customer> = 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<Customer> = ({
|
|
||||||
req,
|
|
||||||
res,
|
|
||||||
body,
|
|
||||||
config,
|
|
||||||
}) => {}
|
|
||||||
|
|
||||||
const handlers = {
|
|
||||||
createCustomer,
|
|
||||||
}
|
|
||||||
|
|
||||||
export default createApiHandler(customersApi, handlers, {})
|
|
36
lib/bigcommerce/api/customers/handlers/create-customer.ts
Normal file
36
lib/bigcommerce/api/customers/handlers/create-customer.ts
Normal file
@ -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
|
59
lib/bigcommerce/api/customers/index.ts
Normal file
59
lib/bigcommerce/api/customers/index.ts
Normal file
@ -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<T> = Partial<T> | 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<CreateCustomerBody>
|
||||||
|
>
|
||||||
|
}
|
||||||
|
|
||||||
|
const METHODS = ['POST']
|
||||||
|
|
||||||
|
const customersApi: BigcommerceApiHandler<Customer, CustomersHandlers> = 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, {})
|
3
pages/api/bigcommerce/customers.ts
Normal file
3
pages/api/bigcommerce/customers.ts
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
import customersApi from '@lib/bigcommerce/api/customers'
|
||||||
|
|
||||||
|
export default customersApi()
|
Loading…
x
Reference in New Issue
Block a user