Merge branch 'master' into feat/1180-loading-ui-enhancements
This commit is contained in:
@@ -9,6 +9,7 @@ import { Where } from '../../types';
|
||||
import sanitizeInternalFields from '../../utilities/sanitizeInternalFields';
|
||||
import { afterChange } from '../../fields/hooks/afterChange';
|
||||
import { afterRead } from '../../fields/hooks/afterRead';
|
||||
import { getLatestCollectionVersion } from '../../versions/getLatestCollectionVersion';
|
||||
|
||||
export type Arguments = {
|
||||
collection: Collection
|
||||
@@ -23,6 +24,7 @@ export type Arguments = {
|
||||
|
||||
async function restoreVersion<T extends TypeWithID = any>(args: Arguments): Promise<T> {
|
||||
const {
|
||||
collection,
|
||||
collection: {
|
||||
Model,
|
||||
config: collectionConfig,
|
||||
@@ -99,10 +101,11 @@ async function restoreVersion<T extends TypeWithID = any>(args: Arguments): Prom
|
||||
// fetch previousDoc
|
||||
// /////////////////////////////////////
|
||||
|
||||
const previousDoc = await payload.findByID({
|
||||
collection: collectionConfig.slug,
|
||||
const prevDocWithLocales = await getLatestCollectionVersion({
|
||||
payload,
|
||||
collection,
|
||||
id: parentDocID,
|
||||
depth,
|
||||
query,
|
||||
});
|
||||
|
||||
// /////////////////////////////////////
|
||||
@@ -123,6 +126,22 @@ async function restoreVersion<T extends TypeWithID = any>(args: Arguments): Prom
|
||||
result = JSON.parse(result);
|
||||
result = sanitizeInternalFields(result);
|
||||
|
||||
// /////////////////////////////////////
|
||||
// Save `previousDoc` as a version after restoring
|
||||
// /////////////////////////////////////
|
||||
|
||||
const prevVersion = { ...prevDocWithLocales };
|
||||
|
||||
delete prevVersion.id;
|
||||
|
||||
await VersionModel.create({
|
||||
parent: parentDocID,
|
||||
version: prevVersion,
|
||||
autosave: false,
|
||||
createdAt: prevVersion.createdAt,
|
||||
updatedAt: new Date().toISOString(),
|
||||
});
|
||||
|
||||
// /////////////////////////////////////
|
||||
// afterRead - Fields
|
||||
// /////////////////////////////////////
|
||||
@@ -156,7 +175,7 @@ async function restoreVersion<T extends TypeWithID = any>(args: Arguments): Prom
|
||||
result = await afterChange({
|
||||
data: result,
|
||||
doc: result,
|
||||
previousDoc,
|
||||
previousDoc: prevDocWithLocales,
|
||||
entityConfig: collectionConfig,
|
||||
operation: 'update',
|
||||
req,
|
||||
@@ -172,7 +191,7 @@ async function restoreVersion<T extends TypeWithID = any>(args: Arguments): Prom
|
||||
result = await hook({
|
||||
doc: result,
|
||||
req,
|
||||
previousDoc,
|
||||
previousDoc: prevDocWithLocales,
|
||||
operation: 'update',
|
||||
}) || result;
|
||||
}, Promise.resolve());
|
||||
|
||||
@@ -6,4 +6,7 @@ export const defaultOptions: InitOptions = {
|
||||
debug: false,
|
||||
supportedLngs: Object.keys(translations),
|
||||
resources: translations,
|
||||
interpolation: {
|
||||
escapeValue: false,
|
||||
},
|
||||
};
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
import { Document } from '../types';
|
||||
import { Payload } from '../payload';
|
||||
import { Collection, TypeWithID } from '../collections/config/types';
|
||||
import sanitizeInternalFields from '../utilities/sanitizeInternalFields';
|
||||
|
||||
type Args = {
|
||||
payload: Payload
|
||||
@@ -32,10 +33,12 @@ export const getLatestCollectionVersion = async <T extends TypeWithID = any>({
|
||||
const collection = await Model.findOne(query, {}, { lean }) as Document;
|
||||
version = await version;
|
||||
if (!version || version.updatedAt < collection.updatedAt) {
|
||||
collection.id = collection._id;
|
||||
return collection;
|
||||
}
|
||||
return {
|
||||
...version.version,
|
||||
id,
|
||||
updatedAt: version.updatedAt,
|
||||
createdAt: version.createdAt,
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user