Files
wireguard-ui/src/pages/api/admin/crud/(tables)/clients/(functions)/run-client-setup.ts
T
2026-09-20 04:14:32 +01:00

59 lines
1.4 KiB
TypeScript

import type {
BUN_SQLITE_WGUI_CLIENTS,
BUN_SQLITE_WGUI_HOSTS,
} from "@/db/types/db";
import setupWireguardClient from "@/src/functions/backend/setup/setup-wireguard-client";
import type { TableType, User } from "@/src/types";
import BunSQLite from "@moduletrace/bun-sqlite";
import type { APIResponseObject } from "@moduletrace/bunext/types";
type Params = {
client_id?: string | number;
user: User;
};
export default async function runClientSetup({
client_id,
user,
}: Params): Promise<APIResponseObject> {
if (!client_id) {
return {
success: false,
msg: `No client id provided`,
};
}
const client_res = await BunSQLite.select<
BUN_SQLITE_WGUI_CLIENTS,
TableType
>({
table: "clients",
targetId: client_id,
});
const client = client_res.singleRes;
if (!client) {
return {
success: false,
msg: `Client record not found`,
};
}
let host: BUN_SQLITE_WGUI_HOSTS | undefined;
if (client?.host_id) {
const host_res = await BunSQLite.select<
BUN_SQLITE_WGUI_HOSTS,
TableType
>({
table: "hosts",
targetId: client.host_id,
});
host = host_res.singleRes || undefined;
}
return await setupWireguardClient({ client, host, user });
}