refactor(api): extract db query from download-links endpoint to service method

This commit is contained in:
yiling
2026-06-11 13:06:01 +08:00
parent f30d68c0a2
commit aed0676032
4 changed files with 111 additions and 68 deletions
+47
View File
@@ -1804,4 +1804,51 @@ describe('AssetService — natural sort by name', () => {
}
})
})
describe('getProjectIdsAndStartingIds', () => {
it('returns empty lists for empty inputs', async () => {
const result = await assetService.getProjectIdsAndStartingIds([])
expect(result.projectIds).toEqual([])
expect(result.startingIds).toEqual([])
})
it('resolves project IDs and starting IDs, including symlinks', async () => {
const team = await prisma.team.create({ data: { name: 'Test Team' } })
const project = await prisma.project.create({
data: { name: 'Test Project', teamId: team.id },
})
const user = await prisma.user.create({
data: { name: 'Test User', email: `test-${Date.now()}@example.com` },
})
const file1 = await prisma.asset.create({
data: {
name: 'file1.txt',
type: AssetType.file,
project: { connect: { id: project.id } },
creator: { connect: { id: user.id } },
status: AssetStatus.uploaded,
},
})
const symlink = await prisma.asset.create({
data: {
name: 'symlink',
type: AssetType.symlink,
project: { connect: { id: project.id } },
creator: { connect: { id: user.id } },
status: AssetStatus.uploaded,
target: { connect: { id: file1.id } },
},
})
const result = await assetService.getProjectIdsAndStartingIds([file1.id, symlink.id])
expect(result.projectIds).toContain(project.id)
expect(result.projectIds).toHaveLength(1)
expect(result.startingIds).toHaveLength(2)
// Since it's a symlink, its starting ID should be resolved to the target file1.id
expect(result.startingIds[0]).toBe(file1.id)
expect(result.startingIds[1]).toBe(file1.id)
})
})
})
+44
View File
@@ -1649,6 +1649,50 @@ export class AssetService {
)
}
async getProjectIdsAndStartingIds(assetIds: string[]): Promise<{
projectIds: string[]
startingIds: string[]
}> {
if (assetIds.length === 0) {
return { projectIds: [], startingIds: [] }
}
const assets = await this.prismaClient.asset.findMany({
where: { id: { in: assetIds }, isDeleted: false },
select: {
id: true,
projectId: true,
type: true,
targetId: true,
target: { select: { projectId: true } },
},
})
const projectIds = new Set<string>()
const startingIds: string[] = []
for (const asset of assets) {
let projId = asset.projectId
if (asset.type === 'symlink' && asset.target?.projectId) {
projId = asset.target.projectId
}
if (projId) {
projectIds.add(projId)
}
if (asset.type === 'symlink' && asset.targetId) {
startingIds.push(asset.targetId)
} else {
startingIds.push(asset.id)
}
}
return {
projectIds: Array.from(projectIds),
startingIds,
}
}
async getDownloadLinks(
startingIds: string[],
): Promise<Array<{ id: string; name: string; url: string }>> {