-
Notifications
You must be signed in to change notification settings - Fork 84
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #104 from dhamolahedonist/feat/15-modify-members
feat: created a change user role endpoint and functionality for acces…
- Loading branch information
Showing
7 changed files
with
71 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
import { Request, Response, NextFunction } from "express"; | ||
import { User } from "../models"; | ||
import { UserRole } from "../enums/userRoles"; | ||
import { ResourceNotFound, HttpError } from "../middleware/error"; | ||
|
||
export const changeUserRole = async (req: Request, res: Response, next: NextFunction) => { | ||
try { | ||
const { user_id, organization_id } = req.params; | ||
const { new_role } = req.body; | ||
|
||
// Validate the provided role | ||
if (!Object.values(UserRole).includes(new_role)) { | ||
throw new HttpError(400, "Invalid role specified"); | ||
} | ||
|
||
// Retrieve the user whose role needs to be updated | ||
const user = await User.findOne({ where: { id: user_id }, relations: ["organizations"] }); | ||
|
||
if (!user) { | ||
throw new ResourceNotFound("User not found"); | ||
} | ||
|
||
// Check if the user belongs to the specified organization | ||
const userOrganization = user.organizations.find(org => org.id === organization_id); | ||
|
||
if (!userOrganization) { | ||
throw new HttpError(400, "User does not belong to the specified team"); | ||
} | ||
|
||
// Update the user's role | ||
user.role = new_role; | ||
await user.save(); | ||
|
||
res.status(200).json({ | ||
message: "Team member role updated successfully", | ||
organization_id, | ||
user_id, | ||
new_role | ||
}); | ||
} catch (error) { | ||
next(error); | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import { Request, Response, NextFunction } from "express"; | ||
import { UserRole } from "../enums/userRoles"; | ||
import { Unauthorized } from "./error"; | ||
|
||
|
||
export const checkPermissions = (roles: UserRole[]) => { | ||
return (req: Request, res: Response, next: NextFunction) => { | ||
const user = req.user; | ||
if (!user || !roles.includes(user.role)) { | ||
throw new Unauthorized("You do not have permission to perform this action"); | ||
} | ||
next(); | ||
}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
export * from "./error"; | ||
export * from "./auth"; | ||
export * from "./checkUserRole"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,18 @@ | ||
import { signUp, verifyOtp, login } from "../controllers"; | ||
import { signUp, verifyOtp, login, changeUserRole } from "../controllers"; | ||
import { Router } from "express"; | ||
import { authMiddleware, checkPermissions } from "../middleware"; | ||
import { UserRole } from "../enums/userRoles"; | ||
|
||
const authRoute = Router(); | ||
|
||
authRoute.post("/signup", signUp); | ||
authRoute.post("/verify-otp", verifyOtp); | ||
authRoute.post("/login", login); | ||
|
||
authRoute.post("/login", login); | ||
authRoute.put( | ||
"/api/v1/organizations/:organization_id/users/:user_id/role", | ||
authMiddleware, | ||
checkPermissions([UserRole.SUPER_ADMIN, UserRole.ADMIN]), | ||
changeUserRole | ||
); | ||
export { authRoute }; |