fix: duplicate with orderable (#12274)

Previously, duplication with orderable collections worked incorrectly,
for example

Document 1 is created - `_order: 'a5'`
Document 2 is duplicated from 1, - `_order: 'a5 - copy'` (result from
47a1eee765/packages/payload/src/fields/setDefaultBeforeDuplicate.ts (L6))

Now, the `_order` value is re-calculated properly.
This commit is contained in:
Sasha
2025-04-30 20:28:13 +03:00
committed by GitHub
parent 4a56597b92
commit 710fe0949b
2 changed files with 45 additions and 0 deletions

View File

@@ -83,6 +83,13 @@ export const addOrderableFieldsAndHook = (
hidden: true,
readOnly: true,
},
hooks: {
beforeDuplicate: [
({ siblingData }) => {
delete siblingData[orderableFieldName]
},
],
},
index: true,
required: true,
// override the schema to make order fields optional for payload.create()
@@ -275,5 +282,6 @@ export const addOrderableEndpoint = (config: SanitizedConfig) => {
if (!config.endpoints) {
config.endpoints = []
}
config.endpoints.push(reorderEndpoint)
}

View File

@@ -444,6 +444,43 @@ describe('Sort', () => {
parseInt(ordered.docs[1]._order, 16),
)
})
it('should allow to duplicate with reordable', async () => {
const doc = await payload.create({
collection: 'orderable',
data: { title: 'new document' },
})
const docDuplicated = await payload.create({
duplicateFromID: doc.id,
collection: 'orderable',
data: {},
})
expect(docDuplicated.title).toBe('new document')
expect(parseInt(doc._order!, 16)).toBeLessThan(parseInt(docDuplicated._order!, 16))
await restClient.POST('/reorder', {
body: JSON.stringify({
collectionSlug: orderableSlug,
docsToMove: [doc.id],
newKeyWillBe: 'greater',
orderableFieldName: '_order',
target: {
id: docDuplicated.id,
key: docDuplicated._order,
},
}),
})
const docAfterReorder = await payload.findByID({ collection: 'orderable', id: doc.id })
const docDuplicatedAfterReorder = await payload.findByID({
collection: 'orderable',
id: docDuplicated.id,
})
expect(parseInt(docAfterReorder._order!, 16)).toBeGreaterThan(
parseInt(docDuplicatedAfterReorder._order!, 16),
)
})
})
describe('Orderable join', () => {