Refactor main host setup
This commit is contained in:
@@ -47,6 +47,7 @@ export const handler: BunextAPIRouteHandler<APIResponseObject> = async (
|
||||
short_description: body?.short_description,
|
||||
listen_port: body?.listen_port,
|
||||
user,
|
||||
is_main_host: body.is_main_host || undefined,
|
||||
});
|
||||
} catch (error: any) {
|
||||
return {
|
||||
@@ -54,4 +55,4 @@ export const handler: BunextAPIRouteHandler<APIResponseObject> = async (
|
||||
msg: error.message,
|
||||
};
|
||||
}
|
||||
};
|
||||
};
|
||||
|
||||
@@ -0,0 +1,60 @@
|
||||
import checkUserAccess from "@/src/functions/backend/auth/check-user-access";
|
||||
import userAuth from "@/src/functions/backend/auth/user-auth";
|
||||
import deleteWireguardHost from "@/src/functions/backend/setup/delete-wireguard-host";
|
||||
import type { ApiReqParams } from "@/src/types";
|
||||
import type {
|
||||
APIResponseObject,
|
||||
BunextAPIRouteHandler,
|
||||
} from "@moduletrace/bunext/types";
|
||||
|
||||
export const handler: BunextAPIRouteHandler<APIResponseObject> = async (
|
||||
params,
|
||||
) => {
|
||||
const req = params.req;
|
||||
const body = params.body as ApiReqParams;
|
||||
|
||||
if (req.method !== "DELETE") {
|
||||
return {
|
||||
success: false,
|
||||
};
|
||||
}
|
||||
|
||||
const { user, user_types } = await userAuth({ req });
|
||||
|
||||
if (!user?.logged_in_status) {
|
||||
return {
|
||||
success: false,
|
||||
msg: `Unauthorized`,
|
||||
logoutUser: true,
|
||||
};
|
||||
}
|
||||
|
||||
try {
|
||||
const is_super_admin = checkUserAccess({ user_types });
|
||||
|
||||
if (!is_super_admin.success) {
|
||||
return {
|
||||
success: false,
|
||||
msg: `Unauthorized`,
|
||||
};
|
||||
}
|
||||
|
||||
const host_id = body?.host_id;
|
||||
|
||||
if (typeof host_id !== "number" && typeof host_id !== "string") {
|
||||
throw new Error(`Please pass host ID to be deleted!`);
|
||||
}
|
||||
|
||||
const delete_host_res = await deleteWireguardHost({
|
||||
host_id: Number(host_id),
|
||||
user,
|
||||
});
|
||||
|
||||
return delete_host_res;
|
||||
} catch (error: any) {
|
||||
return {
|
||||
success: false,
|
||||
msg: error.message,
|
||||
};
|
||||
}
|
||||
};
|
||||
@@ -1,12 +1,11 @@
|
||||
import checkUserAccess from "@/src/functions/backend/auth/check-user-access";
|
||||
import userAuth from "@/src/functions/backend/auth/user-auth";
|
||||
import setupWireguardHost from "@/src/functions/backend/setup/setup-wireguard-host";
|
||||
import createWireguardHost from "@/src/functions/backend/setup/create-wireguard-host";
|
||||
import type { ApiReqParams } from "@/src/types";
|
||||
import type {
|
||||
APIResponseObject,
|
||||
BunextAPIRouteHandler,
|
||||
} from "@moduletrace/bunext/types";
|
||||
import _ from "lodash";
|
||||
|
||||
export const handler: BunextAPIRouteHandler<APIResponseObject> = async (
|
||||
params,
|
||||
@@ -40,11 +39,12 @@ export const handler: BunextAPIRouteHandler<APIResponseObject> = async (
|
||||
};
|
||||
}
|
||||
|
||||
return await setupWireguardHost({
|
||||
host: {
|
||||
wg_ip_address: body.ip_address,
|
||||
},
|
||||
return await createWireguardHost({
|
||||
wg_ip_address: body.ip_address,
|
||||
interface: body.interface,
|
||||
name: "Main",
|
||||
user,
|
||||
is_main_host: true,
|
||||
});
|
||||
} catch (error: any) {
|
||||
return {
|
||||
|
||||
Reference in New Issue
Block a user