2024-11-03 13:57:51 -05:00
|
|
|
import { Request, Response, NextFunction } from "express";
|
|
|
|
import { z } from "zod";
|
|
|
|
import { db } from "@server/db";
|
2025-03-08 22:10:44 -05:00
|
|
|
import { Resource, resources, sites } from "@server/db/schema";
|
2024-11-03 13:57:51 -05:00
|
|
|
import { eq } 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 { fromError } from "zod-validation-error";
|
2024-12-21 21:01:12 -05:00
|
|
|
import logger from "@server/logger";
|
2024-10-01 21:53:49 -04:00
|
|
|
|
2024-12-21 21:01:12 -05:00
|
|
|
const getResourceSchema = z
|
|
|
|
.object({
|
|
|
|
resourceId: z
|
|
|
|
.string()
|
|
|
|
.transform(Number)
|
|
|
|
.pipe(z.number().int().positive())
|
|
|
|
})
|
|
|
|
.strict();
|
2024-10-01 21:34:07 -04:00
|
|
|
|
2025-03-08 22:10:44 -05:00
|
|
|
export type GetResourceResponse = Resource & {
|
|
|
|
siteName: string;
|
|
|
|
};
|
2024-10-19 16:19:47 -04:00
|
|
|
|
2024-11-03 13:57:51 -05:00
|
|
|
export async function getResource(
|
|
|
|
req: Request,
|
|
|
|
res: Response,
|
|
|
|
next: NextFunction
|
|
|
|
): Promise<any> {
|
2024-10-06 18:05:20 -04:00
|
|
|
try {
|
|
|
|
const parsedParams = getResourceSchema.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-06 18:05:20 -04:00
|
|
|
const { resourceId } = parsedParams.data;
|
2024-10-01 21:53:49 -04:00
|
|
|
|
2025-03-08 22:10:44 -05:00
|
|
|
const [resp] = await db
|
2024-11-03 13:57:51 -05:00
|
|
|
.select()
|
2024-10-06 18:05:20 -04:00
|
|
|
.from(resources)
|
|
|
|
.where(eq(resources.resourceId, resourceId))
|
2025-03-08 22:10:44 -05:00
|
|
|
.leftJoin(sites, eq(sites.siteId, resources.siteId))
|
2024-10-06 18:05:20 -04:00
|
|
|
.limit(1);
|
2024-10-01 21:53:49 -04:00
|
|
|
|
2025-03-08 22:10:44 -05:00
|
|
|
const resource = resp.resources;
|
|
|
|
const site = resp.sites;
|
|
|
|
|
|
|
|
if (!resource) {
|
2024-10-06 18:05:20 -04:00
|
|
|
return next(
|
|
|
|
createHttpError(
|
|
|
|
HttpCode.NOT_FOUND,
|
|
|
|
`Resource with ID ${resourceId} not found`
|
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
2024-10-01 21:53:49 -04:00
|
|
|
|
2024-10-06 18:05:20 -04:00
|
|
|
return response(res, {
|
2025-03-08 22:10:44 -05:00
|
|
|
data: {
|
|
|
|
...resource,
|
|
|
|
siteName: site?.name
|
|
|
|
},
|
2024-10-06 18:05:20 -04:00
|
|
|
success: true,
|
|
|
|
error: false,
|
|
|
|
message: "Resource 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
|
|
|
}
|