File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -4,7 +4,6 @@ import type { hookTypes } from 'src/pluginUtils/configHelpers.js'
44import type { AuditHookOperationType } from '../types/pluginOptions.js'
55
66import { defaultCollectionValues } from '../Constant/Constant.js'
7- import { autoLogCleaner } from './hooks/beforeChange.js'
87
98export type AuditorLog = {
109 action : AuditHookOperationType
@@ -71,9 +70,6 @@ const auditor: CollectionConfig = {
7170 required : true ,
7271 } ,
7372 ] ,
74- hooks : {
75- beforeChange : [ autoLogCleaner ] ,
76- } ,
7773 labels : defaultCollectionValues . labels ,
7874 timestamps : false ,
7975}
Original file line number Diff line number Diff line change @@ -11,6 +11,7 @@ type AutoLogCleanerProps = {
1111}
1212
1313export const autoLogCleaner : BeforeChangeHook < AutoLogCleanerProps > = async ( {
14+ context,
1415 data,
1516 operation,
1617 req,
@@ -21,7 +22,9 @@ export const autoLogCleaner: BeforeChangeHook<AutoLogCleanerProps> = async ({
2122 )
2223
2324 const oldLogs = await req . payload . find ( {
24- collection : defaultCollectionValues . slug ,
25+ collection : context . pluginOptions . collection ?. slug
26+ ? context . pluginOptions . collection ?. slug
27+ : defaultCollectionValues . slug ,
2528 limit : 1000 ,
2629 where : {
2730 createdAt : {
@@ -34,7 +37,9 @@ export const autoLogCleaner: BeforeChangeHook<AutoLogCleanerProps> = async ({
3437 const deletePromises = oldLogs . docs . map ( ( log ) =>
3538 req . payload . delete ( {
3639 id : log . id ,
37- collection : defaultCollectionValues . slug ,
40+ collection : context . pluginOptions . collection ?. slug
41+ ? context . pluginOptions . collection ?. slug
42+ : defaultCollectionValues . slug ,
3843 } ) ,
3944 )
4045
Original file line number Diff line number Diff line change @@ -32,6 +32,7 @@ export const auditorPlugin =
3232 // TODO: combine to attachCollectionConfig function
3333 const logsCollection = attachHooksToActivityLogsCollection (
3434 opts . autoDeleteInterval ?? defaultAutoDeleteLog ,
35+ opts ,
3536 )
3637 // TODO: combine to attachCollectionConfig function
3738 config . collections = [ ...( config . collections || [ ] ) , logsCollection ]
Original file line number Diff line number Diff line change @@ -48,14 +48,20 @@ export const hookTypes = [
4848// // }
4949// }
5050
51- export const attachHooksToActivityLogsCollection = ( autoDeleteInterval : Duration ) => {
51+ export const attachHooksToActivityLogsCollection = (
52+ autoDeleteInterval : Duration ,
53+ pluginOptions : PluginOptions ,
54+ ) => {
5255 auditor . hooks = {
5356 ...auditor . hooks ,
5457 beforeChange : [
5558 ...( auditor . hooks ?. beforeChange || [ ] ) ,
5659 ( args ) =>
5760 autoLogCleaner ( {
5861 ...args ,
62+ context : {
63+ pluginOptions,
64+ } ,
5965 data : {
6066 autoDeleteInterval,
6167 } ,
@@ -120,6 +126,10 @@ export const attachCollectionConfig = (
120126 }
121127
122128 // Attaching Log Builders
129+ // if (
130+ // pluginCollectionsConfig.trackCollections &&
131+ // pluginCollectionsConfig.trackCollections.length > 0
132+ // ) {
123133 userCollectionsConfig = ( userCollectionsConfig || [ ] ) . map ( ( collection ) => {
124134 const tracked = pluginCollectionsConfig . trackCollections . find (
125135 ( tc ) => tc . slug === collection . slug ,
@@ -149,6 +159,7 @@ export const attachCollectionConfig = (
149159
150160 return collection
151161 } )
162+ // }
152163
153164 // Attaching settings to plugin's internal collection hooks
154165 // ...
Original file line number Diff line number Diff line change 11import type { RequestContext as OriginalRequestContext } from 'payload'
22
3- import type { TrackedCollection } from './../types/pluginOptions.ts'
3+ import type { PluginOptions , TrackedCollection } from './../types/pluginOptions.ts'
44
55declare module 'payload' {
66 // Create a new interface that merges your additional fields with the original one
77 export interface RequestContext extends OriginalRequestContext {
8+ pluginOptions : PluginOptions
89 userHookConfig ?: TrackedCollection
910 // ...
1011 }
You can’t perform that action at this time.
0 commit comments