2024-11-05 22:38:57 -05:00
|
|
|
import { Request, Response, NextFunction } from "express";
|
|
|
|
import { z } from "zod";
|
|
|
|
import { db } from "@server/db";
|
|
|
|
import { eq } from "drizzle-orm";
|
2024-11-09 23:59:19 -05:00
|
|
|
import { orgs, roleActions, roles, userOrgs } 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 logger from "@server/logger";
|
|
|
|
import { createAdminRole } from "@server/db/ensureActions";
|
2024-11-05 23:55:46 -05:00
|
|
|
import config from "@server/config";
|
2024-11-05 22:38:57 -05:00
|
|
|
import { fromError } from "zod-validation-error";
|
2024-11-09 23:59:19 -05:00
|
|
|
import { defaultRoleAllowedActions } from "../role";
|
2024-10-01 21:53:49 -04:00
|
|
|
|
|
|
|
const createOrgSchema = z.object({
|
2024-10-14 19:30:38 -04:00
|
|
|
orgId: z.string(),
|
2024-10-06 18:05:20 -04:00
|
|
|
name: z.string().min(1).max(255),
|
2024-10-14 19:30:38 -04:00
|
|
|
// domain: z.string().min(1).max(255).optional(),
|
2024-10-01 21:53:49 -04:00
|
|
|
});
|
2024-10-01 21:34:07 -04:00
|
|
|
|
2024-10-03 22:31:20 -04:00
|
|
|
const MAX_ORGS = 5;
|
|
|
|
|
2024-11-05 22:38:57 -05:00
|
|
|
export async function createOrg(
|
|
|
|
req: Request,
|
|
|
|
res: Response,
|
|
|
|
next: NextFunction
|
|
|
|
): Promise<any> {
|
2024-10-06 18:05:20 -04:00
|
|
|
try {
|
|
|
|
const parsedBody = createOrgSchema.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 userOrgIds = req.userOrgIds;
|
|
|
|
if (userOrgIds && userOrgIds.length > MAX_ORGS) {
|
|
|
|
return next(
|
|
|
|
createHttpError(
|
|
|
|
HttpCode.FORBIDDEN,
|
|
|
|
`Maximum number of organizations reached.`
|
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
2024-10-03 22:31:20 -04:00
|
|
|
|
2024-10-14 19:30:38 -04:00
|
|
|
const { orgId, name } = parsedBody.data;
|
2024-10-06 16:43:59 -04:00
|
|
|
|
2024-10-14 19:30:38 -04:00
|
|
|
// make sure the orgId is unique
|
2024-11-05 22:38:57 -05:00
|
|
|
const orgExists = await db
|
|
|
|
.select()
|
2024-10-14 19:30:38 -04:00
|
|
|
.from(orgs)
|
|
|
|
.where(eq(orgs.orgId, orgId))
|
|
|
|
.limit(1);
|
2024-11-05 22:38:57 -05:00
|
|
|
|
2024-10-14 19:30:38 -04:00
|
|
|
if (orgExists.length > 0) {
|
|
|
|
return next(
|
|
|
|
createHttpError(
|
|
|
|
HttpCode.CONFLICT,
|
|
|
|
`Organization with ID ${orgId} already exists`
|
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
2024-10-01 21:53:49 -04:00
|
|
|
|
2024-10-26 12:15:03 -04:00
|
|
|
// create a url from config.app.base_url and get the hostname
|
|
|
|
const domain = new URL(config.app.base_url).hostname;
|
|
|
|
|
2024-11-05 22:38:57 -05:00
|
|
|
const newOrg = await db
|
|
|
|
.insert(orgs)
|
|
|
|
.values({
|
|
|
|
orgId,
|
|
|
|
name,
|
|
|
|
domain,
|
|
|
|
})
|
|
|
|
.returning();
|
2024-10-01 21:53:49 -04:00
|
|
|
|
2024-11-05 22:38:57 -05:00
|
|
|
const roleId = await createAdminRole(newOrg[0].orgId);
|
2024-10-14 19:30:38 -04:00
|
|
|
|
|
|
|
if (!roleId) {
|
|
|
|
return next(
|
|
|
|
createHttpError(
|
|
|
|
HttpCode.INTERNAL_SERVER_ERROR,
|
2024-11-05 22:38:57 -05:00
|
|
|
`Error creating Admin role`
|
2024-10-14 19:30:38 -04:00
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2024-11-05 22:38:57 -05:00
|
|
|
await db
|
|
|
|
.insert(userOrgs)
|
|
|
|
.values({
|
|
|
|
userId: req.user!.userId,
|
|
|
|
orgId: newOrg[0].orgId,
|
|
|
|
roleId: roleId,
|
2024-11-08 00:03:54 -05:00
|
|
|
isOwner: true,
|
2024-11-05 22:38:57 -05:00
|
|
|
})
|
|
|
|
.execute();
|
2024-10-10 21:59:30 -04:00
|
|
|
|
2024-11-09 23:59:19 -05:00
|
|
|
const memberRole = await db
|
|
|
|
.insert(roles)
|
|
|
|
.values({
|
|
|
|
name: "Member",
|
|
|
|
description: "Members can only view resources",
|
|
|
|
orgId,
|
|
|
|
})
|
|
|
|
.returning();
|
|
|
|
|
|
|
|
await db
|
|
|
|
.insert(roleActions)
|
|
|
|
.values(
|
|
|
|
defaultRoleAllowedActions.map((action) => ({
|
|
|
|
roleId: memberRole[0].roleId,
|
|
|
|
actionId: action,
|
|
|
|
orgId,
|
|
|
|
}))
|
|
|
|
)
|
|
|
|
.execute();
|
|
|
|
|
2024-10-06 18:05:20 -04:00
|
|
|
return response(res, {
|
|
|
|
data: newOrg[0],
|
|
|
|
success: true,
|
|
|
|
error: false,
|
|
|
|
message: "Organization created successfully",
|
|
|
|
status: HttpCode.CREATED,
|
|
|
|
});
|
|
|
|
} catch (error) {
|
|
|
|
logger.error(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
|
|
|
}
|