chore: split getLatestEntityVersion to separate globals and collections
This commit is contained in:
@@ -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<T extends TypeWithID = any>(args: Arguments): Prom
|
||||
// fetch previousDoc
|
||||
// /////////////////////////////////////
|
||||
|
||||
const prevDocWithLocales = await getLatestEntityVersion({
|
||||
const prevDocWithLocales = await getLatestCollectionVersion({
|
||||
payload,
|
||||
id: parentDocID,
|
||||
query,
|
||||
|
||||
@@ -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<TSlug extends keyof GeneratedTypes['collections']>(
|
||||
|
||||
const query = await Model.buildQuery(queryToBuild, locale);
|
||||
|
||||
const doc = await getLatestEntityVersion({
|
||||
const doc = await getLatestCollectionVersion({
|
||||
payload,
|
||||
Model,
|
||||
config: collectionConfig,
|
||||
|
||||
@@ -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<T extends { [field: string | number | symbol]: unknown }> = {
|
||||
globalConfig: SanitizedGlobalConfig
|
||||
@@ -83,17 +83,14 @@ async function update<TSlug extends keyof GeneratedTypes['globals']>(
|
||||
// 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<string, unknown> = {};
|
||||
|
||||
if (global) {
|
||||
|
||||
@@ -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<string, unknown>
|
||||
lean?: boolean
|
||||
} & ({
|
||||
entityType: 'global'
|
||||
id?: never
|
||||
Model: GlobalModel
|
||||
config: SanitizedGlobalConfig
|
||||
} | {
|
||||
entityType?: 'collection'
|
||||
id: string | number
|
||||
Model: CollectionModel
|
||||
config: SanitizedCollectionConfig
|
||||
})
|
||||
}
|
||||
|
||||
export const getLatestEntityVersion = async <T extends TypeWithID = any>({
|
||||
export const getLatestCollectionVersion = async <T extends TypeWithID = any>({
|
||||
payload,
|
||||
entityType = 'collection',
|
||||
config,
|
||||
Model,
|
||||
query,
|
||||
@@ -39,23 +30,15 @@ export const getLatestEntityVersion = async <T extends TypeWithID = any>({
|
||||
});
|
||||
}
|
||||
|
||||
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,
|
||||
|
||||
47
src/versions/getLatestGlobalVersion.ts
Normal file
47
src/versions/getLatestGlobalVersion.ts
Normal file
@@ -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<string, unknown>
|
||||
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,
|
||||
};
|
||||
};
|
||||
Reference in New Issue
Block a user