mirror of
https://github.com/fosrl/pangolin.git
synced 2025-08-19 00:40:40 +02:00
95 lines
2.6 KiB
TypeScript
95 lines
2.6 KiB
TypeScript
|
import { Request, Response, NextFunction } from "express";
|
||
|
import { z } from "zod";
|
||
|
import { db } from "@server/db";
|
||
|
import { domains, orgDomains, users } from "@server/db/schemas";
|
||
|
import response from "@server/lib/response";
|
||
|
import HttpCode from "@server/types/HttpCode";
|
||
|
import createHttpError from "http-errors";
|
||
|
import { sql } from "drizzle-orm";
|
||
|
import logger from "@server/logger";
|
||
|
import { fromError } from "zod-validation-error";
|
||
|
import { OpenAPITags, registry } from "@server/openApi";
|
||
|
|
||
|
const querySchema = 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 query(limit: number, offset: number) {
|
||
|
const res = await db.select().from(orgDomains).limit(limit).offset(offset);
|
||
|
return res;
|
||
|
}
|
||
|
|
||
|
export type ListIdpResponse = {
|
||
|
idps: NonNullable<Awaited<ReturnType<typeof query>>>;
|
||
|
pagination: { total: number; limit: number; offset: number };
|
||
|
};
|
||
|
|
||
|
registry.registerPath({
|
||
|
method: "get",
|
||
|
path: "/idp",
|
||
|
description: "List all IDP in the system.",
|
||
|
tags: [OpenAPITags.Idp],
|
||
|
request: {
|
||
|
query: querySchema
|
||
|
},
|
||
|
responses: {}
|
||
|
});
|
||
|
|
||
|
export async function listIdps(
|
||
|
req: Request,
|
||
|
res: Response,
|
||
|
next: NextFunction
|
||
|
): Promise<any> {
|
||
|
try {
|
||
|
const parsedQuery = querySchema.safeParse(req.query);
|
||
|
if (!parsedQuery.success) {
|
||
|
return next(
|
||
|
createHttpError(
|
||
|
HttpCode.BAD_REQUEST,
|
||
|
fromError(parsedQuery.error).toString()
|
||
|
)
|
||
|
);
|
||
|
}
|
||
|
const { limit, offset } = parsedQuery.data;
|
||
|
|
||
|
const list = await query(limit, offset);
|
||
|
|
||
|
const [{ count }] = await db
|
||
|
.select({ count: sql<number>`count(*)` })
|
||
|
.from(domains);
|
||
|
|
||
|
return response<ListIdpResponse>(res, {
|
||
|
data: {
|
||
|
idps: list,
|
||
|
pagination: {
|
||
|
total: count,
|
||
|
limit,
|
||
|
offset
|
||
|
}
|
||
|
},
|
||
|
success: true,
|
||
|
error: false,
|
||
|
message: "Users retrieved successfully",
|
||
|
status: HttpCode.OK
|
||
|
});
|
||
|
} catch (error) {
|
||
|
logger.error(error);
|
||
|
return next(
|
||
|
createHttpError(HttpCode.INTERNAL_SERVER_ERROR, "An error occurred")
|
||
|
);
|
||
|
}
|
||
|
}
|