212 lines
4.5 KiB
TypeScript
212 lines
4.5 KiB
TypeScript
'use server'
|
|
|
|
import { API_BASE_URL, getSignupFormSchema } from '@/app/actions/definitions';
|
|
import { getSessionData } from '@/app/actions/session';
|
|
import { headers } from 'next/headers';
|
|
|
|
export type CompanyUserRegistrationFormState = {
|
|
previousState: {
|
|
fullName: string;
|
|
email: string;
|
|
password: string;
|
|
confirm_password: string;
|
|
phone: string;
|
|
},
|
|
createdUser: string | null
|
|
error: Record<string, string> | null
|
|
} | undefined;
|
|
|
|
export async function companyUserRegistration(
|
|
state: CompanyUserRegistrationFormState | undefined,
|
|
formData: FormData,
|
|
): Promise<CompanyUserRegistrationFormState> {
|
|
const token = await getSessionData('token');
|
|
if (!token) {
|
|
return;
|
|
}
|
|
|
|
const headersList = await headers();
|
|
|
|
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 forwardedFor = headersList.get('x-forwarded-for');
|
|
const realIp = headersList.get('x-real-ip');
|
|
const ip = forwardedFor?.split(',')[0] || realIp || 'unknown';
|
|
const userAgent = headersList.get('user-agent') || 'unknown';
|
|
|
|
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('registration error');
|
|
console.log(errors);
|
|
|
|
return {
|
|
previousState: {
|
|
fullName,
|
|
email,
|
|
password,
|
|
confirm_password,
|
|
phone
|
|
},
|
|
createdUser: null,
|
|
error: errors
|
|
};
|
|
}
|
|
|
|
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,
|
|
accountType: 'b2b',
|
|
authToken: token,
|
|
mail_verified: false,
|
|
ip: ip,
|
|
user_agent: userAgent,
|
|
}
|
|
}),
|
|
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
|
|
};
|
|
console.log({
|
|
fullName: fullName,
|
|
email,
|
|
phone: phone,
|
|
password,
|
|
accountType: 'b2b',
|
|
authToken: token
|
|
});
|
|
|
|
if (parsed.message_code === 0) {
|
|
console.log(`registration - ${email}`);
|
|
console.log({
|
|
fullName: fullName,
|
|
email,
|
|
phone: phone,
|
|
password,
|
|
ip: ip,
|
|
user_agent: userAgent,
|
|
});
|
|
|
|
return {
|
|
previousState: {
|
|
fullName: '',
|
|
email: '',
|
|
password: '',
|
|
confirm_password: '',
|
|
phone: ''
|
|
},
|
|
createdUser: fullName,
|
|
error: null
|
|
}
|
|
} 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
|
|
},
|
|
createdUser: null,
|
|
error: errors
|
|
};
|
|
}
|
|
|
|
return {
|
|
previousState: {
|
|
fullName,
|
|
email,
|
|
password,
|
|
confirm_password,
|
|
phone
|
|
},
|
|
createdUser: null,
|
|
error: errors
|
|
};
|
|
}
|
|
} |