fix: #1184
This commit is contained in:
@@ -171,6 +171,7 @@ async function find<T extends TypeWithID = any>(incomingArgs: Arguments): Promis
|
|||||||
accessResult,
|
accessResult,
|
||||||
payload,
|
payload,
|
||||||
entity: collectionConfig,
|
entity: collectionConfig,
|
||||||
|
entityType: 'collection',
|
||||||
doc,
|
doc,
|
||||||
locale,
|
locale,
|
||||||
}))),
|
}))),
|
||||||
|
|||||||
@@ -127,6 +127,7 @@ async function findByID<T extends TypeWithID = any>(incomingArgs: Arguments): Pr
|
|||||||
result = await replaceWithDraftIfAvailable({
|
result = await replaceWithDraftIfAvailable({
|
||||||
payload,
|
payload,
|
||||||
entity: collectionConfig,
|
entity: collectionConfig,
|
||||||
|
entityType: 'collection',
|
||||||
doc: result,
|
doc: result,
|
||||||
accessResult,
|
accessResult,
|
||||||
locale,
|
locale,
|
||||||
|
|||||||
@@ -89,6 +89,7 @@ async function findOne<T extends TypeWithID = any>(args: Args): Promise<T> {
|
|||||||
doc = await replaceWithDraftIfAvailable({
|
doc = await replaceWithDraftIfAvailable({
|
||||||
payload,
|
payload,
|
||||||
entity: globalConfig,
|
entity: globalConfig,
|
||||||
|
entityType: 'global',
|
||||||
doc,
|
doc,
|
||||||
locale,
|
locale,
|
||||||
accessResult,
|
accessResult,
|
||||||
|
|||||||
@@ -11,6 +11,7 @@ import { SanitizedGlobalConfig } from '../../globals/config/types';
|
|||||||
type Arguments<T> = {
|
type Arguments<T> = {
|
||||||
payload: Payload
|
payload: Payload
|
||||||
entity: SanitizedCollectionConfig | SanitizedGlobalConfig
|
entity: SanitizedCollectionConfig | SanitizedGlobalConfig
|
||||||
|
entityType: 'collection' | 'global'
|
||||||
doc: T
|
doc: T
|
||||||
locale: string
|
locale: string
|
||||||
accessResult: AccessResult
|
accessResult: AccessResult
|
||||||
@@ -19,36 +20,43 @@ type Arguments<T> = {
|
|||||||
const replaceWithDraftIfAvailable = async <T extends TypeWithID>({
|
const replaceWithDraftIfAvailable = async <T extends TypeWithID>({
|
||||||
payload,
|
payload,
|
||||||
entity,
|
entity,
|
||||||
|
entityType,
|
||||||
doc,
|
doc,
|
||||||
locale,
|
locale,
|
||||||
accessResult,
|
accessResult,
|
||||||
}: Arguments<T>): Promise<T> => {
|
}: Arguments<T>): Promise<T> => {
|
||||||
if (docHasTimestamps(doc)) {
|
|
||||||
const VersionModel = payload.versions[entity.slug] as CollectionModel;
|
const VersionModel = payload.versions[entity.slug] as CollectionModel;
|
||||||
|
|
||||||
let useEstimatedCount = false;
|
let useEstimatedCount = false;
|
||||||
|
|
||||||
const queryToBuild: { where: Where } = {
|
const queryToBuild: { where: Where } = {
|
||||||
where: {
|
where: {
|
||||||
and: [
|
and: [
|
||||||
{
|
|
||||||
parent: {
|
|
||||||
equals: doc.id,
|
|
||||||
},
|
|
||||||
},
|
|
||||||
{
|
{
|
||||||
'version._status': {
|
'version._status': {
|
||||||
equals: 'draft',
|
equals: 'draft',
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
{
|
|
||||||
updatedAt: {
|
|
||||||
greater_than: doc.updatedAt,
|
|
||||||
},
|
|
||||||
},
|
|
||||||
],
|
],
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
|
if (entityType === 'collection') {
|
||||||
|
queryToBuild.where.and.push({
|
||||||
|
parent: {
|
||||||
|
equals: doc.id,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
if (docHasTimestamps(doc)) {
|
||||||
|
queryToBuild.where.and.push({
|
||||||
|
updatedAt: {
|
||||||
|
greater_than: doc.updatedAt,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
if (hasWhereAccessResult(accessResult)) {
|
if (hasWhereAccessResult(accessResult)) {
|
||||||
const versionAccessResult = appendVersionToQueryKey(accessResult);
|
const versionAccessResult = appendVersionToQueryKey(accessResult);
|
||||||
queryToBuild.where.and.push(versionAccessResult);
|
queryToBuild.where.and.push(versionAccessResult);
|
||||||
@@ -80,9 +88,6 @@ const replaceWithDraftIfAvailable = async <T extends TypeWithID>({
|
|||||||
createdAt: draft.createdAt,
|
createdAt: draft.createdAt,
|
||||||
updatedAt: draft.updatedAt,
|
updatedAt: draft.updatedAt,
|
||||||
};
|
};
|
||||||
}
|
|
||||||
|
|
||||||
return doc;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
export default replaceWithDraftIfAvailable;
|
export default replaceWithDraftIfAvailable;
|
||||||
|
|||||||
@@ -3,6 +3,7 @@ import AutosavePosts from './collections/Autosave';
|
|||||||
import DraftPosts from './collections/Drafts';
|
import DraftPosts from './collections/Drafts';
|
||||||
import AutosaveGlobal from './globals/Autosave';
|
import AutosaveGlobal from './globals/Autosave';
|
||||||
import { devUser } from '../credentials';
|
import { devUser } from '../credentials';
|
||||||
|
import DraftGlobal from './globals/Draft';
|
||||||
|
|
||||||
export default buildConfig({
|
export default buildConfig({
|
||||||
collections: [
|
collections: [
|
||||||
@@ -11,6 +12,7 @@ export default buildConfig({
|
|||||||
],
|
],
|
||||||
globals: [
|
globals: [
|
||||||
AutosaveGlobal,
|
AutosaveGlobal,
|
||||||
|
DraftGlobal,
|
||||||
],
|
],
|
||||||
localization: {
|
localization: {
|
||||||
locales: ['en', 'es'],
|
locales: ['en', 'es'],
|
||||||
|
|||||||
30
test/versions/globals/Draft.ts
Normal file
30
test/versions/globals/Draft.ts
Normal file
@@ -0,0 +1,30 @@
|
|||||||
|
import { GlobalConfig } from '../../../src/globals/config/types';
|
||||||
|
|
||||||
|
const DraftGlobal: GlobalConfig = {
|
||||||
|
slug: 'draft-global',
|
||||||
|
label: 'Draft Global',
|
||||||
|
versions: {
|
||||||
|
max: 20,
|
||||||
|
drafts: true,
|
||||||
|
},
|
||||||
|
access: {
|
||||||
|
read: ({ draft, req: { user } }) => {
|
||||||
|
// To read a draft of this global, you need to be authenticated
|
||||||
|
if (draft) {
|
||||||
|
return Boolean(user);
|
||||||
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
|
},
|
||||||
|
},
|
||||||
|
fields: [
|
||||||
|
{
|
||||||
|
name: 'title',
|
||||||
|
type: 'text',
|
||||||
|
required: true,
|
||||||
|
localized: true,
|
||||||
|
},
|
||||||
|
],
|
||||||
|
};
|
||||||
|
|
||||||
|
export default DraftGlobal;
|
||||||
Reference in New Issue
Block a user