Some checks failed
Build & Deploy / 🔍 Prepare (push) Successful in 20s
Build & Deploy / 🧪 QA (push) Failing after 34s
Build & Deploy / 🏗️ Build (push) Has started running
Build & Deploy / 🚀 Deploy (push) Has been cancelled
Build & Deploy / 🧪 Smoke Test (push) Has been cancelled
Build & Deploy / ⚡ Lighthouse (push) Has been cancelled
Build & Deploy / 🔔 Notify (push) Has been cancelled
79 lines
2.0 KiB
Plaintext
79 lines
2.0 KiB
Plaintext
import { sanitizeID } from '@payloadcms/ui/shared';
|
|
import { extractID } from 'payload/shared';
|
|
export const getIsLocked = async ({
|
|
id,
|
|
collectionConfig,
|
|
globalConfig,
|
|
isEditing,
|
|
req
|
|
}) => {
|
|
const entityConfig = collectionConfig || globalConfig;
|
|
const entityHasLockingEnabled = entityConfig?.lockDocuments !== undefined ? entityConfig?.lockDocuments : true;
|
|
// Check if the locked-documents collection exists
|
|
if (!req.payload.collections?.['payload-locked-documents']) {
|
|
// If the collection doesn't exist, locking is not available
|
|
return {
|
|
isLocked: false
|
|
};
|
|
}
|
|
if (!entityHasLockingEnabled || !isEditing) {
|
|
return {
|
|
isLocked: false
|
|
};
|
|
}
|
|
const where = {};
|
|
const lockDurationDefault = 300 // Default 5 minutes in seconds
|
|
;
|
|
const lockDuration = typeof entityConfig.lockDocuments === 'object' ? entityConfig.lockDocuments.duration : lockDurationDefault;
|
|
const lockDurationInMilliseconds = lockDuration * 1000;
|
|
const now = new Date().getTime();
|
|
if (globalConfig) {
|
|
where.and = [{
|
|
globalSlug: {
|
|
equals: globalConfig.slug
|
|
}
|
|
}, {
|
|
updatedAt: {
|
|
greater_than: new Date(now - lockDurationInMilliseconds)
|
|
}
|
|
}];
|
|
} else {
|
|
where.and = [{
|
|
'document.value': {
|
|
equals: sanitizeID(id)
|
|
}
|
|
}, {
|
|
'document.relationTo': {
|
|
equals: collectionConfig.slug
|
|
}
|
|
}, {
|
|
updatedAt: {
|
|
greater_than: new Date(now - lockDurationInMilliseconds)
|
|
}
|
|
}];
|
|
}
|
|
const {
|
|
docs
|
|
} = await req.payload.find({
|
|
collection: 'payload-locked-documents',
|
|
depth: 1,
|
|
overrideAccess: false,
|
|
req,
|
|
where
|
|
});
|
|
if (docs.length > 0) {
|
|
const currentEditor = docs[0].user?.value;
|
|
const lastUpdateTime = new Date(docs[0].updatedAt).getTime();
|
|
if (extractID(currentEditor) !== req.user.id) {
|
|
return {
|
|
currentEditor,
|
|
isLocked: true,
|
|
lastUpdateTime
|
|
};
|
|
}
|
|
}
|
|
return {
|
|
isLocked: false
|
|
};
|
|
};
|
|
//# sourceMappingURL=getIsLocked.js.map |