mirror of
https://github.com/fosrl/pangolin.git
synced 2026-03-04 09:46:40 +00:00
✨ add API endpoint for listing blueprints
This commit is contained in:
@@ -116,6 +116,9 @@ export enum ActionsEnum {
|
|||||||
updateLoginPage = "updateLoginPage",
|
updateLoginPage = "updateLoginPage",
|
||||||
getLoginPage = "getLoginPage",
|
getLoginPage = "getLoginPage",
|
||||||
deleteLoginPage = "deleteLoginPage",
|
deleteLoginPage = "deleteLoginPage",
|
||||||
|
|
||||||
|
// blueprints
|
||||||
|
listBlueprints = "listBlueprints",
|
||||||
applyBlueprint = "applyBlueprint"
|
applyBlueprint = "applyBlueprint"
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -193,7 +196,6 @@ export async function checkUserActionPermission(
|
|||||||
.limit(1);
|
.limit(1);
|
||||||
|
|
||||||
return roleActionPermission.length > 0;
|
return roleActionPermission.length > 0;
|
||||||
|
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error("Error checking user action permission:", error);
|
console.error("Error checking user action permission:", error);
|
||||||
throw createHttpError(
|
throw createHttpError(
|
||||||
|
|||||||
1
server/routers/blueprints/index.ts
Normal file
1
server/routers/blueprints/index.ts
Normal file
@@ -0,0 +1 @@
|
|||||||
|
export * from "./listBluePrints";
|
||||||
129
server/routers/blueprints/listBluePrints.ts
Normal file
129
server/routers/blueprints/listBluePrints.ts
Normal file
@@ -0,0 +1,129 @@
|
|||||||
|
import { Request, Response, NextFunction } from "express";
|
||||||
|
import { z } from "zod";
|
||||||
|
import { db, blueprints, orgs } from "@server/db";
|
||||||
|
import response from "@server/lib/response";
|
||||||
|
import HttpCode from "@server/types/HttpCode";
|
||||||
|
import createHttpError from "http-errors";
|
||||||
|
import { sql, eq, or, inArray, and, count } from "drizzle-orm";
|
||||||
|
import logger from "@server/logger";
|
||||||
|
import { fromZodError } from "zod-validation-error";
|
||||||
|
import { OpenAPITags, registry } from "@server/openApi";
|
||||||
|
import { warn } from "console";
|
||||||
|
|
||||||
|
const listBluePrintsParamsSchema = z
|
||||||
|
.object({
|
||||||
|
orgId: z.string()
|
||||||
|
})
|
||||||
|
.strict();
|
||||||
|
|
||||||
|
const listBluePrintsSchema = z
|
||||||
|
.object({
|
||||||
|
limit: z
|
||||||
|
.string()
|
||||||
|
.optional()
|
||||||
|
.default("1000")
|
||||||
|
.transform(Number)
|
||||||
|
.pipe(z.number().int().nonnegative()),
|
||||||
|
offset: z
|
||||||
|
.string()
|
||||||
|
.optional()
|
||||||
|
.default("0")
|
||||||
|
.transform(Number)
|
||||||
|
.pipe(z.number().int().nonnegative())
|
||||||
|
})
|
||||||
|
.strict();
|
||||||
|
|
||||||
|
async function queryBlueprints(orgId: string, limit: number, offset: number) {
|
||||||
|
const res = await db
|
||||||
|
.select({
|
||||||
|
blueprintId: blueprints.blueprintId,
|
||||||
|
name: blueprints.name,
|
||||||
|
source: blueprints.source,
|
||||||
|
succeeded: blueprints.succeeded
|
||||||
|
})
|
||||||
|
.from(blueprints)
|
||||||
|
.leftJoin(orgs, eq(blueprints.orgId, orgs.orgId))
|
||||||
|
.limit(limit)
|
||||||
|
.offset(offset);
|
||||||
|
return res;
|
||||||
|
}
|
||||||
|
|
||||||
|
export type ListBlueprintsResponse = {
|
||||||
|
domains: NonNullable<Awaited<ReturnType<typeof queryBlueprints>>>;
|
||||||
|
pagination: { total: number; limit: number; offset: number };
|
||||||
|
};
|
||||||
|
|
||||||
|
registry.registerPath({
|
||||||
|
method: "get",
|
||||||
|
path: "/org/{orgId}/blueprints",
|
||||||
|
description: "List all blueprints for a organization.",
|
||||||
|
tags: [OpenAPITags.Org],
|
||||||
|
request: {
|
||||||
|
params: z.object({
|
||||||
|
orgId: z.string()
|
||||||
|
}),
|
||||||
|
query: listBluePrintsSchema
|
||||||
|
},
|
||||||
|
responses: {}
|
||||||
|
});
|
||||||
|
|
||||||
|
export async function listBlueprints(
|
||||||
|
req: Request,
|
||||||
|
res: Response,
|
||||||
|
next: NextFunction
|
||||||
|
): Promise<any> {
|
||||||
|
try {
|
||||||
|
const parsedQuery = listBluePrintsSchema.safeParse(req.query);
|
||||||
|
if (!parsedQuery.success) {
|
||||||
|
return next(
|
||||||
|
createHttpError(
|
||||||
|
HttpCode.BAD_REQUEST,
|
||||||
|
fromZodError(parsedQuery.error)
|
||||||
|
)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
const { limit, offset } = parsedQuery.data;
|
||||||
|
|
||||||
|
const parsedParams = listBluePrintsParamsSchema.safeParse(req.params);
|
||||||
|
if (!parsedParams.success) {
|
||||||
|
return next(
|
||||||
|
createHttpError(
|
||||||
|
HttpCode.BAD_REQUEST,
|
||||||
|
fromZodError(parsedParams.error)
|
||||||
|
)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
const { orgId } = parsedParams.data;
|
||||||
|
|
||||||
|
const blueprintsList = await queryBlueprints(
|
||||||
|
orgId.toString(),
|
||||||
|
limit,
|
||||||
|
offset
|
||||||
|
);
|
||||||
|
|
||||||
|
const [{ count }] = await db
|
||||||
|
.select({ count: sql<number>`count(*)` })
|
||||||
|
.from(blueprints);
|
||||||
|
|
||||||
|
return response<ListBlueprintsResponse>(res, {
|
||||||
|
data: {
|
||||||
|
domains: blueprintsList,
|
||||||
|
pagination: {
|
||||||
|
total: count,
|
||||||
|
limit,
|
||||||
|
offset
|
||||||
|
}
|
||||||
|
},
|
||||||
|
success: true,
|
||||||
|
error: false,
|
||||||
|
message: "Blueprints retrieved successfully",
|
||||||
|
status: HttpCode.OK
|
||||||
|
});
|
||||||
|
} catch (error) {
|
||||||
|
logger.error(error);
|
||||||
|
return next(
|
||||||
|
createHttpError(HttpCode.INTERNAL_SERVER_ERROR, "An error occurred")
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -13,6 +13,7 @@ import * as siteResource from "./siteResource";
|
|||||||
import * as supporterKey from "./supporterKey";
|
import * as supporterKey from "./supporterKey";
|
||||||
import * as accessToken from "./accessToken";
|
import * as accessToken from "./accessToken";
|
||||||
import * as idp from "./idp";
|
import * as idp from "./idp";
|
||||||
|
import * as blueprints from "./blueprints";
|
||||||
import * as apiKeys from "./apiKeys";
|
import * as apiKeys from "./apiKeys";
|
||||||
import HttpCode from "@server/types/HttpCode";
|
import HttpCode from "@server/types/HttpCode";
|
||||||
import {
|
import {
|
||||||
@@ -675,8 +676,6 @@ authenticated.post(
|
|||||||
idp.updateOidcIdp
|
idp.updateOidcIdp
|
||||||
);
|
);
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
authenticated.delete("/idp/:idpId", verifyUserIsServerAdmin, idp.deleteIdp);
|
authenticated.delete("/idp/:idpId", verifyUserIsServerAdmin, idp.deleteIdp);
|
||||||
|
|
||||||
authenticated.get("/idp/:idpId", verifyUserIsServerAdmin, idp.getIdp);
|
authenticated.get("/idp/:idpId", verifyUserIsServerAdmin, idp.getIdp);
|
||||||
@@ -705,7 +704,6 @@ authenticated.get(
|
|||||||
idp.listIdpOrgPolicies
|
idp.listIdpOrgPolicies
|
||||||
);
|
);
|
||||||
|
|
||||||
|
|
||||||
authenticated.get("/idp", idp.listIdps); // anyone can see this; it's just a list of idp names and ids
|
authenticated.get("/idp", idp.listIdps); // anyone can see this; it's just a list of idp names and ids
|
||||||
authenticated.get("/idp/:idpId", verifyUserIsServerAdmin, idp.getIdp);
|
authenticated.get("/idp/:idpId", verifyUserIsServerAdmin, idp.getIdp);
|
||||||
|
|
||||||
@@ -814,6 +812,12 @@ authenticated.delete(
|
|||||||
domain.deleteAccountDomain
|
domain.deleteAccountDomain
|
||||||
);
|
);
|
||||||
|
|
||||||
|
authenticated.get(
|
||||||
|
"/org/:orgId/blueprints",
|
||||||
|
verifyOrgAccess,
|
||||||
|
verifyUserHasAction(ActionsEnum.listBlueprints),
|
||||||
|
blueprints.listBlueprints
|
||||||
|
);
|
||||||
// Auth routes
|
// Auth routes
|
||||||
export const authRouter = Router();
|
export const authRouter = Router();
|
||||||
unauthenticated.use("/auth", authRouter);
|
unauthenticated.use("/auth", authRouter);
|
||||||
|
|||||||
Reference in New Issue
Block a user