mirror of
https://github.com/shumaiOne/shumai.git
synced 2026-11-23 00:29:50 +00:00
refactor(comments): simplify teamId lookup and clean up comment attachments in S3 and database
This commit is contained in:
@@ -809,6 +809,71 @@ describe('AssetService', () => {
|
||||
).resolves.not.toThrow()
|
||||
})
|
||||
|
||||
it('deleteComment cleans up S3 attachment files and database records', async () => {
|
||||
const { user, project, assets } = await setupBasicAssets()
|
||||
|
||||
// 1. Add user as team owner
|
||||
await prisma.teamMember.create({
|
||||
data: {
|
||||
teamId: project.teamId,
|
||||
userId: user.id,
|
||||
role: 'owner',
|
||||
},
|
||||
})
|
||||
|
||||
// 2. Create StorageKey and Asset for attachment
|
||||
const storageKey = await prisma.storageKey.create({
|
||||
data: {
|
||||
key: 'attachments/test-attachment-file.png',
|
||||
status: 'active',
|
||||
},
|
||||
})
|
||||
|
||||
const attachmentAsset = await prisma.asset.create({
|
||||
data: {
|
||||
name: 'test-attachment-file.png',
|
||||
type: AssetType.file,
|
||||
projectId: project.id,
|
||||
parentId: assets.root.id,
|
||||
creatorId: user.id,
|
||||
storageKeyId: storageKey.id,
|
||||
sizeByte: 100,
|
||||
status: 'uploaded',
|
||||
},
|
||||
})
|
||||
|
||||
// 3. Create a comment with this attachment
|
||||
const comment = await assetService.createComment({
|
||||
assetId: assets.fileA1.id,
|
||||
userId: user.id,
|
||||
message: 'Comment with attachment',
|
||||
attachmentIds: [attachmentAsset.id],
|
||||
})
|
||||
|
||||
// Verify it is created in DB
|
||||
const dbComment = await prisma.assetComment.findUnique({
|
||||
where: { id: comment.id },
|
||||
include: { attachments: true },
|
||||
})
|
||||
expect(dbComment?.attachments.length).toBe(1)
|
||||
|
||||
// 4. Delete the comment
|
||||
await expect(
|
||||
assetService.deleteComment({ commentId: comment.id, userId: user.id }),
|
||||
).resolves.not.toThrow()
|
||||
|
||||
// 5. Verify the comment is deleted from DB
|
||||
await expect(prisma.assetComment.findUnique({ where: { id: comment.id } })).resolves.toBeNull()
|
||||
|
||||
// 6. Verify that the attachment Asset and StorageKey are deleted from DB
|
||||
await expect(prisma.asset.findUnique({ where: { id: attachmentAsset.id } })).resolves.toBeNull()
|
||||
await expect(prisma.storageKey.findUnique({ where: { id: storageKey.id } })).resolves.toBeNull()
|
||||
|
||||
// 7. Verify S3 deletion was called
|
||||
const { s3Service } = await import('@shumai/core/src/s3/s3')
|
||||
expect(s3Service.deleteObject).toHaveBeenCalledWith('shumai', 'attachments/test-attachment-file.png')
|
||||
})
|
||||
|
||||
it('can create and retrieve comments with video timestamp', async () => {
|
||||
const { user, assets } = await setupBasicAssets()
|
||||
|
||||
|
||||
@@ -1415,19 +1415,20 @@ export class AssetService {
|
||||
}
|
||||
|
||||
async deleteComment({ commentId, userId }: { commentId: string; userId: string }): Promise<void> {
|
||||
interface AssetContext {
|
||||
parentId: string | null
|
||||
teamRootFolder: { id: string } | null
|
||||
project: { id: string; teamId: string } | null
|
||||
}
|
||||
|
||||
const comment = await this.prismaClient.assetComment.findUnique({
|
||||
where: { id: commentId },
|
||||
include: {
|
||||
attachments: {
|
||||
include: {
|
||||
asset: {
|
||||
include: {
|
||||
storageKey: true,
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
asset: {
|
||||
select: {
|
||||
parentId: true,
|
||||
teamRootFolder: { select: { id: true } },
|
||||
project: { select: { id: true, teamId: true } },
|
||||
},
|
||||
},
|
||||
@@ -1438,40 +1439,10 @@ export class AssetService {
|
||||
throw new HTTPException(404, { message: 'Comment not found' })
|
||||
}
|
||||
|
||||
let teamId: string | null = null
|
||||
if (comment.asset.project) {
|
||||
teamId = comment.asset.project.teamId
|
||||
} else if (comment.asset.teamRootFolder) {
|
||||
teamId = comment.asset.teamRootFolder.id
|
||||
} else {
|
||||
let currentAsset: AssetContext | null = comment.asset
|
||||
while (currentAsset && !teamId) {
|
||||
if (currentAsset.project) {
|
||||
teamId = currentAsset.project.teamId
|
||||
break
|
||||
}
|
||||
if (currentAsset.teamRootFolder) {
|
||||
teamId = currentAsset.teamRootFolder.id
|
||||
break
|
||||
}
|
||||
if (currentAsset.parentId) {
|
||||
const parent: AssetContext | null = await this.prismaClient.asset.findUnique({
|
||||
where: { id: currentAsset.parentId },
|
||||
select: {
|
||||
parentId: true,
|
||||
teamRootFolder: { select: { id: true } },
|
||||
project: { select: { id: true, teamId: true } },
|
||||
},
|
||||
})
|
||||
currentAsset = parent
|
||||
} else {
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
const teamId = comment.asset.project?.teamId
|
||||
|
||||
if (!teamId) {
|
||||
throw new HTTPException(403, { message: 'Could not resolve team context for comment' })
|
||||
throw new HTTPException(400, { message: 'Data corruption: Comment asset is not associated with a project' })
|
||||
}
|
||||
|
||||
const member = await this.prismaClient.teamMember.findUnique({
|
||||
@@ -1494,6 +1465,38 @@ export class AssetService {
|
||||
throw new HTTPException(403, { message: 'You do not have permission to delete this comment' })
|
||||
}
|
||||
|
||||
// Clean up attachment files from S3/storage and delete their DB records
|
||||
const bucket = process.env.S3_BUCKET || 'shumai'
|
||||
for (const att of comment.attachments) {
|
||||
const asset = att.asset
|
||||
if (asset.storageKey) {
|
||||
try {
|
||||
const key = asset.storageKey.key
|
||||
const parts = key.split('/')
|
||||
if (parts.length > 2) {
|
||||
const prefix = parts.slice(0, parts.length - 1).join('/') + '/'
|
||||
await s3Service.deletePrefix(bucket, prefix)
|
||||
} else {
|
||||
await s3Service.deleteObject(bucket, key)
|
||||
}
|
||||
} catch (err) {
|
||||
logger.error({ err, assetId: asset.id }, 'Failed to delete attachment file from S3')
|
||||
}
|
||||
}
|
||||
|
||||
await this.prismaClient.asset.delete({
|
||||
where: { id: asset.id },
|
||||
})
|
||||
|
||||
if (asset.storageKey) {
|
||||
await this.prismaClient.storageKey.delete({
|
||||
where: { id: asset.storageKey.id },
|
||||
}).catch(() => {
|
||||
// Silently ignore if already deleted
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
await this.prismaClient.assetComment.delete({
|
||||
where: { id: commentId },
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user