mirror of
https://github.com/shumaiOne/shumai.git
synced 2026-11-23 18:07:13 +00:00
fix(core): correctly query nextAsset for afterIndex reordering
This commit is contained in:
@@ -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()
|
||||
|
||||
@@ -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)
|
||||
|
||||
Reference in New Issue
Block a user