From 7f0ddbd31be1a0d8e6ac48aa01436084c2a23147 Mon Sep 17 00:00:00 2001 From: Your Name Date: Fri, 4 Apr 2025 18:01:13 +0200 Subject: [PATCH 1/8] feat: add get user by id --- src/controllers/user.controller.js | 74 +++++++++++++++++++++++++++++- 1 file changed, 72 insertions(+), 2 deletions(-) diff --git a/src/controllers/user.controller.js b/src/controllers/user.controller.js index 8fb7604..4eed6a3 100644 --- a/src/controllers/user.controller.js +++ b/src/controllers/user.controller.js @@ -1,6 +1,8 @@ import prisma from '../config/prismaClient.js'; -import { authenticate } from '../middlewares/auth.middleware.js'; +// import { getUserByIdValidation } from '../validations/user.validation.js'; +//import { validateUserId, validateCreateUser } from '../validations/user.validation.js'; +/* eslint no-undef:off */ /** * @swagger * /api/users: @@ -52,6 +54,74 @@ export const getAllUsers = async (req, res, next) => { users, }); } catch (error) { - next(error); + console.error('Error in getAllUsers:', error); // Log the error for debugging + return next(error); // Ensure next is called with the error + } +}; + +/** + * @swagger + * /api/users/{id}: + * get: + * summary: Get user by ID + * tags: [Users] + * parameters: + * - in: path + * name: id + * required: true + * schema: + * type: string + * format: uuid + * description: User ID + * responses: + * 200: + * description: User details retrieved successfully + * content: + * application/json: + * schema: + * type: object + * properties: + * id: + * type: string + * email: + * type: string + * firstName: + * type: string + * lastName: + * type: string + * role: + * type: string + * 404: + * description: User not found + * 500: + * description: Server error + */ +export const getUserById = async (req, res, next) => { + const { id } = req.params; + try { + // Validate the ID format (UUID) + // const { error } = getUserByIdValidation(req.body); + // if (error) { + // return res.status(400).json({ message: error.details[0].message }); + // } + // Fetch the user by ID from the database + const user = await prisma.user.findFirst({ + where: { id }, + select: { + id: true, + email: true, + firstName: true, + lastName: true, + role: true, + }, + }); + + if (!user) { + return res.status(404).json({ message: 'User not found' }); + } + + return res.status(200).json(user); + } catch (error) { + next(error); // Ensure next is called with the error } }; From 69ee78b25399cc326764dd8cff2a7164770c3a1d Mon Sep 17 00:00:00 2001 From: Your Name Date: Fri, 4 Apr 2025 18:02:02 +0200 Subject: [PATCH 2/8] feat: add get user by id Route --- src/routes/user.routes.js | 29 ++++++++++++++++++++++++++++- 1 file changed, 28 insertions(+), 1 deletion(-) diff --git a/src/routes/user.routes.js b/src/routes/user.routes.js index d20ee6e..1602d7e 100644 --- a/src/routes/user.routes.js +++ b/src/routes/user.routes.js @@ -1,5 +1,6 @@ import { Router } from 'express'; -import { getAllUsers } from '../controllers/user.controller.js'; +import { getAllUsers, getUserById } from '../controllers/user.controller.js'; +// import{authorizeUser} from '../middlewares/auth.middleware.js'; const router = Router(); @@ -82,4 +83,30 @@ const router = Router(); */ router.get('/api/users', getAllUsers); +/** + * @swagger + * /api/users/{id}: + * get: + * summary: Get user by ID + * tags: [Users] + * parameters: + * - in: path + * name: id + * required: true + * schema: + * type: string + * format: uuid + * description: User ID + * responses: + * 200: + * description: User details retrieved successfully + * 400: + * description: Invalid ID format + * 404: + * description: User not found + * 500: + * description: Server error + */ +router.get('/api/users/:id', getUserById); + export default router; From 905dd13c1dd220486f49077e24b3df873522a867 Mon Sep 17 00:00:00 2001 From: Your Name Date: Fri, 4 Apr 2025 18:02:34 +0200 Subject: [PATCH 3/8] feat: add get user by id validation --- src/validations/user.validation.js | 49 ++++++++++++++++++++++++++++++ 1 file changed, 49 insertions(+) create mode 100644 src/validations/user.validation.js diff --git a/src/validations/user.validation.js b/src/validations/user.validation.js new file mode 100644 index 0000000..55f6fd8 --- /dev/null +++ b/src/validations/user.validation.js @@ -0,0 +1,49 @@ +import Joi from 'joi'; + +export const updateUserAccountValidation = (obj) => { + const schema = Joi.object({ + firstName: Joi.string().trim().min(3).max(30).messages({ + 'string.min': 'First name must be at least 3 characters long.', + 'string.max': 'First name must be at most 30 characters long.', + }), + lastName: Joi.string().trim().messages({ + 'string.min': 'Last name must be at least 3 characters long.', + 'string.max': 'Last name must be at most 30 characters long.', + }), + gender: Joi.string().valid('Male', 'Female').messages({ + 'string.only': "Gender must be either 'male' or 'female'.", + }), + DOB: Joi.date() + .max(new Date(new Date().setFullYear(new Date().getFullYear() - 18))) // Ensures the user is at least 18 years old + .messages({ + 'date.base': 'Invalid date format for DOB.', + 'date.max': 'You must be at least 18 years old.', + }), + mobileNumber: Joi.string().trim().messages({ + 'string.empty': 'Mobile number cannot be empty if provided', + }), + }).min(1); // At least one field must be provided + + return schema.validate(obj); +}; + +export const updateUserPasswordValidation = (obj) => { + const schema = Joi.object({ + password: Joi.string().required().trim().min(8).max(32).messages({ + 'string.empty': 'Password is required.', + 'string.min': 'Password must be at least 8 characters long.', + 'string.max': 'Password must be at most 32 characters long.', + }), + }); + return schema.validate(obj); +}; + +export const getUserByIdValidation = (obj) => { + const schema = Joi.object({ + id: Joi.string().required().trim().messages({ + 'string.empty': 'ID is required.', + }), + }); + + return schema.validate(obj); +}; From e6934fd1cbe2f88e86c0e030671d5f487a818918 Mon Sep 17 00:00:00 2001 From: Your Name Date: Fri, 4 Apr 2025 18:03:08 +0200 Subject: [PATCH 4/8] feat: add get user by id readme --- README.md | 3 +++ src/middlewares/auth.middleware.js | 16 ++++++++++++++++ 2 files changed, 19 insertions(+) diff --git a/README.md b/README.md index 14cede0..a5c735f 100644 --- a/README.md +++ b/README.md @@ -31,6 +31,9 @@ base url: `http://localhost:3000` ### User +- **Get all Users**: GET `/api/users` +- **Get a specific Users**: GET `/api/Users/:userId` + ### Organization - **Create an Organization**: POST `/api/organization` diff --git a/src/middlewares/auth.middleware.js b/src/middlewares/auth.middleware.js index e0235ba..e9789ab 100644 --- a/src/middlewares/auth.middleware.js +++ b/src/middlewares/auth.middleware.js @@ -26,3 +26,19 @@ export const verifyAccessToken = (req, res, next) => { return res.status(403).json({ message: 'Invalid token' }); } }; + +// export const authorizeUser = (req, res, next) => { +// const { user } = req; // Assuming `req.user` is populated by authentication middleware +// const { id } = req.params; + +// if (!user) { +// return res.status(401).json({ message: 'Unauthorized' }); +// } + +// // Allow access if the user is an admin or accessing their own data +// if (user.role === 'ADMIN' || user.id === id) { +// return next(); +// } + +// return res.status(403).json({ message: 'Forbidden: You do not have access to this resource' }); +// }; From 8e5f5cf6d13f02181b56ddc82e203017fd642ec1 Mon Sep 17 00:00:00 2001 From: Your Name Date: Fri, 4 Apr 2025 18:05:02 +0200 Subject: [PATCH 5/8] fix: fix at user controller --- src/controllers/user.controller.js | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/src/controllers/user.controller.js b/src/controllers/user.controller.js index 4eed6a3..bd0cdac 100644 --- a/src/controllers/user.controller.js +++ b/src/controllers/user.controller.js @@ -1,5 +1,5 @@ import prisma from '../config/prismaClient.js'; -// import { getUserByIdValidation } from '../validations/user.validation.js'; +import { getUserByIdValidation } from '../validations/user.validation.js'; //import { validateUserId, validateCreateUser } from '../validations/user.validation.js'; /* eslint no-undef:off */ @@ -54,8 +54,7 @@ export const getAllUsers = async (req, res, next) => { users, }); } catch (error) { - console.error('Error in getAllUsers:', error); // Log the error for debugging - return next(error); // Ensure next is called with the error + next(error); // Ensure next is called with the error } }; From bd12e0c97dd869949fae8af7b5cd1b9f80e89110 Mon Sep 17 00:00:00 2001 From: Kareem Mohamed Date: Fri, 4 Apr 2025 18:11:43 +0200 Subject: [PATCH 6/8] Update src/validations/user.validation.js Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> --- src/validations/user.validation.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/validations/user.validation.js b/src/validations/user.validation.js index 55f6fd8..023a0bc 100644 --- a/src/validations/user.validation.js +++ b/src/validations/user.validation.js @@ -11,7 +11,7 @@ export const updateUserAccountValidation = (obj) => { 'string.max': 'Last name must be at most 30 characters long.', }), gender: Joi.string().valid('Male', 'Female').messages({ - 'string.only': "Gender must be either 'male' or 'female'.", + 'any.only': "Gender must be either 'male' or 'female'.", }), DOB: Joi.date() .max(new Date(new Date().setFullYear(new Date().getFullYear() - 18))) // Ensures the user is at least 18 years old From 16cee19d95f96a8f2810642fc738acc4be10288c Mon Sep 17 00:00:00 2001 From: Kareem Mohamed Date: Fri, 4 Apr 2025 18:11:55 +0200 Subject: [PATCH 7/8] Update src/controllers/user.controller.js Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> --- src/controllers/user.controller.js | 1 - 1 file changed, 1 deletion(-) diff --git a/src/controllers/user.controller.js b/src/controllers/user.controller.js index 4a24cc3..eb36122 100644 --- a/src/controllers/user.controller.js +++ b/src/controllers/user.controller.js @@ -1,5 +1,4 @@ import prisma from '../config/prismaClient.js'; -feat/59/get-user-by-id import { getUserByIdValidation } from '../validations/user.validation.js'; //import { validateUserId, validateCreateUser } from '../validations/user.validation.j // import { authenticate } from '../middlewares/auth.middleware.js'; From aad66fe1207ef694b691aff9ba42ef0756a29d4f Mon Sep 17 00:00:00 2001 From: Kareem Mohamed Date: Fri, 4 Apr 2025 18:12:09 +0200 Subject: [PATCH 8/8] Update src/controllers/user.controller.js Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> --- src/controllers/user.controller.js | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/controllers/user.controller.js b/src/controllers/user.controller.js index eb36122..36284ca 100644 --- a/src/controllers/user.controller.js +++ b/src/controllers/user.controller.js @@ -2,8 +2,6 @@ import prisma from '../config/prismaClient.js'; import { getUserByIdValidation } from '../validations/user.validation.js'; //import { validateUserId, validateCreateUser } from '../validations/user.validation.j // import { authenticate } from '../middlewares/auth.middleware.js'; -main - /* eslint no-undef:off */ /** * @swagger