chore: ensure not to update the version doc that was just created

This commit is contained in:
Jarrod Flesch
2023-09-05 16:04:12 -04:00
parent 3cee0be314
commit b649ad7bb5
2 changed files with 15 additions and 6 deletions

View File

@@ -143,9 +143,9 @@ async function restoreVersion<T extends TypeWithID = any>(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,
});
// /////////////////////////////////////

View File

@@ -79,20 +79,26 @@ export const saveVersion = async ({
const data: Record<string, unknown> = {
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;