Skip to content

Commit 83bd150

Browse files
committed
fix(core): fix not receiving custom slug in plugin core
1 parent 4bc398e commit 83bd150

5 files changed

Lines changed: 22 additions & 8 deletions

File tree

src/collections/auditor.ts

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import type { hookTypes } from 'src/pluginUtils/configHelpers.js'
44
import type { AuditHookOperationType } from '../types/pluginOptions.js'
55

66
import { defaultCollectionValues } from '../Constant/Constant.js'
7-
import { autoLogCleaner } from './hooks/beforeChange.js'
87

98
export 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
}

src/collections/hooks/beforeChange.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ type AutoLogCleanerProps = {
1111
}
1212

1313
export 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

src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff 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]

src/pluginUtils/configHelpers.ts

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff 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
// ...

src/types/types.d.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
import 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

55
declare 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
}

0 commit comments

Comments
 (0)