From 023c650e033f8f084c797083567d902a5ee230b1 Mon Sep 17 00:00:00 2001 From: Patrik Date: Tue, 17 Sep 2024 18:19:18 -0400 Subject: [PATCH] chore: cleans up locked-documents (#8269) Co-authored-by: Dan Ribbens --- .../src/views/Dashboard/Default/index.tsx | 6 +- .../src/collections/operations/delete.ts | 16 +- .../src/collections/operations/deleteByID.ts | 16 +- .../src/collections/operations/find.ts | 8 +- .../src/collections/operations/findByID.ts | 4 +- .../src/collections/operations/update.ts | 16 +- .../src/collections/operations/updateByID.ts | 16 +- .../payload/src/globals/operations/findOne.ts | 4 +- .../payload/src/globals/operations/update.ts | 19 +- .../lockedDocumentsCollection.ts | 31 ++-- .../src/utilities/checkDocumentLockStatus.ts | 61 +++---- packages/ui/src/elements/SelectRow/index.tsx | 6 +- .../ui/src/providers/DocumentInfo/index.tsx | 8 +- packages/ui/src/providers/Selection/index.tsx | 10 +- packages/ui/src/utilities/buildFormState.ts | 25 ++- test/_community/payload-types.ts | 37 +++- test/locked-documents/e2e.spec.ts | 105 +++++------ test/locked-documents/int.spec.ts | 72 +++----- test/locked-documents/payload-types.ts | 166 +++--------------- 19 files changed, 209 insertions(+), 417 deletions(-) diff --git a/packages/next/src/views/Dashboard/Default/index.tsx b/packages/next/src/views/Dashboard/Default/index.tsx index 1f9a34e1ca..01ffb636e4 100644 --- a/packages/next/src/views/Dashboard/Default/index.tsx +++ b/packages/next/src/views/Dashboard/Default/index.tsx @@ -16,7 +16,7 @@ import './index.scss' const baseClass = 'dashboard' export type DashboardProps = { - globalData: Array<{ data: { isLocked: boolean; userEditing: ClientUser | null }; slug: string }> + globalData: Array<{ data: { _isLocked: boolean; _userEditing: ClientUser | null }; slug: string }> Link: React.ComponentType navGroups?: ReturnType permissions: Permissions @@ -131,8 +131,8 @@ export const DefaultDashboard: React.FC = (props) => { (global) => global.slug === entity.slug, ) if (globalLockData) { - lockStatus = globalLockData.data.isLocked - userEditing = globalLockData.data.userEditing + lockStatus = globalLockData.data._isLocked + userEditing = globalLockData.data._userEditing } } diff --git a/packages/payload/src/collections/operations/delete.ts b/packages/payload/src/collections/operations/delete.ts index 8b1d59535f..6453c0b500 100644 --- a/packages/payload/src/collections/operations/delete.ts +++ b/packages/payload/src/collections/operations/delete.ts @@ -122,7 +122,7 @@ export const deleteOperation = async ( // Handle potentially locked documents // ///////////////////////////////////// - const { lockedDocument, shouldUnlockDocument } = await checkDocumentLockStatus({ + await checkDocumentLockStatus({ id, collectionSlug: collectionConfig.slug, lockErrorMessage: `Document with ID ${id} is currently locked and cannot be deleted.`, @@ -152,20 +152,6 @@ export const deleteOperation = async ( req, }) - // ///////////////////////////////////// - // Unlock the document if necessary - // ///////////////////////////////////// - - if (shouldUnlockDocument && lockedDocument) { - await payload.db.deleteOne({ - collection: 'payload-locked-documents', - req, - where: { - id: { equals: lockedDocument.id }, - }, - }) - } - // ///////////////////////////////////// // Delete versions // ///////////////////////////////////// diff --git a/packages/payload/src/collections/operations/deleteByID.ts b/packages/payload/src/collections/operations/deleteByID.ts index f175042d03..060955e379 100644 --- a/packages/payload/src/collections/operations/deleteByID.ts +++ b/packages/payload/src/collections/operations/deleteByID.ts @@ -114,7 +114,7 @@ export const deleteByIDOperation = async ( // Handle potentially locked documents // ///////////////////////////////////// - const { lockedDocument, shouldUnlockDocument } = await checkDocumentLockStatus({ + await checkDocumentLockStatus({ id, collectionSlug: collectionConfig.slug, lockErrorMessage: `Document with ID ${id} is currently locked and cannot be deleted.`, @@ -129,20 +129,6 @@ export const deleteByIDOperation = async ( req, }) - // ///////////////////////////////////// - // Unlock the document if necessary - // ///////////////////////////////////// - - if (shouldUnlockDocument && lockedDocument) { - await payload.db.deleteOne({ - collection: 'payload-locked-documents', - req, - where: { - id: { equals: lockedDocument.id }, - }, - }) - } - // ///////////////////////////////////// // Delete versions // ///////////////////////////////////// diff --git a/packages/payload/src/collections/operations/find.ts b/packages/payload/src/collections/operations/find.ts index fc812f5866..e0d850f49f 100644 --- a/packages/payload/src/collections/operations/find.ts +++ b/packages/payload/src/collections/operations/find.ts @@ -182,15 +182,15 @@ export const findOperation = async ( const lockedDoc = lockedDocs.find((lock) => lock?.document?.value === doc.id) return { ...doc, - isLocked: !!lockedDoc, - userEditing: lockedDoc ? lockedDoc._lastEdited?.user?.value : null, + _isLocked: !!lockedDoc, + _userEditing: lockedDoc ? lockedDoc?.user?.value : null, } }) } catch (error) { result.docs = result.docs.map((doc) => ({ ...doc, - isLocked: false, - userEditing: null, + _isLocked: false, + _userEditing: null, })) } } diff --git a/packages/payload/src/collections/operations/findByID.ts b/packages/payload/src/collections/operations/findByID.ts index 2373d2e527..03ffb71b5e 100644 --- a/packages/payload/src/collections/operations/findByID.ts +++ b/packages/payload/src/collections/operations/findByID.ts @@ -138,8 +138,8 @@ export const findByIDOperation = async ( // swallow error } - result.isLocked = !!lockStatus - result.userEditing = lockStatus?._lastEdited?.user?.value ?? null + result._isLocked = !!lockStatus + result._userEditing = lockStatus?.user?.value ?? null } // ///////////////////////////////////// diff --git a/packages/payload/src/collections/operations/update.ts b/packages/payload/src/collections/operations/update.ts index b7f506d8b8..c880a743b8 100644 --- a/packages/payload/src/collections/operations/update.ts +++ b/packages/payload/src/collections/operations/update.ts @@ -182,7 +182,7 @@ export const updateOperation = async ( // Handle potentially locked documents // ///////////////////////////////////// - const { lockedDocument, shouldUnlockDocument } = await checkDocumentLockStatus({ + await checkDocumentLockStatus({ id, collectionSlug: collectionConfig.slug, lockErrorMessage: `Document with ID ${id} is currently locked by another user and cannot be updated.`, @@ -335,20 +335,6 @@ export const updateOperation = async ( }) } - // ///////////////////////////////////// - // Unlock the document if necessary - // ///////////////////////////////////// - - if (shouldUnlockDocument && lockedDocument) { - await payload.db.deleteOne({ - collection: 'payload-locked-documents', - req, - where: { - id: { equals: lockedDocument.id }, - }, - }) - } - // ///////////////////////////////////// // afterRead - Fields // ///////////////////////////////////// diff --git a/packages/payload/src/collections/operations/updateByID.ts b/packages/payload/src/collections/operations/updateByID.ts index 0da7b768d2..97ac8247bf 100644 --- a/packages/payload/src/collections/operations/updateByID.ts +++ b/packages/payload/src/collections/operations/updateByID.ts @@ -146,7 +146,7 @@ export const updateByIDOperation = async ( // Handle potentially locked documents // ///////////////////////////////////// - const { lockedDocument, shouldUnlockDocument } = await checkDocumentLockStatus({ + await checkDocumentLockStatus({ id, collectionSlug: collectionConfig.slug, lockErrorMessage: `Document with ID ${id} is currently locked by another user and cannot be updated.`, @@ -365,20 +365,6 @@ export const updateByIDOperation = async ( }) } - // ///////////////////////////////////// - // Unlock the document if necessary - // ///////////////////////////////////// - - if (shouldUnlockDocument && lockedDocument) { - await payload.db.deleteOne({ - collection: 'payload-locked-documents', - req, - where: { - id: { equals: lockedDocument.id }, - }, - }) - } - // ///////////////////////////////////// // afterRead - Fields // ///////////////////////////////////// diff --git a/packages/payload/src/globals/operations/findOne.ts b/packages/payload/src/globals/operations/findOne.ts index 29c9f84f81..6ba83a792b 100644 --- a/packages/payload/src/globals/operations/findOne.ts +++ b/packages/payload/src/globals/operations/findOne.ts @@ -86,8 +86,8 @@ export const findOneOperation = async >( // swallow error } - doc.isLocked = !!lockStatus - doc.userEditing = lockStatus?._lastEdited?.user?.value ?? null + doc._isLocked = !!lockStatus + doc._userEditing = lockStatus?.user?.value ?? null } // ///////////////////////////////////// diff --git a/packages/payload/src/globals/operations/update.ts b/packages/payload/src/globals/operations/update.ts index cbcadc129b..a9a113b721 100644 --- a/packages/payload/src/globals/operations/update.ts +++ b/packages/payload/src/globals/operations/update.ts @@ -5,7 +5,6 @@ import type { Operation, PayloadRequest, Where } from '../../types/index.js' import type { DataFromGlobalSlug, SanitizedGlobalConfig } from '../config/types.js' import executeAccess from '../../auth/executeAccess.js' -import { APIError } from '../../errors/index.js' import { afterChange } from '../../fields/hooks/afterChange/index.js' import { afterRead } from '../../fields/hooks/afterRead/index.js' import { beforeChange } from '../../fields/hooks/beforeChange/index.js' @@ -119,7 +118,7 @@ export const updateOperation = async ( // Handle potentially locked global documents // /////////////////////////////////////////// - const { lockedDocument, shouldUnlockDocument } = await checkDocumentLockStatus({ + await checkDocumentLockStatus({ globalSlug: slug, lockErrorMessage: `Global with slug "${slug}" is currently locked by another user and cannot be updated.`, req, @@ -262,22 +261,6 @@ export const updateOperation = async ( } } - // ///////////////////////////////////// - // Unlock the global if necessary - // ///////////////////////////////////// - - if (shouldUnlockDocument && lockedDocument) { - await payload.db.deleteOne({ - collection: 'payload-locked-documents', - req, - where: { - globalSlug: { - equals: slug, - }, - }, - }) - } - // ///////////////////////////////////// // afterRead - Fields // ///////////////////////////////////// diff --git a/packages/payload/src/lockedDocuments/lockedDocumentsCollection.ts b/packages/payload/src/lockedDocuments/lockedDocumentsCollection.ts index d25d580c98..9cc0f1cd73 100644 --- a/packages/payload/src/lockedDocuments/lockedDocumentsCollection.ts +++ b/packages/payload/src/lockedDocuments/lockedDocumentsCollection.ts @@ -14,32 +14,21 @@ export const getLockedDocumentsCollection = (config: Config): CollectionConfig = maxDepth: 0, relationTo: [...config.collections.map((collectionConfig) => collectionConfig.slug)], }, + { + name: 'editedAt', + type: 'date', + }, { name: 'globalSlug', type: 'text', }, { - name: '_lastEdited', - type: 'group', - fields: [ - { - name: 'user', - type: 'relationship', - relationTo: config.collections - .filter((collectionConfig) => collectionConfig.auth) - .map((collectionConfig) => collectionConfig.slug), - required: true, - }, - { - name: 'editedAt', - type: 'date', - }, - ], - }, - { - name: 'isLocked', - type: 'checkbox', - defaultValue: false, + name: 'user', + type: 'relationship', + relationTo: config.collections + .filter((collectionConfig) => collectionConfig.auth) + .map((collectionConfig) => collectionConfig.slug), + required: true, }, ], }) diff --git a/packages/payload/src/utilities/checkDocumentLockStatus.ts b/packages/payload/src/utilities/checkDocumentLockStatus.ts index 2b34243d5c..7a60109058 100644 --- a/packages/payload/src/utilities/checkDocumentLockStatus.ts +++ b/packages/payload/src/utilities/checkDocumentLockStatus.ts @@ -13,11 +13,6 @@ type CheckDocumentLockStatusArgs = { req: PayloadRequest } -type CheckDocumentLockResult = { - lockedDocument?: JsonObject & TypeWithID - shouldUnlockDocument: boolean -} - export const checkDocumentLockStatus = async ({ id, collectionSlug, @@ -25,7 +20,7 @@ export const checkDocumentLockStatus = async ({ lockDurationDefault = 300, // Default 5 minutes in seconds lockErrorMessage, req, -}: CheckDocumentLockStatusArgs): Promise => { +}: CheckDocumentLockStatusArgs): Promise => { const { payload } = req // Retrieve the lockDocuments property for either collection or global @@ -37,7 +32,7 @@ export const checkDocumentLockStatus = async ({ // If lockDocuments is explicitly set to false, skip the lock logic and return early if (isLockingEnabled === false) { - return { lockedDocument: undefined, shouldUnlockDocument: false } + return } let lockedDocumentQuery = {} @@ -67,36 +62,36 @@ export const checkDocumentLockStatus = async ({ limit: 1, pagination: false, req, + sort: '-updatedAt', where: lockedDocumentQuery, }) - let shouldUnlockDocument = false - // If there's a locked document, check lock conditions - if (lockedDocumentResult.docs.length > 0) { - const lockedDoc = lockedDocumentResult.docs[0] - const lastEditedAt = new Date(lockedDoc?._lastEdited?.editedAt) - const now = new Date() - - const lockDuration = - typeof lockDocumentsProp === 'object' ? lockDocumentsProp.duration : lockDurationDefault - - const lockDurationInMilliseconds = lockDuration * 1000 - const currentUserId = req.user?.id - - // If document is locked by another user and the lock hasn't expired - if (lockedDoc._lastEdited?.user?.value?.id !== currentUserId) { - if (now.getTime() - lastEditedAt.getTime() <= lockDurationInMilliseconds) { - throw new APIError(finalLockErrorMessage) - } else { - // If lock has expired, allow unlocking - shouldUnlockDocument = true - } - } else { - // If document is locked by the current user, allow unlocking - shouldUnlockDocument = true - } + const lockedDoc = lockedDocumentResult?.docs[0] + if (!lockedDoc) { + return } - return { lockedDocument: lockedDocumentResult.docs[0], shouldUnlockDocument } + const lastEditedAt = new Date(lockedDoc?.editedAt) + const now = new Date() + + const lockDuration = + typeof lockDocumentsProp === 'object' ? lockDocumentsProp.duration : lockDurationDefault + + const lockDurationInMilliseconds = lockDuration * 1000 + const currentUserId = req.user?.id + + // document is locked by another user and the lock hasn't expired + if ( + lockedDoc.user?.value?.id !== currentUserId && + now.getTime() - lastEditedAt.getTime() <= lockDurationInMilliseconds + ) { + throw new APIError(finalLockErrorMessage) + } + + await payload.db.deleteMany({ + collection: 'payload-locked-documents', + req, + where: lockedDocumentQuery, + }) } diff --git a/packages/ui/src/elements/SelectRow/index.tsx b/packages/ui/src/elements/SelectRow/index.tsx index 780c1476bd..78cedf260d 100644 --- a/packages/ui/src/elements/SelectRow/index.tsx +++ b/packages/ui/src/elements/SelectRow/index.tsx @@ -12,12 +12,12 @@ const baseClass = 'select-row' export const SelectRow: React.FC = () => { const { selected, setSelection } = useSelection() const { rowData } = useTableCell() - const { isLocked, userEditing } = rowData || {} + const { _isLocked, _userEditing } = rowData || {} - const documentIsLocked = isLocked && userEditing + const documentIsLocked = _isLocked && _userEditing if (documentIsLocked) { - return + return } return ( diff --git a/packages/ui/src/providers/DocumentInfo/index.tsx b/packages/ui/src/providers/DocumentInfo/index.tsx index 2d2ee7d394..d4da463400 100644 --- a/packages/ui/src/providers/DocumentInfo/index.tsx +++ b/packages/ui/src/providers/DocumentInfo/index.tsx @@ -193,10 +193,8 @@ const DocumentInfo: React.FC< // Send a patch request to update the _lastEdited info await requests.patch(`${serverURL}${api}/payload-locked-documents/${lockId}`, { body: JSON.stringify({ - _lastEdited: { - editedAt: new Date(), - user: { relationTo: user?.collection, value: user?.id }, - }, + editedAt: new Date(), + user: { relationTo: user?.collection, value: user?.id }, }), headers: { 'Content-Type': 'application/json', @@ -230,7 +228,7 @@ const DocumentInfo: React.FC< const { docs } = await request.json() if (docs.length > 0) { - const newEditor = docs[0]._lastEdited?.user?.value + const newEditor = docs[0].user?.value if (newEditor && newEditor.id !== currentEditor?.id) { setCurrentEditor(newEditor) setDocumentIsLocked(true) diff --git a/packages/ui/src/providers/Selection/index.tsx b/packages/ui/src/providers/Selection/index.tsx index f364ff7749..d7728a2c08 100644 --- a/packages/ui/src/providers/Selection/index.tsx +++ b/packages/ui/src/providers/Selection/index.tsx @@ -56,8 +56,8 @@ export const SelectionProvider: React.FC = ({ children, docs = [], totalD const rows = new Map() if (allAvailable) { setSelectAll(SelectAllStatus.AllAvailable) - docs.forEach(({ id, isLocked }) => { - if (!isLocked) { + docs.forEach(({ id, _isLocked }) => { + if (!_isLocked) { rows.set(id, true) } }) @@ -67,8 +67,8 @@ export const SelectionProvider: React.FC = ({ children, docs = [], totalD ) { setSelectAll(SelectAllStatus.None) } else { - docs.forEach(({ id, isLocked }) => { - if (!isLocked) { + docs.forEach(({ id, _isLocked }) => { + if (!_isLocked) { rows.set(id, selectAll !== SelectAllStatus.Some) } }) @@ -82,7 +82,7 @@ export const SelectionProvider: React.FC = ({ children, docs = [], totalD (id) => { const doc = docs.find((doc) => doc.id === id) - if (doc?.isLocked) { + if (doc?._isLocked) { return // Prevent selection if the document is locked } diff --git a/packages/ui/src/utilities/buildFormState.ts b/packages/ui/src/utilities/buildFormState.ts index 2c755d6e68..7c0a74d42b 100644 --- a/packages/ui/src/utilities/buildFormState.ts +++ b/packages/ui/src/utilities/buildFormState.ts @@ -258,43 +258,40 @@ export const buildFormState = async ({ if (lockedDocument.docs && lockedDocument.docs.length > 0) { const lockedState = { isLocked: true, - user: lockedDocument.docs[0]?._lastEdited?.user?.value, + user: lockedDocument.docs[0]?.user?.value, } if (updateLastEdited) { - await req.payload.update({ + await req.payload.db.updateOne({ id: lockedDocument.docs[0].id, collection: 'payload-locked-documents', data: { - _lastEdited: { - editedAt: new Date().toISOString(), - }, + editedAt: new Date().toISOString(), }, + req, }) } return { lockedState, state: result } } else { // If no lock document exists, create it - await req.payload.create({ + await req.payload.db.create({ collection: 'payload-locked-documents', data: { - _lastEdited: { - editedAt: new Date().toISOString(), - user: { - relationTo: [req.user.collection], - value: req.user.id, - }, - }, document: collectionSlug ? { relationTo: [collectionSlug], value: id, } : undefined, + editedAt: new Date().toISOString(), globalSlug: globalSlug ? globalSlug : undefined, - isLocked: true, + user: { + relationTo: [req.user.collection], + value: req.user.id, + }, }, + req, }) const lockedState = { diff --git a/test/_community/payload-types.ts b/test/_community/payload-types.ts index ecb05fc111..ed131dc1ca 100644 --- a/test/_community/payload-types.ts +++ b/test/_community/payload-types.ts @@ -15,6 +15,7 @@ export interface Config { simple: Simple; media: Media; users: User; + 'payload-locked-documents': PayloadLockedDocument; 'payload-preferences': PayloadPreference; 'payload-migrations': PayloadMigration; }; @@ -162,6 +163,38 @@ export interface User { lockUntil?: string | null; password?: string | null; } +/** + * This interface was referenced by `Config`'s JSON-Schema + * via the `definition` "payload-locked-documents". + */ +export interface PayloadLockedDocument { + id: string; + document?: + | ({ + relationTo: 'posts'; + value: string | Post; + } | null) + | ({ + relationTo: 'simple'; + value: string | Simple; + } | null) + | ({ + relationTo: 'media'; + value: string | Media; + } | null) + | ({ + relationTo: 'users'; + value: string | User; + } | null); + globalSlug?: string | null; + user: { + relationTo: 'users'; + value: string | User; + }; + editedAt?: string | null; + updatedAt: string; + createdAt: string; +} /** * This interface was referenced by `Config`'s JSON-Schema * via the `definition` "payload-preferences". @@ -239,6 +272,6 @@ export interface Auth { declare module 'payload' { - // @ts-ignore + // @ts-ignore export interface GeneratedTypes extends Config {} -} +} \ No newline at end of file diff --git a/test/locked-documents/e2e.spec.ts b/test/locked-documents/e2e.spec.ts index 955f26fcf6..37f273dcf6 100644 --- a/test/locked-documents/e2e.spec.ts +++ b/test/locked-documents/e2e.spec.ts @@ -89,19 +89,16 @@ describe('locked documents', () => { lockedDoc = await payload.create({ collection: lockedDocumentCollection, data: { - _lastEdited: { - editedAt: new Date().toISOString(), - user: { - relationTo: 'users', - value: user2.id, - }, - }, document: { relationTo: 'posts', value: postDoc.id, }, + editedAt: new Date().toISOString(), globalSlug: undefined, - isLocked: true, + user: { + relationTo: 'users', + value: user2.id, + }, }, }) }) @@ -292,19 +289,16 @@ describe('locked documents', () => { lockedDoc = await payload.create({ collection: lockedDocumentCollection, data: { - _lastEdited: { - editedAt: new Date().toISOString(), - user: { - relationTo: 'users', - value: user2.id, - }, - }, document: { relationTo: 'posts', value: postDoc.id, }, + editedAt: new Date().toISOString(), globalSlug: undefined, - isLocked: true, + user: { + relationTo: 'users', + value: user2.id, + }, }, }) }) @@ -399,19 +393,16 @@ describe('locked documents', () => { lockedDoc = await payload.create({ collection: lockedDocumentCollection, data: { - _lastEdited: { - editedAt: new Date().toISOString(), - user: { - relationTo: 'users', - value: user2.id, - }, - }, document: { relationTo: 'posts', value: postDoc.id, }, + editedAt: new Date().toISOString(), globalSlug: undefined, - isLocked: true, + user: { + relationTo: 'users', + value: user2.id, + }, }, }) }) @@ -462,10 +453,10 @@ describe('locked documents', () => { const userEmail = // eslint-disable-next-line playwright/no-conditional-in-test - lockedDoc.docs[0]._lastEdited.user.value && - typeof lockedDoc.docs[0]._lastEdited.user.value === 'object' && - 'email' in lockedDoc.docs[0]._lastEdited.user.value && - lockedDoc.docs[0]._lastEdited.user.value.email + lockedDoc.docs[0].user.value && + typeof lockedDoc.docs[0].user.value === 'object' && + 'email' in lockedDoc.docs[0].user.value && + lockedDoc.docs[0].user.value.email expect(userEmail).toEqual('dev@payloadcms.com') }) @@ -492,19 +483,16 @@ describe('locked documents', () => { lockedDoc = await payload.create({ collection: lockedDocumentCollection, data: { - _lastEdited: { - editedAt: new Date().toISOString(), - user: { - relationTo: 'users', - value: user2.id, - }, - }, document: { relationTo: 'posts', value: postDoc.id, }, + editedAt: new Date().toISOString(), globalSlug: undefined, - isLocked: true, + user: { + relationTo: 'users', + value: user2.id, + }, }, }) }) @@ -557,10 +545,10 @@ describe('locked documents', () => { const userEmail = // eslint-disable-next-line playwright/no-conditional-in-test - lockedDoc.docs[0]._lastEdited.user.value && - typeof lockedDoc.docs[0]._lastEdited.user.value === 'object' && - 'email' in lockedDoc.docs[0]._lastEdited.user.value && - lockedDoc.docs[0]._lastEdited.user.value.email + lockedDoc.docs[0].user.value && + typeof lockedDoc.docs[0].user.value === 'object' && + 'email' in lockedDoc.docs[0].user.value && + lockedDoc.docs[0].user.value.email expect(userEmail).toEqual('dev@payloadcms.com') }) @@ -618,11 +606,9 @@ describe('locked documents', () => { id: lockedDoc.docs[0].id, collection: lockedDocumentCollection, data: { - _lastEdited: { - user: { - relationTo: 'users', - value: user2.id, - }, + user: { + relationTo: 'users', + value: user2.id, }, }, }) @@ -671,11 +657,9 @@ describe('locked documents', () => { id: lockedDoc.docs[0].id, collection: lockedDocumentCollection, data: { - _lastEdited: { - user: { - relationTo: 'users', - value: user2.id, - }, + user: { + relationTo: 'users', + value: user2.id, }, }, }) @@ -729,11 +713,9 @@ describe('locked documents', () => { id: lockedDoc.docs[0].id, collection: lockedDocumentCollection, data: { - _lastEdited: { - user: { - relationTo: 'users', - value: user2.id, - }, + user: { + relationTo: 'users', + value: user2.id, }, }, }) @@ -784,16 +766,13 @@ describe('locked documents', () => { const lockedGlobal = await payload.create({ collection: lockedDocumentCollection, data: { - _lastEdited: { - editedAt: new Date().toISOString(), - user: { - relationTo: 'users', - value: user2.id, - }, - }, document: undefined, + editedAt: new Date().toISOString(), globalSlug: 'menu', - isLocked: true, + user: { + relationTo: 'users', + value: user2.id, + }, }, }) diff --git a/test/locked-documents/int.spec.ts b/test/locked-documents/int.spec.ts index 2671927fd0..f9b2edf5d2 100644 --- a/test/locked-documents/int.spec.ts +++ b/test/locked-documents/int.spec.ts @@ -138,19 +138,16 @@ describe('Locked documents', () => { const lockedDocInstance = await payload.create({ collection: lockedDocumentCollection, data: { - _lastEdited: { - editedAt: pastEditedAt.toISOString(), // stale date - user: { - relationTo: 'users', - value: user2.id, - }, + editedAt: pastEditedAt.toISOString(), + user: { + relationTo: 'users', + value: user2.id, }, document: { relationTo: 'posts', value: newPost2.id, }, globalSlug: undefined, - isLocked: true, }, }) @@ -198,16 +195,13 @@ describe('Locked documents', () => { const lockedGlobalInstance = await payload.create({ collection: lockedDocumentCollection, data: { - _lastEdited: { - editedAt: pastEditedAt.toISOString(), // stale date - user: { - relationTo: 'users', - value: user2.id, - }, + editedAt: pastEditedAt.toISOString(), // stale date + user: { + relationTo: 'users', + value: user2.id, }, document: undefined, globalSlug: menuSlug, - isLocked: true, }, }) @@ -256,19 +250,16 @@ describe('Locked documents', () => { await payload.create({ collection: lockedDocumentCollection, data: { - _lastEdited: { - editedAt: new Date().toISOString(), - user: { - relationTo: 'users', - value: user2.id, - }, - }, document: { relationTo: 'posts', value: newPost.id, }, + editedAt: new Date().toISOString(), globalSlug: undefined, - isLocked: true, + user: { + relationTo: 'users', + value: user2.id, + }, }, }) @@ -294,16 +285,13 @@ describe('Locked documents', () => { await payload.create({ collection: lockedDocumentCollection, data: { - _lastEdited: { - editedAt: new Date().toISOString(), - user: { - relationTo: 'users', - value: user2.id, - }, - }, document: undefined, + editedAt: new Date().toISOString(), globalSlug: menuSlug, - isLocked: true, + user: { + relationTo: 'users', + value: user2.id, + }, }, }) @@ -336,19 +324,16 @@ describe('Locked documents', () => { await payload.create({ collection: lockedDocumentCollection, data: { - _lastEdited: { - editedAt: new Date().toISOString(), - user: { - relationTo: 'users', - value: user2.id, - }, - }, document: { relationTo: 'posts', value: newPost3.id, }, + editedAt: new Date().toISOString(), globalSlug: undefined, - isLocked: true, + user: { + relationTo: 'users', + value: user2.id, + }, }, }) @@ -380,19 +365,16 @@ describe('Locked documents', () => { const lockedDocInstance = await payload.create({ collection: lockedDocumentCollection, data: { - _lastEdited: { - editedAt: pastEditedAt.toISOString(), // stale date - user: { - relationTo: 'users', - value: user2.id, - }, + editedAt: pastEditedAt.toISOString(), // stale date + user: { + relationTo: 'users', + value: user2.id, }, document: { relationTo: 'posts', value: newPost4.id, }, globalSlug: undefined, - isLocked: true, }, }) diff --git a/test/locked-documents/payload-types.ts b/test/locked-documents/payload-types.ts index 7d02310add..44597368e8 100644 --- a/test/locked-documents/payload-types.ts +++ b/test/locked-documents/payload-types.ts @@ -13,10 +13,9 @@ export interface Config { collections: { pages: Page; posts: Post; - simple: Simple; users: User; - 'payload-preferences': PayloadPreference; 'payload-locked-documents': PayloadLockedDocument; + 'payload-preferences': PayloadPreference; 'payload-migrations': PayloadMigration; }; db: { @@ -24,7 +23,6 @@ export interface Config { }; globals: { menu: Menu; - 'custom-ts': CustomT; }; locale: null; user: User & { @@ -56,37 +54,6 @@ export interface UserAuthOperations { export interface Page { id: string; text?: string | null; - richText?: { - root: { - type: string; - children: { - type: string; - version: number; - [k: string]: unknown; - }[]; - direction: ('ltr' | 'rtl') | null; - format: 'left' | 'start' | 'center' | 'right' | 'end' | 'justify' | ''; - indent: number; - version: number; - }; - [k: string]: unknown; - } | null; - myBlocks?: - | ( - | { - test?: string | null; - id?: string | null; - blockName?: string | null; - blockType: 'test'; - } - | { - test2?: string | null; - id?: string | null; - blockName?: string | null; - blockType: 'someBlock2'; - } - )[] - | null; updatedAt: string; createdAt: string; _status?: ('draft' | 'published') | null; @@ -98,51 +65,10 @@ export interface Page { export interface Post { id: string; text?: string | null; - richText?: { - root: { - type: string; - children: { - type: string; - version: number; - [k: string]: unknown; - }[]; - direction: ('ltr' | 'rtl') | null; - format: 'left' | 'start' | 'center' | 'right' | 'end' | 'justify' | ''; - indent: number; - version: number; - }; - [k: string]: unknown; - } | null; - myBlocks?: - | ( - | { - test?: string | null; - id?: string | null; - blockName?: string | null; - blockType: 'test'; - } - | { - test2?: string | null; - id?: string | null; - blockName?: string | null; - blockType: 'someBlock2'; - } - )[] - | null; updatedAt: string; createdAt: string; _status?: ('draft' | 'published') | null; } -/** - * This interface was referenced by `Config`'s JSON-Schema - * via the `definition` "simple". - */ -export interface Simple { - id: string; - text?: string | null; - updatedAt: string; - createdAt: string; -} /** * This interface was referenced by `Config`'s JSON-Schema * via the `definition` "users". @@ -161,6 +87,34 @@ export interface User { lockUntil?: string | null; password?: string | null; } +/** + * This interface was referenced by `Config`'s JSON-Schema + * via the `definition` "payload-locked-documents". + */ +export interface PayloadLockedDocument { + id: string; + document?: + | ({ + relationTo: 'pages'; + value: string | Page; + } | null) + | ({ + relationTo: 'posts'; + value: string | Post; + } | null) + | ({ + relationTo: 'users'; + value: string | User; + } | null); + globalSlug?: string | null; + user: { + relationTo: 'users'; + value: string | User; + }; + editedAt?: string | null; + updatedAt: string; + createdAt: string; +} /** * This interface was referenced by `Config`'s JSON-Schema * via the `definition` "payload-preferences". @@ -184,45 +138,6 @@ export interface PayloadPreference { updatedAt: string; createdAt: string; } -/** - * This interface was referenced by `Config`'s JSON-Schema - * via the `definition` "payload-locked-documents". - */ -export interface PayloadLockedDocument { - id: string; - document?: - | ({ - relationTo: 'pages'; - value: string | Page; - } | null) - | ({ - relationTo: 'posts'; - value: string | Post; - } | null) - | ({ - relationTo: 'simple'; - value: string | Simple; - } | null) - | ({ - relationTo: 'users'; - value: string | User; - } | null) - | ({ - relationTo: 'payload-preferences'; - value: string | PayloadPreference; - } | null); - globalSlug?: string | null; - _lastEdited: { - user: { - relationTo: 'users'; - value: string | User; - }; - editedAt?: string | null; - }; - isLocked?: boolean | null; - updatedAt: string; - createdAt: string; -} /** * This interface was referenced by `Config`'s JSON-Schema * via the `definition` "payload-migrations". @@ -244,29 +159,6 @@ export interface Menu { updatedAt?: string | null; createdAt?: string | null; } -/** - * This interface was referenced by `Config`'s JSON-Schema - * via the `definition` "custom-ts". - */ -export interface CustomT { - id: string; - custom?: 'hello' | 'world'; - withDefinitionsUsage?: ObjectWithNumber[]; - json: { - id: string; - name: string; - age?: number; - }[]; - updatedAt?: string | null; - createdAt?: string | null; -} -/** - * This interface was referenced by `Config`'s JSON-Schema - * via the `definition` "objectWithNumber". - */ -export interface ObjectWithNumber { - id?: number; -} /** * This interface was referenced by `Config`'s JSON-Schema * via the `definition` "auth".