|
@@ -1,92 +1,147 @@
|
|
-import { Request, Response, NextFunction } from 'express';
|
|
|
|
-import { z } from 'zod';
|
|
|
|
-import { db } from '@server/db';
|
|
|
|
-import { sites, orgs, exitNodes, userSites, roleSites } from '@server/db/schema';
|
|
|
|
|
|
+import { ActionsEnum, checkUserActionPermission } from "@server/auth/actions";
|
|
|
|
+import { db } from "@server/db";
|
|
|
|
+import {
|
|
|
|
+ orgs,
|
|
|
|
+ roleSites,
|
|
|
|
+ sites,
|
|
|
|
+ userSites,
|
|
|
|
+} from "@server/db/schema";
|
|
|
|
+import HttpCode from "@server/types/HttpCode";
|
|
import response from "@server/utils/response";
|
|
import response from "@server/utils/response";
|
|
-import HttpCode from '@server/types/HttpCode';
|
|
|
|
-import createHttpError from 'http-errors';
|
|
|
|
-import { sql, eq, and, or, inArray } from 'drizzle-orm';
|
|
|
|
-import { ActionsEnum, checkUserActionPermission } from '@server/auth/actions';
|
|
|
|
-import logger from '@server/logger';
|
|
|
|
|
|
+import { and, eq, inArray, or, sql } from "drizzle-orm";
|
|
|
|
+import { NextFunction, Request, Response } from "express";
|
|
|
|
+import createHttpError from "http-errors";
|
|
|
|
+import { z } from "zod";
|
|
|
|
+import { fromError } from "zod-validation-error";
|
|
|
|
|
|
const listSitesParamsSchema = z.object({
|
|
const listSitesParamsSchema = z.object({
|
|
- orgId: z.string().optional().transform(Number).pipe(z.number().int().positive()),
|
|
|
|
|
|
+ orgId: z.string(),
|
|
});
|
|
});
|
|
|
|
|
|
const listSitesSchema = z.object({
|
|
const listSitesSchema = 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()
|
|
|
|
+ .default("1000")
|
|
|
|
+ .transform(Number)
|
|
|
|
+ .pipe(z.number().int().positive()),
|
|
|
|
+ offset: z
|
|
|
|
+ .string()
|
|
|
|
+ .optional()
|
|
|
|
+ .default("0")
|
|
|
|
+ .transform(Number)
|
|
|
|
+ .pipe(z.number().int().nonnegative()),
|
|
});
|
|
});
|
|
|
|
|
|
-export async function listSites(req: Request, res: Response, next: NextFunction): Promise<any> {
|
|
|
|
|
|
+function querySites(orgId: string, accessibleSiteIds: number[]) {
|
|
|
|
+ return db
|
|
|
|
+ .select({
|
|
|
|
+ siteId: sites.siteId,
|
|
|
|
+ name: sites.name,
|
|
|
|
+ subdomain: sites.subdomain,
|
|
|
|
+ pubKey: sites.pubKey,
|
|
|
|
+ subnet: sites.subnet,
|
|
|
|
+ megabytesIn: sites.megabytesIn,
|
|
|
|
+ megabytesOut: sites.megabytesOut,
|
|
|
|
+ orgName: orgs.name,
|
|
|
|
+ })
|
|
|
|
+ .from(sites)
|
|
|
|
+ .leftJoin(orgs, eq(sites.orgId, orgs.orgId))
|
|
|
|
+ .where(
|
|
|
|
+ and(
|
|
|
|
+ inArray(sites.siteId, accessibleSiteIds),
|
|
|
|
+ eq(sites.orgId, orgId),
|
|
|
|
+ ),
|
|
|
|
+ );
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+export type ListSitesResponse = {
|
|
|
|
+ sites: Awaited<ReturnType<typeof querySites>>;
|
|
|
|
+ pagination: { total: number; limit: number; offset: number };
|
|
|
|
+};
|
|
|
|
+
|
|
|
|
+export async function listSites(
|
|
|
|
+ req: Request,
|
|
|
|
+ res: Response,
|
|
|
|
+ next: NextFunction,
|
|
|
|
+): Promise<any> {
|
|
try {
|
|
try {
|
|
const parsedQuery = listSitesSchema.safeParse(req.query);
|
|
const parsedQuery = listSitesSchema.safeParse(req.query);
|
|
if (!parsedQuery.success) {
|
|
if (!parsedQuery.success) {
|
|
- return next(createHttpError(HttpCode.BAD_REQUEST, parsedQuery.error.errors.map(e => e.message).join(', ')));
|
|
|
|
|
|
+ return next(
|
|
|
|
+ createHttpError(
|
|
|
|
+ HttpCode.BAD_REQUEST,
|
|
|
|
+ fromError(parsedQuery.error),
|
|
|
|
+ ),
|
|
|
|
+ );
|
|
}
|
|
}
|
|
const { limit, offset } = parsedQuery.data;
|
|
const { limit, offset } = parsedQuery.data;
|
|
|
|
|
|
-
|
|
|
|
const parsedParams = listSitesParamsSchema.safeParse(req.params);
|
|
const parsedParams = listSitesParamsSchema.safeParse(req.params);
|
|
if (!parsedParams.success) {
|
|
if (!parsedParams.success) {
|
|
- return next(createHttpError(HttpCode.BAD_REQUEST, parsedParams.error.errors.map(e => e.message).join(', ')));
|
|
|
|
|
|
+ return next(
|
|
|
|
+ createHttpError(
|
|
|
|
+ HttpCode.BAD_REQUEST,
|
|
|
|
+ parsedParams.error.errors.map((e) => e.message).join(", "),
|
|
|
|
+ ),
|
|
|
|
+ );
|
|
}
|
|
}
|
|
const { orgId } = parsedParams.data;
|
|
const { orgId } = parsedParams.data;
|
|
|
|
|
|
// Check if the user has permission to list sites
|
|
// Check if the user has permission to list sites
|
|
- const hasPermission = await checkUserActionPermission(ActionsEnum.listSites, req);
|
|
|
|
|
|
+ const hasPermission = await checkUserActionPermission(
|
|
|
|
+ ActionsEnum.listSites,
|
|
|
|
+ req,
|
|
|
|
+ );
|
|
if (!hasPermission) {
|
|
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",
|
|
|
|
+ ),
|
|
|
|
+ );
|
|
}
|
|
}
|
|
|
|
|
|
if (orgId && orgId !== req.userOrgId) {
|
|
if (orgId && orgId !== req.userOrgId) {
|
|
- return next(createHttpError(HttpCode.FORBIDDEN, 'User does not have access to this organization'));
|
|
|
|
|
|
+ return next(
|
|
|
|
+ createHttpError(
|
|
|
|
+ HttpCode.FORBIDDEN,
|
|
|
|
+ "User does not have access to this organization",
|
|
|
|
+ ),
|
|
|
|
+ );
|
|
}
|
|
}
|
|
|
|
|
|
const accessibleSites = await db
|
|
const accessibleSites = await db
|
|
- .select({ siteId: sql<number>`COALESCE(${userSites.siteId}, ${roleSites.siteId})` })
|
|
|
|
|
|
+ .select({
|
|
|
|
+ siteId: sql<number>`COALESCE(${userSites.siteId}, ${roleSites.siteId})`,
|
|
|
|
+ })
|
|
.from(userSites)
|
|
.from(userSites)
|
|
.fullJoin(roleSites, eq(userSites.siteId, roleSites.siteId))
|
|
.fullJoin(roleSites, eq(userSites.siteId, roleSites.siteId))
|
|
.where(
|
|
.where(
|
|
or(
|
|
or(
|
|
eq(userSites.userId, req.user!.userId),
|
|
eq(userSites.userId, req.user!.userId),
|
|
- eq(roleSites.roleId, req.userOrgRoleId!)
|
|
|
|
- )
|
|
|
|
|
|
+ eq(roleSites.roleId, req.userOrgRoleId!),
|
|
|
|
+ ),
|
|
);
|
|
);
|
|
|
|
|
|
- const accessibleSiteIds = accessibleSites.map(site => site.siteId);
|
|
|
|
-
|
|
|
|
- let baseQuery: any = db
|
|
|
|
- .select({
|
|
|
|
- siteId: sites.siteId,
|
|
|
|
- name: sites.name,
|
|
|
|
- subdomain: sites.subdomain,
|
|
|
|
- pubKey: sites.pubKey,
|
|
|
|
- subnet: sites.subnet,
|
|
|
|
- megabytesIn: sites.megabytesIn,
|
|
|
|
- megabytesOut: sites.megabytesOut,
|
|
|
|
- orgName: orgs.name,
|
|
|
|
- exitNodeName: exitNodes.name,
|
|
|
|
- })
|
|
|
|
- .from(sites)
|
|
|
|
- .leftJoin(orgs, eq(sites.orgId, orgs.orgId))
|
|
|
|
- .where(inArray(sites.siteId, accessibleSiteIds));
|
|
|
|
|
|
+ const accessibleSiteIds = accessibleSites.map((site) => site.siteId);
|
|
|
|
+ const baseQuery = querySites(orgId, accessibleSiteIds);
|
|
|
|
|
|
- let countQuery: any = db
|
|
|
|
|
|
+ let countQuery = db
|
|
.select({ count: sql<number>`cast(count(*) as integer)` })
|
|
.select({ count: sql<number>`cast(count(*) as integer)` })
|
|
.from(sites)
|
|
.from(sites)
|
|
- .where(inArray(sites.siteId, accessibleSiteIds));
|
|
|
|
-
|
|
|
|
- if (orgId) {
|
|
|
|
- baseQuery = baseQuery.where(eq(sites.orgId, orgId));
|
|
|
|
- countQuery = countQuery.where(eq(sites.orgId, orgId));
|
|
|
|
- }
|
|
|
|
|
|
+ .where(
|
|
|
|
+ and(
|
|
|
|
+ inArray(sites.siteId, accessibleSiteIds),
|
|
|
|
+ eq(sites.orgId, orgId),
|
|
|
|
+ ),
|
|
|
|
+ );
|
|
|
|
|
|
const sitesList = await baseQuery.limit(limit).offset(offset);
|
|
const sitesList = await baseQuery.limit(limit).offset(offset);
|
|
const totalCountResult = await countQuery;
|
|
const totalCountResult = await countQuery;
|
|
const totalCount = totalCountResult[0].count;
|
|
const totalCount = totalCountResult[0].count;
|
|
|
|
|
|
- return response(res, {
|
|
|
|
|
|
+ return response<ListSitesResponse>(res, {
|
|
data: {
|
|
data: {
|
|
sites: sitesList,
|
|
sites: sitesList,
|
|
pagination: {
|
|
pagination: {
|
|
@@ -101,7 +156,11 @@ export async function listSites(req: Request, res: Response, next: NextFunction)
|
|
status: HttpCode.OK,
|
|
status: HttpCode.OK,
|
|
});
|
|
});
|
|
} catch (error) {
|
|
} 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...",
|
|
|
|
+ ),
|
|
|
|
+ );
|
|
}
|
|
}
|
|
}
|
|
}
|