forked from crowetic/commerce
Handle validation for duplicated emails
This commit is contained in:
parent
9257b44c0d
commit
0922c87621
@ -1,3 +1,4 @@
|
|||||||
|
import { BigcommerceApiError } from '../../utils/errors'
|
||||||
import { CustomersHandlers } from '..'
|
import { CustomersHandlers } from '..'
|
||||||
|
|
||||||
const createCustomer: CustomersHandlers['createCustomer'] = async ({
|
const createCustomer: CustomersHandlers['createCustomer'] = async ({
|
||||||
@ -12,25 +13,46 @@ const createCustomer: CustomersHandlers['createCustomer'] = async ({
|
|||||||
errors: [{ message: 'Invalid request' }],
|
errors: [{ message: 'Invalid request' }],
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
// TODO: validate the password.
|
// TODO: validate the password and email
|
||||||
// Passwords must be at least 7 characters and contain both alphabetic
|
// Passwords must be at least 7 characters and contain both alphabetic
|
||||||
// and numeric characters.
|
// and numeric characters.
|
||||||
|
|
||||||
const { data } = await config.storeApiFetch('/v3/customers', {
|
try {
|
||||||
method: 'POST',
|
const { data } = await config.storeApiFetch('/v3/customers', {
|
||||||
body: JSON.stringify([
|
method: 'POST',
|
||||||
{
|
body: JSON.stringify([
|
||||||
first_name: firstName,
|
{
|
||||||
last_name: lastName,
|
first_name: firstName,
|
||||||
email,
|
last_name: lastName,
|
||||||
authentication: {
|
email,
|
||||||
new_password: password,
|
authentication: {
|
||||||
|
new_password: password,
|
||||||
|
},
|
||||||
},
|
},
|
||||||
},
|
]),
|
||||||
]),
|
})
|
||||||
})
|
|
||||||
|
|
||||||
res.status(200).json({ data })
|
res.status(200).json({ data })
|
||||||
|
} 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
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export default createCustomer
|
export default createCustomer
|
||||||
|
@ -27,7 +27,7 @@ export type BigcommerceHandlers<T = any> = {
|
|||||||
|
|
||||||
export type BigcommerceApiResponse<T> = {
|
export type BigcommerceApiResponse<T> = {
|
||||||
data: T | null
|
data: T | null
|
||||||
errors?: { message: string }[]
|
errors?: { message: string; code?: string }[]
|
||||||
}
|
}
|
||||||
|
|
||||||
export default function createApiHandler<
|
export default function createApiHandler<
|
||||||
|
Loading…
x
Reference in New Issue
Block a user