Added op to get the logged in customer

This commit is contained in:
Luis Alvarez 2020-10-21 09:53:19 -05:00
parent 377015ceac
commit 2aae613f1b
5 changed files with 83 additions and 95 deletions

View File

@ -8,7 +8,7 @@
},
"documents": [
{
"./lib/bigcommerce/api/{operations,fragments}/**/*.ts": {
"./lib/bigcommerce/api/**/*.ts": {
"noRequire": true
}
}

View File

@ -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

View File

@ -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>(
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

View File

@ -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<T> = Partial<T> | 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<CreateCustomerBody>
>
getLoggedInCustomer: BigcommerceHandler<CustomerData, null>
}
const METHODS = ['POST']
const METHODS = ['GET']
const customersApi: BigcommerceApiHandler<Customer, CustomersHandlers> = 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<Customer, CustomersHandlers> = async (
}
}
const handlers = { createCustomer }
const handlers = { getLoggedInCustomer }
export default createApiHandler(customersApi, handlers, {})

View File

@ -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<Money, 'value' | 'currencyCode'>
>
}
>
}
export type CategoryTreeItemFragment = {
__typename?: 'CategoryTreeItem'
} & Pick<