mirror of
https://github.com/fosrl/pangolin.git
synced 2025-07-10 05:54:58 +02:00
65 lines
1.9 KiB
TypeScript
65 lines
1.9 KiB
TypeScript
|
import { Request, Response, NextFunction } from "express";
|
||
|
import { z } from "zod";
|
||
|
import { db } from "@server/db";
|
||
|
import { resourceWhitelist, users } from "@server/db/schema"; // Assuming these are the correct tables
|
||
|
import { eq } from "drizzle-orm";
|
||
|
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";
|
||
|
|
||
|
const getResourceWhitelistSchema = z.object({
|
||
|
resourceId: z.string().transform(Number).pipe(z.number().int().positive())
|
||
|
});
|
||
|
|
||
|
async function queryWhitelist(resourceId: number) {
|
||
|
return await db
|
||
|
.select({
|
||
|
email: resourceWhitelist.email
|
||
|
})
|
||
|
.from(resourceWhitelist)
|
||
|
.where(eq(resourceWhitelist.resourceId, resourceId));
|
||
|
}
|
||
|
|
||
|
export type GetResourceWhitelistResponse = {
|
||
|
whitelist: NonNullable<Awaited<ReturnType<typeof queryWhitelist>>>;
|
||
|
};
|
||
|
|
||
|
export async function getResourceWhitelist(
|
||
|
req: Request,
|
||
|
res: Response,
|
||
|
next: NextFunction
|
||
|
): Promise<any> {
|
||
|
try {
|
||
|
const parsedParams = getResourceWhitelistSchema.safeParse(req.params);
|
||
|
if (!parsedParams.success) {
|
||
|
return next(
|
||
|
createHttpError(
|
||
|
HttpCode.BAD_REQUEST,
|
||
|
fromError(parsedParams.error).toString()
|
||
|
)
|
||
|
);
|
||
|
}
|
||
|
|
||
|
const { resourceId } = parsedParams.data;
|
||
|
|
||
|
const whitelist = await queryWhitelist(resourceId);
|
||
|
|
||
|
return response<GetResourceWhitelistResponse>(res, {
|
||
|
data: {
|
||
|
whitelist
|
||
|
},
|
||
|
success: true,
|
||
|
error: false,
|
||
|
message: "Resource whitelist retrieved successfully",
|
||
|
status: HttpCode.OK
|
||
|
});
|
||
|
} catch (error) {
|
||
|
logger.error(error);
|
||
|
return next(
|
||
|
createHttpError(HttpCode.INTERNAL_SERVER_ERROR, "An error occurred")
|
||
|
);
|
||
|
}
|
||
|
}
|