mirror of
https://github.com/shumaiOne/shumai.git
synced 2026-11-26 10:09:21 +00:00
refactor(api): extract db query from download-links endpoint to service method
This commit is contained in:
@@ -455,22 +455,12 @@ describe('file api', () => {
|
||||
})
|
||||
|
||||
it('POST /files/download-links generates links if assets belong to the same project', async () => {
|
||||
const mockFindMany = vi.spyOn(prisma.asset, 'findMany').mockResolvedValue([
|
||||
{
|
||||
id: 'asset1',
|
||||
projectId: 'project-id-1',
|
||||
type: 'file',
|
||||
targetId: null,
|
||||
},
|
||||
{
|
||||
id: 'asset2',
|
||||
projectId: 'project-id-1',
|
||||
type: 'folder',
|
||||
targetId: null,
|
||||
},
|
||||
// Mocking only the required subset of Prisma Asset properties for the test
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
] as any)
|
||||
const mockGetProjectIdsAndStartingIds = vi
|
||||
.spyOn(assetService, 'getProjectIdsAndStartingIds')
|
||||
.mockResolvedValue({
|
||||
projectIds: ['project-id-1'],
|
||||
startingIds: ['asset1', 'asset2'],
|
||||
})
|
||||
|
||||
const mockGetDownloadLinks = vi.spyOn(assetService, 'getDownloadLinks').mockResolvedValue([
|
||||
{ id: 'file1', name: 'file1.txt', url: 'http://link1' },
|
||||
@@ -498,27 +488,17 @@ describe('file api', () => {
|
||||
|
||||
expect(assetService.getDownloadLinks).toHaveBeenCalledWith(['asset1', 'asset2'])
|
||||
|
||||
mockFindMany.mockRestore()
|
||||
mockGetProjectIdsAndStartingIds.mockRestore()
|
||||
mockGetDownloadLinks.mockRestore()
|
||||
})
|
||||
|
||||
it('POST /files/download-links rejects if assets belong to different projects', async () => {
|
||||
const mockFindMany = vi.spyOn(prisma.asset, 'findMany').mockResolvedValue([
|
||||
{
|
||||
id: 'asset1',
|
||||
projectId: 'project-id-1',
|
||||
type: 'file',
|
||||
targetId: null,
|
||||
},
|
||||
{
|
||||
id: 'asset2',
|
||||
projectId: 'project-id-2',
|
||||
type: 'file',
|
||||
targetId: null,
|
||||
},
|
||||
// Mocking only the required subset of Prisma Asset properties for the test
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
] as any)
|
||||
const mockGetProjectIdsAndStartingIds = vi
|
||||
.spyOn(assetService, 'getProjectIdsAndStartingIds')
|
||||
.mockResolvedValue({
|
||||
projectIds: ['project-id-1', 'project-id-2'],
|
||||
startingIds: ['asset1', 'asset2'],
|
||||
})
|
||||
|
||||
const app = new Hono().use('*', authMiddleware).route('/', fileRoute)
|
||||
const res = await app.request('/files/download-links', {
|
||||
@@ -531,6 +511,6 @@ describe('file api', () => {
|
||||
const json = await res.json()
|
||||
expect(json.error).toBe('All selected items must belong to the same project')
|
||||
|
||||
mockFindMany.mockRestore()
|
||||
mockGetProjectIdsAndStartingIds.mockRestore()
|
||||
})
|
||||
})
|
||||
|
||||
@@ -273,39 +273,19 @@ const route = new Hono<{ Variables: { user: User } }>()
|
||||
return c.json({ files: [] })
|
||||
}
|
||||
|
||||
// 1. Fetch input assets to verify project grouping
|
||||
const assets = await prisma.asset.findMany({
|
||||
where: { id: { in: req.ids }, isDeleted: false },
|
||||
select: {
|
||||
id: true,
|
||||
projectId: true,
|
||||
type: true,
|
||||
targetId: true,
|
||||
target: { select: { projectId: true } },
|
||||
},
|
||||
})
|
||||
// 1. Fetch project IDs and starting IDs from the service layer
|
||||
const { projectIds, startingIds } = await assetService.getProjectIdsAndStartingIds(req.ids)
|
||||
|
||||
if (assets.length === 0) {
|
||||
if (startingIds.length === 0) {
|
||||
return c.json({ files: [] })
|
||||
}
|
||||
|
||||
const projectIds = new Set<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)
|
||||
}
|
||||
}
|
||||
|
||||
// 2. Validate same-project constraint
|
||||
if (projectIds.size !== 1) {
|
||||
if (projectIds.length !== 1) {
|
||||
return c.json({ error: 'All selected items must belong to the same project' }, 400)
|
||||
}
|
||||
|
||||
const projectId = Array.from(projectIds)[0]
|
||||
const projectId = projectIds[0]
|
||||
|
||||
// 3. Verify user has READ permission on this project
|
||||
await authzService.hasPermission({
|
||||
@@ -315,15 +295,7 @@ const route = new Hono<{ Variables: { user: User } }>()
|
||||
id: projectId,
|
||||
})
|
||||
|
||||
// 4. Resolve starting IDs (dereference top-level symlinks)
|
||||
const startingIds = assets.map((asset) => {
|
||||
if (asset.type === 'symlink' && asset.targetId) {
|
||||
return asset.targetId
|
||||
}
|
||||
return asset.id
|
||||
})
|
||||
|
||||
// 5. Generate download links
|
||||
// 4. Generate download links
|
||||
const files = await assetService.getDownloadLinks(startingIds)
|
||||
return c.json({ files })
|
||||
})
|
||||
|
||||
@@ -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