fosrl.pangolin/server/routers/target/listTargets.ts

95 lines
3.4 KiB
TypeScript
Raw Normal View History

2024-10-02 21:17:38 -04:00
import { Request, Response, NextFunction } from 'express';
import { z } from 'zod';
import { db } from '@server/db';
import { targets, resources } from '@server/db/schema';
import response from "@server/utils/response";
import HttpCode from '@server/types/HttpCode';
import createHttpError from 'http-errors';
import { sql, eq } from 'drizzle-orm';
2024-10-06 16:43:59 -04:00
import { ActionsEnum, checkUserActionPermission } from '@server/auth/actions';
2024-10-06 18:05:20 -04:00
import logger from '@server/logger';
2024-10-02 21:17:38 -04:00
2024-10-02 22:05:21 -04:00
const listTargetsParamsSchema = z.object({
2024-10-02 22:17:43 -04:00
resourceId: z.string().optional()
2024-10-02 22:05:21 -04:00
});
2024-10-02 21:17:38 -04:00
const listTargetsSchema = z.object({
2024-10-06 18:05:20 -04:00
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)),
2024-10-02 21:17:38 -04:00
});
export async function listTargets(req: Request, res: Response, next: NextFunction): Promise<any> {
2024-10-06 18:05:20 -04:00
try {
const parsedQuery = listTargetsSchema.safeParse(req.query);
if (!parsedQuery.success) {
return next(
createHttpError(
HttpCode.BAD_REQUEST,
parsedQuery.error.errors.map(e => e.message).join(', ')
)
);
}
2024-10-02 21:17:38 -04:00
2024-10-06 18:05:20 -04:00
const { limit, offset } = parsedQuery.data;
2024-10-02 22:05:21 -04:00
2024-10-06 18:05:20 -04:00
const parsedParams = listTargetsParamsSchema.safeParse(req.params);
if (!parsedParams.success) {
return next(
createHttpError(
HttpCode.BAD_REQUEST,
parsedParams.error.errors.map(e => e.message).join(', ')
)
);
}
2024-10-02 22:05:21 -04:00
2024-10-06 18:05:20 -04:00
const { resourceId } = parsedParams.data;
2024-10-02 21:17:38 -04:00
2024-10-06 18:05:20 -04:00
// Check if the user has permission to list sites
const hasPermission = await checkUserActionPermission(ActionsEnum.listTargets, req);
if (!hasPermission) {
return next(createHttpError(HttpCode.FORBIDDEN, 'User does not have permission to list sites'));
}
2024-10-02 21:17:38 -04:00
2024-10-06 18:05:20 -04:00
let baseQuery: any = db
.select({
targetId: targets.targetId,
ip: targets.ip,
method: targets.method,
port: targets.port,
protocol: targets.protocol,
enabled: targets.enabled,
resourceName: resources.name,
})
.from(targets)
.leftJoin(resources, eq(targets.resourceId, resources.resourceId));
2024-10-02 21:17:38 -04:00
2024-10-06 18:05:20 -04:00
let countQuery: any = db.select({ count: sql<number>`cast(count(*) as integer)` }).from(targets);
if (resourceId) {
baseQuery = baseQuery.where(eq(targets.resourceId, resourceId));
countQuery = countQuery.where(eq(targets.resourceId, resourceId));
}
2024-10-02 21:17:38 -04:00
2024-10-06 18:05:20 -04:00
const targetsList = await baseQuery.limit(limit).offset(offset);
const totalCountResult = await countQuery;
const totalCount = totalCountResult[0].count;
2024-10-02 21:17:38 -04:00
2024-10-06 18:05:20 -04:00
return response(res, {
data: {
targets: targetsList,
pagination: {
total: totalCount,
limit,
offset,
},
},
success: true,
error: false,
message: "Targets retrieved successfully",
status: HttpCode.OK,
});
} catch (error) {
logger.error(error);
return next(createHttpError(HttpCode.INTERNAL_SERVER_ERROR, "An error occurred..."));
}
2024-10-02 21:17:38 -04:00
}