Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(sanity): only include create action when restoring a deleted document #6937

Merged
merged 1 commit into from
Jun 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@ import {isLiveEditEnabled} from '../utils/isLiveEditEnabled'

export const restore: OperationImpl<[fromRevision: string]> = {
disabled: (): false => false,
execute: ({historyStore, schema, idPair, typeName}, fromRevision: string) => {
execute: ({snapshots, historyStore, schema, idPair, typeName}, fromRevision: string) => {
const targetId = isLiveEditEnabled(schema, typeName) ? idPair.publishedId : idPair.draftId
return historyStore.restore(idPair.publishedId, targetId, fromRevision, {
fromDeleted: !snapshots.draft && !snapshots.published,
useServerDocumentActions: true,
})
},
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {type SanityClient} from '@sanity/client'
import {type Action, type SanityClient} from '@sanity/client'
import {
isReference,
type Reference,
Expand Down Expand Up @@ -40,6 +40,7 @@ export interface HistoryStore {
}

interface RestoreOptions {
fromDeleted: boolean
useServerDocumentActions?: boolean
}

Expand Down Expand Up @@ -209,19 +210,25 @@ function restore(
}),
mergeMap((restoredDraft) => {
if (options?.useServerDocumentActions) {
return client.observable.action([
{
actionType: 'sanity.action.document.create',
publishedId: documentId,
attributes: restoredDraft,
ifExists: 'ignore',
},
{
actionType: 'sanity.action.document.replaceDraft',
publishedId: documentId,
attributes: restoredDraft,
},
])
const replaceDraftAction: Action = {
actionType: 'sanity.action.document.replaceDraft',
publishedId: documentId,
attributes: restoredDraft,
}
return client.observable.action(
options.fromDeleted
? [
{
actionType: 'sanity.action.document.create',
publishedId: documentId,
attributes: restoredDraft,
// This will guard against a race where someone else restores a deleted document at the same time
ifExists: 'fail',
},
replaceDraftAction,
]
: replaceDraftAction,
)
}

return client.observable.createOrReplace(restoredDraft, {visibility: 'async'})
Expand Down
Loading