From 8f757e04c42f039fd839dc7261f502ffd4cf8179 Mon Sep 17 00:00:00 2001 From: Ndahimana Bonheur Date: Thu, 30 May 2024 11:03:55 +0200 Subject: [PATCH] mend --- src/middlewares/validation.ts | 37 +------------------ .../auth/controller/authControllers.ts | 2 +- .../auth/repository/authRepositories.ts | 4 +- .../user/controller/userControllers.ts | 10 ++--- src/modules/user/test/user.spec.ts | 6 +-- .../user/validation/userValidations.ts | 9 +++++ src/routes/userRouter.ts | 6 +-- 7 files changed, 23 insertions(+), 51 deletions(-) diff --git a/src/middlewares/validation.ts b/src/middlewares/validation.ts index 7afb0143..09e222f4 100644 --- a/src/middlewares/validation.ts +++ b/src/middlewares/validation.ts @@ -99,42 +99,7 @@ const verifyUserCredentials = async (req: Request, res: Response, next: NextFunc -const updateUserRoleSchema = Joi.object({ - role: Joi.string().valid("Admin", "Buyer", "Seller").required().messages({ - "any.required": "The 'role' parameter is required.", - "string.base": "The 'role' parameter must be a string.", - "any.only": "The 'role' parameter must be one of ['Admin', 'Buyer', 'Seller']." - }) -}); - - - -const validateUpdateUserRole = async (req: Request, res: Response, next: NextFunction) => { - const { id } = req.params; - const { error } = updateUserRoleSchema.validate(req.body); - - if (error) { - return res.status(httpStatus.BAD_REQUEST).json({ - status: httpStatus.BAD_REQUEST, - message: error.details[0].message - }); - } - try { - const user = await authRepositories.findUserByAttributes("id", id); - if (!user) { - return res - .status(httpStatus.NOT_FOUND) - .json({ status: httpStatus.NOT_FOUND, message: "User doesn't exist." }); - } - next(); - } catch (err) { - return res.status(httpStatus.INTERNAL_SERVER_ERROR).json({ - status: httpStatus.INTERNAL_SERVER_ERROR, - message: err.message - }); - } -}; -export { validation, isUserExist, isAccountVerified, validateUpdateUserRole, updateUserRoleSchema,verifyUserCredentials }; \ No newline at end of file +export { validation, isUserExist, isAccountVerified,verifyUserCredentials }; \ No newline at end of file diff --git a/src/modules/auth/controller/authControllers.ts b/src/modules/auth/controller/authControllers.ts index 4b6dde4e..772ca29f 100644 --- a/src/modules/auth/controller/authControllers.ts +++ b/src/modules/auth/controller/authControllers.ts @@ -35,7 +35,7 @@ const sendVerifyEmail = async (req: any, res: Response) => { const verifyEmail = async (req: any, res: Response) => { try { await authRepositories.destroySession(req.user.id, req.session.token) - await authRepositories.UpdateUserByAttributes("isVerified", true, "id", req.user.id); + await authRepositories.updateUserByAttributes("isVerified", true, "id", req.user.id); res.status(httpStatus.OK).json({ status: httpStatus.OK, message: "Account verified successfully, now login." }); } catch (error) { return res.status(httpStatus.INTERNAL_SERVER_ERROR).json({ status: httpStatus.INTERNAL_SERVER_ERROR, message: error.message }); diff --git a/src/modules/auth/repository/authRepositories.ts b/src/modules/auth/repository/authRepositories.ts index afed5226..4eb75488 100644 --- a/src/modules/auth/repository/authRepositories.ts +++ b/src/modules/auth/repository/authRepositories.ts @@ -11,7 +11,7 @@ const findUserByAttributes = async (key:string, value:any) =>{ return await Users.findOne({ where: { [key]: value} }) } -const UpdateUserByAttributes = async (updatedKey:string, updatedValue:any, whereKey:string, whereValue:any) =>{ +const updateUserByAttributes = async (updatedKey:string, updatedValue:any, whereKey:string, whereValue:any) =>{ await Users.update({ [updatedKey]: updatedValue }, { where: { [whereKey]: whereValue} }); return await findUserByAttributes(whereKey, whereValue) } @@ -28,4 +28,4 @@ const destroySession = async (userId: number, token:string) =>{ return await Session.destroy({ where: {userId, token } }); } -export default { createUser, createSession, findUserByAttributes, destroySession, UpdateUserByAttributes, findSessionByUserId } \ No newline at end of file +export default { createUser, createSession, findUserByAttributes, destroySession, updateUserByAttributes, findSessionByUserId } \ No newline at end of file diff --git a/src/modules/user/controller/userControllers.ts b/src/modules/user/controller/userControllers.ts index a1307807..40f1e77c 100644 --- a/src/modules/user/controller/userControllers.ts +++ b/src/modules/user/controller/userControllers.ts @@ -6,17 +6,15 @@ import httpStatus from "http-status"; const updateUserRole = async (req: Request, res: Response) => { try { - await authRepositories.UpdateUserByAttributes("role", req.body.role, "id", req.params.id) - const updatedUser = await authRepositories.findUserByAttributes("id", req.params.id) + const data = await authRepositories.updateUserByAttributes("role", req.body.role, "id", req.params.id) return res.status(httpStatus.OK).json({ - status: httpStatus.OK, message: "User role updated successfully", - new: updatedUser + data }); } catch (error) { return res.status(httpStatus.INTERNAL_SERVER_ERROR).json({ status: httpStatus.INTERNAL_SERVER_ERROR, - message: error + message: error.message }); } }; @@ -25,7 +23,7 @@ const updateUserRole = async (req: Request, res: Response) => { const updateUserStatus = async (req: Request, res: Response): Promise => { try { const userId: number = Number(req.params.id); - const data = await authRepositories.UpdateUserByAttributes("status", req.body.status, "id", userId); + const data = await authRepositories.updateUserByAttributes("status", req.body.status, "id", userId); res.status(httpStatus.OK).json({ message: "Status updated successfully.", data }); } catch (error) { res.status(httpStatus.INTERNAL_SERVER_ERROR).json({ status: httpStatus.INTERNAL_SERVER_ERROR, message: error.message }); diff --git a/src/modules/user/test/user.spec.ts b/src/modules/user/test/user.spec.ts index 43b39de1..f1446c7d 100644 --- a/src/modules/user/test/user.spec.ts +++ b/src/modules/user/test/user.spec.ts @@ -113,7 +113,7 @@ describe("User Repository Functions", () => { it("should update the user status successfully", async () => { updateStub.resolves([1]); const user = { id: 1, status: true }; - const result = await authRepositories.UpdateUserByAttributes("status", "enabled", "id", 1); + const result = await authRepositories.updateUserByAttributes("status", "enabled", "id", 1); expect(updateStub.calledOnce).to.be.true; expect(updateStub.calledWith({ status: true }, { where: { id: 1 } })).to.be.false; }); @@ -121,7 +121,7 @@ describe("User Repository Functions", () => { it("should throw an error if there is a database error", async () => { updateStub.rejects(new Error("Database error")); try { - await authRepositories.UpdateUserByAttributes("status", "enabled", "id", 1); + await authRepositories.updateUserByAttributes("status", "enabled", "id", 1); } catch (error) { expect(updateStub.calledOnce).to.be.true; expect(error.message).to.equal("Database error"); @@ -211,7 +211,7 @@ describe("Admin update User roles", () => { router().put("/api/users/admin-update-role/10001").send({ role: "Admin" }).end((err, res) => { expect(res).to.have.status(httpStatus.NOT_FOUND); expect(res.body).to.be.an("object"); - expect(res.body).to.have.property("message", "User doesn't exist.") + expect(res.body).to.have.property("message", "User not found") done(err) }) }) diff --git a/src/modules/user/validation/userValidations.ts b/src/modules/user/validation/userValidations.ts index 4b775e53..efc3b03d 100644 --- a/src/modules/user/validation/userValidations.ts +++ b/src/modules/user/validation/userValidations.ts @@ -7,3 +7,12 @@ export const statusSchema = Joi.object({ "any.required": "Status is required" }) }); + + +export const roleSchema = Joi.object({ + role: Joi.string().valid("Admin", "Buyer", "Seller").required().messages({ + "any.required": "The 'role' parameter is required.", + "string.base": "The 'role' parameter must be a string.", + "any.only": "The 'role' parameter must be one of ['Admin', 'Buyer', 'Seller']." + }) +}); diff --git a/src/routes/userRouter.ts b/src/routes/userRouter.ts index 2d632181..ec66fdda 100644 --- a/src/routes/userRouter.ts +++ b/src/routes/userRouter.ts @@ -1,12 +1,12 @@ import { Router } from "express"; import userControllers from "../modules/user/controller/userControllers"; -import {isUserExist, validation,validateUpdateUserRole,updateUserRoleSchema} from "../middlewares/validation"; -import { statusSchema } from "../modules/user/validation/userValidations"; +import {isUserExist, validation} from "../middlewares/validation"; +import { statusSchema,roleSchema } from "../modules/user/validation/userValidations"; const router: Router = Router() router.put("/admin-update-user-status/:id", validation(statusSchema), isUserExist, userControllers.updateUserStatus); -router.put("/admin-update-role/:id",validation(updateUserRoleSchema),validateUpdateUserRole, userControllers.updateUserRole); +router.put("/admin-update-role/:id",validation(roleSchema),isUserExist, userControllers.updateUserRole); export default router; \ No newline at end of file