fix create user

This commit is contained in:
2025-10-13 12:37:33 +07:00
parent 9b8cb9d752
commit 110c73ae9d

View File

@@ -42,31 +42,35 @@ class UserService {
try {
if (!data || typeof data !== 'object') data = {};
const creatorId = data.userId;
const creatorId = data.userId || null;
const existingEmail = await getUserByUserEmailDb(data.user_email);
const existingUsername = await getUserByUsernameDb(data.user_name);
// cek duplikasi username & email
const [existingUsername, existingEmail] = await Promise.all([
getUserByUsernameDb(data.user_name),
getUserByUserEmailDb(data.user_email)
]);
if (existingUsername) {
throw new ErrorHandler(400, 'Username is already taken');
}
if (existingEmail) {
throw new ErrorHandler(400, 'Email is already taken');
}
if (existingUsername) throw new ErrorHandler(400, 'Username is already taken');
if (existingEmail) throw new ErrorHandler(400, 'Email is already taken');
if (data.user_password) {
data.user_password = await hashPassword(data.user_password);
}
// hash password
const hashedPassword = await hashPassword(data.user_password);
data.is_approve = 1;
data.approved_by = creatorId;
data.created_by = creatorId;
data.updated_by = creatorId;
data.is_sa = 0;
data.is_active = 1;
delete data.userId;
// siapkan data untuk insert
const userData = {
...data,
user_password: hashedPassword,
is_approve: 1,
approved_by: creatorId,
created_by: creatorId,
updated_by: creatorId,
is_sa: 0,
is_active: 1
};
const result = await createUserDb(data);
delete userData.userId;
const result = await createUserDb(userData);
return result;
} catch (error) {
throw new ErrorHandler(error.statusCode || 500, error.message);