fix(core): correctly query nextAsset for afterIndex reordering

This commit is contained in:
yiling
2026-06-15 20:25:49 +08:00
parent e8e04edfee
commit 45318d1eec
2 changed files with 57 additions and 1 deletions
+48
View File
@@ -1232,6 +1232,54 @@ describe('AssetService', () => {
expect(updatedC.sortIndex! < assetB.sortIndex!).toBe(true)
})
it('correctly bounds the new sortIndex when moving an asset after another asset in the middle of the list', async () => {
const { user, project } = await setupBasicAssets()
// Create three assets: A, B, C with specific sort indexes
const assetA = await prisma.asset.create({
data: {
name: 'Asset A',
type: AssetType.file,
projectId: project.id,
creatorId: user.id,
sortIndex: 'a0',
status: 'uploaded',
},
})
const assetB = await prisma.asset.create({
data: {
name: 'Asset B',
type: AssetType.file,
projectId: project.id,
creatorId: user.id,
sortIndex: 'a1',
status: 'uploaded',
},
})
const assetC = await prisma.asset.create({
data: {
name: 'Asset C',
type: AssetType.file,
projectId: project.id,
creatorId: user.id,
sortIndex: 'a11',
status: 'uploaded',
},
})
// Move Asset A to be after Asset B (it should be placed between B ("a1") and C ("a11"))
const updatedA = await assetService.updateAssetOrder(assetA.id, {
afterIndex: assetB.sortIndex!,
})
// Verify it is placed after B
expect(updatedA.sortIndex! > assetB.sortIndex!).toBe(true)
// Verify it is placed before C (this fails under the buggy code because C's bound is ignored, generating a key greater than "a11")
expect(updatedA.sortIndex! < assetC.sortIndex!).toBe(true)
})
describe('Symlinks', () => {
it('toAssetInfo resolves symlink target metadata', async () => {
const { project, user } = await setupBasicAssets()
+9 -1
View File
@@ -973,7 +973,15 @@ export class AssetService {
afterSortIndex = prevAsset?.sortIndex || null
} else if (req.afterIndex) {
afterSortIndex = req.afterIndex
beforeSortIndex = null
const nextAsset = await this.prismaClient.asset.findFirst({
where: {
parentId: asset.parentId,
sortIndex: { gt: req.afterIndex },
id: { not: id },
},
orderBy: { sortIndex: 'asc' },
})
beforeSortIndex = nextAsset?.sortIndex || null
}
const newSortIndex = generateKeyBetween(afterSortIndex, beforeSortIndex)