Skip to content

Commit a670438

Browse files
authored
fix: restore as draft function was passing a published status (#13599)
### What When using the `Restore as draft` action from the version view, the restored document incorrectly appears as `published` in the API. This issue was reported by a client. ### Why In the `restoreVersion` operation, the document is updated via `db.updateOne` (line 265). The update passes along the status stored in the version being restored but does not respect the `draft` query parameter. ### How Ensures that the result status is explicitly set to `draft` when the `draft` argument is `true`.
1 parent ded8ec4 commit a670438

File tree

2 files changed

+34
-0
lines changed

2 files changed

+34
-0
lines changed

packages/payload/src/collections/operations/restoreVersion.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -260,6 +260,8 @@ export const restoreVersionOperation = async <TData extends TypeWithID = any>(
260260

261261
// Ensure updatedAt date is always updated
262262
result.updatedAt = new Date().toISOString()
263+
// Ensure status respects restoreAsDraft arg
264+
result._status = draftArg ? 'draft' : result._status
263265
result = await req.payload.db.updateOne({
264266
id: parentDocID,
265267
collection: collectionConfig.slug,

test/versions/e2e.spec.ts

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -243,6 +243,38 @@ describe('Versions', () => {
243243
await expect(page.locator('#field-title')).toHaveValue('v1')
244244
})
245245

246+
test('should restore version as draft', async () => {
247+
await page.goto(url.create)
248+
await page.locator('#field-title').fill('v1')
249+
await saveDocAndAssert(page, '#action-save-draft')
250+
await page.locator('#field-title').fill('v2')
251+
await page.locator('#field-description').fill('restore me as draft')
252+
await saveDocAndAssert(page)
253+
await page.locator('#field-title').fill('v3')
254+
await page.locator('#field-description').fill('published')
255+
await saveDocAndAssert(page)
256+
257+
const savedDocURL = page.url()
258+
await page.goto(`${savedDocURL}/versions`)
259+
const row2 = page.locator('tbody .row-2')
260+
const versionID = await row2.locator('.cell-id').textContent()
261+
await page.goto(`${savedDocURL}/versions/${versionID}`)
262+
await expect(page.locator('.render-field-diffs')).toBeVisible()
263+
await page.locator('.restore-version .popup__trigger-wrap button').click()
264+
await page.getByRole('button', { name: 'Restore as draft' }).click()
265+
await page.locator('button:has-text("Confirm")').click()
266+
await page.waitForURL(savedDocURL)
267+
268+
await expect(page.locator('#field-title')).toHaveValue('v2')
269+
await page.goto(`${savedDocURL}/api`)
270+
const values = page.locator('.query-inspector__value')
271+
const count = await values.count()
272+
273+
for (let i = 0; i < count; i++) {
274+
await expect(values.nth(i)).not.toHaveText(/published/i)
275+
}
276+
})
277+
246278
test('should show currently published version status in versions view', async () => {
247279
const publishedDoc = await payload.create({
248280
collection: draftCollectionSlug,

0 commit comments

Comments
 (0)