diff --git a/src/collections/operations/restoreVersion.ts b/src/collections/operations/restoreVersion.ts index 8988a4f302..0bbc60bf61 100644 --- a/src/collections/operations/restoreVersion.ts +++ b/src/collections/operations/restoreVersion.ts @@ -9,7 +9,7 @@ import { Where } from '../../types'; import sanitizeInternalFields from '../../utilities/sanitizeInternalFields'; import { afterChange } from '../../fields/hooks/afterChange'; import { afterRead } from '../../fields/hooks/afterRead'; -import { getLatestEntityVersion } from '../../versions/getLatestCollectionVersion'; +import { getLatestCollectionVersion } from '../../versions/getLatestCollectionVersion'; export type Arguments = { collection: Collection @@ -100,7 +100,7 @@ async function restoreVersion(args: Arguments): Prom // fetch previousDoc // ///////////////////////////////////// - const prevDocWithLocales = await getLatestEntityVersion({ + const prevDocWithLocales = await getLatestCollectionVersion({ payload, id: parentDocID, query, diff --git a/src/collections/operations/update.ts b/src/collections/operations/update.ts index 8d50105016..fd6da5dd52 100644 --- a/src/collections/operations/update.ts +++ b/src/collections/operations/update.ts @@ -17,7 +17,7 @@ import { beforeValidate } from '../../fields/hooks/beforeValidate'; import { afterChange } from '../../fields/hooks/afterChange'; import { afterRead } from '../../fields/hooks/afterRead'; import { generateFileData } from '../../uploads/generateFileData'; -import { getLatestEntityVersion } from '../../versions/getLatestCollectionVersion'; +import { getLatestCollectionVersion } from '../../versions/getLatestCollectionVersion'; import { mapAsync } from '../../utilities/mapAsync'; import fileExists from '../../uploads/fileExists'; import { FileData } from '../../uploads/types'; @@ -119,7 +119,7 @@ async function update( const query = await Model.buildQuery(queryToBuild, locale); - const doc = await getLatestEntityVersion({ + const doc = await getLatestCollectionVersion({ payload, Model, config: collectionConfig, diff --git a/src/globals/operations/update.ts b/src/globals/operations/update.ts index efb9f71090..28545a3310 100644 --- a/src/globals/operations/update.ts +++ b/src/globals/operations/update.ts @@ -10,7 +10,7 @@ import { afterRead } from '../../fields/hooks/afterRead'; import { PayloadRequest } from '../../express/types'; import { saveVersion } from '../../versions/saveVersion'; import sanitizeInternalFields from '../../utilities/sanitizeInternalFields'; -import { getLatestEntityVersion } from '../../versions/getLatestCollectionVersion'; +import { getLatestGlobalVersion } from '../../versions/getLatestGlobalVersion'; type Args = { globalConfig: SanitizedGlobalConfig @@ -83,17 +83,14 @@ async function update( // 2. Retrieve document // ///////////////////////////////////// - const global = await getLatestEntityVersion({ + const { global, globalExists } = await getLatestGlobalVersion({ payload, Model, config: globalConfig, query, lean: true, - entityType: 'global', }); - const globalExists = global?._globalExists || false; - let globalJSON: Record = {}; if (global) { diff --git a/src/versions/getLatestCollectionVersion.ts b/src/versions/getLatestCollectionVersion.ts index ee6d6ceca2..e636b39947 100644 --- a/src/versions/getLatestCollectionVersion.ts +++ b/src/versions/getLatestCollectionVersion.ts @@ -1,27 +1,18 @@ -import { docHasTimestamps, Document } from '../types'; +import { docHasTimestamps } from '../types'; import { Payload } from '../payload'; import { CollectionModel, SanitizedCollectionConfig, TypeWithID } from '../collections/config/types'; -import { GlobalModel, SanitizedGlobalConfig } from '../globals/config/types'; type Args = { payload: Payload query: Record lean?: boolean -} & ({ - entityType: 'global' - id?: never - Model: GlobalModel - config: SanitizedGlobalConfig -} | { - entityType?: 'collection' id: string | number Model: CollectionModel config: SanitizedCollectionConfig -}) +} -export const getLatestEntityVersion = async ({ +export const getLatestCollectionVersion = async ({ payload, - entityType = 'collection', config, Model, query, @@ -39,23 +30,15 @@ export const getLatestEntityVersion = async ({ }); } - const doc = await (Model as any).findOne(query, {}, { lean }) as Document; + const doc = await Model.findOne(query, {}, { lean }); if (!latestVersion || (docHasTimestamps(doc) && latestVersion.updatedAt < doc.updatedAt)) { - if (entityType === 'collection') { - doc.id = doc._id; - return doc; - } - - return { - ...doc, - _globalExists: Boolean(doc), - }; + doc.id = doc._id; + return doc; } return { ...latestVersion.version, - ...(entityType === 'global' && { _globalExists: Boolean(doc) }), id, updatedAt: latestVersion.updatedAt, createdAt: latestVersion.createdAt, diff --git a/src/versions/getLatestGlobalVersion.ts b/src/versions/getLatestGlobalVersion.ts new file mode 100644 index 0000000000..e556ad5167 --- /dev/null +++ b/src/versions/getLatestGlobalVersion.ts @@ -0,0 +1,47 @@ +import { Payload } from '../payload'; +import { docHasTimestamps, Document } from '../types'; +import { GlobalModel, SanitizedGlobalConfig } from '../globals/config/types'; + +type Args = { + payload: Payload + query: Record + lean?: boolean + Model: GlobalModel + config: SanitizedGlobalConfig +} + +export const getLatestGlobalVersion = async ({ + payload, + config, + Model, + query, + lean = true, +}: Args): Promise<{global: Document, globalExists: boolean}> => { + let latestVersion; + + if (config.versions?.drafts) { + latestVersion = await payload.versions[config.slug].findOne({}, {}, { + sort: { updatedAt: 'desc' }, + lean, + }); + } + + const global = await (Model as any).findOne(query, {}, { lean }) as Document; + const globalExists = Boolean(global); + + if (!latestVersion || (docHasTimestamps(global) && latestVersion.updatedAt < global.updatedAt)) { + return { + global, + globalExists, + }; + } + + return { + global: { + ...latestVersion.version, + updatedAt: latestVersion.updatedAt, + createdAt: latestVersion.createdAt, + }, + globalExists, + }; +};