feat(trash): implement two-stage trashcan and project cleanup

This commit is contained in:
yiling
2026-05-23 23:26:43 +08:00
parent f05247f372
commit effec44870
15 changed files with 272 additions and 171 deletions
+5 -3
View File
@@ -261,7 +261,7 @@ describe('ProjectService', () => {
// Verify asset is soft-deleted and detached
const a = await prisma.asset.findUnique({ where: { id: asset.id } })
expect(a).toBeDefined()
expect(a?.removed).toBe(true)
expect(a?.isDeleted).toBe(true)
expect(a?.projectId).toBeNull()
expect(a?.parentId).toBeNull()
@@ -333,9 +333,11 @@ describe('ProjectService', () => {
// Delete project
await projectService.deleteProject(project.id)
// Manually trigger the private cleanup method for testing
// Manually trigger the private cleanup methods for testing
// eslint-disable-next-line @typescript-eslint/no-explicit-any
await (assetService as any).cleanupOldAssets()
await (assetService as any).expireTrashedAssets()
// eslint-disable-next-line @typescript-eslint/no-explicit-any
await (assetService as any).purgePendingAssets()
// Verify everything is wiped from DB
expect(await prisma.project.findUnique({ where: { id: project.id } })).toBeNull()
+27 -17
View File
@@ -233,28 +233,38 @@ export class ProjectService {
const immediateCleanupDate = new Date(0)
await prisma.$transaction(async (tx) => {
// 1. Unlink root folders from project to avoid cycle during deletion
// 1. Mark ALL project assets as isDeleted and detach projectId
// We keep parentId intact so the folder structure remains for Stage 1 cleanup.
await tx.asset.updateMany({
where: { projectId },
data: {
isDeleted: true,
projectId: null,
},
})
// 2. Mark root folders as 'trashed' with deletedAt = 0 to trigger immediate background purge
const rootIds = [project.rootFolderId, project.shareRootId].filter(
(id): id is string => id !== null,
)
if (rootIds.length > 0) {
await tx.asset.updateMany({
where: { id: { in: rootIds } },
data: {
status: 'trashed',
deletedAt: immediateCleanupDate,
},
})
}
// 3. Unlink root folders from project record to avoid FK cycles during project deletion
await tx.project.update({
where: { id: projectId },
data: { rootFolderId: null, shareRootId: null },
})
// 2. Detach ALL assets (files and folders) from project and soft-delete them
// This queues them for the background cleanup job.
await tx.asset.updateMany({
where: { projectId },
data: {
projectId: null,
parentId: null,
targetId: null,
removed: true,
deletedAt: immediateCleanupDate,
status: 'trashed',
},
})
// 3. Delete the project itself
// Thanks to cascade deletes, project members, invites, etc. will be cleaned up.
// 4. Delete the project itself
// Cascade deletes will handle project members, invites, notifications, etc.
await tx.project.delete({
where: { id: projectId },
})