Skip to content

Commit

Permalink
Merge pull request #104 from dhamolahedonist/feat/15-modify-members
Browse files Browse the repository at this point in the history
feat: created a change user role endpoint and functionality for acces…
  • Loading branch information
incredible-phoenix246 authored Jul 22, 2024
2 parents 62bd6e6 + d41c6c9 commit dcdb249
Show file tree
Hide file tree
Showing 7 changed files with 71 additions and 4 deletions.
2 changes: 1 addition & 1 deletion src/controllers/OrgController.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Request, Response } from "express";
import { Request, Response } from "express";
import { OrgService } from "../services/OrgService";

export class OrgController {
Expand Down
1 change: 1 addition & 0 deletions src/controllers/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ export * from "./AuthController";
export * from "./UserController";
export * from "./HelpController";
export * from "./NotificationController";
export * from "./roleController"
43 changes: 43 additions & 0 deletions src/controllers/roleController.ts
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);
}
};
2 changes: 1 addition & 1 deletion src/middleware/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import jwt from "jsonwebtoken";
import config from "../config";

export const authMiddleware = async (
req: Request,
req: Request & { user?: User },
res: Response,
next: NextFunction
) => {
Expand Down
14 changes: 14 additions & 0 deletions src/middleware/checkUserRole.ts
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();
};
};
1 change: 1 addition & 0 deletions src/middleware/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
export * from "./error";
export * from "./auth";
export * from "./checkUserRole";
12 changes: 10 additions & 2 deletions src/routes/auth.ts
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 };

0 comments on commit dcdb249

Please sign in to comment.