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
+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> {