mirror of
https://github.com/xtr-dev/payload-automation.git
synced 2025-12-11 01:03:23 +00:00
Compare commits
5 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 3c54f00f57 | |||
| cbb74206e9 | |||
| 41c4d8bdcb | |||
| 46c9f11534 | |||
| 08a4022a41 |
38
CHANGELOG.md
Normal file
38
CHANGELOG.md
Normal file
@@ -0,0 +1,38 @@
|
||||
# Changelog
|
||||
|
||||
All notable changes to the PayloadCMS Automation Plugin will be documented in this file.
|
||||
|
||||
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
|
||||
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
|
||||
|
||||
## [0.0.16] - 2025-09-01
|
||||
|
||||
### Fixed
|
||||
- **Critical Bug**: Removed problematic `hooksInitialized` flag that prevented proper hook registration in development environments
|
||||
- **Silent Failures**: Added comprehensive error logging with "AUTOMATION PLUGIN:" prefix for easier debugging
|
||||
- **Hook Execution**: Added try/catch blocks in hook execution to prevent silent failures and ensure workflow execution continues
|
||||
- **Development Mode**: Fixed issue where workflows would not execute even when properly configured due to hook registration being skipped
|
||||
|
||||
### Changed
|
||||
- Enhanced logging throughout the hook execution pipeline for better debugging visibility
|
||||
- Improved error handling to prevent workflow execution failures from breaking other hooks
|
||||
|
||||
### Migration Notes
|
||||
- No breaking changes - this is a critical bug fix release
|
||||
- Existing workflows should now execute properly after updating to this version
|
||||
- Enhanced logging will provide better visibility into workflow execution
|
||||
|
||||
## [0.0.15] - 2025-08-XX
|
||||
|
||||
### Changed
|
||||
- Updated workflow condition evaluation to use JSONPath expressions
|
||||
- Changed step configuration from `type`/`inputs` to `step`/`input`
|
||||
- Updated workflow collection schema for improved flexibility
|
||||
|
||||
## [0.0.14] - 2025-08-XX
|
||||
|
||||
### Added
|
||||
- Initial workflow automation functionality
|
||||
- Collection trigger support
|
||||
- Step execution engine
|
||||
- Basic workflow management
|
||||
116
CRITICAL-CONFIG-FIX.md
Normal file
116
CRITICAL-CONFIG-FIX.md
Normal file
@@ -0,0 +1,116 @@
|
||||
# 🚨 CRITICAL: Configuration Field Name Issue Found
|
||||
|
||||
## ❌ The Root Problem
|
||||
|
||||
Your plugin configuration is using the **wrong field name**!
|
||||
|
||||
### What You're Probably Using (WRONG):
|
||||
```javascript
|
||||
automationPlugin({
|
||||
collections: ['orders', 'users', 'products'], // ❌ Wrong field name
|
||||
steps: [...],
|
||||
})
|
||||
```
|
||||
|
||||
### What You MUST Use (CORRECT):
|
||||
```javascript
|
||||
automationPlugin({
|
||||
collectionTriggers: { // ✅ Correct field name
|
||||
'orders': true,
|
||||
'users': true,
|
||||
'products': true
|
||||
},
|
||||
steps: [...],
|
||||
})
|
||||
```
|
||||
|
||||
## 🔧 Immediate Fix Required
|
||||
|
||||
**Update your `payload.config.ts` file:**
|
||||
|
||||
```typescript
|
||||
import { automationPlugin } from '@xtr-dev/payload-automation'
|
||||
|
||||
export default buildConfig({
|
||||
// ... your other config
|
||||
|
||||
plugins: [
|
||||
automationPlugin({
|
||||
collectionTriggers: { // ← CHANGE THIS FIELD NAME
|
||||
orders: true, // Enable all hooks (create, read, update, delete)
|
||||
users: true,
|
||||
products: true
|
||||
},
|
||||
steps: [
|
||||
// ... your step configurations
|
||||
]
|
||||
})
|
||||
]
|
||||
})
|
||||
```
|
||||
|
||||
## 🎯 Why This Fixes Everything
|
||||
|
||||
1. **Hook Registration**: The plugin only registers hooks for collections listed in `collectionTriggers`
|
||||
2. **No Hooks = No Execution**: If `collectionTriggers` is empty/missing, no hooks get registered
|
||||
3. **Silent Failure**: The plugin logs "No collection triggers configured" and returns early
|
||||
|
||||
## 🔍 Advanced Configuration Options
|
||||
|
||||
You can also be more specific about which operations trigger workflows:
|
||||
|
||||
```javascript
|
||||
automationPlugin({
|
||||
collectionTriggers: {
|
||||
orders: {
|
||||
update: true, // Only trigger on updates
|
||||
create: true // Only trigger on creates
|
||||
// read and delete are false by default
|
||||
},
|
||||
users: true // Enable all operations
|
||||
},
|
||||
// ...
|
||||
})
|
||||
```
|
||||
|
||||
## ✅ Expected Results After Fix
|
||||
|
||||
Once you update your configuration and restart:
|
||||
|
||||
1. **Plugin logs will show**:
|
||||
```
|
||||
Starting collection hook registration
|
||||
Collection hooks registered successfully - collectionSlug: "orders"
|
||||
```
|
||||
|
||||
2. **Hook counts will be > 0**:
|
||||
```javascript
|
||||
payload.collections.orders.config.hooks.afterChange.length
|
||||
// Should return a number > 0
|
||||
```
|
||||
|
||||
3. **Workflow execution will work**:
|
||||
- "AUTOMATION PLUGIN: Collection hook triggered" messages
|
||||
- Workflow runs created in database
|
||||
- Jobs processing successfully
|
||||
|
||||
## 🆘 If Still Not Working
|
||||
|
||||
If you fix the configuration and it still doesn't work:
|
||||
|
||||
1. **Check your exact collection slugs**:
|
||||
```javascript
|
||||
console.log('Available collections:', Object.keys(payload.collections))
|
||||
```
|
||||
|
||||
2. **Verify case sensitivity**: Collection slugs are case-sensitive
|
||||
- Use exactly what appears in `Object.keys(payload.collections)`
|
||||
|
||||
3. **Restart completely**:
|
||||
- Stop dev server
|
||||
- Clear any caches
|
||||
- Restart with new configuration
|
||||
|
||||
---
|
||||
|
||||
**This configuration field name issue explains why no hooks were being registered, despite the plugin loading successfully. The v0.0.16 bug fix was valid, but this configuration issue was preventing hooks from being registered in the first place.**
|
||||
145
UPDATE-GUIDANCE-v0.0.16.md
Normal file
145
UPDATE-GUIDANCE-v0.0.16.md
Normal file
@@ -0,0 +1,145 @@
|
||||
# 🚨 Critical Update: PayloadCMS Automation Plugin v0.0.16
|
||||
|
||||
## ⚡ Immediate Action Required
|
||||
|
||||
A **critical bug** has been identified and fixed in v0.0.15 that prevented workflows from executing in development environments. **Please update immediately** to resolve workflow execution issues.
|
||||
|
||||
## 🔧 Quick Update Steps
|
||||
|
||||
### 1. Update the Package
|
||||
```bash
|
||||
npm update @xtr-dev/payload-automation
|
||||
# OR
|
||||
yarn upgrade @xtr-dev/payload-automation
|
||||
# OR
|
||||
pnpm update @xtr-dev/payload-automation
|
||||
```
|
||||
|
||||
### 2. Verify Version
|
||||
Check that you're now on v0.0.16:
|
||||
```bash
|
||||
npm list @xtr-dev/payload-automation
|
||||
```
|
||||
|
||||
### 3. Restart Your Development Server
|
||||
```bash
|
||||
# Stop your current dev server (Ctrl+C)
|
||||
# Then restart
|
||||
npm run dev
|
||||
# OR
|
||||
yarn dev
|
||||
# OR
|
||||
pnpm dev
|
||||
```
|
||||
|
||||
### 4. Test Your Workflows
|
||||
Your workflows should now execute properly! Look for these log messages:
|
||||
|
||||
```
|
||||
[payload-automation] Plugin initialized successfully - all hooks registered
|
||||
AUTOMATION PLUGIN: Collection hook triggered
|
||||
executeTriggeredWorkflows called
|
||||
Found workflows to check
|
||||
```
|
||||
|
||||
## 🐛 What Was Fixed
|
||||
|
||||
### Critical Bug: Hook Registration Failure
|
||||
- **Problem**: The `hooksInitialized` flag prevented proper hook registration in development mode
|
||||
- **Symptom**: Workflows would not execute even when correctly configured
|
||||
- **Fix**: Removed the problematic flag, ensuring hooks register on every initialization
|
||||
|
||||
### Enhanced Debugging
|
||||
- **Added**: Comprehensive logging with "AUTOMATION PLUGIN:" prefix
|
||||
- **Added**: Try/catch blocks to prevent silent failures
|
||||
- **Added**: Better error messages and stack traces
|
||||
|
||||
## 🔍 Troubleshooting
|
||||
|
||||
### If workflows still don't execute after updating:
|
||||
|
||||
1. **Check your workflow configuration** (should now use v0.0.15+ schema):
|
||||
```javascript
|
||||
// ✅ Correct v0.0.15+ schema
|
||||
{
|
||||
triggers: [{
|
||||
type: 'collection-trigger',
|
||||
collectionSlug: 'orders',
|
||||
operation: 'update',
|
||||
condition: '$.trigger.doc.status == "Paid"' // JSONPath format
|
||||
}],
|
||||
steps: [{
|
||||
step: 'uppercaseText', // 'step' not 'type'
|
||||
name: 'Process Order',
|
||||
input: { // 'input' not 'inputs'
|
||||
inputText: '$.trigger.doc.orderName has been paid!'
|
||||
}
|
||||
}]
|
||||
}
|
||||
```
|
||||
|
||||
2. **Verify plugin configuration** includes your collections:
|
||||
```javascript
|
||||
automationPlugin({
|
||||
collections: ['orders', 'users', 'products'], // Must include target collections
|
||||
// ... other config
|
||||
})
|
||||
```
|
||||
|
||||
3. **Check the logs** for "AUTOMATION PLUGIN:" messages during hook execution
|
||||
|
||||
4. **Ensure workflow status**: If using versioning, make sure workflows are "Published" not "Draft"
|
||||
|
||||
## 📋 Schema Changes Recap (from v0.0.14 → v0.0.15+)
|
||||
|
||||
If you haven't updated your workflows since v0.0.14, you'll also need to update the schema:
|
||||
|
||||
### Triggers
|
||||
```javascript
|
||||
// ❌ OLD v0.0.14
|
||||
conditions: [
|
||||
{ field: 'status', operator: 'equals', value: 'Paid' }
|
||||
]
|
||||
|
||||
// ✅ NEW v0.0.15+
|
||||
condition: '$.trigger.doc.status == "Paid"'
|
||||
```
|
||||
|
||||
### Steps
|
||||
```javascript
|
||||
// ❌ OLD v0.0.14
|
||||
{
|
||||
type: 'uppercaseText',
|
||||
inputs: { inputText: 'Hello' }
|
||||
}
|
||||
|
||||
// ✅ NEW v0.0.15+
|
||||
{
|
||||
step: 'uppercaseText',
|
||||
input: { inputText: 'Hello' }
|
||||
}
|
||||
```
|
||||
|
||||
## 🆘 Support
|
||||
|
||||
If you're still experiencing issues after updating to v0.0.16:
|
||||
|
||||
1. **Check console logs** for "AUTOMATION PLUGIN:" messages
|
||||
2. **Verify your workflow schema** matches v0.0.15+ format
|
||||
3. **Confirm plugin configuration** includes target collections
|
||||
4. **File an issue** at https://github.com/anthropics/claude-code/issues with:
|
||||
- PayloadCMS version
|
||||
- Complete console logs during workflow execution
|
||||
- Your workflow configuration (sanitized)
|
||||
|
||||
## ✅ Success Indicators
|
||||
|
||||
After updating, you should see:
|
||||
- ✅ Workflow runs created in `workflow-runs` collection
|
||||
- ✅ "AUTOMATION PLUGIN:" log messages during hook execution
|
||||
- ✅ Jobs appearing in `payload-jobs` collection
|
||||
- ✅ Workflow steps executing successfully
|
||||
|
||||
---
|
||||
|
||||
**This is a critical bug fix release - no breaking changes, just fixes the core functionality that wasn't working in v0.0.15.**
|
||||
153
diagnostic-hooks.js
Normal file
153
diagnostic-hooks.js
Normal file
@@ -0,0 +1,153 @@
|
||||
// Comprehensive diagnostic script for hook registration issues
|
||||
// This should be run in your actual PayloadCMS environment
|
||||
|
||||
console.log('🔍 === COMPREHENSIVE HOOK DIAGNOSTIC ===')
|
||||
|
||||
// STEP 1: Add this to your payload.config.ts or wherever you initialize PayloadCMS
|
||||
console.log(`
|
||||
📋 STEP 1: Add this diagnostic code to your PayloadCMS initialization:
|
||||
|
||||
// After PayloadCMS is initialized, run this diagnostic
|
||||
const diagnostic = async () => {
|
||||
console.log('🔍 === HOOK REGISTRATION DIAGNOSTIC ===')
|
||||
|
||||
// Check if orders collection exists
|
||||
const ordersCollection = payload.collections.orders
|
||||
if (!ordersCollection) {
|
||||
console.log('❌ CRITICAL: orders collection not found!')
|
||||
console.log('Available collections:', Object.keys(payload.collections))
|
||||
return
|
||||
}
|
||||
|
||||
console.log('✅ orders collection found')
|
||||
|
||||
// Check hooks on orders collection
|
||||
const hooks = ordersCollection.config.hooks
|
||||
console.log('Orders collection hooks:')
|
||||
console.log(' - afterChange:', hooks?.afterChange?.length || 0)
|
||||
console.log(' - afterRead:', hooks?.afterRead?.length || 0)
|
||||
console.log(' - afterDelete:', hooks?.afterDelete?.length || 0)
|
||||
|
||||
// If no hooks, something is wrong
|
||||
if (!hooks?.afterChange || hooks.afterChange.length === 0) {
|
||||
console.log('❌ CRITICAL: No afterChange hooks registered on orders collection!')
|
||||
console.log('This means the automation plugin hook registration failed.')
|
||||
return
|
||||
}
|
||||
|
||||
// Test hook execution by manually calling them
|
||||
console.log('\\n🧪 Testing hook execution manually...')
|
||||
|
||||
const testDoc = {
|
||||
id: 'test-' + Date.now(),
|
||||
orderName: 'Test Order',
|
||||
status: 'Paid',
|
||||
customerEmail: 'test@example.com',
|
||||
totalPrice: 1000
|
||||
}
|
||||
|
||||
const previousDoc = {
|
||||
...testDoc,
|
||||
status: 'Unpaid'
|
||||
}
|
||||
|
||||
// Create a mock change object
|
||||
const mockChange = {
|
||||
collection: { slug: 'orders' },
|
||||
operation: 'update',
|
||||
doc: testDoc,
|
||||
previousDoc: previousDoc,
|
||||
req: {} // minimal request object
|
||||
}
|
||||
|
||||
console.log('Calling hooks manually with test data...')
|
||||
|
||||
for (let i = 0; i < hooks.afterChange.length; i++) {
|
||||
try {
|
||||
console.log(\`Calling hook #\${i + 1}...\`)
|
||||
await hooks.afterChange[i](mockChange)
|
||||
console.log(\`✅ Hook #\${i + 1} completed\`)
|
||||
} catch (error) {
|
||||
console.log(\`❌ Hook #\${i + 1} failed:\`, error.message)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Run diagnostic after PayloadCMS is fully initialized
|
||||
setTimeout(diagnostic, 2000)
|
||||
`)
|
||||
|
||||
console.log(`
|
||||
📋 STEP 2: Check your plugin configuration
|
||||
|
||||
Verify your payload.config.ts includes the orders collection:
|
||||
|
||||
automationPlugin({
|
||||
collections: ['orders'], // ← MUST include 'orders'
|
||||
// ... other config
|
||||
})
|
||||
|
||||
NOT:
|
||||
automationPlugin({
|
||||
collections: ['users', 'products'], // ← Missing 'orders'!
|
||||
})
|
||||
`)
|
||||
|
||||
console.log(`
|
||||
📋 STEP 3: Alternative hook registration test
|
||||
|
||||
Add this to your order update code to manually verify hooks:
|
||||
|
||||
// Before updating the order
|
||||
console.log('🔍 Pre-update hook check:')
|
||||
const orderCollection = payload.collections.orders
|
||||
console.log('afterChange hooks count:', orderCollection.config.hooks?.afterChange?.length)
|
||||
|
||||
// Update the order
|
||||
const result = await payload.update({...})
|
||||
|
||||
// Check for workflow runs immediately
|
||||
const runs = await payload.find({ collection: 'workflow-runs' })
|
||||
console.log('Workflow runs after update:', runs.docs.length)
|
||||
`)
|
||||
|
||||
console.log(`
|
||||
📋 STEP 4: Most likely root causes
|
||||
|
||||
1. Plugin Configuration Issue:
|
||||
- 'orders' not included in collections array
|
||||
- Plugin disabled or not properly applied
|
||||
|
||||
2. Collection Name Mismatch:
|
||||
- Your collection might be named differently (e.g., 'order' vs 'orders')
|
||||
- Case sensitivity issue
|
||||
|
||||
3. Hook Registration Timing:
|
||||
- Plugin hooks registered before collection is fully initialized
|
||||
- Race condition in PayloadCMS startup
|
||||
|
||||
4. Development Environment Issue:
|
||||
- Hot reloading interfering with hook registration
|
||||
- Multiple PayloadCMS instances
|
||||
|
||||
5. Database/Collection Issue:
|
||||
- Collection doesn't exist in database
|
||||
- Collection configuration mismatch
|
||||
`)
|
||||
|
||||
console.log(`
|
||||
🆘 QUICK DEBUG COMMANDS
|
||||
|
||||
Run these in your browser console or Node.js environment:
|
||||
|
||||
// Check available collections
|
||||
Object.keys(payload.collections)
|
||||
|
||||
// Check specific collection hooks
|
||||
payload.collections.orders?.config?.hooks?.afterChange?.length
|
||||
|
||||
// Check plugin configuration (if accessible)
|
||||
// This depends on how your config is structured
|
||||
`)
|
||||
|
||||
process.exit(0)
|
||||
130
hook-verification-test.js
Normal file
130
hook-verification-test.js
Normal file
@@ -0,0 +1,130 @@
|
||||
// Hook verification test - run this in your PayloadCMS environment
|
||||
// This will help identify why registered hooks aren't executing
|
||||
|
||||
console.log('🔍 === HOOK VERIFICATION TEST ===')
|
||||
|
||||
console.log(`
|
||||
Add this code to your PayloadCMS environment after initialization:
|
||||
|
||||
const verifyHooks = async () => {
|
||||
console.log('🔍 === HOOK VERIFICATION DIAGNOSTIC ===')
|
||||
|
||||
// 1. Check if hooks are still registered
|
||||
const ordersCollection = payload.collections.orders
|
||||
const hooks = ordersCollection.config.hooks.afterChange || []
|
||||
|
||||
console.log('Hook count:', hooks.length)
|
||||
console.log('Hook types:', hooks.map((h, i) => \`#\${i}: \${typeof h}\`))
|
||||
|
||||
// 2. Check if hooks are actually functions
|
||||
for (let i = 0; i < hooks.length; i++) {
|
||||
const hook = hooks[i]
|
||||
console.log(\`Hook #\${i}:\`)
|
||||
console.log(\` - Type: \${typeof hook}\`)
|
||||
console.log(\` - Is Function: \${typeof hook === 'function'}\`)
|
||||
console.log(\` - Has Name: \${hook.name || 'anonymous'}\`)
|
||||
console.log(\` - String Preview: \${hook.toString().substring(0, 100)}...\`)
|
||||
}
|
||||
|
||||
// 3. Try to manually execute each hook
|
||||
console.log('\\n🧪 MANUAL HOOK EXECUTION TEST')
|
||||
|
||||
const mockChange = {
|
||||
collection: { slug: 'orders' },
|
||||
operation: 'update',
|
||||
doc: {
|
||||
id: 'test-' + Date.now(),
|
||||
orderName: 'Test Order',
|
||||
status: 'Paid',
|
||||
customerEmail: 'test@example.com'
|
||||
},
|
||||
previousDoc: {
|
||||
id: 'test-' + Date.now(),
|
||||
orderName: 'Test Order',
|
||||
status: 'Unpaid',
|
||||
customerEmail: 'test@example.com'
|
||||
},
|
||||
req: { user: null } // Minimal request object
|
||||
}
|
||||
|
||||
for (let i = 0; i < hooks.length; i++) {
|
||||
try {
|
||||
console.log(\`\\nTesting hook #\${i}...\`)
|
||||
console.log('About to call hook with mock data')
|
||||
|
||||
const result = await hooks[i](mockChange)
|
||||
|
||||
console.log(\`✅ Hook #\${i} executed successfully\`)
|
||||
console.log('Result:', result)
|
||||
|
||||
} catch (error) {
|
||||
console.log(\`❌ Hook #\${i} failed:\`)
|
||||
console.log('Error:', error.message)
|
||||
console.log('Stack:', error.stack)
|
||||
}
|
||||
}
|
||||
|
||||
// 4. Check if hooks are being called during real operations
|
||||
console.log('\\n🔍 REAL OPERATION TEST')
|
||||
console.log('Creating a test order to see if hooks fire...')
|
||||
|
||||
// Add a simple test hook to verify hook execution
|
||||
const testHook = async (change) => {
|
||||
console.log('🚨 TEST HOOK FIRED! 🚨')
|
||||
console.log('Collection:', change.collection.slug)
|
||||
console.log('Operation:', change.operation)
|
||||
}
|
||||
|
||||
// Add test hook at the beginning
|
||||
ordersCollection.config.hooks.afterChange.unshift(testHook)
|
||||
console.log('Added test hook at position 0')
|
||||
|
||||
try {
|
||||
const testOrder = await payload.create({
|
||||
collection: 'orders',
|
||||
data: {
|
||||
orderName: 'Hook Verification Test',
|
||||
status: 'Unpaid',
|
||||
customerEmail: 'hooktest@example.com',
|
||||
totalPrice: 1000,
|
||||
items: [{ name: 'Test Item', quantity: 1, price: 1000 }]
|
||||
}
|
||||
})
|
||||
|
||||
console.log('Test order created:', testOrder.id)
|
||||
|
||||
// Update the order to trigger hooks
|
||||
const updatedOrder = await payload.update({
|
||||
collection: 'orders',
|
||||
id: testOrder.id,
|
||||
data: { status: 'Paid' }
|
||||
})
|
||||
|
||||
console.log('Test order updated to:', updatedOrder.status)
|
||||
|
||||
} catch (error) {
|
||||
console.log('Error during test operation:', error.message)
|
||||
}
|
||||
}
|
||||
|
||||
// Run after PayloadCMS is initialized
|
||||
setTimeout(verifyHooks, 3000)
|
||||
`)
|
||||
|
||||
console.log(`
|
||||
🎯 Expected Results:
|
||||
|
||||
If you see "🚨 TEST HOOK FIRED! 🚨" but NOT the automation plugin messages:
|
||||
- Hook execution works, but the automation plugin hook has an issue
|
||||
- Likely problem: Hook function malformed or has runtime error
|
||||
|
||||
If you DON'T see "🚨 TEST HOOK FIRED! 🚨":
|
||||
- Hook execution is completely broken
|
||||
- PayloadCMS configuration or timing issue
|
||||
|
||||
If hooks execute manually but not during real operations:
|
||||
- Hook registration timing issue
|
||||
- PayloadCMS lifecycle problem
|
||||
`)
|
||||
|
||||
process.exit(0)
|
||||
4
package-lock.json
generated
4
package-lock.json
generated
@@ -1,12 +1,12 @@
|
||||
{
|
||||
"name": "@xtr-dev/payload-workflows",
|
||||
"version": "0.0.16",
|
||||
"version": "0.0.18",
|
||||
"lockfileVersion": 3,
|
||||
"requires": true,
|
||||
"packages": {
|
||||
"": {
|
||||
"name": "@xtr-dev/payload-workflows",
|
||||
"version": "0.0.16",
|
||||
"version": "0.0.18",
|
||||
"license": "MIT",
|
||||
"dependencies": {
|
||||
"jsonpath-plus": "^10.3.0",
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "@xtr-dev/payload-automation",
|
||||
"version": "0.0.16",
|
||||
"version": "0.0.18",
|
||||
"description": "PayloadCMS Automation Plugin - Comprehensive workflow automation system with visual workflow building, execution tracking, and step types",
|
||||
"license": "MIT",
|
||||
"type": "module",
|
||||
|
||||
@@ -787,6 +787,13 @@ export class WorkflowExecutor {
|
||||
previousDoc: unknown,
|
||||
req: PayloadRequest
|
||||
): Promise<void> {
|
||||
console.log('🚨 EXECUTOR: executeTriggeredWorkflows called!')
|
||||
console.log('🚨 EXECUTOR: Collection =', collection)
|
||||
console.log('🚨 EXECUTOR: Operation =', operation)
|
||||
console.log('🚨 EXECUTOR: Doc ID =', (doc as any)?.id)
|
||||
console.log('🚨 EXECUTOR: Has payload?', !!this.payload)
|
||||
console.log('🚨 EXECUTOR: Has logger?', !!this.logger)
|
||||
|
||||
this.logger.info({
|
||||
collection,
|
||||
operation,
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
import type {Config} from 'payload'
|
||||
|
||||
import type {WorkflowsPluginConfig} from "./config-types.js"
|
||||
import type {WorkflowsPluginConfig, CollectionTriggerConfigCrud} from "./config-types.js"
|
||||
|
||||
import {createWorkflowCollection} from '../collections/Workflow.js'
|
||||
import {WorkflowRunsCollection} from '../collections/WorkflowRuns.js'
|
||||
@@ -15,6 +15,18 @@ import {getConfigLogger, initializeLogger} from './logger.js'
|
||||
|
||||
export {getLogger} from './logger.js'
|
||||
|
||||
// Global executor registry for config-phase hooks
|
||||
let globalExecutor: WorkflowExecutor | null = null
|
||||
|
||||
const setWorkflowExecutor = (executor: WorkflowExecutor) => {
|
||||
console.log('🚨 SETTING GLOBAL EXECUTOR')
|
||||
globalExecutor = executor
|
||||
}
|
||||
|
||||
const getWorkflowExecutor = (): WorkflowExecutor | null => {
|
||||
return globalExecutor
|
||||
}
|
||||
|
||||
const applyCollectionsConfig = <T extends string>(pluginOptions: WorkflowsPluginConfig<T>, config: Config) => {
|
||||
// Add workflow collections
|
||||
if (!config.collections) {
|
||||
@@ -27,6 +39,82 @@ 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}`)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
export const workflowsPlugin =
|
||||
<TSlug extends string>(pluginOptions: WorkflowsPluginConfig<TSlug>) =>
|
||||
@@ -37,6 +125,9 @@ export const workflowsPlugin =
|
||||
}
|
||||
|
||||
applyCollectionsConfig<TSlug>(pluginOptions, config)
|
||||
|
||||
// CRITICAL FIX: Apply hooks during config phase, not onInit
|
||||
applyHooksToCollections<TSlug>(pluginOptions, config)
|
||||
|
||||
if (!config.jobs) {
|
||||
config.jobs = {tasks: []}
|
||||
@@ -79,11 +170,16 @@ export const workflowsPlugin =
|
||||
logger.info(`Plugin configuration: ${Object.keys(pluginOptions.collectionTriggers || {}).length} collection triggers, ${pluginOptions.steps?.length || 0} steps`)
|
||||
|
||||
// Create workflow executor instance
|
||||
console.log('🚨 CREATING WORKFLOW EXECUTOR INSTANCE')
|
||||
const executor = new WorkflowExecutor(payload, logger)
|
||||
console.log('🚨 EXECUTOR CREATED:', typeof executor)
|
||||
console.log('🚨 EXECUTOR METHODS:', Object.getOwnPropertyNames(Object.getPrototypeOf(executor)))
|
||||
|
||||
// Register executor globally for config-phase hooks
|
||||
setWorkflowExecutor(executor)
|
||||
|
||||
// Initialize hooks
|
||||
logger.info('Initializing collection hooks...')
|
||||
initCollectionHooks(pluginOptions, payload, logger, 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')
|
||||
|
||||
logger.info('Initializing global hooks...')
|
||||
initGlobalHooks(payload, logger, executor)
|
||||
|
||||
@@ -39,14 +39,27 @@ export function initCollectionHooks<T extends string>(pluginOptions: WorkflowsPl
|
||||
collection.config.hooks.afterChange = collection.config.hooks.afterChange || []
|
||||
collection.config.hooks.afterChange.push(async (change) => {
|
||||
const operation = change.operation as 'create' | 'update'
|
||||
|
||||
// AGGRESSIVE LOGGING - this should ALWAYS appear
|
||||
console.log('🚨 AUTOMATION PLUGIN HOOK CALLED! 🚨')
|
||||
console.log('Collection:', change.collection.slug)
|
||||
console.log('Operation:', operation)
|
||||
console.log('Doc ID:', change.doc?.id)
|
||||
console.log('Has executor?', !!executor)
|
||||
console.log('Executor type:', typeof executor)
|
||||
|
||||
logger.info({
|
||||
slug: change.collection.slug,
|
||||
operation,
|
||||
docId: change.doc?.id,
|
||||
previousDocId: change.previousDoc?.id,
|
||||
hasExecutor: !!executor,
|
||||
executorType: typeof executor
|
||||
}, 'AUTOMATION PLUGIN: Collection hook triggered')
|
||||
|
||||
try {
|
||||
console.log('🚨 About to call executeTriggeredWorkflows')
|
||||
|
||||
// Execute workflows for this trigger
|
||||
await executor.executeTriggeredWorkflows(
|
||||
change.collection.slug,
|
||||
@@ -55,12 +68,17 @@ export function initCollectionHooks<T extends string>(pluginOptions: WorkflowsPl
|
||||
change.previousDoc,
|
||||
change.req
|
||||
)
|
||||
|
||||
console.log('🚨 executeTriggeredWorkflows completed without error')
|
||||
|
||||
logger.info({
|
||||
slug: change.collection.slug,
|
||||
operation,
|
||||
docId: change.doc?.id
|
||||
}, 'AUTOMATION PLUGIN: executeTriggeredWorkflows completed successfully')
|
||||
} catch (error) {
|
||||
console.log('🚨 AUTOMATION PLUGIN ERROR:', error)
|
||||
|
||||
logger.error({
|
||||
slug: change.collection.slug,
|
||||
operation,
|
||||
|
||||
Reference in New Issue
Block a user