@@ -2,7 +2,8 @@ import type { BeforeChangeHook } from 'node_modules/payload/dist/collections/con
22
33import type { Duration } from './../../utils/toMS.js'
44
5- import { defaultAutoDeleteLog , defaultCollectionValues } from './../../Constant/Constant.js'
5+ import { cleanupStrategiesDefaultValues } from './../../Constant/automation.js'
6+ import { defaultCollectionValues } from './../../Constant/Constant.js'
67import ms from './../../utils/toMS.js'
78
89type AutoLogCleanerProps = {
@@ -17,30 +18,32 @@ export const autoLogCleaner: BeforeChangeHook<AutoLogCleanerProps> = async ({
1718 req,
1819} ) => {
1920 try {
20- const millisecondsAgo = new Date ( Date . now ( ) - ms ( data . olderThan || defaultAutoDeleteLog ) )
21- const oldLogs = await req . payload . find ( {
22- collection : context . pluginOptions . collection ?. slug
23- ? context . pluginOptions . collection ?. slug
24- : defaultCollectionValues . slug ,
25- limit : context . pluginOptions . automation ?. logCleanup ?. strategy ?. amount || 100 ,
21+ const millisecondsAgo = new Date (
22+ Date . now ( ) - ms ( data . olderThan || cleanupStrategiesDefaultValues . manual . olderThan ) ,
23+ )
24+ const collectionSlug = context . pluginOptions . collection ?. slug ?? defaultCollectionValues . slug
25+ const limit = context . pluginOptions . automation ?. logCleanup ?. strategy ?. amount ?? 100
26+
27+ const oldLogsToDelete = await req . payload . find ( {
28+ collection : collectionSlug ,
29+ limit,
2630 where : {
2731 createdAt : {
2832 less_than : millisecondsAgo . toISOString ( ) ,
2933 } ,
3034 } ,
3135 } )
3236
33- if ( oldLogs . docs . length > 0 ) {
34- const deletePromises = oldLogs . docs . map ( ( log ) =>
35- req . payload . delete ( {
36- id : log . id ,
37- collection : context . pluginOptions . collection ?. slug
38- ? context . pluginOptions . collection ?. slug
39- : defaultCollectionValues . slug ,
40- } ) ,
41- )
42-
43- await Promise . all ( deletePromises )
37+ const ids = oldLogsToDelete . docs . map ( ( doc ) => doc . id )
38+ if ( ids . length > 0 ) {
39+ await req . payload . delete ( {
40+ collection : collectionSlug ,
41+ where : {
42+ id : {
43+ in : ids ,
44+ } ,
45+ } ,
46+ } )
4447 }
4548 } catch ( err ) {
4649 console . error ( 'Error cleaning old logs:' , err )
0 commit comments