feat(trash): add recently-deleted banner and empty trash option for admin

- Added emptyTrash method in AssetService to purge trashed assets from DB and S3

- Added POST /projects/:projectId/empty-trash endpoint in project API

- Implemented unit and integration tests for service and API

- Prevented double click redirect in recently deleted folder

- Added top banner with Delete Now option in recently-deleted folder with confirmation dialog
This commit is contained in:
yiling
2026-06-11 16:19:35 +08:00
parent 4d0972391c
commit f093b490c6
6 changed files with 247 additions and 4 deletions
+114
View File
@@ -1883,4 +1883,118 @@ describe('AssetService — natural sort by name', () => {
expect(result).toHaveLength(1)
})
})
describe('emptyTrash', () => {
it('should permanently delete trashed assets of the target project only', async () => {
const { s3Service } = await import('../s3/s3')
const team = await prisma.team.create({ data: { name: 'Empty Trash Team' } })
const project1 = await prisma.project.create({
data: { name: 'Project 1', teamId: team.id },
})
const project2 = await prisma.project.create({
data: { name: 'Project 2', teamId: team.id },
})
const user = await prisma.user.create({
data: { name: 'Test User', email: `test-${Date.now()}@example.com` },
})
// 1. Create a trashed folder with a child file in Project 1
const folderA = await prisma.asset.create({
data: {
name: 'Folder A',
type: AssetType.folder,
status: 'trashed',
isDeleted: true,
project: { connect: { id: project1.id } },
creator: { connect: { id: user.id } },
deletedAt: new Date(),
},
})
const fileA1 = await prisma.asset.create({
data: {
name: 'File A1',
type: AssetType.file,
status: 'uploaded',
isDeleted: true,
project: { connect: { id: project1.id } },
parent: { connect: { id: folderA.id } },
creator: { connect: { id: user.id } },
storageKey: {
create: {
key: 'files/project1-trashed/raw',
createdAt: new Date(Date.now() - 25 * 60 * 60 * 1000),
},
},
},
})
// 2. Create an active file in Project 1
const fileActive = await prisma.asset.create({
data: {
name: 'File Active',
type: AssetType.file,
status: 'uploaded',
isDeleted: false,
project: { connect: { id: project1.id } },
creator: { connect: { id: user.id } },
storageKey: {
create: {
key: 'files/project1-active/raw',
createdAt: new Date(Date.now() - 25 * 60 * 60 * 1000),
},
},
},
})
// 3. Create a trashed file in Project 2
const fileOtherTrashed = await prisma.asset.create({
data: {
name: 'File Other Trashed',
type: AssetType.file,
status: 'trashed',
isDeleted: true,
project: { connect: { id: project2.id } },
creator: { connect: { id: user.id } },
deletedAt: new Date(),
storageKey: {
create: {
key: 'files/project2-trashed/raw',
createdAt: new Date(Date.now() - 25 * 60 * 60 * 1000),
},
},
},
})
// Act
await assetService.emptyTrash(project1.id)
// Assert: Project 1 trashed assets are deleted from DB
expect(await prisma.asset.findUnique({ where: { id: folderA.id } })).toBeNull()
expect(await prisma.asset.findUnique({ where: { id: fileA1.id } })).toBeNull()
// Assert: Project 1 active assets are NOT deleted
expect(await prisma.asset.findUnique({ where: { id: fileActive.id } })).not.toBeNull()
// Assert: Project 2 trashed assets are NOT deleted
expect(await prisma.asset.findUnique({ where: { id: fileOtherTrashed.id } })).not.toBeNull()
// Assert: S3 deletion was called for Project 1's trashed file
expect(s3Service.deletePrefix).toHaveBeenCalledWith(
expect.any(String),
'files/project1-trashed/',
)
// Assert: S3 deletion was NOT called for Project 1's active file or Project 2's trashed file
expect(s3Service.deletePrefix).not.toHaveBeenCalledWith(
expect.any(String),
'files/project1-active/',
)
expect(s3Service.deletePrefix).not.toHaveBeenCalledWith(
expect.any(String),
'files/project2-trashed/',
)
})
})
})
+30
View File
@@ -1033,6 +1033,36 @@ export class AssetService {
}
}
async emptyTrash(projectId: string): Promise<void> {
const trashedRoots = await this.prismaClient.asset.findMany({
where: {
projectId,
status: 'trashed',
isDeleted: true,
},
select: { id: true },
})
for (const root of trashedRoots) {
await this.prismaClient.$transaction(async (tx) => {
// Use recursive CTE to find all descendants and mark them all as pending_purge
await tx.$executeRaw`
WITH RECURSIVE descendant AS (
SELECT id FROM assets WHERE id = ${root.id}
UNION ALL
SELECT a.id FROM assets a
INNER JOIN descendant d ON a.parent_id = d.id
)
UPDATE assets SET status = 'pending_purge', updated_at = NOW()
WHERE id IN (SELECT id FROM descendant);
`
})
}
await this.purgePendingAssets()
await this.purgeUnreferencedStorageKeys()
}
private cleanupJobsRunning = false
async startCleanupJob() {