Merge branch 'main' into NCFRONT-72

This commit is contained in:
smanylov
2026-02-17 18:20:50 +07:00
107 changed files with 7351 additions and 1431 deletions
+132 -59
View File
@@ -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');
@@ -11,9 +12,9 @@ export async function logout() {
await fetch(`${API_BASE_URL}/v1/api/auth/logout`, {
method: 'POST',
headers: {
"Content-Type": "application/json",
"Accept": "application/json",
"Authorization": `Bearer ${token}`,
'Content-Type': 'application/json',
'Accept': 'application/json',
'Authorization': `Bearer ${token}`,
}
});
} catch (error) {
@@ -36,8 +37,8 @@ export async function authorization(
const password = formData.get('password');
/* для теста */
if (email === "test" && password === "test") {
await createSession("1111", "test");
if (email === 'test' && password === 'test') {
await createSession('1111', 'test');
redirect('/pages/dashboard');
}
/* для теста */
@@ -81,13 +82,13 @@ export async function authorization(
}
}),
headers: {
"Content-Type": "application/json",
"Accept": "application/json"
'Content-Type': 'application/json',
'Accept': 'application/json'
}
});
if (response.ok) {
let parsed = await response.json();
if (parsed.message_desc === 'Operation successful') {
if (parsed.message_code === 0) {
await createSession(parsed.message_body.token, email);
} else {
throw (`${parsed.message_code}`);
@@ -130,14 +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;
},
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,
userId?: number,
error: Record<string, string> | null
@@ -147,6 +150,8 @@ export async function registration(
state: FormState | undefined,
formData: FormData,
): Promise<FormState> {
const accountType = formData.get('accountType') as 'b2b' | 'b2c';
const fullName = formData.get('fullName') as string || '';
const email = formData.get('email') as string || '';
const password = formData.get('password') as string || '';
@@ -154,8 +159,10 @@ export async function registration(
const phone = formData.get('phone') as string || '';
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();
const SignupFormSchema = await getSignupFormSchema(accountType);
const dataToValidate = {
fullName,
@@ -164,23 +171,41 @@ export async function registration(
password,
confirm_password,
agree,
companyName
...(accountType === 'b2b' && { companyName, inn })
};
const validatedFields = SignupFormSchema.safeParse(dataToValidate);
const validatedFields = await SignupFormSchema.safeParse(dataToValidate);
if (!validatedFields.success) {
console.log(`try to register ${accountType}`);
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: {
@@ -189,8 +214,8 @@ export async function registration(
password,
confirm_password,
phone,
companyName,
agree
agree,
...(accountType === 'b2b' && { companyName, inn })
},
error: errors
};
@@ -208,25 +233,47 @@ export async function registration(
email,
phone: phone,
password,
companyName: companyName,
accountType,
...(accountType === 'b2b' && { companyName: companyName, inn: inn }),
...(referralCode !== '' && { referralLink: referralCode })
}
}),
headers: {
"Content-Type": "application/json",
"Accept": "application/json"
'Content-Type': 'application/json',
'Accept': 'application/json'
}
});
console.log({
fullName: fullName,
email,
phone: phone,
password,
accountType,
...(accountType === 'b2b' && { companyName: companyName, inn: inn }),
...(referralCode !== '' && { referralLink: referralCode })
})
if (response.ok) {
let parsed = await response.json() as {
message_desc: string,
message_body: { token: string, userId: number },
message_code: string
message_code: number
};
console.log(parsed);
if (parsed.message_desc === 'Operation successful') {
if (parsed.message_code === 0) {
console.log(`registration - ${accountType}`);
console.log({
fullName: fullName,
email,
phone: phone,
password,
accountType,
...(referralCode !== '' && { referralLink: referralCode }),
...(accountType === 'b2b' && { companyName: companyName, inn: inn })
});
/* await createSession(parsed.message_body.token, email as string); */
return {
previousState: {
fullName,
@@ -234,14 +281,17 @@ export async function registration(
password,
confirm_password,
phone,
companyName,
agree
agree,
...(referralCode !== '' && { referralLink: referralCode }),
...(accountType === 'b2b' && { companyName: companyName, inn: inn })
},
mailConfirm: true,
userId: parsed.message_body.userId,
error: null
};
} else {
console.log(parsed);
throw parsed;
}
} else {
@@ -249,26 +299,35 @@ export async function registration(
}
} catch (error: unknown) {
const errors: Record<string, string> = {};
if (error) {
const typedError = error as any;
const errors: Record<string, string> = {};
switch (typedError.message_code) {
case 1:
errors['server'] = 'email-or-already-registered'
break;
if (typedError.message_desc === 'Refferal link is not exist') {
errors['server'] = 'referral-link-is-not-exist'
break;
} else {
errors['server'] = 'email-or-already-registered'
break;
}
case 2:
typedError.message_body.fieldErrors.forEach((obj: {
code: string,
field: string
}) => {
if (errors[obj.field]) {
errors[obj.field] = `${errors[obj.field]}&${obj.code.replaceAll('.', '-')}`;
} else {
errors[obj.field] = obj.code.replaceAll('.', '-');
}
});
if (typedError.message_body?.fieldErrors) {
typedError.message_body?.fieldErrors?.forEach((obj: {
code: string,
field: string
}) => {
if (errors[obj.field]) {
errors[obj.field] = `${errors[obj.field]}&${obj.code.replaceAll('.', '-')}`;
} else {
errors[obj.field] = obj.code.replaceAll('.', '-');
}
});
} else {
errors['server'] = 'unknown-error'
}
break;
default:
@@ -284,14 +343,27 @@ export async function registration(
confirm_password,
phone,
companyName,
agree
agree,
inn
},
mailConfirm: false,
error: errors
};
}
throw error;
return {
previousState: {
fullName,
email,
password,
confirm_password,
phone,
companyName,
agree,
inn
},
error: errors
};
}
redirect('/pages/dashboard');
@@ -306,8 +378,8 @@ export async function tokenLifeExtension() {
const response = await fetch(`${API_BASE_URL}/api/v1/data`, {
method: 'POST',
headers: {
"Content-Type": "application/json",
"Accept": "application/json",
'Content-Type': 'application/json',
'Accept': 'application/json',
},
body: JSON.stringify({
version: 1,
@@ -331,11 +403,12 @@ export async function tokenLifeExtension() {
}
export async function confirmEmail(
state: {error: string} | undefined,
state: { error: string } | undefined,
formData: FormData
) {
console.log('confirmEmail');
const emailConfirm = formData.get('emailConfirm') as string || '';
console.log(emailConfirm);
const userId = formData.get('userId') as string || '';
try {