wip: directory/file move name conflict check
This commit is contained in:
@@ -171,6 +171,43 @@ export async function move(
|
|||||||
sourceDirectories: DirectoryHandle[]
|
sourceDirectories: DirectoryHandle[]
|
||||||
},
|
},
|
||||||
): Promise<void> {
|
): 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(
|
await Promise.all(
|
||||||
sourceDirectories.map((directory) =>
|
sourceDirectories.map((directory) =>
|
||||||
ctx.db.patch(directory.id, { parentId: targetDirectory.id }),
|
ctx.db.patch(directory.id, { parentId: targetDirectory.id }),
|
||||||
|
@@ -1,6 +1,7 @@
|
|||||||
import { ConvexError } from "convex/values"
|
import { ConvexError } from "convex/values"
|
||||||
|
|
||||||
export enum Code {
|
export enum Code {
|
||||||
|
Conflict = "Conflict",
|
||||||
DirectoryExists = "DirectoryExists",
|
DirectoryExists = "DirectoryExists",
|
||||||
DirectoryNotFound = "DirectoryNotFound",
|
DirectoryNotFound = "DirectoryNotFound",
|
||||||
FileExists = "FileExists",
|
FileExists = "FileExists",
|
||||||
@@ -8,13 +9,13 @@ export enum Code {
|
|||||||
Unauthenticated = "Unauthenticated",
|
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 {
|
export function isApplicationError(error: unknown): error is ApplicationError {
|
||||||
return error instanceof ConvexError && "code" in error.data
|
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({
|
return new ConvexError({
|
||||||
code,
|
code,
|
||||||
message:
|
message:
|
||||||
|
Reference in New Issue
Block a user