2024-11-05 22:38:57 -05:00
|
|
|
import { Request, Response, NextFunction } from "express";
|
|
|
|
import { z } from "zod";
|
|
|
|
import { db } from "@server/db";
|
|
|
|
import {
|
|
|
|
orgs,
|
2024-11-09 00:08:17 -05:00
|
|
|
Resource,
|
2024-11-05 22:38:57 -05:00
|
|
|
resources,
|
|
|
|
roleResources,
|
|
|
|
roles,
|
|
|
|
userResources,
|
|
|
|
} from "@server/db/schema";
|
2024-10-01 21:34:07 -04:00
|
|
|
import response from "@server/utils/response";
|
2024-11-05 22:38:57 -05:00
|
|
|
import HttpCode from "@server/types/HttpCode";
|
|
|
|
import createHttpError from "http-errors";
|
|
|
|
import { eq, and } from "drizzle-orm";
|
|
|
|
import stoi from "@server/utils/stoi";
|
|
|
|
import { fromError } from "zod-validation-error";
|
2024-11-15 18:25:27 -05:00
|
|
|
import { subdomainSchema } from "@server/schemas/subdomainSchema";
|
2024-10-01 21:53:49 -04:00
|
|
|
|
2024-10-02 22:05:21 -04:00
|
|
|
const createResourceParamsSchema = z.object({
|
2024-11-05 22:38:57 -05:00
|
|
|
siteId: z
|
|
|
|
.string()
|
|
|
|
.optional()
|
|
|
|
.transform(stoi)
|
|
|
|
.pipe(z.number().int().positive().optional()),
|
|
|
|
orgId: z.string(),
|
2024-10-02 22:05:21 -04:00
|
|
|
});
|
|
|
|
|
2024-11-14 00:00:17 -05:00
|
|
|
const createResourceSchema = z
|
|
|
|
.object({
|
|
|
|
name: z.string().min(1).max(255),
|
2024-11-15 18:25:27 -05:00
|
|
|
subdomain: subdomainSchema,
|
2024-11-14 00:00:17 -05:00
|
|
|
})
|
|
|
|
.strict();
|
2024-10-01 21:34:07 -04:00
|
|
|
|
2024-11-09 00:08:17 -05:00
|
|
|
export type CreateResourceResponse = Resource;
|
|
|
|
|
2024-11-05 22:38:57 -05:00
|
|
|
export async function createResource(
|
|
|
|
req: Request,
|
|
|
|
res: Response,
|
|
|
|
next: NextFunction
|
|
|
|
): Promise<any> {
|
2024-10-06 18:05:20 -04:00
|
|
|
try {
|
|
|
|
const parsedBody = createResourceSchema.safeParse(req.body);
|
|
|
|
if (!parsedBody.success) {
|
|
|
|
return next(
|
|
|
|
createHttpError(
|
|
|
|
HttpCode.BAD_REQUEST,
|
2024-11-03 13:57:51 -05:00
|
|
|
fromError(parsedBody.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 { name, subdomain } = parsedBody.data;
|
2024-10-02 22:05:21 -04:00
|
|
|
|
2024-10-06 18:05:20 -04:00
|
|
|
// Validate request params
|
|
|
|
const parsedParams = createResourceParamsSchema.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-02 22:05:21 -04:00
|
|
|
|
2024-10-06 18:05:20 -04:00
|
|
|
const { siteId, orgId } = parsedParams.data;
|
2024-10-01 21:53:49 -04:00
|
|
|
|
2024-10-12 21:36:14 -04:00
|
|
|
if (!req.userOrgRoleId) {
|
2024-11-05 22:38:57 -05:00
|
|
|
return next(
|
|
|
|
createHttpError(HttpCode.FORBIDDEN, "User does not have a role")
|
|
|
|
);
|
2024-10-06 18:05:20 -04:00
|
|
|
}
|
2024-10-06 16:43:59 -04:00
|
|
|
|
2024-10-19 16:19:47 -04:00
|
|
|
// get the org
|
2024-11-05 22:38:57 -05:00
|
|
|
const org = await db
|
|
|
|
.select()
|
2024-10-19 16:19:47 -04:00
|
|
|
.from(orgs)
|
|
|
|
.where(eq(orgs.orgId, orgId))
|
|
|
|
.limit(1);
|
2024-10-26 12:15:03 -04:00
|
|
|
|
2024-10-19 16:19:47 -04:00
|
|
|
if (org.length === 0) {
|
|
|
|
return next(
|
|
|
|
createHttpError(
|
|
|
|
HttpCode.NOT_FOUND,
|
|
|
|
`Organization with ID ${orgId} not found`
|
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2024-11-05 22:38:57 -05:00
|
|
|
const newResource = await db
|
|
|
|
.insert(resources)
|
|
|
|
.values({
|
|
|
|
siteId,
|
|
|
|
orgId,
|
|
|
|
name,
|
|
|
|
subdomain,
|
|
|
|
})
|
|
|
|
.returning();
|
|
|
|
|
|
|
|
const adminRole = await db
|
|
|
|
.select()
|
2024-10-12 21:36:14 -04:00
|
|
|
.from(roles)
|
2024-11-05 22:38:57 -05:00
|
|
|
.where(and(eq(roles.isAdmin, true), eq(roles.orgId, orgId)))
|
2024-10-12 21:36:14 -04:00
|
|
|
.limit(1);
|
|
|
|
|
2024-11-05 22:38:57 -05:00
|
|
|
if (adminRole.length === 0) {
|
2024-10-12 21:36:14 -04:00
|
|
|
return next(
|
2024-11-05 22:38:57 -05:00
|
|
|
createHttpError(HttpCode.NOT_FOUND, `Admin role not found`)
|
2024-10-12 21:36:14 -04:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
await db.insert(roleResources).values({
|
2024-11-05 22:38:57 -05:00
|
|
|
roleId: adminRole[0].roleId,
|
2024-10-12 21:36:14 -04:00
|
|
|
resourceId: newResource[0].resourceId,
|
|
|
|
});
|
|
|
|
|
2024-11-05 22:38:57 -05:00
|
|
|
if (req.userOrgRoleId != adminRole[0].roleId) {
|
2024-10-12 21:36:14 -04:00
|
|
|
// make sure the user can access the resource
|
|
|
|
await db.insert(userResources).values({
|
2024-10-13 17:13:47 -04:00
|
|
|
userId: req.user?.userId!,
|
2024-10-12 21:36:14 -04:00
|
|
|
resourceId: newResource[0].resourceId,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2024-11-09 00:08:17 -05:00
|
|
|
response<CreateResourceResponse>(res, {
|
2024-10-06 18:05:20 -04:00
|
|
|
data: newResource[0],
|
|
|
|
success: true,
|
|
|
|
error: false,
|
|
|
|
message: "Resource created successfully",
|
|
|
|
status: HttpCode.CREATED,
|
|
|
|
});
|
|
|
|
} catch (error) {
|
2024-11-05 22:38:57 -05:00
|
|
|
return next(
|
2024-11-09 00:08:17 -05:00
|
|
|
createHttpError(HttpCode.INTERNAL_SERVER_ERROR, "An error occurred")
|
2024-11-05 22:38:57 -05:00
|
|
|
);
|
2024-10-06 18:05:20 -04:00
|
|
|
}
|
2024-10-02 00:04:40 -04:00
|
|
|
}
|