mirror of
https://github.com/shumaiOne/shumai.git
synced 2026-12-02 14:04:50 +00:00
refactor(api): extract db query from download-links endpoint to service method
This commit is contained in:
@@ -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)
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
@@ -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 }>> {
|
||||
|
||||
Reference in New Issue
Block a user