mirror of
https://github.com/fosrl/pangolin.git
synced 2025-07-23 20:24:34 +02:00
Fix return response and add list
This commit is contained in:
parent
e85c94d21d
commit
ecdf2dfd04
22 changed files with 354 additions and 17 deletions
|
@ -31,7 +31,7 @@ export async function createOrg(req: Request, res: Response, next: NextFunction)
|
|||
}).returning();
|
||||
|
||||
return res.status(HttpCode.CREATED).send(
|
||||
response({
|
||||
response(res, {
|
||||
data: newOrg[0],
|
||||
success: true,
|
||||
error: false,
|
||||
|
|
|
@ -39,7 +39,7 @@ export async function deleteOrg(req: Request, res: Response, next: NextFunction)
|
|||
}
|
||||
|
||||
return res.status(HttpCode.OK).send(
|
||||
response({
|
||||
response(res, {
|
||||
data: null,
|
||||
success: true,
|
||||
error: false,
|
||||
|
|
|
@ -40,7 +40,7 @@ export async function getOrg(req: Request, res: Response, next: NextFunction): P
|
|||
}
|
||||
|
||||
return res.status(HttpCode.OK).send(
|
||||
response({
|
||||
response(res, {
|
||||
data: org[0],
|
||||
success: true,
|
||||
error: false,
|
||||
|
|
57
server/routers/org/listOrgs.ts
Normal file
57
server/routers/org/listOrgs.ts
Normal file
|
@ -0,0 +1,57 @@
|
|||
import { Request, Response, NextFunction } from 'express';
|
||||
import { z } from 'zod';
|
||||
import { db } from '@server/db';
|
||||
import { orgs } from '@server/db/schema';
|
||||
import response from "@server/utils/response";
|
||||
import HttpCode from '@server/types/HttpCode';
|
||||
import createHttpError from 'http-errors';
|
||||
import { sql } from 'drizzle-orm';
|
||||
|
||||
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)),
|
||||
});
|
||||
|
||||
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(', ')
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
const { limit, offset } = parsedQuery.data;
|
||||
|
||||
const organizations = await db.select()
|
||||
.from(orgs)
|
||||
.limit(limit)
|
||||
.offset(offset);
|
||||
|
||||
const totalCountResult = await db.select({ count: sql<number>`cast(count(*) as integer)` })
|
||||
.from(orgs);
|
||||
const totalCount = totalCountResult[0].count;
|
||||
|
||||
return res.status(HttpCode.OK).send(
|
||||
response(res, {
|
||||
data: {
|
||||
organizations,
|
||||
pagination: {
|
||||
total: totalCount,
|
||||
limit,
|
||||
offset,
|
||||
},
|
||||
},
|
||||
success: true,
|
||||
error: false,
|
||||
message: "Organizations retrieved successfully",
|
||||
status: HttpCode.OK,
|
||||
})
|
||||
);
|
||||
} catch (error) {
|
||||
next(error);
|
||||
}
|
||||
}
|
|
@ -58,7 +58,7 @@ export async function updateOrg(req: Request, res: Response, next: NextFunction)
|
|||
}
|
||||
|
||||
return res.status(HttpCode.OK).send(
|
||||
response({
|
||||
response(res, {
|
||||
data: updatedOrg[0],
|
||||
success: true,
|
||||
error: false,
|
||||
|
|
|
@ -40,7 +40,7 @@ export async function createResource(req: Request, res: Response, next: NextFunc
|
|||
}).returning();
|
||||
|
||||
return res.status(HttpCode.CREATED).send(
|
||||
response({
|
||||
response(res, {
|
||||
data: newResource[0],
|
||||
success: true,
|
||||
error: false,
|
||||
|
|
|
@ -42,7 +42,7 @@ export async function deleteResource(req: Request, res: Response, next: NextFunc
|
|||
}
|
||||
|
||||
return res.status(HttpCode.OK).send(
|
||||
response({
|
||||
response(res, {
|
||||
data: null,
|
||||
success: true,
|
||||
error: false,
|
||||
|
|
|
@ -43,7 +43,7 @@ export async function getResource(req: Request, res: Response, next: NextFunctio
|
|||
}
|
||||
|
||||
return res.status(HttpCode.OK).send(
|
||||
response({
|
||||
response(res, {
|
||||
data: resource[0],
|
||||
success: true,
|
||||
error: false,
|
||||
|
|
70
server/routers/resource/listResources.ts
Normal file
70
server/routers/resource/listResources.ts
Normal file
|
@ -0,0 +1,70 @@
|
|||
import { Request, Response, NextFunction } from 'express';
|
||||
import { z } from 'zod';
|
||||
import { db } from '@server/db';
|
||||
import { resources, sites } 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';
|
||||
|
||||
const listResourcesSchema = 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)),
|
||||
siteId: z.string().optional().transform(Number).pipe(z.number().int().positive()),
|
||||
});
|
||||
|
||||
export async function listResources(req: Request, res: Response, next: NextFunction): Promise<any> {
|
||||
try {
|
||||
const parsedQuery = listResourcesSchema.safeParse(req.query);
|
||||
if (!parsedQuery.success) {
|
||||
return next(
|
||||
createHttpError(
|
||||
HttpCode.BAD_REQUEST,
|
||||
parsedQuery.error.errors.map(e => e.message).join(', ')
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
const { limit, offset, siteId } = parsedQuery.data;
|
||||
|
||||
let baseQuery = db
|
||||
.select({
|
||||
resourceId: resources.resourceId,
|
||||
name: resources.name,
|
||||
subdomain: resources.subdomain,
|
||||
siteName: sites.name,
|
||||
})
|
||||
.from(resources)
|
||||
.leftJoin(sites, eq(resources.siteId, sites.siteId));
|
||||
|
||||
let countQuery = db.select({ count: sql<number>`cast(count(*) as integer)` }).from(resources);
|
||||
|
||||
if (siteId) {
|
||||
baseQuery = baseQuery.where(eq(resources.siteId, siteId));
|
||||
countQuery = countQuery.where(eq(resources.siteId, siteId));
|
||||
}
|
||||
|
||||
const resourcesList = await baseQuery.limit(limit).offset(offset);
|
||||
const totalCountResult = await countQuery;
|
||||
const totalCount = totalCountResult[0].count;
|
||||
|
||||
return res.status(HttpCode.OK).send(
|
||||
response(res, {
|
||||
data: {
|
||||
resources: resourcesList,
|
||||
pagination: {
|
||||
total: totalCount,
|
||||
limit,
|
||||
offset,
|
||||
},
|
||||
},
|
||||
success: true,
|
||||
error: false,
|
||||
message: "Resources retrieved successfully",
|
||||
status: HttpCode.OK,
|
||||
})
|
||||
);
|
||||
} catch (error) {
|
||||
next(error);
|
||||
}
|
||||
}
|
|
@ -63,7 +63,7 @@ export async function updateResource(req: Request, res: Response, next: NextFunc
|
|||
}
|
||||
|
||||
return res.status(HttpCode.OK).send(
|
||||
response({
|
||||
response(res, {
|
||||
data: updatedResource[0],
|
||||
success: true,
|
||||
error: false,
|
||||
|
|
|
@ -42,7 +42,7 @@ export async function deleteSite(req: Request, res: Response, next: NextFunction
|
|||
}
|
||||
|
||||
return res.status(HttpCode.OK).send(
|
||||
response({
|
||||
response(res, {
|
||||
data: null,
|
||||
success: true,
|
||||
error: false,
|
||||
|
|
|
@ -43,7 +43,7 @@ export async function getSite(req: Request, res: Response, next: NextFunction):
|
|||
}
|
||||
|
||||
return res.status(HttpCode.OK).send(
|
||||
response({
|
||||
response(res, {
|
||||
data: site[0],
|
||||
success: true,
|
||||
error: false,
|
||||
|
|
76
server/routers/site/listSites.ts
Normal file
76
server/routers/site/listSites.ts
Normal file
|
@ -0,0 +1,76 @@
|
|||
import { Request, Response, NextFunction } from 'express';
|
||||
import { z } from 'zod';
|
||||
import { db } from '@server/db';
|
||||
import { sites, orgs, exitNodes } 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';
|
||||
|
||||
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)),
|
||||
orgId: z.string().optional().transform(Number).pipe(z.number().int().positive()),
|
||||
});
|
||||
|
||||
export async function listSites(req: Request, res: Response, next: NextFunction): Promise<any> {
|
||||
try {
|
||||
const parsedQuery = listSitesSchema.safeParse(req.query);
|
||||
if (!parsedQuery.success) {
|
||||
return next(
|
||||
createHttpError(
|
||||
HttpCode.BAD_REQUEST,
|
||||
parsedQuery.error.errors.map(e => e.message).join(', ')
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
const { limit, offset, orgId } = parsedQuery.data;
|
||||
|
||||
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))
|
||||
.leftJoin(exitNodes, eq(sites.exitNode, exitNodes.exitNodeId));
|
||||
|
||||
let countQuery: any = db.select({ count: sql<number>`cast(count(*) as integer)` }).from(sites);
|
||||
|
||||
if (orgId) {
|
||||
baseQuery = baseQuery.where(eq(sites.orgId, orgId));
|
||||
countQuery = countQuery.where(eq(sites.orgId, orgId));
|
||||
}
|
||||
|
||||
const sitesList = await baseQuery.limit(limit).offset(offset);
|
||||
const totalCountResult = await countQuery;
|
||||
const totalCount = totalCountResult[0].count;
|
||||
|
||||
return res.status(HttpCode.OK).send(
|
||||
response(res, {
|
||||
data: {
|
||||
sites: sitesList,
|
||||
pagination: {
|
||||
total: totalCount,
|
||||
limit,
|
||||
offset,
|
||||
},
|
||||
},
|
||||
success: true,
|
||||
error: false,
|
||||
message: "Sites retrieved successfully",
|
||||
status: HttpCode.OK,
|
||||
})
|
||||
);
|
||||
} catch (error) {
|
||||
next(error);
|
||||
}
|
||||
}
|
|
@ -68,7 +68,7 @@ export async function updateSite(req: Request, res: Response, next: NextFunction
|
|||
}
|
||||
|
||||
return res.status(HttpCode.OK).send(
|
||||
response({
|
||||
response(res, {
|
||||
data: updatedSite[0],
|
||||
success: true,
|
||||
error: false,
|
||||
|
|
|
@ -32,7 +32,7 @@ export async function createTarget(req: Request, res: Response, next: NextFuncti
|
|||
const newTarget = await db.insert(targets).values(targetData).returning();
|
||||
|
||||
return res.status(HttpCode.CREATED).send(
|
||||
response({
|
||||
response(res, {
|
||||
data: newTarget[0],
|
||||
success: true,
|
||||
error: false,
|
||||
|
|
|
@ -39,7 +39,7 @@ export async function deleteTarget(req: Request, res: Response, next: NextFuncti
|
|||
}
|
||||
|
||||
return res.status(HttpCode.OK).send(
|
||||
response({
|
||||
response(res, {
|
||||
data: null,
|
||||
success: true,
|
||||
error: false,
|
||||
|
|
|
@ -40,7 +40,7 @@ export async function getTarget(req: Request, res: Response, next: NextFunction)
|
|||
}
|
||||
|
||||
return res.status(HttpCode.OK).send(
|
||||
response({
|
||||
response(res, {
|
||||
data: target[0],
|
||||
success: true,
|
||||
error: false,
|
||||
|
|
73
server/routers/target/listTargets.ts
Normal file
73
server/routers/target/listTargets.ts
Normal file
|
@ -0,0 +1,73 @@
|
|||
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';
|
||||
|
||||
const listTargetsSchema = 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)),
|
||||
resourceId: z.string().optional(),
|
||||
});
|
||||
|
||||
export async function listTargets(req: Request, res: Response, next: NextFunction): Promise<any> {
|
||||
try {
|
||||
const parsedQuery = listTargetsSchema.safeParse(req.query);
|
||||
if (!parsedQuery.success) {
|
||||
return next(
|
||||
createHttpError(
|
||||
HttpCode.BAD_REQUEST,
|
||||
parsedQuery.error.errors.map(e => e.message).join(', ')
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
const { limit, offset, resourceId } = parsedQuery.data;
|
||||
|
||||
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));
|
||||
|
||||
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));
|
||||
}
|
||||
|
||||
const targetsList = await baseQuery.limit(limit).offset(offset);
|
||||
const totalCountResult = await countQuery;
|
||||
const totalCount = totalCountResult[0].count;
|
||||
|
||||
return res.status(HttpCode.OK).send(
|
||||
response(res, {
|
||||
data: {
|
||||
targets: targetsList,
|
||||
pagination: {
|
||||
total: totalCount,
|
||||
limit,
|
||||
offset,
|
||||
},
|
||||
},
|
||||
success: true,
|
||||
error: false,
|
||||
message: "Targets retrieved successfully",
|
||||
status: HttpCode.OK,
|
||||
})
|
||||
);
|
||||
} catch (error) {
|
||||
next(error);
|
||||
}
|
||||
}
|
|
@ -61,7 +61,7 @@ export async function updateTarget(req: Request, res: Response, next: NextFuncti
|
|||
}
|
||||
|
||||
return res.status(HttpCode.OK).send(
|
||||
response({
|
||||
response(res, {
|
||||
data: updatedTarget[0],
|
||||
success: true,
|
||||
error: false,
|
||||
|
|
|
@ -39,7 +39,7 @@ export async function deleteUser(req: Request, res: Response, next: NextFunction
|
|||
}
|
||||
|
||||
return res.status(HttpCode.OK).send(
|
||||
response({
|
||||
response(res, {
|
||||
data: null,
|
||||
success: true,
|
||||
error: false,
|
||||
|
|
|
@ -43,7 +43,7 @@ export async function getUser(req: Request, res: Response, next: NextFunction):
|
|||
const { passwordHash: _, ...userWithoutPassword } = user[0];
|
||||
|
||||
return res.status(HttpCode.OK).send(
|
||||
response({
|
||||
response(res, {
|
||||
data: userWithoutPassword,
|
||||
success: true,
|
||||
error: false,
|
||||
|
|
61
server/routers/user/listUsers.ts
Normal file
61
server/routers/user/listUsers.ts
Normal file
|
@ -0,0 +1,61 @@
|
|||
import { Request, Response, NextFunction } from 'express';
|
||||
import { z } from 'zod';
|
||||
import { db } from '@server/db';
|
||||
import { users } from '@server/db/schema';
|
||||
import response from "@server/utils/response";
|
||||
import HttpCode from '@server/types/HttpCode';
|
||||
import createHttpError from 'http-errors';
|
||||
import { sql } from 'drizzle-orm';
|
||||
|
||||
const listUsersSchema = 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)),
|
||||
});
|
||||
|
||||
export async function listUsers(req: Request, res: Response, next: NextFunction): Promise<any> {
|
||||
try {
|
||||
const parsedQuery = listUsersSchema.safeParse(req.query);
|
||||
if (!parsedQuery.success) {
|
||||
return next(
|
||||
createHttpError(
|
||||
HttpCode.BAD_REQUEST,
|
||||
parsedQuery.error.errors.map(e => e.message).join(', ')
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
const { limit, offset } = parsedQuery.data;
|
||||
|
||||
const usersList = await db.select()
|
||||
.from(users)
|
||||
.limit(limit)
|
||||
.offset(offset);
|
||||
|
||||
const totalCountResult = await db
|
||||
.select({ count: sql<number>`cast(count(*) as integer)` })
|
||||
.from(users);
|
||||
const totalCount = totalCountResult[0].count;
|
||||
|
||||
// Remove passwordHash from each user object
|
||||
const usersWithoutPassword = usersList.map(({ passwordHash, ...userWithoutPassword }) => userWithoutPassword);
|
||||
|
||||
return res.status(HttpCode.OK).send(
|
||||
response(res, {
|
||||
data: {
|
||||
users: usersWithoutPassword,
|
||||
pagination: {
|
||||
total: totalCount,
|
||||
limit,
|
||||
offset,
|
||||
},
|
||||
},
|
||||
success: true,
|
||||
error: false,
|
||||
message: "Users retrieved successfully",
|
||||
status: HttpCode.OK,
|
||||
})
|
||||
);
|
||||
} catch (error) {
|
||||
next(error);
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue