This commit is contained in:
2026-09-20 17:04:17 +01:00
parent 69111e461e
commit aa0a292943
16 changed files with 295 additions and 50 deletions
+90
View File
@@ -0,0 +1,90 @@
import type { BUN_SQLITE_WGUI_HOSTS } from "@/db/types/db";
import checkUserAccess from "@/src/functions/backend/auth/check-user-access";
import userAuth from "@/src/functions/backend/auth/user-auth";
import createWireguardHost from "@/src/functions/backend/setup/create-wireguard-host";
import setupWireguardHost from "@/src/functions/backend/setup/setup-wireguard-host";
import type { ApiReqParams, TableType } from "@/src/types";
import BunSQLite from "@moduletrace/bun-sqlite";
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 !== "POST") {
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`,
};
}
if (typeof body.host_id !== "number") {
throw new Error(`Invalid host id`);
}
const host_record_res = await BunSQLite.select<
BUN_SQLITE_WGUI_HOSTS,
TableType
>({
table: "hosts",
targetId: body.host_id,
});
const host = host_record_res.singleRes;
if (!host?.created_at) {
throw new Error(`Couldn't find host record`);
}
if (!body.update_data) {
throw new Error(`No data to update`);
}
const update_host_record = await BunSQLite.update<
BUN_SQLITE_WGUI_HOSTS,
TableType
>({
table: "hosts",
data: body.update_data,
targetId: String(host.id),
});
const update = await setupWireguardHost({
host,
user,
});
return {
success: true,
};
} catch (error: any) {
return {
success: false,
msg: error.message,
};
}
};