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

119 lines
3.4 KiB
TypeScript
Raw Normal View History

2024-10-19 18:47:41 -04:00
import { db } from "@server/db";
import { targets } from "@server/db/schema";
2024-10-19 18:47:41 -04:00
import HttpCode from "@server/types/HttpCode";
2025-01-01 21:41:31 -05:00
import response from "@server/lib/response";
2024-10-19 18:47:41 -04:00
import { eq, 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";
import logger from "@server/logger";
2024-10-02 21:17:38 -04:00
2024-12-21 21:01:12 -05:00
const listTargetsParamsSchema = z
.object({
resourceId: z
.string()
.transform(Number)
.pipe(z.number().int().positive())
})
.strict();
2024-10-02 22:05:21 -04:00
2024-10-02 21:17:38 -04:00
const listTargetsSchema = z.object({
2024-10-19 18:47:41 -04:00
limit: z
.string()
.optional()
.default("1000")
.transform(Number)
.pipe(z.number().int().positive()),
offset: z
.string()
.optional()
.default("0")
.transform(Number)
2024-12-21 21:01:12 -05:00
.pipe(z.number().int().nonnegative())
2024-10-02 21:17:38 -04:00
});
2024-10-26 12:15:03 -04:00
function queryTargets(resourceId: number) {
2024-10-19 18:47:41 -04:00
let baseQuery = db
.select({
targetId: targets.targetId,
ip: targets.ip,
method: targets.method,
port: targets.port,
enabled: targets.enabled,
2024-12-21 21:01:12 -05:00
resourceId: targets.resourceId
2024-10-19 18:47:41 -04:00
// resourceName: resources.name,
})
.from(targets)
// .leftJoin(resources, eq(targets.resourceId, resources.resourceId))
.where(eq(targets.resourceId, resourceId));
return baseQuery;
}
export type ListTargetsResponse = {
targets: Awaited<ReturnType<typeof queryTargets>>;
pagination: { total: number; limit: number; offset: number };
};
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,
2024-10-19 18:47:41 -04:00
fromError(parsedQuery.error)
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,
2024-10-19 18:47:41 -04:00
fromError(parsedParams.error)
2024-10-06 18:05:20 -04:00
)
);
}
const { resourceId } = parsedParams.data;
2024-10-02 21:17:38 -04:00
2024-10-19 18:47:41 -04:00
const baseQuery = queryTargets(resourceId);
2024-10-06 18:05:20 -04:00
2024-10-19 18:47:41 -04:00
let countQuery = db
.select({ count: sql<number>`cast(count(*) as integer)` })
.from(targets)
.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-19 18:47:41 -04:00
return response<ListTargetsResponse>(res, {
2024-10-06 18:05:20 -04:00
data: {
targets: targetsList,
pagination: {
total: totalCount,
limit,
2024-12-21 21:01:12 -05:00
offset
}
2024-10-06 18:05:20 -04:00
},
success: true,
error: false,
message: "Targets retrieved successfully",
2024-12-21 21:01:12 -05:00
status: HttpCode.OK
2024-10-06 18:05:20 -04:00
});
} catch (error) {
logger.error(error);
2024-10-19 18:47:41 -04:00
return next(
createHttpError(HttpCode.INTERNAL_SERVER_ERROR, "An error occurred")
2024-10-19 18:47:41 -04:00
);
2024-10-06 18:05:20 -04:00
}
}