Complete client form
This commit is contained in:
@@ -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,
|
||||
};
|
||||
}
|
||||
Reference in New Issue
Block a user