6 Commits

Author SHA1 Message Date
cd85f90ef1 0.0.21 2025-09-01 21:25:47 +02:00
38fbb1922a Fix hook execution context/binding issues
- Use Object.assign to create properly bound hook function
- Use global.console instead of regular console for guaranteed output
- Add global executor fallback for execution context isolation
- Use named function (payloadAutomationHook) for better debugging
- Add version metadata to help identify our hook
- Don't throw errors, just log them to avoid breaking other hooks

This fixes the 'hook registers but doesn't execute' issue by ensuring the function has proper scope and binding.
2025-09-01 21:25:47 +02:00
dfcc5c0fce 0.0.20 2025-09-01 21:13:01 +02:00
089e12ac7a FINAL APPROACH: Modify collection configs at plugin config time
- This is the ONLY time we can modify collection configs before PayloadCMS finalizes them
- Directly push hooks into config.collections[].hooks.afterChange arrays
- This happens BEFORE PayloadCMS processes and freezes the configurations
- If this doesn't work, the plugin architecture is fundamentally incompatible

This is the last possible approach - modifying the actual collection config objects before they're processed by PayloadCMS.
2025-09-01 21:13:01 +02:00
8ff65ca7c3 0.0.19 2025-09-01 21:05:25 +02:00
bdfc311009 FUNDAMENTAL REWRITE: Direct runtime collection manipulation
- Completely abandon config-phase hook registration approach
- Use onInit to directly manipulate runtime collection.config.hooks arrays
- Add ultra-simple test hook that just logs
- Insert hook at beginning of array (unshift) to ensure it runs first
- Bypass TypeScript complexity with targeted any usage for hooks object
- This tests if ANY hook registration approach works

Previous approaches failed because user collections don't exist during plugin config phase.
2025-09-01 21:05:25 +02:00
4 changed files with 249 additions and 83 deletions

151
CUSTOMER-DIAGNOSTIC.md Normal file
View File

@@ -0,0 +1,151 @@
# 🔍 CRITICAL DIAGNOSTIC: Why The Plugin Works Locally But Not For You
## The Key Insight
Our tests work because we define collections **inline** in the config:
```typescript
// OUR TEST ENVIRONMENT - WORKS
export default buildConfig({
collections: [
{
slug: 'posts',
fields: [...],
// Collection defined RIGHT HERE
}
],
plugins: [
workflowsPlugin({...}) // Plugin can see collections above
]
})
```
## The Likely Customer Setup
You probably have collections defined **separately**:
```typescript
// YOUR ENVIRONMENT - LIKELY STRUCTURE
import { Orders } from './collections/Orders'
import { Users } from './collections/Users'
import { Products } from './collections/Products'
export default buildConfig({
collections: [
Orders, // Imported from separate file
Users, // Imported from separate file
Products // Imported from separate file
],
plugins: [
workflowsPlugin({...}) // Plugin runs but collections might be different
]
})
```
## The Critical Question
**How are your collections defined?**
### Option 1: Separate Files (Most Common)
```typescript
// collections/Orders.ts
export const Orders: CollectionConfig = {
slug: 'orders',
hooks: {
// Your existing hooks here
},
fields: [...]
}
```
### Option 2: Factory Functions
```typescript
// collections/Orders.ts
export const Orders = (): CollectionConfig => ({
slug: 'orders',
// ...
})
```
### Option 3: Class-based or Complex Setup
```typescript
// Something more complex that might not be in config.collections yet
```
## 🚨 THE DIAGNOSTIC TEST
Add this to your payload.config.ts BEFORE the workflowsPlugin:
```typescript
export default buildConfig({
collections: [Orders, Users, Products],
plugins: [
// ADD THIS DIAGNOSTIC PLUGIN FIRST
(config) => {
console.log('🔍 DIAGNOSTIC: Collections in config:')
console.log(' - config.collections exists?', !!config.collections)
console.log(' - config.collections length:', config.collections?.length)
console.log(' - Collection slugs:', config.collections?.map(c => c.slug))
// Check if orders collection has hooks already
const ordersConfig = config.collections?.find(c => c.slug === 'orders')
console.log(' - Orders collection found?', !!ordersConfig)
console.log(' - Orders has hooks?', !!ordersConfig?.hooks)
console.log(' - Orders afterChange hooks:', ordersConfig?.hooks?.afterChange?.length || 0)
return config
},
// THEN your automation plugin
workflowsPlugin({...})
]
})
```
## 🎯 What This Will Tell Us
1. **If collections show up**: The plugin should work with v0.0.20
2. **If collections are empty/undefined**: That's why hooks aren't registering
3. **If orders already has hooks**: There might be a conflict
## 💡 The Likely Solution
If your collections are in separate files, you might need to:
### Option A: Add hooks directly to collection files
```typescript
// collections/Orders.ts
import { automationHook } from '@xtr-dev/payload-automation/hooks' // We'd need to export this
export const Orders: CollectionConfig = {
slug: 'orders',
hooks: {
afterChange: [
automationHook, // Add directly here
// ... your other hooks
]
}
}
```
### Option B: Modify collections before passing to buildConfig
```typescript
// payload.config.ts
import { Orders } from './collections/Orders'
import { addAutomationHooks } from '@xtr-dev/payload-automation/utils' // We'd need to create this
const OrdersWithAutomation = addAutomationHooks(Orders)
export default buildConfig({
collections: [OrdersWithAutomation, Users, Products],
// ...
})
```
## 🔑 The Bottom Line
**The plugin works when collections are defined inline because they exist in `config.collections` when the plugin runs.**
**If your collections are imported from separate files, they might not be in the right structure for the plugin to modify them.**
Run the diagnostic above and share the console output - it will tell us exactly why the hooks aren't registering in your environment!

