Spaces:
Runtime error
Runtime error
Commit ·
3d832f6
1
Parent(s): 099ffb7
create controller for creating user
Browse files
src/controllers/user/create.controller.ts
ADDED
|
@@ -0,0 +1,42 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import { Request, Response } from 'express';
|
| 2 |
+
import User from '../../models/users';
|
| 3 |
+
import { UserInterface } from 'shared/interfaces/user.interface';
|
| 4 |
+
import { hashPassword } from '../../utils/passwordUtils';
|
| 5 |
+
|
| 6 |
+
export const createUser = async (req: Request, res: Response) => {
|
| 7 |
+
|
| 8 |
+
try {
|
| 9 |
+
const { name, email, role_id, status, password } = req.body;
|
| 10 |
+
|
| 11 |
+
const hashedPassword = await hashPassword(password);
|
| 12 |
+
|
| 13 |
+
const newUser: UserInterface = {
|
| 14 |
+
name,
|
| 15 |
+
email,
|
| 16 |
+
role_id,
|
| 17 |
+
status,
|
| 18 |
+
password: hashedPassword,
|
| 19 |
+
};
|
| 20 |
+
|
| 21 |
+
const createdUser = await User.create(newUser);
|
| 22 |
+
|
| 23 |
+
const userResponse = {
|
| 24 |
+
id: createdUser.id,
|
| 25 |
+
name: createdUser.name,
|
| 26 |
+
email: createdUser.email,
|
| 27 |
+
status: createdUser.status,
|
| 28 |
+
role_id: createdUser.role_id,
|
| 29 |
+
};
|
| 30 |
+
|
| 31 |
+
return res.status(201).json({
|
| 32 |
+
message: 'User created successfully',
|
| 33 |
+
data: userResponse,
|
| 34 |
+
});
|
| 35 |
+
|
| 36 |
+
} catch (error) {
|
| 37 |
+
console.error(error);
|
| 38 |
+
return res.status(500).json({
|
| 39 |
+
error: 'Internal Server Error',
|
| 40 |
+
});
|
| 41 |
+
}
|
| 42 |
+
};
|