add company user registration, update phone validation

This commit is contained in:
smanylov
2026-02-05 14:53:43 +07:00
parent c7a0131624
commit c24a4febc9
6 changed files with 440 additions and 32 deletions
+180
View File
@@ -0,0 +1,180 @@
'use server'
import { API_BASE_URL, getSignupFormSchema } from '@/app/actions/definitions';
import { createSession } from '@/app/actions/session';
import { redirect } from 'next/navigation';
export type CompanyUserRegistrationFormState = {
previousState: {
fullName: string;
email: string;
password: string;
confirm_password: string;
phone: string;
},
error: Record<string, string> | null
} | undefined;
export async function companyUserRegistration(
state: CompanyUserRegistrationFormState | undefined,
formData: FormData,
): Promise<CompanyUserRegistrationFormState> {
const fullName = formData.get('fullName') as string || '';
const email = formData.get('email') as string || '';
const password = formData.get('password') as string || '';
const confirm_password = formData.get('confirm_password') as string || '';
const phone = formData.get('phone') as string || '';
const SignupFormSchema = await getSignupFormSchema('b2c');
const dataToValidate = {
fullName,
email,
phone: phone.replace(/[-\(\)\s]/g, ''),
password,
confirm_password,
agree: 'on'
};
const validatedFields = SignupFormSchema.safeParse(dataToValidate);
console.log(`try to register company users ${email}`);
if (!validatedFields.success) {
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;
}
});
console.log(errors);
return {
previousState: {
fullName,
email,
password,
confirm_password,
phone
},
error: errors
};
}
return {
previousState: {
fullName,
email,
password,
confirm_password,
phone
},
error: null
};
try {
const { fullName, email, password, phone } = validatedFields.data;
const response = await fetch(`${API_BASE_URL}/api/v1/data`, {
method: 'POST',
body: JSON.stringify({
version: 1,
msg_id: 20002,
message_body: {
fullName: fullName,
email,
phone: phone,
password
}
}),
headers: {
'Content-Type': 'application/json',
'Accept': 'application/json'
}
});
if (response.ok) {
let parsed = await response.json() as {
message_desc: string,
message_body: { token: string },
message_code: number
};
if (parsed.message_code === 0) {
console.log(`registration - ${email}`);
console.log({
fullName: fullName,
email,
phone: phone,
password
});
await createSession(parsed.message_body.token, email as string);
} else {
throw parsed;
}
} else {
throw (`${response.status}`);
}
} catch (error: unknown) {
const errors: Record<string, string> = {};
if (error) {
const typedError = error as any;
switch (typedError.message_code) {
case 1:
errors['server'] = 'email-or-already-registered'
break;
case 2:
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:
errors['server'] = 'request-ended-with-an-error'
break;
}
return {
previousState: {
fullName,
email,
password,
confirm_password,
phone
},
error: errors
};
}
return {
previousState: {
fullName,
email,
password,
confirm_password,
phone
},
error: errors
};
}
}