mirror of
https://github.com/shumaiOne/shumai.git
synced 2026-12-04 23:24:21 +00:00
refactor(core): update getAssetContext to throw error on missing asset
This commit is contained in:
@@ -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',
|
||||
)
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
@@ -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> {
|
||||
|
||||
Reference in New Issue
Block a user