2024-10-19 15:49:16 -04:00
|
|
|
import { Request, Response, NextFunction } from "express";
|
|
|
|
import { z } from "zod";
|
|
|
|
import { db } from "@server/db";
|
|
|
|
import { Org, orgs } from "@server/db/schema";
|
|
|
|
import { eq } from "drizzle-orm";
|
2024-10-01 21:34:07 -04:00
|
|
|
import response from "@server/utils/response";
|
2024-10-19 15:49:16 -04:00
|
|
|
import HttpCode from "@server/types/HttpCode";
|
|
|
|
import createHttpError from "http-errors";
|
|
|
|
import logger from "@server/logger";
|
2024-10-01 21:53:49 -04:00
|
|
|
|
|
|
|
const getOrgSchema = z.object({
|
2024-10-19 15:49:16 -04:00
|
|
|
orgId: z.string(),
|
2024-10-01 21:53:49 -04:00
|
|
|
});
|
2024-10-01 21:34:07 -04:00
|
|
|
|
2024-10-19 15:49:16 -04:00
|
|
|
export type GetOrgResponse = {
|
|
|
|
org: Org;
|
2024-11-05 23:55:46 -05:00
|
|
|
};
|
2024-10-19 15:49:16 -04:00
|
|
|
|
|
|
|
export async function getOrg(
|
|
|
|
req: Request,
|
|
|
|
res: Response,
|
2024-11-05 23:55:46 -05:00
|
|
|
next: NextFunction
|
2024-10-19 15:49:16 -04:00
|
|
|
): Promise<any> {
|
2024-10-06 18:05:20 -04:00
|
|
|
try {
|
|
|
|
const parsedParams = getOrgSchema.safeParse(req.params);
|
|
|
|
if (!parsedParams.success) {
|
|
|
|
return next(
|
|
|
|
createHttpError(
|
|
|
|
HttpCode.BAD_REQUEST,
|
2024-11-05 23:55:46 -05:00
|
|
|
parsedParams.error.errors.map((e) => e.message).join(", ")
|
|
|
|
)
|
2024-10-06 18:05:20 -04:00
|
|
|
);
|
|
|
|
}
|
2024-10-01 21:53:49 -04:00
|
|
|
|
2024-10-06 18:05:20 -04:00
|
|
|
const { orgId } = parsedParams.data;
|
2024-10-01 21:53:49 -04:00
|
|
|
|
2024-10-19 15:49:16 -04:00
|
|
|
const org = await db
|
|
|
|
.select()
|
2024-10-06 18:05:20 -04:00
|
|
|
.from(orgs)
|
|
|
|
.where(eq(orgs.orgId, orgId))
|
|
|
|
.limit(1);
|
2024-10-01 21:53:49 -04:00
|
|
|
|
2024-10-06 18:05:20 -04:00
|
|
|
if (org.length === 0) {
|
|
|
|
return next(
|
|
|
|
createHttpError(
|
|
|
|
HttpCode.NOT_FOUND,
|
2024-11-05 23:55:46 -05:00
|
|
|
`Organization with ID ${orgId} not found`
|
|
|
|
)
|
2024-10-06 18:05:20 -04:00
|
|
|
);
|
|
|
|
}
|
2024-10-01 21:53:49 -04:00
|
|
|
|
2024-10-19 15:49:16 -04:00
|
|
|
return response<GetOrgResponse>(res, {
|
|
|
|
data: {
|
|
|
|
org: org[0],
|
|
|
|
},
|
2024-10-06 18:05:20 -04:00
|
|
|
success: true,
|
|
|
|
error: false,
|
|
|
|
message: "Organization retrieved successfully",
|
|
|
|
status: HttpCode.OK,
|
|
|
|
});
|
|
|
|
} catch (error) {
|
|
|
|
logger.error(error);
|
2024-10-19 15:49:16 -04:00
|
|
|
return next(
|
2024-11-05 23:55:46 -05:00
|
|
|
createHttpError(HttpCode.INTERNAL_SERVER_ERROR, "An error occurred")
|
2024-10-19 15:49:16 -04:00
|
|
|
);
|
2024-10-06 18:05:20 -04:00
|
|
|
}
|
2024-10-02 00:04:40 -04:00
|
|
|
}
|