mirror of
https://github.com/fosrl/pangolin.git
synced 2026-06-04 14:56:14 +00:00
Agent-Logs-Url: https://github.com/fosrl/pangolin/sessions/7b395a8e-7fae-4f4d-952e-4030fea08262 Co-authored-by: oschwartz10612 <4999704+oschwartz10612@users.noreply.github.com>
93 lines
2.4 KiB
TypeScript
93 lines
2.4 KiB
TypeScript
import { Request, Response, NextFunction } from "express";
|
|
import { z } from "zod";
|
|
import { createApiResponseSchema } from "@server/lib/openapi/createApiResponseSchema";
|
|
import { db } from "@server/db";
|
|
import { Org, orgs } from "@server/db";
|
|
import { eq } from "drizzle-orm";
|
|
import response from "@server/lib/response";
|
|
import HttpCode from "@server/types/HttpCode";
|
|
import createHttpError from "http-errors";
|
|
import logger from "@server/logger";
|
|
import { fromZodError } from "zod-validation-error";
|
|
import { OpenAPITags, registry } from "@server/openApi";
|
|
|
|
const getOrgSchema = z.strictObject({
|
|
orgId: z.string()
|
|
});
|
|
|
|
export type GetOrgResponse = {
|
|
org: Org;
|
|
};
|
|
const GetOrgResponseDataSchema = z.object({org: z.object({}).passthrough()});
|
|
|
|
|
|
registry.registerPath({
|
|
method: "get",
|
|
path: "/org/{orgId}",
|
|
description: "Get an organization",
|
|
tags: [OpenAPITags.Org],
|
|
request: {
|
|
params: getOrgSchema
|
|
},
|
|
responses: {
|
|
200: {
|
|
description: "Successful response",
|
|
content: {
|
|
"application/json": {
|
|
schema: createApiResponseSchema(GetOrgResponseDataSchema)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
});
|
|
|
|
export async function getOrg(
|
|
req: Request,
|
|
res: Response,
|
|
next: NextFunction
|
|
): Promise<any> {
|
|
try {
|
|
const parsedParams = getOrgSchema.safeParse(req.params);
|
|
if (!parsedParams.success) {
|
|
return next(
|
|
createHttpError(
|
|
HttpCode.BAD_REQUEST,
|
|
fromZodError(parsedParams.error)
|
|
)
|
|
);
|
|
}
|
|
|
|
const { orgId } = parsedParams.data;
|
|
|
|
const [org] = await db
|
|
.select()
|
|
.from(orgs)
|
|
.where(eq(orgs.orgId, orgId))
|
|
.limit(1);
|
|
|
|
if (!org) {
|
|
return next(
|
|
createHttpError(
|
|
HttpCode.NOT_FOUND,
|
|
`Organization with ID ${orgId} not found`
|
|
)
|
|
);
|
|
}
|
|
|
|
return response<GetOrgResponse>(res, {
|
|
data: {
|
|
org
|
|
},
|
|
success: true,
|
|
error: false,
|
|
message: "Organization retrieved successfully",
|
|
status: HttpCode.OK
|
|
});
|
|
} catch (error) {
|
|
logger.error(error);
|
|
return next(
|
|
createHttpError(HttpCode.INTERNAL_SERVER_ERROR, "An error occurred")
|
|
);
|
|
}
|
|
}
|