import { Request, Response, NextFunction } from "express"; import { z } from "zod"; import { db, resources } from "@server/db"; import { roleResources, roles } from "@server/db"; import response from "@server/lib/response"; import HttpCode from "@server/types/HttpCode"; import createHttpError from "http-errors"; import logger from "@server/logger"; import { fromError } from "zod-validation-error"; import { eq, and } from "drizzle-orm"; import { OpenAPITags, registry } from "@server/openApi"; const removeRoleFromResourceBodySchema = z .object({ roleId: z.number().int().positive() }) .strict(); const removeRoleFromResourceParamsSchema = z .object({ resourceId: z .string() .transform(Number) .pipe(z.number().int().positive()) }) .strict(); registry.registerPath({ method: "post", path: "/resource/{resourceId}/roles/remove", description: "Remove a single role from a resource.", tags: [OpenAPITags.Resource, OpenAPITags.Role], request: { params: removeRoleFromResourceParamsSchema, body: { content: { "application/json": { schema: removeRoleFromResourceBodySchema } } } }, responses: {} }); export async function removeRoleFromResource( req: Request, res: Response, next: NextFunction ): Promise { try { const parsedBody = removeRoleFromResourceBodySchema.safeParse(req.body); if (!parsedBody.success) { return next( createHttpError( HttpCode.BAD_REQUEST, fromError(parsedBody.error).toString() ) ); } const { roleId } = parsedBody.data; const parsedParams = removeRoleFromResourceParamsSchema.safeParse( req.params ); if (!parsedParams.success) { return next( createHttpError( HttpCode.BAD_REQUEST, fromError(parsedParams.error).toString() ) ); } const { resourceId } = parsedParams.data; // get the resource const [resource] = await db .select() .from(resources) .where(eq(resources.resourceId, resourceId)) .limit(1); if (!resource) { return next( createHttpError(HttpCode.NOT_FOUND, "Resource not found") ); } // Check if the role is an admin role const [roleToCheck] = await db .select() .from(roles) .where( and(eq(roles.roleId, roleId), eq(roles.orgId, resource.orgId)) ) .limit(1); if (!roleToCheck) { return next( createHttpError( HttpCode.NOT_FOUND, "Role not found or does not belong to the same organization" ) ); } if (roleToCheck.isAdmin) { return next( createHttpError( HttpCode.BAD_REQUEST, "Admin role cannot be removed from resources" ) ); } // Check if role exists in resource const existingEntry = await db .select() .from(roleResources) .where( and( eq(roleResources.resourceId, resourceId), eq(roleResources.roleId, roleId) ) ); if (existingEntry.length === 0) { return next( createHttpError( HttpCode.NOT_FOUND, "Role not found in resource" ) ); } await db .delete(roleResources) .where( and( eq(roleResources.resourceId, resourceId), eq(roleResources.roleId, roleId) ) ); return response(res, { data: {}, success: true, error: false, message: "Role removed from resource successfully", status: HttpCode.OK }); } catch (error) { logger.error(error); return next( createHttpError(HttpCode.INTERNAL_SERVER_ERROR, "An error occurred") ); } }