add INN registration field
This commit is contained in:
+49
-29
@@ -3,6 +3,7 @@
|
||||
import { loginFormSchema, API_BASE_URL, getSignupFormSchema } from '@/app/actions/definitions';
|
||||
import { createSession, deleteSession, getSessionData, updateSession } from '@/app/actions/session';
|
||||
import { redirect } from 'next/navigation';
|
||||
import { fetchINN } from '@/app/actions/action';
|
||||
|
||||
export async function logout() {
|
||||
const token = await getSessionData('token');
|
||||
@@ -130,15 +131,16 @@ export async function authorization(
|
||||
|
||||
export type FormState = {
|
||||
previousState: {
|
||||
fullName: string;
|
||||
email: string;
|
||||
password: string;
|
||||
confirm_password: string;
|
||||
phone: string;
|
||||
companyName?: string;
|
||||
agree: string;
|
||||
referralCode?: string
|
||||
},
|
||||
fullName?: string | undefined;
|
||||
email?: string | undefined;
|
||||
password?: string | undefined;
|
||||
confirm_password?: string | undefined;
|
||||
phone?: string | undefined;
|
||||
companyName?: string | undefined;
|
||||
agree?: string | undefined;
|
||||
referralCode?: string | undefined;
|
||||
inn?: string | undefined;
|
||||
} | undefined,
|
||||
mailConfirm?: boolean,
|
||||
error: Record<string, string> | null
|
||||
};
|
||||
@@ -157,6 +159,7 @@ export async function registration(
|
||||
const companyName = formData.get('companyName') as string || '';
|
||||
const agree = formData.get('agree') as string || '';
|
||||
const referralCode = formData.get('referralCode');
|
||||
const inn = formData.get('inn') as string || '';
|
||||
|
||||
const SignupFormSchema = await getSignupFormSchema(accountType);
|
||||
|
||||
@@ -167,25 +170,41 @@ export async function registration(
|
||||
password,
|
||||
confirm_password,
|
||||
agree,
|
||||
...(accountType === 'b2b' && { companyName })
|
||||
...(accountType === 'b2b' && { companyName, inn })
|
||||
};
|
||||
|
||||
const validatedFields = await SignupFormSchema.safeParse(dataToValidate);
|
||||
|
||||
console.log(`try to register ${accountType}`);
|
||||
|
||||
if (!validatedFields.success) {
|
||||
let confirmedCompanyName: boolean = true;
|
||||
if (accountType === 'b2b') {
|
||||
try {
|
||||
const response = await fetchINN(inn);
|
||||
confirmedCompanyName = response?.companyName ? true : false;
|
||||
} catch (error) {
|
||||
confirmedCompanyName = false;
|
||||
}
|
||||
}
|
||||
|
||||
if (!validatedFields.success || !confirmedCompanyName) {
|
||||
const errors: Record<string, string> = {}
|
||||
JSON.parse(validatedFields.error.message).forEach((obj: {
|
||||
message: string,
|
||||
path: string
|
||||
}) => {
|
||||
if (errors[obj.path[0]]) {
|
||||
errors[obj.path[0]] = `${errors[obj.path[0]]}&${obj.message}`;
|
||||
} else {
|
||||
errors[obj.path[0]] = obj.message;
|
||||
}
|
||||
});
|
||||
if (validatedFields?.error?.message) {
|
||||
JSON?.parse(validatedFields?.error?.message).forEach((obj: {
|
||||
message: string,
|
||||
path: string
|
||||
}) => {
|
||||
if (errors[obj.path[0]]) {
|
||||
errors[obj.path[0]] = `${errors[obj.path[0]]}&${obj.message}`;
|
||||
} else {
|
||||
errors[obj.path[0]] = obj.message;
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
if (!confirmedCompanyName) {
|
||||
errors.inn = 'inn-invalid'
|
||||
}
|
||||
|
||||
return {
|
||||
previousState: {
|
||||
@@ -195,7 +214,7 @@ export async function registration(
|
||||
confirm_password,
|
||||
phone,
|
||||
agree,
|
||||
...(accountType === 'b2b' && { companyName })
|
||||
...(accountType === 'b2b' && { companyName, inn })
|
||||
},
|
||||
error: errors
|
||||
};
|
||||
@@ -214,7 +233,7 @@ export async function registration(
|
||||
phone: phone,
|
||||
password,
|
||||
accountType,
|
||||
...(accountType === 'b2b' && { companyName: companyName }),
|
||||
...(accountType === 'b2b' && { companyName: companyName, inn: inn }),
|
||||
...(referralCode !== '' && { referralLink: referralCode })
|
||||
}
|
||||
}),
|
||||
@@ -230,7 +249,7 @@ export async function registration(
|
||||
phone: phone,
|
||||
password,
|
||||
accountType,
|
||||
...(accountType === 'b2b' && { companyName: companyName }),
|
||||
...(accountType === 'b2b' && { companyName: companyName, inn: inn }),
|
||||
...(referralCode !== '' && { referralLink: referralCode })
|
||||
})
|
||||
|
||||
@@ -241,8 +260,6 @@ export async function registration(
|
||||
message_code: number
|
||||
};
|
||||
|
||||
console.log(parsed);
|
||||
|
||||
if (parsed.message_code === 0) {
|
||||
console.log(`registration - ${accountType}`);
|
||||
console.log({
|
||||
@@ -252,10 +269,11 @@ export async function registration(
|
||||
password,
|
||||
accountType,
|
||||
referralLink: 'fds324',
|
||||
...(accountType === 'b2b' && { companyName: companyName })
|
||||
...(accountType === 'b2b' && { companyName: companyName, inn: inn })
|
||||
});
|
||||
await createSession(parsed.message_body.token, email as string);
|
||||
} else {
|
||||
console.log(parsed);
|
||||
throw parsed;
|
||||
}
|
||||
} else {
|
||||
@@ -320,7 +338,8 @@ export async function registration(
|
||||
confirm_password,
|
||||
phone,
|
||||
companyName,
|
||||
agree
|
||||
agree,
|
||||
inn
|
||||
},
|
||||
error: errors
|
||||
};
|
||||
@@ -334,7 +353,8 @@ export async function registration(
|
||||
confirm_password,
|
||||
phone,
|
||||
companyName,
|
||||
agree
|
||||
agree,
|
||||
inn
|
||||
},
|
||||
error: errors
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user