refactor(core): update getAssetContext to throw error on missing asset

This commit is contained in:
yiling
2026-08-01 11:21:56 +08:00
parent 0324b95c7e
commit 6d80f06c22
4 changed files with 117 additions and 100 deletions
+58 -70
View File
@@ -77,15 +77,13 @@ const route = new Hono<{ Variables: { user: User } }>()
})
const context = await assetService.getAssetContext(fileId)
if (context?.teamId) {
await auditLogService.logAction({
action: AuditAction.asset_update,
teamId: context.teamId,
userId: user.id,
projectId: context.projectId,
itemId: fileId,
})
}
await auditLogService.logAction({
action: AuditAction.asset_update,
teamId: context.teamId,
userId: user.id,
projectId: context.projectId,
itemId: fileId,
})
return c.json(updatedAsset)
})
@@ -108,15 +106,13 @@ const route = new Hono<{ Variables: { user: User } }>()
for (let i = 0; i < req.ids.length; i++) {
const ctx = contexts[i]
if (ctx?.teamId) {
await auditLogService.logAction({
action: AuditAction.asset_delete,
teamId: ctx.teamId,
userId: user.id,
projectId: ctx.projectId,
itemId: req.ids[i],
})
}
await auditLogService.logAction({
action: AuditAction.asset_delete,
teamId: ctx.teamId,
userId: user.id,
projectId: ctx.projectId,
itemId: req.ids[i],
})
}
return c.body(null, 204)
@@ -148,14 +144,12 @@ const route = new Hono<{ Variables: { user: User } }>()
const hasStatus = req.some((m) => m.key === 'status')
if (hasStatus) {
const context = await assetService.getAssetContext(fileId)
if (context?.teamId) {
notificationService.create({
type: 'metadata_field_updated_status',
teamId: context.teamId,
creatorId: user.id,
assetId: fileId,
})
}
notificationService.create({
type: 'metadata_field_updated_status',
teamId: context.teamId,
creatorId: user.id,
assetId: fileId,
})
}
return c.json('')
@@ -190,35 +184,33 @@ const route = new Hono<{ Variables: { user: User } }>()
const context = await assetService.getAssetContext(fileId)
if (context?.teamId) {
let targetUserId: string | undefined
if (req.replyToId) {
const parentComment = await prisma.assetComment.findUnique({
where: { id: req.replyToId },
})
if (parentComment?.creatorId) {
targetUserId = parentComment.creatorId
}
let targetUserId: string | undefined
if (req.replyToId) {
const parentComment = await prisma.assetComment.findUnique({
where: { id: req.replyToId },
})
if (parentComment?.creatorId) {
targetUserId = parentComment.creatorId
}
notificationService.create({
type: notifType,
teamId: context.teamId,
creatorId: user.id,
assetId: fileId,
userId: targetUserId,
commentMessage: req.message,
})
await auditLogService.logAction({
action: AuditAction.comment_create,
teamId: context.teamId,
userId: user.id,
projectId: context.projectId,
itemId: comment.id,
})
}
notificationService.create({
type: notifType,
teamId: context.teamId,
creatorId: user.id,
assetId: fileId,
userId: targetUserId,
commentMessage: req.message,
})
await auditLogService.logAction({
action: AuditAction.comment_create,
teamId: context.teamId,
userId: user.id,
projectId: context.projectId,
itemId: comment.id,
})
return c.json(comment, 201)
},
)
@@ -261,15 +253,13 @@ const route = new Hono<{ Variables: { user: User } }>()
if (commentData?.assetId) {
const context = await assetService.getAssetContext(commentData.assetId)
if (context?.teamId) {
await auditLogService.logAction({
action: AuditAction.comment_complete,
teamId: context.teamId,
userId: user.id,
projectId: context.projectId,
itemId: commentId,
})
}
await auditLogService.logAction({
action: AuditAction.comment_complete,
teamId: context.teamId,
userId: user.id,
projectId: context.projectId,
itemId: commentId,
})
}
return c.json(updated)
@@ -295,15 +285,13 @@ const route = new Hono<{ Variables: { user: User } }>()
if (commentData?.assetId) {
const context = await assetService.getAssetContext(commentData.assetId)
if (context?.teamId) {
await auditLogService.logAction({
action: AuditAction.comment_delete,
teamId: context.teamId,
userId: user.id,
projectId: context.projectId,
itemId: commentId,
})
}
await auditLogService.logAction({
action: AuditAction.comment_delete,
teamId: context.teamId,
userId: user.id,
projectId: context.projectId,
itemId: commentId,
})
}
return c.json({ success: true })
+21 -27
View File
@@ -36,15 +36,13 @@ const route = new Hono<{ Variables: { user: User } }>()
})
const parentCtx = await assetService.getAssetContext(req.parentId)
if (parentCtx?.teamId) {
await auditLogService.logAction({
action: AuditAction.asset_create,
teamId: parentCtx.teamId,
userId: user.id,
projectId: parentCtx.projectId,
itemId: newAsset.id,
})
}
await auditLogService.logAction({
action: AuditAction.asset_create,
teamId: parentCtx.teamId,
userId: user.id,
projectId: parentCtx.projectId,
itemId: newAsset.id,
})
return c.json(newAsset)
})
@@ -85,15 +83,13 @@ const route = new Hono<{ Variables: { user: User } }>()
})
const ctx = await assetService.getAssetContext(folderId)
if (ctx?.teamId) {
await auditLogService.logAction({
action: AuditAction.asset_update,
teamId: ctx.teamId,
userId: user.id,
projectId: ctx.projectId,
itemId: folderId,
})
}
await auditLogService.logAction({
action: AuditAction.asset_update,
teamId: ctx.teamId,
userId: user.id,
projectId: ctx.projectId,
itemId: folderId,
})
return c.json(updatedAsset)
})
@@ -152,15 +148,13 @@ const route = new Hono<{ Variables: { user: User } }>()
for (let i = 0; i < req.ids.length; i++) {
const ctx = contexts[i]
if (ctx?.teamId) {
await auditLogService.logAction({
action: AuditAction.asset_delete,
teamId: ctx.teamId,
userId: user.id,
projectId: ctx.projectId,
itemId: req.ids[i],
})
}
await auditLogService.logAction({
action: AuditAction.asset_delete,
teamId: ctx.teamId,
userId: user.id,
projectId: ctx.projectId,
itemId: req.ids[i],
})
}
return c.body(null, 204)
+33
View File
@@ -2721,4 +2721,37 @@ describe('AssetService — natural sort by name', () => {
expect(info.sizeByte).toBe(largeSize)
})
})
describe('getAssetContext', () => {
it('returns context for project asset', async () => {
const user = await prisma.user.create({
data: { name: 'User_Ctx_' + Date.now(), email: `test-ctx-${Date.now()}@example.com` },
})
const team = await prisma.team.create({
data: { name: 'Team_Ctx_' + Date.now() },
})
const project = await prisma.project.create({
data: { name: 'Project_Ctx', teamId: team.id },
})
const asset = await prisma.asset.create({
data: {
name: 'Asset_Ctx',
type: AssetType.folder,
projectId: project.id,
creatorId: user.id,
status: AssetStatus.uploaded,
},
})
const context = await assetService.getAssetContext(asset.id)
expect(context.projectId).toBe(project.id)
expect(context.teamId).toBe(team.id)
})
it('throws error when asset is not found', async () => {
await expect(assetService.getAssetContext('non-existent-id')).rejects.toThrow(
'Asset not found: non-existent-id',
)
})
})
})
+5 -3
View File
@@ -864,7 +864,7 @@ export class AssetService {
return info
}
async getAssetContext(assetId: string): Promise<{ teamId: string; projectId?: string } | null> {
async getAssetContext(assetId: string): Promise<{ teamId: string; projectId?: string }> {
const asset = await this.prismaClient.asset.findUnique({
where: { id: assetId },
select: {
@@ -874,7 +874,9 @@ export class AssetService {
},
})
if (!asset) return null
if (!asset) {
throw new Error(`Asset not found: ${assetId}`)
}
if (asset.project) {
return { teamId: asset.project.teamId, projectId: asset.projectId ?? undefined }
@@ -884,7 +886,7 @@ export class AssetService {
return { teamId: asset.teamRootFolder.id }
}
return null
throw new Error(`Asset has no team context: ${assetId}`)
}
async resolveTargetAssetId(assetId: string): Promise<string> {