show resources table, check org access, and handle redirects on root

This commit is contained in:
Milo Schwartz
2024-10-19 15:49:16 -04:00
parent edde7a247a
commit f6c7c017cb
14 changed files with 416 additions and 95 deletions

View File

@@ -1,39 +1,56 @@
import { Request, Response, NextFunction } from 'express';
import { z } from 'zod';
import { db } from '@server/db';
import { orgs } 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 { Org, orgs } 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";
const getOrgSchema = z.object({
orgId: z.string()
orgId: z.string(),
});
export async function getOrg(req: Request, res: Response, next: NextFunction): Promise<any> {
export type GetOrgResponse = {
org: Org;
}
export async function getOrg(
req: Request,
res: Response,
next: NextFunction,
): Promise<any> {
try {
const parsedParams = getOrgSchema.safeParse(req.params);
if (!parsedParams.success) {
return next(
createHttpError(
HttpCode.BAD_REQUEST,
parsedParams.error.errors.map(e => e.message).join(', ')
)
parsedParams.error.errors.map((e) => e.message).join(", "),
),
);
}
const { orgId } = parsedParams.data;
// Check if the user has permission to list sites
const hasPermission = await checkUserActionPermission(ActionsEnum.getOrg, req);
const hasPermission = await checkUserActionPermission(
ActionsEnum.getOrg,
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 org = await db.select()
const org = await db
.select()
.from(orgs)
.where(eq(orgs.orgId, orgId))
.limit(1);
@@ -42,13 +59,15 @@ export async function getOrg(req: Request, res: Response, next: NextFunction): P
return next(
createHttpError(
HttpCode.NOT_FOUND,
`Organization with ID ${orgId} not found`
)
`Organization with ID ${orgId} not found`,
),
);
}
return response(res, {
data: org[0],
return response<GetOrgResponse>(res, {
data: {
org: org[0],
},
success: true,
error: false,
message: "Organization retrieved successfully",
@@ -56,6 +75,11 @@ export async function getOrg(req: Request, res: Response, next: NextFunction): P
});
} 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...",
),
);
}
}