Files
cobble/src/functions/backend/db/workspaces/delete-workspace.ts
T
2026-10-21 17:56:09 +01:00

58 lines
1.7 KiB
TypeScript

import deleteMedia from "@/src/functions/backend/db/media/delete-media";
import purgeStorage, { grabStorageMediaIds } from "@/src/functions/backend/db/bot-files/purge-storage";
import BunSQLite from "@moduletrace/bun-sqlite";
import AppError from "@/src/utils/app-error";
import grabUserWorkspace from "./grab-user-workspace";
import assertCrewManager from "@/src/functions/backend/db/access/assert-crew-manager";
type Params = {
user_id: number | string;
workspace_id: number | string;
};
/**
* Deletes one of the user's workspaces with its conversations, its
* links and its models. Its bots and computers stay.
*/
export default async function deleteWorkspace({ user_id, workspace_id }: Params) {
await assertCrewManager({ user_id });
const workspace = await grabUserWorkspace({ workspace_id, user_id });
if (!workspace?.id) {
throw new AppError(`Workspace not found.`);
}
/**
* Read before the delete: the shared files' rows go with the workspace.
*/
const file_media_ids = await grabStorageMediaIds({
scope: "workspace",
owner_id: workspace.id!,
}).catch(() => []);
const res = await BunSQLite.sql({
sql: `DELETE FROM workspaces WHERE id = ?`,
values: [workspace.id],
});
if (!res.success) {
throw new Error(`Couldn't delete the workspace: ${res.error}`);
}
if (workspace.image_media_id) {
await deleteMedia({ id: workspace.image_media_id, user_id: Number(user_id) }).catch(() => {});
}
/**
* The bots' shared folder there goes with it.
*/
await purgeStorage({
scope: "workspace",
owner_id: workspace.id!,
media_ids: file_media_ids,
});
return workspace;
}