2024-11-03 13:57:51 -05:00
|
|
|
import { Request, Response, NextFunction } from "express";
|
|
|
|
import { z } from "zod";
|
|
|
|
import { db } from "@server/db";
|
|
|
|
import { sites } from "@server/db/schema";
|
|
|
|
import { eq, and } from "drizzle-orm";
|
2025-01-01 21:41:31 -05:00
|
|
|
import response from "@server/lib/response";
|
2024-11-03 13:57:51 -05:00
|
|
|
import HttpCode from "@server/types/HttpCode";
|
|
|
|
import createHttpError from "http-errors";
|
|
|
|
import logger from "@server/logger";
|
2025-01-01 21:41:31 -05:00
|
|
|
import stoi from "@server/lib/stoi";
|
2024-11-03 13:57:51 -05:00
|
|
|
import { fromError } from "zod-validation-error";
|
2024-10-01 21:53:49 -04:00
|
|
|
|
2024-12-21 21:01:12 -05:00
|
|
|
const getSiteSchema = z
|
|
|
|
.object({
|
|
|
|
siteId: z
|
|
|
|
.string()
|
|
|
|
.optional()
|
|
|
|
.transform(stoi)
|
|
|
|
.pipe(z.number().int().positive().optional())
|
|
|
|
.optional(),
|
|
|
|
niceId: z.string().optional(),
|
|
|
|
orgId: z.string().optional()
|
|
|
|
})
|
|
|
|
.strict();
|
2024-10-01 21:34:07 -04:00
|
|
|
|
2024-12-30 23:41:06 -05:00
|
|
|
async function query(siteId?: number, niceId?: string, orgId?: string) {
|
|
|
|
if (siteId) {
|
|
|
|
const [res] = await db
|
|
|
|
.select()
|
|
|
|
.from(sites)
|
|
|
|
.where(eq(sites.siteId, siteId))
|
|
|
|
.limit(1);
|
|
|
|
return res;
|
|
|
|
} else if (niceId && orgId) {
|
|
|
|
const [res] = await db
|
|
|
|
.select()
|
|
|
|
.from(sites)
|
|
|
|
.where(and(eq(sites.niceId, niceId), eq(sites.orgId, orgId)))
|
|
|
|
.limit(1);
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export type GetSiteResponse = NonNullable<Awaited<ReturnType<typeof query>>>;
|
2024-10-13 22:45:48 -04:00
|
|
|
|
2024-11-03 13:57:51 -05:00
|
|
|
export async function getSite(
|
|
|
|
req: Request,
|
|
|
|
res: Response,
|
|
|
|
next: NextFunction
|
|
|
|
): Promise<any> {
|
2024-10-06 18:05:20 -04:00
|
|
|
try {
|
|
|
|
const parsedParams = getSiteSchema.safeParse(req.params);
|
|
|
|
if (!parsedParams.success) {
|
|
|
|
return next(
|
|
|
|
createHttpError(
|
|
|
|
HttpCode.BAD_REQUEST,
|
2024-11-03 13:57:51 -05:00
|
|
|
fromError(parsedParams.error).toString()
|
2024-10-06 18:05:20 -04:00
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
2024-10-01 21:53:49 -04:00
|
|
|
|
2024-10-14 22:26:32 -04:00
|
|
|
const { siteId, niceId, orgId } = parsedParams.data;
|
2024-10-01 21:53:49 -04:00
|
|
|
|
2024-12-30 23:41:06 -05:00
|
|
|
const site = await query(siteId, niceId, orgId);
|
2024-10-14 22:26:32 -04:00
|
|
|
|
|
|
|
if (!site) {
|
2024-11-03 13:57:51 -05:00
|
|
|
return next(createHttpError(HttpCode.NOT_FOUND, "Site not found"));
|
2024-10-14 22:26:32 -04:00
|
|
|
}
|
2024-10-01 21:53:49 -04:00
|
|
|
|
2024-12-30 23:41:06 -05:00
|
|
|
return response<GetSiteResponse>(res, {
|
|
|
|
data: site,
|
2024-10-06 18:05:20 -04:00
|
|
|
success: true,
|
|
|
|
error: false,
|
|
|
|
message: "Site retrieved successfully",
|
2024-12-21 21:01:12 -05:00
|
|
|
status: HttpCode.OK
|
2024-10-06 18:05:20 -04:00
|
|
|
});
|
|
|
|
} catch (error) {
|
2024-12-21 21:01:12 -05:00
|
|
|
logger.error(error);
|
2024-11-03 13:57:51 -05:00
|
|
|
return next(
|
2024-11-05 23:55:46 -05:00
|
|
|
createHttpError(HttpCode.INTERNAL_SERVER_ERROR, "An error occurred")
|
2024-11-03 13:57:51 -05:00
|
|
|
);
|
2024-10-06 18:05:20 -04:00
|
|
|
}
|
2024-10-02 00:04:40 -04:00
|
|
|
}
|