mirror of
https://github.com/fosrl/pangolin.git
synced 2026-03-03 01:06:39 +00:00
refactor contexts, format zod errors, and more refactoring
This commit is contained in:
@@ -1,26 +1,31 @@
|
||||
import { Request, Response, NextFunction } from 'express';
|
||||
import { z } from 'zod';
|
||||
import { db } from '@server/db';
|
||||
import { roleActions, actions } from '@server/db/schema';
|
||||
import { eq } from 'drizzle-orm';
|
||||
import { Request, Response, NextFunction } from "express";
|
||||
import { z } from "zod";
|
||||
import { db } from "@server/db";
|
||||
import { roleActions, actions } from "@server/db/schema";
|
||||
import { eq } from "drizzle-orm";
|
||||
import response from "@server/utils/response";
|
||||
import HttpCode from '@server/types/HttpCode';
|
||||
import createHttpError from 'http-errors';
|
||||
import { ActionsEnum, checkUserActionPermission } from '@server/auth/actions';
|
||||
import logger from '@server/logger';
|
||||
import HttpCode from "@server/types/HttpCode";
|
||||
import createHttpError from "http-errors";
|
||||
import { ActionsEnum, checkUserActionPermission } from "@server/auth/actions";
|
||||
import logger from "@server/logger";
|
||||
import { fromError } from "zod-validation-error";
|
||||
|
||||
const listRoleActionsSchema = z.object({
|
||||
roleId: z.string().transform(Number).pipe(z.number().int().positive()),
|
||||
});
|
||||
|
||||
export async function listRoleActions(req: Request, res: Response, next: NextFunction): Promise<any> {
|
||||
export async function listRoleActions(
|
||||
req: Request,
|
||||
res: Response,
|
||||
next: NextFunction
|
||||
): Promise<any> {
|
||||
try {
|
||||
const parsedParams = listRoleActionsSchema.safeParse(req.params);
|
||||
if (!parsedParams.success) {
|
||||
return next(
|
||||
createHttpError(
|
||||
HttpCode.BAD_REQUEST,
|
||||
parsedParams.error.errors.map(e => e.message).join(', ')
|
||||
fromError(parsedParams.error).toString()
|
||||
)
|
||||
);
|
||||
}
|
||||
@@ -28,9 +33,17 @@ export async function listRoleActions(req: Request, res: Response, next: NextFun
|
||||
const { roleId } = parsedParams.data;
|
||||
|
||||
// Check if the user has permission to list role actions
|
||||
const hasPermission = await checkUserActionPermission(ActionsEnum.listRoleActions, req);
|
||||
const hasPermission = await checkUserActionPermission(
|
||||
ActionsEnum.listRoleActions,
|
||||
req
|
||||
);
|
||||
if (!hasPermission) {
|
||||
return next(createHttpError(HttpCode.FORBIDDEN, 'User does not have permission to perform this action'));
|
||||
return next(
|
||||
createHttpError(
|
||||
HttpCode.FORBIDDEN,
|
||||
"User does not have permission to perform this action"
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
const roleActionsList = await db
|
||||
@@ -43,7 +56,7 @@ export async function listRoleActions(req: Request, res: Response, next: NextFun
|
||||
.innerJoin(actions, eq(roleActions.actionId, actions.actionId))
|
||||
.where(eq(roleActions.roleId, roleId));
|
||||
|
||||
// TODO: Do we need to filter out what the user can see?
|
||||
// TODO: Do we need to filter out what the user can see?
|
||||
|
||||
return response(res, {
|
||||
data: roleActionsList,
|
||||
@@ -54,6 +67,11 @@ export async function listRoleActions(req: Request, res: Response, next: NextFun
|
||||
});
|
||||
} catch (error) {
|
||||
logger.error(error);
|
||||
return next(createHttpError(HttpCode.INTERNAL_SERVER_ERROR, "An error occurred..."));
|
||||
return next(
|
||||
createHttpError(
|
||||
HttpCode.INTERNAL_SERVER_ERROR,
|
||||
"An error occurred..."
|
||||
)
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user