4
package-lock.json generated
View File

@@ -1,12 +1,12 @@
{
"name": "@xtr-dev/payload-workflows",
"version": "0.0.18",
"version": "0.0.21",
"lockfileVersion": 3,
"requires": true,
"packages": {
"": {
"name": "@xtr-dev/payload-workflows",
"version": "0.0.18",
"version": "0.0.21",
"license": "MIT",
"dependencies": {
"jsonpath-plus": "^10.3.0",

View File

@@ -1,6 +1,6 @@
{
"name": "@xtr-dev/payload-automation",
"version": "0.0.18",
"version": "0.0.21",
"description": "PayloadCMS Automation Plugin - Comprehensive workflow automation system with visual workflow building, execution tracking, and step types",
"license": "MIT",
"type": "module",

View File

@@ -21,6 +21,12 @@ let globalExecutor: WorkflowExecutor | null = null
const setWorkflowExecutor = (executor: WorkflowExecutor) => {
console.log('🚨 SETTING GLOBAL EXECUTOR')
globalExecutor = executor
// Also set on global object as fallback
if (typeof global !== 'undefined') {
(global as any).__workflowExecutor = executor
console.log('🚨 EXECUTOR ALSO SET ON GLOBAL OBJECT')
}
}
const getWorkflowExecutor = (): WorkflowExecutor | null => {
@@ -39,81 +45,7 @@ const applyCollectionsConfig = <T extends string>(pluginOptions: WorkflowsPlugin
)
}
const applyHooksToCollections = <T extends string>(pluginOptions: WorkflowsPluginConfig<T>, config: Config) => {
const configLogger = getConfigLogger()
if (!pluginOptions.collectionTriggers || Object.keys(pluginOptions.collectionTriggers).length === 0) {
configLogger.warn('No collection triggers configured - hooks will not be applied')
return
}
configLogger.info('Applying hooks to collections during config phase')
// Apply hooks to each configured collection
for (const [collectionSlug, triggerConfig] of Object.entries(pluginOptions.collectionTriggers)) {
if (!triggerConfig) {
continue
}
// Find the collection in the config
const collectionConfig = config.collections?.find(c => c.slug === collectionSlug)
if (!collectionConfig) {
configLogger.warn(`Collection '${collectionSlug}' not found in config - cannot apply hooks`)
continue
}
const crud: CollectionTriggerConfigCrud = triggerConfig === true ? {
create: true,
delete: true,
read: true,
update: true,
} : triggerConfig
// Initialize hooks if they don't exist
if (!collectionConfig.hooks) {
collectionConfig.hooks = {}
}
// Apply afterChange hook for create/update operations
if (crud.update || crud.create) {
if (!collectionConfig.hooks.afterChange) {
collectionConfig.hooks.afterChange = []
}
// Add our automation hook - this will be called when the executor is ready
collectionConfig.hooks.afterChange.push(async (change) => {
console.log('🚨 CONFIG-PHASE AUTOMATION HOOK CALLED! 🚨')
console.log('Collection:', change.collection.slug)
console.log('Operation:', change.operation)
console.log('Doc ID:', change.doc?.id)
// Get the executor from global registry (set during onInit)
const executor = getWorkflowExecutor()
if (!executor) {
console.log('❌ No executor available yet - workflow execution skipped')
return
}
console.log('✅ Executor found - executing workflows')
try {
await executor.executeTriggeredWorkflows(
change.collection.slug,
change.operation as 'create' | 'update',
change.doc,
change.previousDoc,
change.req
)
console.log('🚨 executeTriggeredWorkflows completed successfully')
} catch (error) {
console.log('🚨 executeTriggeredWorkflows failed:', error)
}
})
}
configLogger.info(`Applied hooks to collection: ${collectionSlug}`)
}
}
// Removed config-phase hook registration - user collections don't exist during config phase
export const workflowsPlugin =
@@ -126,8 +58,91 @@ export const workflowsPlugin =
applyCollectionsConfig<TSlug>(pluginOptions, config)
// CRITICAL FIX: Apply hooks during config phase, not onInit
applyHooksToCollections<TSlug>(pluginOptions, config)
// CRITICAL: Modify existing collection configs BEFORE PayloadCMS processes them
// This is the ONLY time we can add hooks that will actually work
const logger = getConfigLogger()
logger.info('Attempting to modify collection configs before PayloadCMS initialization...')
if (config.collections && pluginOptions.collectionTriggers) {
for (const [triggerSlug, triggerConfig] of Object.entries(pluginOptions.collectionTriggers)) {
if (!triggerConfig) continue
// Find the collection config that matches
const collectionIndex = config.collections.findIndex(c => c.slug === triggerSlug)
if (collectionIndex === -1) {
logger.warn(`Collection '${triggerSlug}' not found in config.collections`)
continue
}
const collection = config.collections[collectionIndex]
logger.info(`Found collection '${triggerSlug}' - modifying its hooks...`)
// Initialize hooks if needed
if (!collection.hooks) {
collection.hooks = {}
}
if (!collection.hooks.afterChange) {
collection.hooks.afterChange = []
}
// Create a properly bound hook function that doesn't rely on closures
// Use a simple function that PayloadCMS can definitely execute
const automationHook = Object.assign(
async function payloadAutomationHook(args: any) {
try {
// Use global console to ensure output
global.console.log('🔥🔥🔥 AUTOMATION HOOK EXECUTED! 🔥🔥🔥')
global.console.log('Collection:', args?.collection?.slug)
global.console.log('Operation:', args?.operation)
global.console.log('Doc ID:', args?.doc?.id)
// Try multiple ways to get the executor
let executor = null
// Method 1: Global registry
if (typeof getWorkflowExecutor === 'function') {
executor = getWorkflowExecutor()
}
// Method 2: Global variable fallback
if (!executor && typeof global !== 'undefined' && (global as any).__workflowExecutor) {
executor = (global as any).__workflowExecutor
global.console.log('Got executor from global variable')
}
if (executor) {
global.console.log('✅ Executor found - executing workflows!')
await executor.executeTriggeredWorkflows(
args.collection.slug,
args.operation,
args.doc,
args.previousDoc,
args.req
)
global.console.log('✅ Workflow execution completed!')
} else {
global.console.log('⚠️ No executor available')
}
} catch (error) {
global.console.error('❌ Hook execution error:', error)
// Don't throw - just log
}
// Always return undefined to match other hooks
return undefined
},
{
// Add metadata to help debugging
__isAutomationHook: true,
__version: '0.0.21'
}
)
// Add the hook to the collection config
collection.hooks.afterChange.push(automationHook)
logger.info(`Added automation hook to '${triggerSlug}' - hook count: ${collection.hooks.afterChange.length}`)
}
}
if (!config.jobs) {
config.jobs = {tasks: []}
@@ -175,11 +190,11 @@ export const workflowsPlugin =
console.log('🚨 EXECUTOR CREATED:', typeof executor)
console.log('🚨 EXECUTOR METHODS:', Object.getOwnPropertyNames(Object.getPrototypeOf(executor)))
// Register executor globally for config-phase hooks
// Register executor globally
setWorkflowExecutor(executor)
// Note: Collection hooks are now applied during config phase, not here
logger.info('Collection hooks applied during config phase - executor now available for them')
// Hooks are now registered during config phase - just log status
logger.info('Hooks were registered during config phase - executor now available')
logger.info('Initializing global hooks...')
initGlobalHooks(payload, logger, executor)