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(core): drafts not using server actions always generate a created at #7503

Merged
merged 5 commits into from
Sep 16, 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
14 changes: 9 additions & 5 deletions packages/@sanity/mutator/src/document/Mutation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,17 +106,21 @@ export class Mutation {
compile(): void {
const operations: ((doc: Doc | null) => Doc | null)[] = []

// creation requires a _createdAt
const getGuaranteedCreatedAt = (doc: Doc): string =>
doc?._createdAt || this.params.timestamp || new Date().toISOString()

this.mutations.forEach((mutation) => {
if (mutation.create) {
// TODO: Fail entire patch if document did exist
const create = mutation.create || {}
const create = (mutation.create || {}) as Doc
operations.push((doc): Doc => {
if (doc) {
return doc
}

return Object.assign(create as Doc, {
_createdAt: create._createdAt || this.params.timestamp,
return Object.assign(create, {
_createdAt: getGuaranteedCreatedAt(create),
})
})
return
Expand All @@ -127,7 +131,7 @@ export class Mutation {
operations.push((doc) =>
doc === null
? Object.assign(createIfNotExists, {
_createdAt: createIfNotExists._createdAt || this.params.timestamp,
_createdAt: getGuaranteedCreatedAt(createIfNotExists),
})
: doc,
)
Expand All @@ -138,7 +142,7 @@ export class Mutation {
const createOrReplace = mutation.createOrReplace || {}
operations.push(() =>
Object.assign(createOrReplace, {
_createdAt: createOrReplace._createdAt || this.params.timestamp,
_createdAt: getGuaranteedCreatedAt(createOrReplace),
}),
)
return
Expand Down
1 change: 1 addition & 0 deletions packages/@sanity/mutator/src/document/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ export interface Doc {
_type: string
_rev?: string
_updatedAt?: string
_createdAt?: string
[attribute: string]: unknown
}

Expand Down
42 changes: 38 additions & 4 deletions packages/@sanity/mutator/test/SquashingBuffer.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {expect, test} from '@jest/globals'
import {expect, jest, test} from '@jest/globals'
import {type PatchMutationOperation} from '@sanity/types'

import {Mutation} from '../src/document/Mutation'
Expand Down Expand Up @@ -109,7 +109,39 @@ test('de-duplicate createIfNotExists', () => {
expect(tx2 && tx2.mutations.length).toBe(1)
})

test.each(['create', 'createIfNotExists', 'createOrReplace'])(
'%s defaults to current created at time',
(createFnc) => {
const globalMockDate = new Date('2020-01-01T12:34:55.000Z')
const globalDateSpy = jest.spyOn(global, 'Date').mockReturnValue(globalMockDate)

const sb = new SquashingBuffer(null)

add(sb, {[createFnc]: {_id: '1', _type: 'test', a: 'A string value'}})

const tx = sb.purge('txn_id')
if (!tx) {
throw new Error('buffer purge did not result in a mutation')
}

const final = tx.apply(null)

expect(final).toEqual({
_id: '1',
_rev: 'txn_id',
_createdAt: '2020-01-01T12:34:55.000Z',
_type: 'test',
a: 'A string value',
})

globalDateSpy.mockRestore()
},
)

test('de-duplicate create respects deletes', () => {
const globalMockDate = new Date('2020-01-01T12:34:55.000Z')
const globalDateSpy = jest.spyOn(global, 'Date').mockReturnValue(globalMockDate)

const initial = {_id: '1', _type: 'test', a: 'A string value', c: 'Some value'}
const sb = new SquashingBuffer(initial)
add(sb, {createIfNotExists: {_id: '1', _type: 'test', a: 'A string value', c: 'Some value'}})
Expand All @@ -124,7 +156,7 @@ test('de-duplicate create respects deletes', () => {
if (!tx) {
throw new Error('buffer purge did not result in a mutation')
}
tx.params.timestamp = '2021-01-01T12:34:55Z'
tx.params.timestamp = '2021-01-01T12:34:55.000Z'

jordanl17 marked this conversation as resolved.
Show resolved Hide resolved
const creates = tx.mutations.filter((mut) => !!mut.createIfNotExists)
expect(creates.length).toBe(2) // Only a single create mutation expected (note: bn - is this correct?)
Expand All @@ -134,14 +166,16 @@ test('de-duplicate create respects deletes', () => {
expect(final).toEqual({
_id: '1',
_type: 'test',
_createdAt: '2021-01-01T12:34:55Z',
_updatedAt: '2021-01-01T12:34:55Z',
_createdAt: '2020-01-01T12:34:55.000Z',
_updatedAt: '2021-01-01T12:34:55.000Z',
_rev: 'txn_id',
a: {
b: 'A wrapped value',
},
c: 'Changed',
})

globalDateSpy.mockRestore()
})

test('de-duplicate create respects rebasing', () => {
Expand Down
Loading