From 6d80f06c22401826d5a9386c53f45120fcef5650 Mon Sep 17 00:00:00 2001 From: yiling Date: Sat, 1 Aug 2026 11:21:56 +0800 Subject: [PATCH] refactor(core): update getAssetContext to throw error on missing asset --- packages/api/src/file.ts | 128 ++++++++++++-------------- packages/api/src/folder.ts | 48 +++++----- packages/core/src/asset/asset.test.ts | 33 +++++++ packages/core/src/asset/asset.ts | 8 +- 4 files changed, 117 insertions(+), 100 deletions(-) diff --git a/packages/api/src/file.ts b/packages/api/src/file.ts index 3a50e67f..20a8d74d 100644 --- a/packages/api/src/file.ts +++ b/packages/api/src/file.ts @@ -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 }) diff --git a/packages/api/src/folder.ts b/packages/api/src/folder.ts index fbcd086a..b2191d26 100644 --- a/packages/api/src/folder.ts +++ b/packages/api/src/folder.ts @@ -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) diff --git a/packages/core/src/asset/asset.test.ts b/packages/core/src/asset/asset.test.ts index dcd44975..b7714f3f 100644 --- a/packages/core/src/asset/asset.test.ts +++ b/packages/core/src/asset/asset.test.ts @@ -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', + ) + }) + }) }) diff --git a/packages/core/src/asset/asset.ts b/packages/core/src/asset/asset.ts index 20e5f052..a4fb684d 100644 --- a/packages/core/src/asset/asset.ts +++ b/packages/core/src/asset/asset.ts @@ -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 {