|
@@ -1,79 +1,98 @@
|
|
|
-import { Request, Response, NextFunction } from 'express';
|
|
|
-import { z } from 'zod';
|
|
|
-import { db } from '@server/db';
|
|
|
-import { orgs } from '@server/db/schema';
|
|
|
+import { Request, Response, NextFunction } from "express";
|
|
|
+import { z } from "zod";
|
|
|
+import { db } from "@server/db";
|
|
|
+import { Org, orgs } from "@server/db/schema";
|
|
|
import response from "@server/utils/response";
|
|
|
-import HttpCode from '@server/types/HttpCode';
|
|
|
-import createHttpError from 'http-errors';
|
|
|
-import { sql, inArray } from 'drizzle-orm';
|
|
|
-import { ActionsEnum, checkUserActionPermission } from '@server/auth/actions';
|
|
|
-import logger from '@server/logger';
|
|
|
+import HttpCode from "@server/types/HttpCode";
|
|
|
+import createHttpError from "http-errors";
|
|
|
+import { sql, inArray } from "drizzle-orm";
|
|
|
+import { ActionsEnum, checkUserActionPermission } from "@server/auth/actions";
|
|
|
+import logger from "@server/logger";
|
|
|
|
|
|
const listOrgsSchema = z.object({
|
|
|
- limit: z.string().optional().transform(Number).pipe(z.number().int().positive().default(10)),
|
|
|
- offset: z.string().optional().transform(Number).pipe(z.number().int().nonnegative().default(0)),
|
|
|
+ limit: z
|
|
|
+ .string()
|
|
|
+ .optional()
|
|
|
+ .transform(Number)
|
|
|
+ .pipe(z.number().int().positive().default(10)),
|
|
|
+ offset: z
|
|
|
+ .string()
|
|
|
+ .optional()
|
|
|
+ .transform(Number)
|
|
|
+ .pipe(z.number().int().nonnegative().default(0)),
|
|
|
});
|
|
|
|
|
|
-export async function listOrgs(req: Request, res: Response, next: NextFunction): Promise<any> {
|
|
|
+export type ListOrgsResponse = {
|
|
|
+ organizations: Org[];
|
|
|
+ pagination: { total: number; limit: number; offset: number };
|
|
|
+};
|
|
|
+
|
|
|
+export async function listOrgs(
|
|
|
+ req: Request,
|
|
|
+ res: Response,
|
|
|
+ next: NextFunction,
|
|
|
+): Promise<any> {
|
|
|
try {
|
|
|
const parsedQuery = listOrgsSchema.safeParse(req.query);
|
|
|
if (!parsedQuery.success) {
|
|
|
return next(
|
|
|
createHttpError(
|
|
|
HttpCode.BAD_REQUEST,
|
|
|
- parsedQuery.error.errors.map(e => e.message).join(', ')
|
|
|
- )
|
|
|
+ parsedQuery.error.errors.map((e) => e.message).join(", "),
|
|
|
+ ),
|
|
|
);
|
|
|
}
|
|
|
|
|
|
const { limit, offset } = parsedQuery.data;
|
|
|
|
|
|
// Check if the user has permission to list sites
|
|
|
- const hasPermission = await checkUserActionPermission(ActionsEnum.listOrgs, req);
|
|
|
+ const hasPermission = await checkUserActionPermission(
|
|
|
+ ActionsEnum.listOrgs,
|
|
|
+ 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",
|
|
|
+ ),
|
|
|
+ );
|
|
|
}
|
|
|
|
|
|
// Use the userOrgs passed from the middleware
|
|
|
const userOrgIds = req.userOrgIds;
|
|
|
|
|
|
if (!userOrgIds || userOrgIds.length === 0) {
|
|
|
- return res.status(HttpCode.OK).send(
|
|
|
- response(res, {
|
|
|
- data: {
|
|
|
- organizations: [],
|
|
|
- pagination: {
|
|
|
- total: 0,
|
|
|
- limit,
|
|
|
- offset,
|
|
|
- },
|
|
|
+ return response<ListOrgsResponse>(res, {
|
|
|
+ data: {
|
|
|
+ organizations: [],
|
|
|
+ pagination: {
|
|
|
+ total: 0,
|
|
|
+ limit,
|
|
|
+ offset,
|
|
|
},
|
|
|
- success: true,
|
|
|
- error: false,
|
|
|
- message: "No organizations found for the user",
|
|
|
- status: HttpCode.OK,
|
|
|
- })
|
|
|
- );
|
|
|
+ },
|
|
|
+ success: true,
|
|
|
+ error: false,
|
|
|
+ message: "No organizations found for the user",
|
|
|
+ status: HttpCode.OK,
|
|
|
+ });
|
|
|
}
|
|
|
|
|
|
- const organizations = await db.select()
|
|
|
+ const organizations = await db
|
|
|
+ .select()
|
|
|
.from(orgs)
|
|
|
.where(inArray(orgs.orgId, userOrgIds))
|
|
|
.limit(limit)
|
|
|
.offset(offset);
|
|
|
|
|
|
- const totalCountResult = await db.select({ count: sql<number>`cast(count(*) as integer)` })
|
|
|
+ const totalCountResult = await db
|
|
|
+ .select({ count: sql<number>`cast(count(*) as integer)` })
|
|
|
.from(orgs)
|
|
|
.where(inArray(orgs.orgId, userOrgIds));
|
|
|
const totalCount = totalCountResult[0].count;
|
|
|
|
|
|
- // // Add the user's role for each organization
|
|
|
- // const organizationsWithRoles = organizations.map(org => ({
|
|
|
- // ...org,
|
|
|
- // userRole: req.userOrgRoleIds[org.orgId],
|
|
|
- // }));
|
|
|
-
|
|
|
- return response(res, {
|
|
|
+ return response<ListOrgsResponse>(res, {
|
|
|
data: {
|
|
|
organizations,
|
|
|
pagination: {
|
|
@@ -89,6 +108,11 @@ export async function listOrgs(req: Request, res: Response, next: NextFunction):
|
|
|
});
|
|
|
} 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...",
|
|
|
+ ),
|
|
|
+ );
|
|
|
}
|
|
|
}
|