mirror of
https://github.com/fosrl/pangolin.git
synced 2025-05-13 05:40:38 +01:00
70 lines
1.8 KiB
TypeScript
70 lines
1.8 KiB
TypeScript
import { Request, Response, NextFunction } from "express";
|
|
import { z } from "zod";
|
|
import { db } from "@server/db";
|
|
import { Org, orgs } from "@server/db/schemas";
|
|
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";
|
|
|
|
const getOrgSchema = z
|
|
.object({
|
|
orgId: z.string()
|
|
})
|
|
.strict();
|
|
|
|
export type GetOrgResponse = {
|
|
org: Org;
|
|
};
|
|
|
|
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.length === 0) {
|
|
return next(
|
|
createHttpError(
|
|
HttpCode.NOT_FOUND,
|
|
`Organization with ID ${orgId} not found`
|
|
)
|
|
);
|
|
}
|
|
|
|
return response<GetOrgResponse>(res, {
|
|
data: {
|
|
org: org[0]
|
|
},
|
|
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")
|
|
);
|
|
}
|
|
}
|