Skip to content

Commit

Permalink
fix: validate personal account
Browse files Browse the repository at this point in the history
  • Loading branch information
b3aton committed Feb 2, 2023
1 parent 765e651 commit fed8834
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 8 deletions.
15 changes: 10 additions & 5 deletions apps/storefront/src/pages/registered/RegisteredAccount.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import RegisteredStepButton from './component/RegisteredStepButton'

import {
checkUserEmail,
checkUserBCEmail,
} from '@/shared/service/b2b'

import {
Expand Down Expand Up @@ -117,19 +118,23 @@ export default function RegisteredAccount(props: RegisteredAccountProps) {
const emailName = contactInformation?.find((item: CustomFieldItems) => item.fieldId === 'field_email')?.name || 'email'

const validateEmailValue = async (emailValue: string) => {
const isB2BUser = accountType === '1'
const fn = isB2BUser ? checkUserEmail : checkUserBCEmail
const key = isB2BUser ? 'userEmailCheck' : 'customerEmailCheck'

const {
userEmailCheck: {
[key]: {
userType,
userInfo: {
companyName,
},
companyName = '',
} = {},
},
}: CustomFieldItems = await checkUserEmail({
}: CustomFieldItems = await fn({
email: emailValue,
channelId: currentChannelId,
})

const isValid = [1].includes(userType)
const isValid = isB2BUser ? [1].includes(userType) : ![2].includes(userType)

if (!isValid) {
setErrorTips(b3Lang(emailError[userType], {
Expand Down
17 changes: 15 additions & 2 deletions apps/storefront/src/shared/service/b2b/graphql/users.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ const deleteUsersQl = (data: CustomFieldItems) => `mutation{
}
}`

const checkUserBCEmail = (data: CustomFieldItems) => `{
const checkUserB2BEmail = (data: CustomFieldItems) => `{
userEmailCheck (
email: "${data.email}"
companyId: ${data.companyId || null}
Expand All @@ -87,6 +87,16 @@ const checkUserBCEmail = (data: CustomFieldItems) => `{
}
}`

const checkCustomerBCEmail = (data: CustomFieldItems) => `{
customerEmailCheck (
email: "${data.email}"
storeHash: "${storeHash}"
channelId: ${data.channelId || null}
){
userType,
}
}`

export const getUsers = (data: CustomFieldItems): CustomFieldItems => B3Request.graphqlB2B({
query: getUsersQl(data),
})
Expand All @@ -100,5 +110,8 @@ export const deleteUsers = (data: CustomFieldItems): CustomFieldItems => B3Reque
})

export const checkUserEmail = (data: CustomFieldItems): CustomFieldItems => B3Request.graphqlB2B({
query: checkUserBCEmail(data),
query: checkUserB2BEmail(data),
})
export const checkUserBCEmail = (data: CustomFieldItems): CustomFieldItems => B3Request.graphqlB2B({
query: checkCustomerBCEmail(data),
})
2 changes: 2 additions & 0 deletions apps/storefront/src/shared/service/b2b/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ import {
addOrUpdateUsers,
deleteUsers,
checkUserEmail,
checkUserBCEmail,
} from './graphql/users'

import {
Expand Down Expand Up @@ -147,4 +148,5 @@ export {
updateB2BShoppingListsItem,
deleteB2BShoppingListItem,
checkUserEmail,
checkUserBCEmail,
}
4 changes: 3 additions & 1 deletion apps/storefront/src/shared/service/request/fetch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@ function b3Fetch(path: string, init: any, type?: string, customMessage = false)
return new Promise((resolve, reject) => {
originFetch(path, init).then((res: Response) => (path.includes('current.jwt') ? res.text() : res.json())).then(async (res) => {
if (res?.code === 500) {
reject(res.message)
const data = res?.data || {}
const message = data.errMsg || res.message || ''
reject(message)
return
}
// jwt 15 minutes expected
Expand Down

0 comments on commit fed8834

Please sign in to comment.