mirror of
https://github.com/fosrl/pangolin.git
synced 2026-02-27 15:26:41 +00:00
refactor contexts, format zod errors, and more refactoring
This commit is contained in:
@@ -1,27 +1,32 @@
|
||||
import { Request, Response, NextFunction } from 'express';
|
||||
import { z } from 'zod';
|
||||
import { db } from '@server/db';
|
||||
import { resources, userResources, userSites } from '@server/db/schema';
|
||||
import { Request, Response, NextFunction } from "express";
|
||||
import { z } from "zod";
|
||||
import { db } from "@server/db";
|
||||
import { resources, userResources, userSites } from "@server/db/schema";
|
||||
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 { eq } from 'drizzle-orm';
|
||||
import HttpCode from "@server/types/HttpCode";
|
||||
import createHttpError from "http-errors";
|
||||
import { ActionsEnum, checkUserActionPermission } from "@server/auth/actions";
|
||||
import logger from "@server/logger";
|
||||
import { eq } from "drizzle-orm";
|
||||
import { fromError } from "zod-validation-error";
|
||||
|
||||
const addUserSiteSchema = z.object({
|
||||
userId: z.string(),
|
||||
siteId: z.string().transform(Number).pipe(z.number().int().positive()),
|
||||
});
|
||||
|
||||
export async function addUserSite(req: Request, res: Response, next: NextFunction): Promise<any> {
|
||||
export async function addUserSite(
|
||||
req: Request,
|
||||
res: Response,
|
||||
next: NextFunction
|
||||
): Promise<any> {
|
||||
try {
|
||||
const parsedBody = addUserSiteSchema.safeParse(req.body);
|
||||
if (!parsedBody.success) {
|
||||
return next(
|
||||
createHttpError(
|
||||
HttpCode.BAD_REQUEST,
|
||||
parsedBody.error.errors.map(e => e.message).join(', ')
|
||||
fromError(parsedBody.error).toString()
|
||||
)
|
||||
);
|
||||
}
|
||||
@@ -29,18 +34,30 @@ export async function addUserSite(req: Request, res: Response, next: NextFunctio
|
||||
const { userId, siteId } = parsedBody.data;
|
||||
|
||||
// Check if the user has permission to add user sites
|
||||
const hasPermission = await checkUserActionPermission(ActionsEnum.addUserSite, req);
|
||||
const hasPermission = await checkUserActionPermission(
|
||||
ActionsEnum.addUserSite,
|
||||
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 newUserSite = await db.insert(userSites).values({
|
||||
userId,
|
||||
siteId,
|
||||
}).returning();
|
||||
const newUserSite = await db
|
||||
.insert(userSites)
|
||||
.values({
|
||||
userId,
|
||||
siteId,
|
||||
})
|
||||
.returning();
|
||||
|
||||
// Add all resources associated with the site to the user
|
||||
const siteResources = await db.select()
|
||||
const siteResources = await db
|
||||
.select()
|
||||
.from(resources)
|
||||
.where(eq(resources.siteId, siteId));
|
||||
|
||||
@@ -60,6 +77,11 @@ export async function addUserSite(req: Request, res: Response, next: NextFunctio
|
||||
});
|
||||
} 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