2024-11-03 13:57:51 -05:00
|
|
|
import { Request, Response, NextFunction } from "express";
|
|
|
|
import { z } from "zod";
|
|
|
|
import { db } from "@server/db";
|
|
|
|
import { roles, orgs } from "@server/db/schema";
|
2024-10-12 21:36:14 -04:00
|
|
|
import response from "@server/utils/response";
|
2024-11-03 13:57:51 -05:00
|
|
|
import HttpCode from "@server/types/HttpCode";
|
|
|
|
import createHttpError from "http-errors";
|
|
|
|
import { sql, eq } from "drizzle-orm";
|
|
|
|
import { ActionsEnum, checkUserActionPermission } from "@server/auth/actions";
|
|
|
|
import logger from "@server/logger";
|
|
|
|
import { fromError } from "zod-validation-error";
|
2024-10-12 21:36:14 -04:00
|
|
|
|
|
|
|
const listRolesParamsSchema = z.object({
|
2024-11-03 13:57:51 -05:00
|
|
|
orgId: z.string(),
|
2024-10-12 21:36:14 -04:00
|
|
|
});
|
|
|
|
|
|
|
|
const listRolesSchema = z.object({
|
2024-11-03 13:57:51 -05:00
|
|
|
limit: z
|
|
|
|
.string()
|
|
|
|
.optional()
|
|
|
|
.transform(Number)
|
|
|
|
.pipe(z.number().int().positive().default(10)),
|
|
|
|
offset: z
|
|
|
|
.string()
|
|
|
|
.optional()
|
|
|
|
.transform(Number)
|
|
|
|
.pipe(z.number().int().nonnegative().default(0)),
|
|
|
|
orgId: z
|
|
|
|
.string()
|
|
|
|
.optional()
|
|
|
|
.transform(Number)
|
|
|
|
.pipe(z.number().int().positive()),
|
2024-10-12 21:36:14 -04:00
|
|
|
});
|
|
|
|
|
2024-11-03 13:57:51 -05:00
|
|
|
export async function listRoles(
|
|
|
|
req: Request,
|
|
|
|
res: Response,
|
|
|
|
next: NextFunction
|
|
|
|
): Promise<any> {
|
2024-10-12 21:36:14 -04:00
|
|
|
try {
|
|
|
|
const parsedQuery = listRolesSchema.safeParse(req.query);
|
|
|
|
if (!parsedQuery.success) {
|
|
|
|
return next(
|
|
|
|
createHttpError(
|
|
|
|
HttpCode.BAD_REQUEST,
|
2024-11-03 13:57:51 -05:00
|
|
|
fromError(parsedQuery.error).toString()
|
2024-10-12 21:36:14 -04:00
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
const { limit, offset } = parsedQuery.data;
|
|
|
|
|
|
|
|
const parsedParams = listRolesParamsSchema.safeParse(req.params);
|
|
|
|
if (!parsedParams.success) {
|
|
|
|
return next(
|
|
|
|
createHttpError(
|
|
|
|
HttpCode.BAD_REQUEST,
|
2024-11-03 13:57:51 -05:00
|
|
|
fromError(parsedParams.error).toString()
|
2024-10-12 21:36:14 -04:00
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
const { orgId } = parsedParams.data;
|
|
|
|
|
|
|
|
// Check if the user has permission to list roles
|
2024-11-03 13:57:51 -05:00
|
|
|
const hasPermission = await checkUserActionPermission(
|
|
|
|
ActionsEnum.listRoles,
|
|
|
|
req
|
|
|
|
);
|
2024-10-12 21:36:14 -04:00
|
|
|
if (!hasPermission) {
|
2024-11-03 13:57:51 -05:00
|
|
|
return next(
|
|
|
|
createHttpError(
|
|
|
|
HttpCode.FORBIDDEN,
|
|
|
|
"User does not have permission to perform this action"
|
|
|
|
)
|
|
|
|
);
|
2024-10-12 21:36:14 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
let baseQuery: any = db
|
|
|
|
.select({
|
|
|
|
roleId: roles.roleId,
|
|
|
|
orgId: roles.orgId,
|
2024-11-05 22:38:57 -05:00
|
|
|
isAdmin: roles.isAdmin,
|
2024-10-12 21:36:14 -04:00
|
|
|
name: roles.name,
|
|
|
|
description: roles.description,
|
|
|
|
orgName: orgs.name,
|
|
|
|
})
|
|
|
|
.from(roles)
|
2024-10-20 12:55:28 -04:00
|
|
|
.leftJoin(orgs, eq(roles.orgId, orgs.orgId))
|
|
|
|
.where(eq(roles.orgId, orgId));
|
2024-10-12 21:36:14 -04:00
|
|
|
|
2024-11-03 13:57:51 -05:00
|
|
|
let countQuery: any = db
|
|
|
|
.select({ count: sql<number>`cast(count(*) as integer)` })
|
|
|
|
.from(roles)
|
|
|
|
.where(eq(roles.orgId, orgId));
|
2024-10-12 21:36:14 -04:00
|
|
|
|
|
|
|
const rolesList = await baseQuery.limit(limit).offset(offset);
|
|
|
|
const totalCountResult = await countQuery;
|
|
|
|
const totalCount = totalCountResult[0].count;
|
|
|
|
|
|
|
|
return response(res, {
|
|
|
|
data: {
|
|
|
|
roles: rolesList,
|
|
|
|
pagination: {
|
|
|
|
total: totalCount,
|
|
|
|
limit,
|
|
|
|
offset,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
success: true,
|
|
|
|
error: false,
|
|
|
|
message: "Roles retrieved successfully",
|
|
|
|
status: HttpCode.OK,
|
|
|
|
});
|
|
|
|
} catch (error) {
|
|
|
|
logger.error(error);
|
2024-11-03 13:57:51 -05:00
|
|
|
return next(
|
|
|
|
createHttpError(
|
|
|
|
HttpCode.INTERNAL_SERVER_ERROR,
|
|
|
|
"An error occurred..."
|
|
|
|
)
|
|
|
|
);
|
2024-10-12 21:36:14 -04:00
|
|
|
}
|
2024-11-03 13:57:51 -05:00
|
|
|
}
|