2024-10-02 21:17:38 -04:00
|
|
|
import { Request, Response, NextFunction } from 'express';
|
|
|
|
import { z } from 'zod';
|
|
|
|
import { db } from '@server/db';
|
|
|
|
import { resources, sites } from '@server/db/schema';
|
|
|
|
import response from "@server/utils/response";
|
|
|
|
import HttpCode from '@server/types/HttpCode';
|
|
|
|
import createHttpError from 'http-errors';
|
|
|
|
import { sql, eq } from 'drizzle-orm';
|
|
|
|
|
2024-10-02 22:05:21 -04:00
|
|
|
const listResourcesParamsSchema = z.object({
|
|
|
|
siteId: z.string().optional().transform(Number).pipe(z.number().int().positive()),
|
|
|
|
orgId: z.string().optional().transform(Number).pipe(z.number().int().positive()),
|
|
|
|
});
|
|
|
|
|
2024-10-02 21:17:38 -04:00
|
|
|
const listResourcesSchema = z.object({
|
|
|
|
limit: z.string().optional().transform(Number).pipe(z.number().int().positive().default(10)),
|
|
|
|
offset: z.string().optional().transform(Number).pipe(z.number().int().nonnegative().default(0)),
|
|
|
|
});
|
|
|
|
|
|
|
|
export async function listResources(req: Request, res: Response, next: NextFunction): Promise<any> {
|
|
|
|
try {
|
|
|
|
const parsedQuery = listResourcesSchema.safeParse(req.query);
|
|
|
|
if (!parsedQuery.success) {
|
|
|
|
return next(
|
|
|
|
createHttpError(
|
|
|
|
HttpCode.BAD_REQUEST,
|
|
|
|
parsedQuery.error.errors.map(e => e.message).join(', ')
|
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
2024-10-02 22:05:21 -04:00
|
|
|
const { limit, offset } = parsedQuery.data;
|
2024-10-02 21:17:38 -04:00
|
|
|
|
2024-10-02 22:05:21 -04:00
|
|
|
const parsedParams = listResourcesParamsSchema.safeParse(req.params);
|
|
|
|
if (!parsedParams.success) {
|
|
|
|
return next(
|
|
|
|
createHttpError(
|
|
|
|
HttpCode.BAD_REQUEST,
|
|
|
|
parsedParams.error.errors.map(e => e.message).join(', ')
|
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
const { siteId, orgId } = parsedParams.data;
|
2024-10-02 21:17:38 -04:00
|
|
|
|
2024-10-02 22:05:21 -04:00
|
|
|
let baseQuery: any = db
|
2024-10-02 21:17:38 -04:00
|
|
|
.select({
|
|
|
|
resourceId: resources.resourceId,
|
|
|
|
name: resources.name,
|
|
|
|
subdomain: resources.subdomain,
|
|
|
|
siteName: sites.name,
|
|
|
|
})
|
|
|
|
.from(resources)
|
|
|
|
.leftJoin(sites, eq(resources.siteId, sites.siteId));
|
|
|
|
|
2024-10-02 22:05:21 -04:00
|
|
|
let countQuery: any = db.select({ count: sql<number>`cast(count(*) as integer)` }).from(resources);
|
2024-10-02 21:17:38 -04:00
|
|
|
|
|
|
|
if (siteId) {
|
|
|
|
baseQuery = baseQuery.where(eq(resources.siteId, siteId));
|
|
|
|
countQuery = countQuery.where(eq(resources.siteId, siteId));
|
2024-10-02 22:05:21 -04:00
|
|
|
} else if (orgId) {
|
|
|
|
baseQuery = baseQuery.where(eq(resources.orgId, orgId));
|
|
|
|
countQuery = countQuery.where(eq(resources.orgId, orgId));
|
|
|
|
} else {
|
|
|
|
return next(
|
|
|
|
createHttpError(
|
|
|
|
HttpCode.BAD_REQUEST,
|
|
|
|
'Either siteId or orgId must be provided'
|
|
|
|
)
|
|
|
|
);
|
2024-10-02 21:17:38 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
const resourcesList = await baseQuery.limit(limit).offset(offset);
|
|
|
|
const totalCountResult = await countQuery;
|
|
|
|
const totalCount = totalCountResult[0].count;
|
|
|
|
|
|
|
|
return res.status(HttpCode.OK).send(
|
|
|
|
response(res, {
|
|
|
|
data: {
|
|
|
|
resources: resourcesList,
|
|
|
|
pagination: {
|
|
|
|
total: totalCount,
|
|
|
|
limit,
|
|
|
|
offset,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
success: true,
|
|
|
|
error: false,
|
|
|
|
message: "Resources retrieved successfully",
|
|
|
|
status: HttpCode.OK,
|
|
|
|
})
|
|
|
|
);
|
|
|
|
} catch (error) {
|
|
|
|
next(error);
|
|
|
|
}
|
|
|
|
}
|