wip: directory/file move name conflict check
This commit is contained in:
@@ -171,6 +171,43 @@ export async function move(
|
||||
sourceDirectories: DirectoryHandle[]
|
||||
},
|
||||
): Promise<void> {
|
||||
const conflictCheckResults = await Promise.allSettled(
|
||||
sourceDirectories.map((directory) =>
|
||||
ctx.db.get(directory.id).then((d) => {
|
||||
if (!d) {
|
||||
throw Err.create(
|
||||
Err.Code.DirectoryNotFound,
|
||||
`Directory ${directory.id} not found`,
|
||||
)
|
||||
}
|
||||
return ctx.db
|
||||
.query("directories")
|
||||
.withIndex("uniqueDirectoryInDirectory", (q) =>
|
||||
q
|
||||
.eq("userId", ctx.user._id)
|
||||
.eq("parentId", targetDirectory.id)
|
||||
.eq("name", d.name)
|
||||
.eq("deletedAt", undefined),
|
||||
)
|
||||
.first()
|
||||
}),
|
||||
),
|
||||
)
|
||||
|
||||
const errors: Err.ApplicationError[] = []
|
||||
for (const result of conflictCheckResults) {
|
||||
if (result.status === "fulfilled" && result.value) {
|
||||
errors.push(
|
||||
Err.create(
|
||||
Err.Code.Conflict,
|
||||
`Directory ${targetDirectory.id} already contains a directory with name ${result.value.name}`,
|
||||
),
|
||||
)
|
||||
} else if (result.status === "rejected") {
|
||||
errors.push(Err.create(Err.Code.Internal))
|
||||
}
|
||||
}
|
||||
|
||||
await Promise.all(
|
||||
sourceDirectories.map((directory) =>
|
||||
ctx.db.patch(directory.id, { parentId: targetDirectory.id }),
|
||||
|
@@ -1,6 +1,7 @@
|
||||
import { ConvexError } from "convex/values"
|
||||
|
||||
export enum Code {
|
||||
Conflict = "Conflict",
|
||||
DirectoryExists = "DirectoryExists",
|
||||
DirectoryNotFound = "DirectoryNotFound",
|
||||
FileExists = "FileExists",
|
||||
@@ -8,13 +9,13 @@ export enum Code {
|
||||
Unauthenticated = "Unauthenticated",
|
||||
}
|
||||
|
||||
export type ApplicationError = ConvexError<{ code: Code; message: string }>
|
||||
export type ApplicationError = ConvexError<{ code: Code; message?: string }>
|
||||
|
||||
export function isApplicationError(error: unknown): error is ApplicationError {
|
||||
return error instanceof ConvexError && "code" in error.data
|
||||
}
|
||||
|
||||
export function create(code: Code, message?: string) {
|
||||
export function create(code: Code, message?: string): ApplicationError {
|
||||
return new ConvexError({
|
||||
code,
|
||||
message:
|
||||
|
Reference in New Issue
Block a user