fix: max versions incorrectly sorting, causing incorrect versions to be held onto

This commit is contained in:
Jarrod Flesch
2023-02-07 11:29:25 -05:00
parent bdb2f8939a
commit 2e4f7ab35c
4 changed files with 45 additions and 8 deletions

View File

@@ -23,14 +23,12 @@ export const enforceMaxVersions = async ({
if (id) query.parent = id;
const oldestAllowedDoc = await Model.find(query).limit(1).skip(max).sort({ createdAt: -1 });
if (oldestAllowedDoc?.[0]?.createdAt) {
const deleteLessThan = oldestAllowedDoc[0].createdAt;
const oldestAllowedDoc = await Model.find(query).limit(1).skip(max).sort({ updatedAt: -1 });
if (oldestAllowedDoc?.[0]?.updatedAt) {
await Model.deleteMany({
createdAt: {
$lte: deleteLessThan,
updatedAt: {
$lte: oldestAllowedDoc[0].updatedAt,
},
});
}

View File

@@ -100,7 +100,7 @@ export const saveVersion = async ({
if (global && typeof global.versions.max === 'number') max = global.versions.max;
if (max > 0) {
enforceMaxVersions({
await enforceMaxVersions({
id,
payload,
Model: VersionModel,