diff --git a/src/collections/operations/restoreVersion.ts b/src/collections/operations/restoreVersion.ts index bc0127db6b..6b930ddf3b 100644 --- a/src/collections/operations/restoreVersion.ts +++ b/src/collections/operations/restoreVersion.ts @@ -143,9 +143,9 @@ async function restoreVersion(args: Arguments): Prom parent: parentDocID, version: rawVersion.version, autosave: false, - latest: true, createdAt: prevVersion.createdAt, updatedAt: new Date().toISOString(), + latest: payload.config.database.queryDrafts_2_0 ? true : undefined, }); // ///////////////////////////////////// diff --git a/src/versions/saveVersion.ts b/src/versions/saveVersion.ts index 156ca244a1..0da1b2f2b1 100644 --- a/src/versions/saveVersion.ts +++ b/src/versions/saveVersion.ts @@ -79,20 +79,26 @@ export const saveVersion = async ({ const data: Record = { autosave: Boolean(autosave), version: versionData, - latest: true, createdAt: doc?.createdAt ? new Date(doc.createdAt).toISOString() : now, updatedAt: draft ? now : new Date(doc.updatedAt).toISOString(), }; + if (payload.config.database.queryDrafts_2_0) { + data.latest = true; + } + if (collection) { data.parent = id; + } + result = await VersionModel.create(data); + + if (collection) { await VersionModel.updateMany({ $and: [ - // TODO: is this _id necessary? Is this when you publish from a version? { _id: { - $ne: doc._id, + $ne: result._id, }, }, { @@ -110,6 +116,11 @@ export const saveVersion = async ({ } else if (global) { await VersionModel.updateMany({ $and: [ + { + _id: { + $ne: result._id, + }, + }, { latest: { $eq: true, @@ -118,8 +129,6 @@ export const saveVersion = async ({ ], }, { $unset: { latest: 1 } }); } - - result = await VersionModel.create(data); } } catch (err) { let errorMessage: string;