2024-10-02 22:05:21 -04:00
|
|
|
import { Request, Response, NextFunction } from 'express';
|
|
|
|
import { z } from 'zod';
|
|
|
|
import { db } from '@server/db';
|
2024-10-12 21:36:14 -04:00
|
|
|
import { roles, userSites, sites, roleSites } from '@server/db/schema';
|
2024-10-01 21:34:07 -04:00
|
|
|
import response from "@server/utils/response";
|
2024-10-02 22:05:21 -04:00
|
|
|
import HttpCode from '@server/types/HttpCode';
|
|
|
|
import createHttpError from 'http-errors';
|
2024-10-03 22:31:20 -04:00
|
|
|
import fetch from 'node-fetch';
|
2024-10-06 16:43:59 -04:00
|
|
|
import { ActionsEnum, checkUserActionPermission } from '@server/auth/actions';
|
2024-10-06 18:05:20 -04:00
|
|
|
import logger from '@server/logger';
|
2024-10-12 21:36:14 -04:00
|
|
|
import { eq, and } from 'drizzle-orm';
|
2024-10-14 21:59:35 -04:00
|
|
|
import { getUniqueName } from '@server/db/names';
|
2024-10-03 22:31:20 -04:00
|
|
|
|
|
|
|
const API_BASE_URL = "http://localhost:3000";
|
2024-10-01 21:34:07 -04:00
|
|
|
|
2024-10-06 18:05:20 -04:00
|
|
|
const createSiteParamsSchema = z.object({
|
2024-10-14 15:11:18 -04:00
|
|
|
orgId: z.string()
|
2024-10-02 22:05:21 -04:00
|
|
|
});
|
2024-10-01 21:34:07 -04:00
|
|
|
|
2024-10-02 22:05:21 -04:00
|
|
|
// Define Zod schema for request body validation
|
|
|
|
const createSiteSchema = z.object({
|
2024-10-06 18:05:20 -04:00
|
|
|
name: z.string().min(1).max(255),
|
|
|
|
subdomain: z.string().min(1).max(255).optional(),
|
|
|
|
pubKey: z.string().optional(),
|
|
|
|
subnet: z.string().optional(),
|
2024-10-02 22:05:21 -04:00
|
|
|
});
|
|
|
|
|
2024-10-20 12:55:28 -04:00
|
|
|
export type CreateSiteResponse = {
|
2024-10-14 21:59:35 -04:00
|
|
|
name: string;
|
|
|
|
siteId: number;
|
|
|
|
orgId: string;
|
|
|
|
niceId: string;
|
|
|
|
// niceId: string;
|
|
|
|
// subdomain: string;
|
|
|
|
// subnet: string;
|
|
|
|
};
|
|
|
|
|
2024-10-02 22:05:21 -04:00
|
|
|
export async function createSite(req: Request, res: Response, next: NextFunction): Promise<any> {
|
2024-10-06 18:05:20 -04:00
|
|
|
try {
|
|
|
|
// Validate request body
|
|
|
|
const parsedBody = createSiteSchema.safeParse(req.body);
|
|
|
|
if (!parsedBody.success) {
|
|
|
|
return next(
|
|
|
|
createHttpError(
|
|
|
|
HttpCode.BAD_REQUEST,
|
|
|
|
parsedBody.error.errors.map(e => e.message).join(', ')
|
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
2024-10-02 22:05:21 -04:00
|
|
|
|
2024-10-06 18:05:20 -04:00
|
|
|
const { name, subdomain, pubKey, subnet } = parsedBody.data;
|
2024-10-02 22:05:21 -04:00
|
|
|
|
2024-10-06 18:05:20 -04:00
|
|
|
// Validate request params
|
|
|
|
const parsedParams = createSiteParamsSchema.safeParse(req.params);
|
|
|
|
if (!parsedParams.success) {
|
|
|
|
return next(
|
|
|
|
createHttpError(
|
|
|
|
HttpCode.BAD_REQUEST,
|
|
|
|
parsedParams.error.errors.map(e => e.message).join(', ')
|
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
2024-10-02 22:05:21 -04:00
|
|
|
|
2024-10-06 18:05:20 -04:00
|
|
|
const { orgId } = parsedParams.data;
|
|
|
|
|
|
|
|
// Check if the user has permission to list sites
|
|
|
|
const hasPermission = await checkUserActionPermission(ActionsEnum.createSite, req);
|
|
|
|
if (!hasPermission) {
|
2024-10-12 21:36:14 -04:00
|
|
|
return next(createHttpError(HttpCode.FORBIDDEN, 'User does not have permission perform this action'));
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!req.userOrgRoleId) {
|
|
|
|
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-14 22:33:51 -04:00
|
|
|
const niceId = await getUniqueName(orgId);
|
2024-10-14 21:59:35 -04:00
|
|
|
|
|
|
|
// TODO: pick a subnet
|
|
|
|
|
2024-10-06 18:05:20 -04:00
|
|
|
// Create new site in the database
|
2024-10-14 21:59:35 -04:00
|
|
|
const [newSite] = await db.insert(sites).values({
|
2024-10-06 18:05:20 -04:00
|
|
|
orgId,
|
|
|
|
name,
|
2024-10-14 21:59:35 -04:00
|
|
|
niceId,
|
2024-10-06 18:05:20 -04:00
|
|
|
pubKey,
|
|
|
|
subnet,
|
|
|
|
}).returning();
|
2024-10-12 21:36:14 -04:00
|
|
|
// find the superuser roleId and also add the resource to the superuser role
|
|
|
|
const superuserRole = await db.select()
|
|
|
|
.from(roles)
|
|
|
|
.where(and(eq(roles.isSuperuserRole, true), eq(roles.orgId, orgId)))
|
|
|
|
.limit(1);
|
2024-10-13 17:13:47 -04:00
|
|
|
|
2024-10-12 21:36:14 -04:00
|
|
|
if (superuserRole.length === 0) {
|
|
|
|
return next(
|
|
|
|
createHttpError(
|
|
|
|
HttpCode.NOT_FOUND,
|
|
|
|
`Superuser role not found`
|
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
2024-10-13 17:13:47 -04:00
|
|
|
|
|
|
|
await db.insert(roleSites).values({
|
2024-10-12 21:36:14 -04:00
|
|
|
roleId: superuserRole[0].roleId,
|
2024-10-14 21:59:35 -04:00
|
|
|
siteId: newSite.siteId,
|
2024-10-12 21:36:14 -04:00
|
|
|
});
|
2024-10-02 22:05:21 -04:00
|
|
|
|
2024-10-12 21:36:14 -04:00
|
|
|
if (req.userOrgRoleId != superuserRole[0].roleId) {
|
|
|
|
// make sure the user can access the site
|
2024-10-13 17:13:47 -04:00
|
|
|
db.insert(userSites).values({
|
|
|
|
userId: req.user?.userId!,
|
2024-10-14 21:59:35 -04:00
|
|
|
siteId: newSite.siteId,
|
2024-10-12 21:36:14 -04:00
|
|
|
});
|
|
|
|
}
|
2024-10-13 17:13:47 -04:00
|
|
|
|
2024-10-06 18:05:20 -04:00
|
|
|
return response(res, {
|
2024-10-14 21:59:35 -04:00
|
|
|
data: {
|
|
|
|
name: newSite.name,
|
|
|
|
niceId: newSite.niceId,
|
|
|
|
siteId: newSite.siteId,
|
|
|
|
orgId: newSite.orgId,
|
|
|
|
// subdomain: newSite.subdomain,
|
|
|
|
// subnet: newSite.subnet,
|
|
|
|
},
|
2024-10-06 18:05:20 -04:00
|
|
|
success: true,
|
|
|
|
error: false,
|
|
|
|
message: "Site created successfully",
|
|
|
|
status: HttpCode.CREATED,
|
|
|
|
});
|
|
|
|
} catch (error) {
|
2024-10-14 22:26:32 -04:00
|
|
|
throw error;
|
2024-10-06 18:05:20 -04:00
|
|
|
return next(createHttpError(HttpCode.INTERNAL_SERVER_ERROR, "An error occurred..."));
|
|
|
|
}
|
2024-10-03 22:31:20 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
async function addPeer(peer: string) {
|
2024-10-06 18:05:20 -04:00
|
|
|
try {
|
|
|
|
const response = await fetch(`${API_BASE_URL}/peer`, {
|
|
|
|
method: 'POST',
|
|
|
|
headers: {
|
|
|
|
'Content-Type': 'application/json',
|
|
|
|
},
|
|
|
|
body: JSON.stringify(peer),
|
|
|
|
});
|
2024-10-03 22:31:20 -04:00
|
|
|
|
2024-10-06 18:05:20 -04:00
|
|
|
if (!response.ok) {
|
|
|
|
throw new Error(`HTTP error! status: ${response.status}`);
|
|
|
|
}
|
2024-10-03 22:31:20 -04:00
|
|
|
|
2024-10-06 18:05:20 -04:00
|
|
|
const data: any = await response.json();
|
|
|
|
logger.info('Peer added successfully:', data.status);
|
|
|
|
return data;
|
|
|
|
} catch (error: any) {
|
|
|
|
throw error;
|
|
|
|
}
|
2024-10-03 22:31:20 -04:00
|
|
|
}
|
|
|
|
|