fosrl.pangolin/server/routers/org/checkId.ts

69 lines
1.8 KiB
TypeScript
Raw Normal View History

import { Request, Response, NextFunction } from "express";
import { z } from "zod";
import { db } from "@server/db";
import { orgs } from "@server/db/schema";
import { eq } from "drizzle-orm";
2025-01-01 21:41:31 -05:00
import response from "@server/lib/response";
import HttpCode from "@server/types/HttpCode";
import createHttpError from "http-errors";
import logger from "@server/logger";
import { fromError } from "zod-validation-error";
2024-10-14 19:30:38 -04:00
2024-12-21 21:01:12 -05:00
const getOrgSchema = z
.object({
orgId: z.string()
})
.strict();
2024-10-14 19:30:38 -04:00
export async function checkId(
req: Request,
res: Response,
next: NextFunction
): Promise<any> {
2024-10-14 19:30:38 -04:00
try {
const parsedQuery = getOrgSchema.safeParse(req.query);
if (!parsedQuery.success) {
return next(
createHttpError(
HttpCode.BAD_REQUEST,
fromError(parsedQuery.error).toString()
2024-10-14 19:30:38 -04:00
)
);
}
const { orgId } = parsedQuery.data;
const org = await db
.select()
2024-10-14 19:30:38 -04:00
.from(orgs)
.where(eq(orgs.orgId, orgId))
.limit(1);
if (org.length > 0) {
return response(res, {
data: {},
success: true,
error: false,
message: "Organization ID already exists",
2024-12-21 21:01:12 -05:00
status: HttpCode.OK
2024-10-14 19:30:38 -04:00
});
}
return response(res, {
data: {},
success: true,
error: false,
message: "Organization ID is available",
2024-12-21 21:01:12 -05:00
status: HttpCode.NOT_FOUND
2024-10-14 19:30:38 -04:00
});
} catch (error) {
logger.error(error);
return next(
createHttpError(
HttpCode.INTERNAL_SERVER_ERROR,
"An error occurred..."
)
);
2024-10-14 19:30:38 -04:00
}
}