Complete client form
This commit is contained in:
@@ -0,0 +1,57 @@
|
||||
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 });
|
||||
}
|
||||
@@ -0,0 +1,63 @@
|
||||
import _ from "lodash";
|
||||
import type { AdminCrudAPIParams, TableType } from "@/src/types";
|
||||
import type { APIResponseObject } from "@moduletrace/bunext/types";
|
||||
import checkUserAccess from "@/src/functions/backend/auth/check-user-access";
|
||||
import type {
|
||||
BUN_SQLITE_WGUI_ALL_TYPEDEFS,
|
||||
BUN_SQLITE_WGUI_CLIENTS,
|
||||
} from "@/db/types/db";
|
||||
import BunSQLite from "@moduletrace/bun-sqlite";
|
||||
import type { ServerQueryParam } from "@moduletrace/bun-sqlite/dist/types";
|
||||
|
||||
export default async function (
|
||||
params: AdminCrudAPIParams,
|
||||
): Promise<APIResponseObject> {
|
||||
const { user, user_types, body, id } = params;
|
||||
|
||||
const can_delete_client = checkUserAccess({
|
||||
user_types,
|
||||
// includes: ["admin"],
|
||||
});
|
||||
|
||||
if (!can_delete_client.success) {
|
||||
return {
|
||||
success: false,
|
||||
msg: `Can't delete client`,
|
||||
};
|
||||
}
|
||||
|
||||
let query = _.merge<
|
||||
ServerQueryParam<BUN_SQLITE_WGUI_ALL_TYPEDEFS>,
|
||||
ServerQueryParam<BUN_SQLITE_WGUI_ALL_TYPEDEFS>
|
||||
>(body?.sql_query || {}, {});
|
||||
|
||||
const clients_to_delete = await BunSQLite.select<
|
||||
BUN_SQLITE_WGUI_CLIENTS,
|
||||
TableType
|
||||
>({
|
||||
table: "clients",
|
||||
query,
|
||||
targetId: id,
|
||||
});
|
||||
|
||||
if (!clients_to_delete.payload) {
|
||||
return {
|
||||
success: false,
|
||||
msg: `No Media to Delete.`,
|
||||
};
|
||||
}
|
||||
|
||||
for (let i = 0; i < clients_to_delete.payload.length; i++) {
|
||||
const client = clients_to_delete.payload[i];
|
||||
if (!client?.id) continue;
|
||||
|
||||
await BunSQLite.delete<BUN_SQLITE_WGUI_CLIENTS, TableType>({
|
||||
table: "clients",
|
||||
targetId: client.id,
|
||||
});
|
||||
}
|
||||
|
||||
return {
|
||||
success: true,
|
||||
};
|
||||
}
|
||||
@@ -0,0 +1,38 @@
|
||||
import type { BUN_SQLITE_WGUI_CLIENTS } from "@/db/types/db";
|
||||
import checkUserAccess from "@/src/functions/backend/auth/check-user-access";
|
||||
import type { AdminCrudAPIParams, TableType } from "@/src/types";
|
||||
import BunSQLite from "@moduletrace/bun-sqlite";
|
||||
import type { APIResponseObject } from "@moduletrace/bunext/types";
|
||||
import _ from "lodash";
|
||||
|
||||
export default async function (
|
||||
params: AdminCrudAPIParams,
|
||||
): Promise<APIResponseObject> {
|
||||
const { table, user_types, body, id, query, user } = params;
|
||||
|
||||
let final_sql_query = _.merge(query?.sql_query, body?.sql_query) || {};
|
||||
|
||||
delete final_sql_query.join;
|
||||
|
||||
const can_user_see_all_clients = checkUserAccess({
|
||||
user_types,
|
||||
includes: ["admin"],
|
||||
});
|
||||
|
||||
if (!can_user_see_all_clients.success) {
|
||||
final_sql_query.query = {
|
||||
...final_sql_query.query,
|
||||
user_id: {
|
||||
value: user.id,
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
const GET = await BunSQLite.select<BUN_SQLITE_WGUI_CLIENTS, TableType>({
|
||||
table,
|
||||
query: final_sql_query,
|
||||
targetId: id,
|
||||
});
|
||||
|
||||
return GET;
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
import type { AdminCrudAPIParams } from "@/src/types";
|
||||
import type { APIResponseObject } from "@moduletrace/bunext/types";
|
||||
import _ from "lodash";
|
||||
import get from "./get";
|
||||
import post from "./post";
|
||||
import put from "./put";
|
||||
import del from "./del";
|
||||
|
||||
export default async function (
|
||||
params: AdminCrudAPIParams,
|
||||
): Promise<APIResponseObject> {
|
||||
const { req, query } = params;
|
||||
|
||||
switch (req.method) {
|
||||
case "GET":
|
||||
return await get(params);
|
||||
case "POST":
|
||||
return await post(params);
|
||||
case "PUT":
|
||||
return await put(params);
|
||||
case "DELETE":
|
||||
return await del(params);
|
||||
|
||||
default:
|
||||
return {
|
||||
success: false,
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,52 @@
|
||||
import type { BUN_SQLITE_WGUI_CLIENTS } from "@/db/types/db";
|
||||
import checkUserAccess from "@/src/functions/backend/auth/check-user-access";
|
||||
import type { AdminCrudAPIParams } from "@/src/types";
|
||||
import BunSQLite from "@moduletrace/bun-sqlite";
|
||||
import type { APIResponseObject } from "@moduletrace/bunext/types";
|
||||
import runClientSetup from "./(functions)/run-client-setup";
|
||||
import _ from "lodash";
|
||||
|
||||
export default async function (
|
||||
params: AdminCrudAPIParams,
|
||||
): Promise<APIResponseObject> {
|
||||
const { table, body, user_types, user } = params;
|
||||
|
||||
let final_insert_data: BUN_SQLITE_WGUI_CLIENTS[] = [
|
||||
...(body?.insert_data || []),
|
||||
].map((d) => ({ ...d, user_id: user.id }));
|
||||
|
||||
const is_user_allowed_to_post_any = checkUserAccess({
|
||||
// includes: ["admin"],
|
||||
user_types,
|
||||
});
|
||||
|
||||
if (!is_user_allowed_to_post_any.success) {
|
||||
final_insert_data = final_insert_data.map((fid) => ({
|
||||
...fid,
|
||||
user_id: user.id,
|
||||
}));
|
||||
}
|
||||
|
||||
const POST = await BunSQLite.insert({
|
||||
table,
|
||||
data: final_insert_data,
|
||||
update_on_duplicate: body?.update_on_duplicate,
|
||||
});
|
||||
|
||||
if (POST.success) {
|
||||
const client_setup_res = await runClientSetup({
|
||||
client_id: POST.postInsertReturn?.insertId,
|
||||
user,
|
||||
});
|
||||
|
||||
if (!client_setup_res.success) {
|
||||
POST.debug = {
|
||||
...(POST.debug || {}),
|
||||
client_setup: client_setup_res,
|
||||
};
|
||||
POST.error = client_setup_res.msg || client_setup_res.error;
|
||||
}
|
||||
}
|
||||
|
||||
return POST;
|
||||
}
|
||||
@@ -0,0 +1,56 @@
|
||||
import checkUserAccess from "@/src/functions/backend/auth/check-user-access";
|
||||
import type { AdminCrudAPIParams } from "@/src/types";
|
||||
import BunSQLite from "@moduletrace/bun-sqlite";
|
||||
import type { APIResponseObject } from "@moduletrace/bunext/types";
|
||||
import runClientSetup from "./(functions)/run-client-setup";
|
||||
import _ from "lodash";
|
||||
|
||||
export default async function (
|
||||
params: AdminCrudAPIParams,
|
||||
): Promise<APIResponseObject> {
|
||||
const { table, body, user_types, id, query, user } = params;
|
||||
|
||||
if (!body?.update_data) {
|
||||
return {
|
||||
success: false,
|
||||
msg: `No Update Data`,
|
||||
};
|
||||
}
|
||||
|
||||
const is_user_allowed_to_put = checkUserAccess({
|
||||
// includes: ["admin"],
|
||||
user_types,
|
||||
});
|
||||
|
||||
let final_sql_query = _.merge(query?.sql_query, body?.sql_query) || {};
|
||||
let targetId = id;
|
||||
|
||||
if (!is_user_allowed_to_put.success) {
|
||||
final_sql_query = {};
|
||||
targetId = user.id;
|
||||
}
|
||||
|
||||
const PUT = await BunSQLite.update({
|
||||
table,
|
||||
data: body?.update_data,
|
||||
targetId,
|
||||
query: final_sql_query,
|
||||
});
|
||||
|
||||
if (PUT.success) {
|
||||
const client_setup_res = await runClientSetup({
|
||||
client_id: targetId,
|
||||
user,
|
||||
});
|
||||
|
||||
if (!client_setup_res.success) {
|
||||
PUT.debug = {
|
||||
...(PUT.debug || {}),
|
||||
client_setup: client_setup_res,
|
||||
};
|
||||
PUT.error = client_setup_res.msg || client_setup_res.error;
|
||||
}
|
||||
}
|
||||
|
||||
return PUT;
|
||||
}
|
||||
@@ -13,6 +13,7 @@ import type {
|
||||
import type { AdminCrudAPIParams, ApiReqParams, TableType } from "@/src/types";
|
||||
import userAuth from "@/src/functions/backend/auth/user-auth";
|
||||
import checkUserAccess from "@/src/functions/backend/auth/check-user-access";
|
||||
import clients from "./(tables)/clients";
|
||||
|
||||
export const handler: BunextAPIRouteHandler<APIResponseObject> = async (
|
||||
params,
|
||||
@@ -58,6 +59,8 @@ export const handler: BunextAPIRouteHandler<APIResponseObject> = async (
|
||||
switch (table) {
|
||||
case "media":
|
||||
return await media(crud_params);
|
||||
case "clients":
|
||||
return await clients(crud_params);
|
||||
|
||||
default:
|
||||
break;
|
||||
|
||||
@@ -0,0 +1,55 @@
|
||||
import userAuth from "@/src/functions/backend/auth/user-auth";
|
||||
import grabNextAvailableClientIPAddress from "@/src/functions/backend/setup/grab-next-available-client-ip";
|
||||
import BunSQLite from "@moduletrace/bun-sqlite";
|
||||
import type { BUN_SQLITE_WGUI_HOSTS } from "@/db/types/db";
|
||||
import type { ApiReqParams, TableType } 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 !== "POST") {
|
||||
return {
|
||||
success: false,
|
||||
};
|
||||
}
|
||||
|
||||
const { user, user_types } = await userAuth({ req });
|
||||
|
||||
if (!user?.logged_in_status) {
|
||||
return {
|
||||
success: false,
|
||||
msg: `Unauthorized`,
|
||||
logoutUser: true,
|
||||
};
|
||||
}
|
||||
|
||||
try {
|
||||
let host: BUN_SQLITE_WGUI_HOSTS | undefined;
|
||||
|
||||
if (body?.host_id) {
|
||||
const host_res = await BunSQLite.select<
|
||||
BUN_SQLITE_WGUI_HOSTS,
|
||||
TableType
|
||||
>({
|
||||
table: "hosts",
|
||||
targetId: body.host_id,
|
||||
});
|
||||
|
||||
host = host_res.singleRes || undefined;
|
||||
}
|
||||
|
||||
return await grabNextAvailableClientIPAddress({ host });
|
||||
} catch (error: any) {
|
||||
return {
|
||||
success: false,
|
||||
msg: error.message,
|
||||
};
|
||||
}
|
||||
};
|
||||
Reference in New Issue
Block a user