Spaces:
Runtime error
Runtime error
import express from "express"; | |
import { createUser, getUserById, getAllUsers, updateUserById, deleteUserById, getCurrentUser } from "../controllers/user.controller"; | |
import { validateUser, validateUserUpdate } from "../validators/user.validator"; | |
import { handleValidationErrors } from "../middlewares/handleValidatorError"; | |
import { jwtMiddleware } from '../middlewares/authMiddleware'; | |
import { checkPermission } from "../middlewares/checkPermissions"; | |
import { Permission } from "../shared/interfaces/rolePermission.interface"; | |
const userRouter = express.Router(); | |
userRouter.use(jwtMiddleware); | |
/** | |
* @swagger | |
* components: | |
* schemas: | |
* User: | |
* type: object | |
* required: | |
* - name | |
* - password | |
* - status | |
* - role_id | |
* properties: | |
* id: | |
* type: integer | |
* description: The auto-generated id of the user | |
* name: | |
* type: string | |
* description: The name of the user | |
* email: | |
* type: string | |
* description: The email of the user | |
* status: | |
* type: string | |
* description: The status of the user | |
* role_id: | |
* type: integer | |
* description: The id of the role assigned to the user | |
* password: | |
* type: string | |
* description: The password of the user | |
* example: | |
* id: 1 | |
* name: John Doe | |
* email: john.doe@example.com | |
* status: active | |
* role_id: 2 | |
* | |
* UserUpdate: | |
* type: object | |
* properties: | |
* name: | |
* type: string | |
* description: The name of the user | |
* email: | |
* type: string | |
* description: The email of the user | |
* status: | |
* type: string | |
* description: The status of the user | |
* role_id: | |
* type: integer | |
* description: The id of the role assigned to the user | |
* password: | |
* type: string | |
* description: The password of the user | |
* example: | |
* name: John Doe | |
* email: john.doe@example.com | |
* status: active | |
* role_id: 2 | |
* password: secretpassword | |
*/ | |
/** | |
* @swagger | |
* /api/users: | |
* post: | |
* summary: Create a new user | |
* tags: [Users] | |
* requestBody: | |
* required: true | |
* content: | |
* application/json: | |
* schema: | |
* $ref: '#/components/schemas/User' | |
* responses: | |
* 201: | |
* description: User created successfully | |
* content: | |
* application/json: | |
* schema: | |
* $ref: '#/components/schemas/User' | |
* 400: | |
* description: Validation errors | |
* 500: | |
* description: Error while creating a User | |
*/ | |
userRouter.post("/",checkPermission(Permission.CREATE_USER), validateUser, handleValidationErrors, createUser); | |
/** | |
* @swagger | |
* /api/users/me: | |
* get: | |
* summary: Get the current authenticated user's information | |
* tags: [Users] | |
* responses: | |
* 200: | |
* description: The current user information | |
* content: | |
* application/json: | |
* schema: | |
* $ref: '#/components/schemas/User' | |
* 401: | |
* description: Unauthorized, no valid authentication provided | |
* 404: | |
* description: User not found | |
* 500: | |
* description: Error while fetching User details | |
*/ | |
userRouter.get("/me", getCurrentUser); | |
/** | |
* @swagger | |
* /api/users/{id}: | |
* get: | |
* summary: Get a user by ID | |
* tags: [Users] | |
* parameters: | |
* - in: path | |
* name: id | |
* required: true | |
* schema: | |
* type: integer | |
* description: The user ID | |
* responses: | |
* 200: | |
* description: The user information | |
* content: | |
* application/json: | |
* schema: | |
* $ref: '#/components/schemas/User' | |
* 404: | |
* description: User not found | |
* 500: | |
* description: Error while fetching User details | |
*/ | |
userRouter.get("/:id", checkPermission(Permission.GET_SINGLE_USER), getUserById); | |
/** | |
* @swagger | |
* /api/users: | |
* get: | |
* summary: Get all users | |
* tags: [Users] | |
* parameters: | |
* - in: query | |
* name: filter | |
* required: false | |
* style: deepObject | |
* explode: true | |
* schema: | |
* type: object | |
* properties: | |
* name: | |
* type: string | |
* date: | |
* type: string | |
* format: date | |
* role_id: | |
* type: integer | |
* status: | |
* type: string | |
* created_before: | |
* type: string | |
* format: date | |
* created_after: | |
* type: string | |
* format: date | |
* description: Filters for querying users | |
* - in: query | |
* name: sort_by | |
* schema: | |
* type: string | |
* enum: [id, name, email, role_id, created_at] | |
* default: id | |
* description: The field to sort by | |
* - in: query | |
* name: sort_order | |
* schema: | |
* type: string | |
* enum: [asc, desc] | |
* default: asc | |
* description: The sort order | |
* - in: query | |
* name: page | |
* schema: | |
* type: integer | |
* default: 1 | |
* description: The page number | |
* - in: query | |
* name: limit | |
* schema: | |
* type: integer | |
* default: 10 | |
* description: The number of items per page | |
* responses: | |
* 200: | |
* description: List of all users | |
* content: | |
* application/json: | |
* schema: | |
* type: object | |
* properties: | |
* page: | |
* type: integer | |
* description: Current page number | |
* limit: | |
* type: integer | |
* description: Number of items per page | |
* total: | |
* type: integer | |
* description: Total number of users | |
* data: | |
* type: array | |
* items: | |
* $ref: '#/components/schemas/User' | |
* 500: | |
* description: Error fetching users | |
*/ | |
userRouter.get("/", checkPermission(Permission.GET_ALL_USERS), getAllUsers); | |
/** | |
* @swagger | |
* /api/users/{id}: | |
* put: | |
* summary: Update a user by ID | |
* tags: [Users] | |
* parameters: | |
* - in: path | |
* name: id | |
* required: true | |
* schema: | |
* type: integer | |
* description: The user ID | |
* requestBody: | |
* required: true | |
* content: | |
* application/json: | |
* schema: | |
* $ref: '#/components/schemas/UserUpdate' | |
* responses: | |
* 200: | |
* description: User updated successfully | |
* content: | |
* application/json: | |
* schema: | |
* $ref: '#/components/schemas/User' | |
* 400: | |
* description: Validation errors | |
* 404: | |
* description: User not found | |
* 500: | |
* description: Internal server error | |
*/ | |
userRouter.put("/:id", checkPermission(Permission.UPDATE_USER), validateUserUpdate, handleValidationErrors, updateUserById); | |
/** | |
* @swagger | |
* /api/users/{id}: | |
* delete: | |
* summary: Delete a user by ID | |
* tags: [Users] | |
* parameters: | |
* - in: path | |
* name: id | |
* required: true | |
* schema: | |
* type: integer | |
* description: The user ID | |
* responses: | |
* 204: | |
* description: User deleted successfully | |
* 404: | |
* description: User not found | |
* 500: | |
* description: Error while deleting User | |
*/ | |
userRouter.delete("/:id", checkPermission(Permission.DELETE_USER), deleteUserById); | |
export default userRouter; | |