fix: root directory creation

Co-authored-by: Ona <no-reply@ona.com>
This commit is contained in:
2025-10-05 22:14:44 +00:00
parent 1fcdaf4f86
commit f7bc5fd958
14 changed files with 95 additions and 107 deletions

View File

@@ -2,6 +2,7 @@ import { v } from "convex/values"
import type { Doc, Id } from "../_generated/dataModel"
import type { AuthenticatedMutationCtx } from "../functions"
import * as Directories from "./directories"
import * as Err from "./error"
import * as Files from "./files"
export enum FileType {
@@ -81,6 +82,29 @@ export const VFileHandle = v.object({
})
export const VFileSystemHandle = v.union(VFileHandle, VDirectoryHandle)
export async function ensureRootDirectory(
ctx: AuthenticatedMutationCtx,
): Promise<Id<"directories">> {
const existing = await ctx.db
.query("directories")
.withIndex("byParentId", (q) =>
q.eq("userId", ctx.user._id).eq("parentId", undefined),
)
.first()
if (existing) {
return existing._id
}
const now = Date.now()
return await ctx.db.insert("directories", {
name: "",
createdAt: now,
updatedAt: now,
userId: ctx.user._id,
})
}
/**
* Recursively collects all file and directory handles from the given handles,
* including all nested items. Only includes items that are in trash (deletedAt >= 0).