chore: use updatedAt instead of editedAt for locked documents (#8324)
- Removes locked documents `editedAt` as it was redundant with the `updatedAt` timestamp - Adjust stale lock tests to configure the duration down to 1 second and await it to not lose any test coverage - DB performance changes: 1. Switch to payload.db.find instead of payload.find for checkDocumentLockStatus to avoid populating the user and other payload find overhead 2. Add maxDepth: 1 to user relationship 3. Add index to global slug
This commit is contained in:
@@ -25,13 +25,13 @@ const formatDate = (date) => {
|
||||
}
|
||||
|
||||
export const DocumentLocked: React.FC<{
|
||||
editedAt?: null | number
|
||||
handleGoBack: () => void
|
||||
isActive: boolean
|
||||
onReadOnly: () => void
|
||||
onTakeOver: () => void
|
||||
updatedAt?: null | number
|
||||
user?: ClientUser
|
||||
}> = ({ editedAt, handleGoBack, isActive, onReadOnly, onTakeOver, user }) => {
|
||||
}> = ({ handleGoBack, isActive, onReadOnly, onTakeOver, updatedAt, user }) => {
|
||||
const { closeModal, openModal } = useModal()
|
||||
const { t } = useTranslation()
|
||||
|
||||
@@ -52,7 +52,7 @@ export const DocumentLocked: React.FC<{
|
||||
<strong>{user?.email ?? user?.id}</strong> {t('general:currentlyEditing')}
|
||||
</p>
|
||||
<p>
|
||||
{t('general:editedSince')} <strong>{formatDate(editedAt)}</strong>
|
||||
{t('general:editedSince')} <strong>{formatDate(updatedAt)}</strong>
|
||||
</p>
|
||||
</div>
|
||||
<div className={`${baseClass}__controls`}>
|
||||
|
||||
@@ -421,7 +421,6 @@ export const DefaultEditView: React.FC = () => {
|
||||
{BeforeDocument}
|
||||
{isLockingEnabled && shouldShowDocumentLockedModal && !isReadOnlyForIncomingUser && (
|
||||
<DocumentLocked
|
||||
editedAt={lastUpdateTime}
|
||||
handleGoBack={handleGoBack}
|
||||
isActive={shouldShowDocumentLockedModal}
|
||||
onReadOnly={() => {
|
||||
@@ -429,6 +428,7 @@ export const DefaultEditView: React.FC = () => {
|
||||
setShowTakeOverModal(false)
|
||||
}}
|
||||
onTakeOver={handleTakeOver}
|
||||
updatedAt={lastUpdateTime}
|
||||
user={currentEditor}
|
||||
/>
|
||||
)}
|
||||
|
||||
@@ -14,17 +14,15 @@ export const getLockedDocumentsCollection = (config: Config): CollectionConfig =
|
||||
maxDepth: 0,
|
||||
relationTo: [...config.collections.map((collectionConfig) => collectionConfig.slug)],
|
||||
},
|
||||
{
|
||||
name: 'editedAt',
|
||||
type: 'date',
|
||||
},
|
||||
{
|
||||
name: 'globalSlug',
|
||||
type: 'text',
|
||||
index: true,
|
||||
},
|
||||
{
|
||||
name: 'user',
|
||||
type: 'relationship',
|
||||
maxDepth: 1,
|
||||
relationTo: config.collections
|
||||
.filter((collectionConfig) => collectionConfig.auth)
|
||||
.map((collectionConfig) => collectionConfig.slug),
|
||||
|
||||
@@ -55,9 +55,8 @@ export const checkDocumentLockStatus = async ({
|
||||
|
||||
const finalLockErrorMessage = lockErrorMessage || defaultLockErrorMessage
|
||||
|
||||
const lockedDocumentResult: PaginatedDocs<JsonObject & TypeWithID> = await payload.find({
|
||||
const lockedDocumentResult: PaginatedDocs<JsonObject & TypeWithID> = await payload.db.find({
|
||||
collection: 'payload-locked-documents',
|
||||
depth: 1,
|
||||
limit: 1,
|
||||
pagination: false,
|
||||
req,
|
||||
@@ -68,8 +67,8 @@ export const checkDocumentLockStatus = async ({
|
||||
// If there's a locked document, check lock conditions
|
||||
const lockedDoc = lockedDocumentResult?.docs[0]
|
||||
if (lockedDoc) {
|
||||
const lastEditedAt = new Date(lockedDoc?.editedAt)
|
||||
const now = new Date()
|
||||
const lastEditedAt = new Date(lockedDoc?.updatedAt).getTime()
|
||||
const now = new Date().getTime()
|
||||
|
||||
const lockDuration =
|
||||
typeof lockDocumentsProp === 'object' ? lockDocumentsProp.duration : lockDurationDefault
|
||||
@@ -79,8 +78,8 @@ export const checkDocumentLockStatus = async ({
|
||||
|
||||
// document is locked by another user and the lock hasn't expired
|
||||
if (
|
||||
lockedDoc.user?.value?.id !== currentUserId &&
|
||||
now.getTime() - lastEditedAt.getTime() <= lockDurationInMilliseconds
|
||||
lockedDoc.user?.value !== currentUserId &&
|
||||
now - lastEditedAt <= lockDurationInMilliseconds
|
||||
) {
|
||||
throw new Locked(finalLockErrorMessage)
|
||||
}
|
||||
|
||||
@@ -193,7 +193,6 @@ 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({
|
||||
editedAt: new Date(),
|
||||
user: { relationTo: user?.collection, value: user?.id },
|
||||
}),
|
||||
headers: {
|
||||
|
||||
@@ -265,9 +265,7 @@ export const buildFormState = async ({
|
||||
await req.payload.db.updateOne({
|
||||
id: lockedDocument.docs[0].id,
|
||||
collection: 'payload-locked-documents',
|
||||
data: {
|
||||
editedAt: new Date().toISOString(),
|
||||
},
|
||||
data: {},
|
||||
req,
|
||||
})
|
||||
}
|
||||
@@ -284,7 +282,6 @@ export const buildFormState = async ({
|
||||
value: id,
|
||||
}
|
||||
: undefined,
|
||||
editedAt: new Date().toISOString(),
|
||||
globalSlug: globalSlug ? globalSlug : undefined,
|
||||
user: {
|
||||
relationTo: [req.user.collection],
|
||||
|
||||
Reference in New Issue
Block a user