Merge pull request #2034 from payloadcms/hotfix/max-versions
fix: max versions incorrectly sorting and removing
This commit is contained in:
@@ -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,
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
@@ -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,
|
||||
|
||||
Reference in New Issue
Block a user