fosrl.pangolin/server/routers/user/addUserRole.ts

108 lines
3.1 KiB
TypeScript
Raw Normal View History

import { Request, Response, NextFunction } from "express";
import { z } from "zod";
import { db } from "@server/db";
import { userOrgs, roles } from "@server/db/schema";
import { eq, and } from "drizzle-orm";
2024-10-12 21:36:14 -04:00
import response from "@server/utils/response";
import HttpCode from "@server/types/HttpCode";
import createHttpError from "http-errors";
import logger from "@server/logger";
import { fromError } from "zod-validation-error";
2024-11-10 21:19:41 -05:00
import stoi from "@server/utils/stoi";
2024-10-12 21:36:14 -04:00
2024-11-09 23:59:19 -05:00
const addUserRoleParamsSchema = z.object({
2024-10-12 21:36:14 -04:00
userId: z.string(),
2024-11-10 21:19:41 -05:00
roleId: z.string().transform(stoi).pipe(z.number()),
2024-10-12 21:36:14 -04:00
});
2024-11-09 23:59:19 -05:00
export type AddUserRoleResponse = z.infer<typeof addUserRoleParamsSchema>;
export async function addUserRole(
req: Request,
res: Response,
next: NextFunction
): Promise<any> {
2024-10-12 21:36:14 -04:00
try {
2024-11-10 21:19:41 -05:00
const parsedParams = addUserRoleParamsSchema.safeParse(req.params);
if (!parsedParams.success) {
2024-10-12 21:36:14 -04:00
return next(
createHttpError(
HttpCode.BAD_REQUEST,
2024-11-10 21:19:41 -05:00
fromError(parsedParams.error).toString()
2024-10-12 21:36:14 -04:00
)
);
}
2024-11-10 21:19:41 -05:00
const { userId, roleId } = parsedParams.data;
2024-11-09 23:59:19 -05:00
if (!req.userOrg) {
return next(
createHttpError(
HttpCode.FORBIDDEN,
"You do not have access to this organization"
)
);
}
const orgId = req.userOrg.orgId;
const existingUser = await db
.select()
.from(userOrgs)
.where(and(eq(userOrgs.userId, userId), eq(userOrgs.orgId, orgId)))
.limit(1);
if (existingUser.length === 0) {
return next(
createHttpError(
HttpCode.NOT_FOUND,
"User not found or does not belong to the specified organization"
)
);
}
if (existingUser[0].isOwner) {
return next(
createHttpError(
HttpCode.FORBIDDEN,
"Cannot change the role of the owner of the organization"
)
);
}
2024-10-12 21:36:14 -04:00
const roleExists = await db
.select()
2024-10-12 21:36:14 -04:00
.from(roles)
.where(and(eq(roles.roleId, roleId), eq(roles.orgId, orgId)))
.limit(1);
if (roleExists.length === 0) {
return next(
createHttpError(
HttpCode.NOT_FOUND,
"Role not found or does not belong to the specified organization"
)
);
2024-10-12 21:36:14 -04:00
}
const newUserRole = await db
.update(userOrgs)
2024-10-12 21:36:14 -04:00
.set({ roleId })
.where(and(eq(userOrgs.userId, userId), eq(userOrgs.orgId, orgId)))
.returning();
return response(res, {
data: newUserRole[0],
success: true,
error: false,
message: "Role added to user successfully",
2024-11-09 23:59:19 -05:00
status: HttpCode.OK,
2024-10-12 21:36:14 -04:00
});
} catch (error) {
logger.error(error);
return next(
createHttpError(HttpCode.INTERNAL_SERVER_ERROR, "An error occurred")
);
2024-10-12 21:36:14 -04:00
}
}