Update host update form for main host (host 0)

This commit is contained in:
2026-09-20 20:48:47 +01:00
parent b2706ce878
commit a681354eab
5 changed files with 4 additions and 160 deletions
@@ -1,156 +0,0 @@
import type { BUN_SQLITE_WGUI_CLIENT_RULES } from "@/db/types/db";
import expandClientRuleDestinations from "@/src/functions/backend/setup/expand-client-rule-destinations";
import sanitizeClientRule from "@/src/functions/backend/setup/sanitize-client-rule";
import validateClientRule from "@/src/functions/backend/setup/validate-client-rule";
import type { TableType } from "@/src/types";
import BunSQLite from "@moduletrace/bun-sqlite";
import type { APIResponseObject } from "@moduletrace/bunext/types";
type Params = {
client_id: number;
user_id?: number | "";
rules?: BUN_SQLITE_WGUI_CLIENT_RULES[];
};
export default async function syncClientRules({
client_id,
user_id,
rules,
}: Params): Promise<APIResponseObject> {
const existing_res = await BunSQLite.select<
BUN_SQLITE_WGUI_CLIENT_RULES,
TableType
>({
table: "client_rules",
query: {
query: {
client_id: {
value: String(client_id),
},
},
},
});
if (!existing_res.success) {
return {
success: false,
msg: existing_res.msg || `Could not load client rules`,
};
}
const incoming: BUN_SQLITE_WGUI_CLIENT_RULES[] = [];
for (let i = 0; i < (rules || []).length; i++) {
const sanitized = sanitizeClientRule({
rule: rules?.[i],
client_id,
user_id,
});
if (!sanitized) {
continue;
}
const expanded = expandClientRuleDestinations({ rule: sanitized });
for (let e = 0; e < expanded.length; e++) {
const rule = expanded[e];
if (!rule) {
continue;
}
const valid = validateClientRule({ rule });
if (!valid.success) {
return {
success: false,
msg: valid.msg,
};
}
incoming.push(rule);
}
}
const incoming_ids = new Set(
incoming
.map((rule) => rule.id)
.filter((id): id is number => Boolean(id) && typeof id == "number"),
);
const existing = existing_res.payload || [];
for (let i = 0; i < existing.length; i++) {
const rule = existing[i];
if (!rule?.id || incoming_ids.has(rule.id)) {
continue;
}
const del_res = await BunSQLite.delete<
BUN_SQLITE_WGUI_CLIENT_RULES,
TableType
>({
table: "client_rules",
targetId: rule.id,
});
if (!del_res.success) {
return {
success: false,
msg: del_res.msg || `Could not delete client rule`,
};
}
}
const to_insert = incoming.filter((rule) => !rule.id);
const to_update = incoming.filter(
(rule) => rule.id && typeof rule.id == "number",
);
for (let i = 0; i < to_update.length; i++) {
const rule = to_update[i];
if (!rule?.id) {
continue;
}
const update_res = await BunSQLite.update<
BUN_SQLITE_WGUI_CLIENT_RULES,
TableType
>({
table: "client_rules",
data: rule,
targetId: rule.id,
});
if (!update_res.success) {
return {
success: false,
msg: update_res.msg || `Could not update client rule`,
};
}
}
if (to_insert[0]) {
const insert_res = await BunSQLite.insert<
BUN_SQLITE_WGUI_CLIENT_RULES,
TableType
>({
table: "client_rules",
data: to_insert,
});
if (!insert_res.success) {
return {
success: false,
msg: insert_res.msg || `Could not insert client rules`,
};
}
}
return {
success: true,
};
}