Update clients fetch function

This commit is contained in:
2026-09-27 06:59:16 +01:00
parent c23c8f73cd
commit 8ed5fd1845
24 changed files with 280 additions and 151 deletions
@@ -0,0 +1,134 @@
import { AppData } from "@/src/data/app-data";
import type { ApiReqParams, SQLInsertGenValueType } from "@/src/types";
import type { BUN_SQLITE_WGUI_CLIENTS_JOIN } from "@/src/types/sql-joins";
import type { ServerQueryParamOrder } from "@moduletrace/bun-sqlite/dist/types";
import BunSQLite from "@moduletrace/bun-sqlite";
import type { APIResponseObject } from "@moduletrace/bunext/types";
type Params = {
query?: ApiReqParams;
/**
* For search terms (ie `search_term_*`) parameters.
* This forces all searches to `=` instead of `LIKE`
*/
exact_match?: boolean;
host_id?: string | number;
user_id?: string | number;
client_id?: string | number;
};
const ORDERABLE_FIELDS = [
"id",
"created_at",
"updated_at",
"user_id",
"host_id",
"name",
"wg_ip_address",
"public_ip_address",
"public_key",
"allowed_ips",
"allow_all_ips",
"notes",
"host_name",
] as const;
export default async function grabClients(
params?: Params,
): Promise<APIResponseObject<BUN_SQLITE_WGUI_CLIENTS_JOIN>> {
const { query } = params || {};
const sql_query = query?.sql_query;
try {
const LIMIT = AppData["DefaultQueryLimit"];
let sql = ``;
let values: SQLInsertGenValueType[] = [];
sql += `SELECT\n`;
sql += ` c.*,\n`;
sql += ` h.name AS host_name\n`;
sql += `FROM clients c\n`;
sql += `LEFT JOIN hosts h ON c.host_id = h.id\n`;
sql += `WHERE 1=1\n`;
const target_host_id = params?.host_id || query?.host_id;
const target_user_id = params?.user_id || query?.user_id;
const target_client_id = params?.client_id || query?.client_id || query?.id;
if (target_host_id) {
sql += ` AND c.host_id=?\n`;
values.push(target_host_id);
}
if (target_user_id) {
sql += ` AND c.user_id=?\n`;
values.push(target_user_id);
}
if (target_client_id) {
sql += ` AND c.id=?\n`;
values.push(target_client_id);
}
let search_terms = [
{ field: "name", value: query?.name },
{ field: "wg_ip_address", value: query?.wg_ip_address },
] as const;
for (const term of search_terms) {
if (term.value) {
const match_value = params?.exact_match ? "=" : "LIKE";
const match_value_param = params?.exact_match
? term.value
: `%${term.value.replace(/ /g, "%")}%`;
sql += ` AND c.${term.field} ${match_value} ?\n`;
values.push(match_value_param);
}
}
const order = sql_query?.order as
| ServerQueryParamOrder<any>
| ServerQueryParamOrder<any>[]
| undefined;
const order_terms = (Array.isArray(order) ? order : order ? [order] : [])
.filter((o) => ORDERABLE_FIELDS.includes(o?.field as any))
.map((o) => {
const is_host_field = o.field === "host_name";
return `${is_host_field ? "h" : "c"}.${String(o.field)} ${o.strategy === "DESC" ? "DESC" : "ASC"}`;
});
let sql_order = ``;
if (order_terms[0]) {
sql_order += `ORDER BY ${order_terms.join(", ")}\n`;
}
let sql_limit = ``;
const target_limit = sql_query?.limit || LIMIT;
const target_page = sql_query?.page || query?.page;
sql_limit += `LIMIT ${target_limit}\n`;
if (target_page) {
const offset = target_limit * (target_page - 1);
sql_limit += `OFFSET ${offset}\n`;
}
const res = await BunSQLite.sql<BUN_SQLITE_WGUI_CLIENTS_JOIN>({
sql: `${sql}\n${sql_order}\n${sql_limit}`,
values,
});
if (query?.count) {
const count_sql = `SELECT COUNT(*) AS count FROM (${sql})`;
const count_res = await BunSQLite.sql({ sql: count_sql, values });
res.count = count_res.singleRes?.count;
}
return res;
} catch (error: any) {
return {
success: false,
msg: error.message,
};
}
}