mirror of
https://github.com/get-drexa/drive.git
synced 2025-11-30 21:41:39 +00:00
feat: impl file/dir restoration from trash
This commit is contained in:
@@ -148,3 +148,46 @@ export async function deletePermanently(
|
||||
|
||||
return { deleted: successfulDeletions, errors }
|
||||
}
|
||||
|
||||
export async function restore(
|
||||
ctx: AuthenticatedMutationCtx,
|
||||
{
|
||||
items,
|
||||
}: {
|
||||
items: FileHandle[]
|
||||
},
|
||||
) {
|
||||
if (items.length === 0) {
|
||||
return null
|
||||
}
|
||||
|
||||
const itemsToBeRestored = await Promise.allSettled(
|
||||
items.map((item) => ctx.db.get(item.id)),
|
||||
).then((results) =>
|
||||
results.filter(
|
||||
(result): result is PromiseFulfilledResult<Doc<"files">> =>
|
||||
result.status === "fulfilled" && result.value !== null,
|
||||
),
|
||||
)
|
||||
|
||||
const restoreFilePromises = itemsToBeRestored.map((item) =>
|
||||
ctx.db.patch(item.value._id, {
|
||||
deletedAt: undefined,
|
||||
updatedAt: Date.now(),
|
||||
}),
|
||||
)
|
||||
|
||||
const restoreResults = await Promise.allSettled(restoreFilePromises)
|
||||
|
||||
const errors: Err.ApplicationErrorData[] = []
|
||||
let successfulRestorations = 0
|
||||
for (const result of restoreResults) {
|
||||
if (result.status === "rejected") {
|
||||
errors.push(Err.createJson(Err.Code.Internal))
|
||||
} else {
|
||||
successfulRestorations += 1
|
||||
}
|
||||
}
|
||||
|
||||
return { restored: successfulRestorations, errors }
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user