mirror of
https://github.com/xtr-dev/payload-automation.git
synced 2025-12-11 17:23:23 +00:00
Compare commits
18 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 397559079f | |||
| c352da91fa | |||
| d6aedbc59d | |||
| cd85f90ef1 | |||
| 38fbb1922a | |||
| dfcc5c0fce | |||
| 089e12ac7a | |||
| 8ff65ca7c3 | |||
| bdfc311009 | |||
| 3c54f00f57 | |||
| cbb74206e9 | |||
| 41c4d8bdcb | |||
| 46c9f11534 | |||
| 08a4022a41 | |||
| c24610b3d9 | |||
| 87893ac612 | |||
| a711fbdbea | |||
| 4adc5cbdaa |
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
|
||||||
@@ -217,7 +217,7 @@ export interface Workflow {
|
|||||||
/**
|
/**
|
||||||
* Collection that triggers the workflow
|
* Collection that triggers the workflow
|
||||||
*/
|
*/
|
||||||
collectionSlug?: 'posts' | null;
|
collectionSlug?: ('posts' | 'media') | null;
|
||||||
/**
|
/**
|
||||||
* Collection operation that triggers the workflow
|
* Collection operation that triggers the workflow
|
||||||
*/
|
*/
|
||||||
|
|||||||
@@ -103,7 +103,8 @@ const buildConfigWithMemoryDB = async () => {
|
|||||||
plugins: [
|
plugins: [
|
||||||
workflowsPlugin<CollectionSlug>({
|
workflowsPlugin<CollectionSlug>({
|
||||||
collectionTriggers: {
|
collectionTriggers: {
|
||||||
posts: true
|
posts: true,
|
||||||
|
media: true
|
||||||
},
|
},
|
||||||
steps: [
|
steps: [
|
||||||
HttpRequestStepTask,
|
HttpRequestStepTask,
|
||||||
|
|||||||
4
package-lock.json
generated
4
package-lock.json
generated
@@ -1,12 +1,12 @@
|
|||||||
{
|
{
|
||||||
"name": "@xtr-dev/payload-workflows",
|
"name": "@xtr-dev/payload-workflows",
|
||||||
"version": "0.0.14",
|
"version": "0.0.22",
|
||||||
"lockfileVersion": 3,
|
"lockfileVersion": 3,
|
||||||
"requires": true,
|
"requires": true,
|
||||||
"packages": {
|
"packages": {
|
||||||
"": {
|
"": {
|
||||||
"name": "@xtr-dev/payload-workflows",
|
"name": "@xtr-dev/payload-workflows",
|
||||||
"version": "0.0.14",
|
"version": "0.0.22",
|
||||||
"license": "MIT",
|
"license": "MIT",
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"jsonpath-plus": "^10.3.0",
|
"jsonpath-plus": "^10.3.0",
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "@xtr-dev/payload-automation",
|
"name": "@xtr-dev/payload-automation",
|
||||||
"version": "0.0.14",
|
"version": "0.0.22",
|
||||||
"description": "PayloadCMS Automation Plugin - Comprehensive workflow automation system with visual workflow building, execution tracking, and step types",
|
"description": "PayloadCMS Automation Plugin - Comprehensive workflow automation system with visual workflow building, execution tracking, and step types",
|
||||||
"license": "MIT",
|
"license": "MIT",
|
||||||
"type": "module",
|
"type": "module",
|
||||||
|
|||||||
@@ -197,11 +197,17 @@ export const createWorkflowCollection: <T extends string>(options: WorkflowsPlug
|
|||||||
}
|
}
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
{
|
...(steps || []).flatMap(step => (step.inputSchema || []).map(field => ({
|
||||||
name: 'input',
|
...field,
|
||||||
type: 'json',
|
admin: {
|
||||||
required: false
|
...(field.admin || {}),
|
||||||
},
|
condition: (...args) => args[1]?.step === step.slug && (
|
||||||
|
field.admin?.condition ?
|
||||||
|
field.admin.condition.call(this, ...args) :
|
||||||
|
true
|
||||||
|
),
|
||||||
|
},
|
||||||
|
} as Field))),
|
||||||
{
|
{
|
||||||
name: 'dependencies',
|
name: 'dependencies',
|
||||||
type: 'text',
|
type: 'text',
|
||||||
|
|||||||
@@ -480,7 +480,7 @@ export class WorkflowExecutor {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Evaluate a condition using JSONPath
|
* Evaluate a condition using JSONPath and comparison operators
|
||||||
*/
|
*/
|
||||||
public evaluateCondition(condition: string, context: ExecutionContext): boolean {
|
public evaluateCondition(condition: string, context: ExecutionContext): boolean {
|
||||||
this.logger.debug({
|
this.logger.debug({
|
||||||
@@ -492,34 +492,94 @@ export class WorkflowExecutor {
|
|||||||
}, 'Starting condition evaluation')
|
}, 'Starting condition evaluation')
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const result = JSONPath({
|
// Check if this is a comparison expression
|
||||||
json: context,
|
const comparisonMatch = condition.match(/^(.+?)\s*(==|!=|>|<|>=|<=)\s*(.+)$/)
|
||||||
path: condition,
|
|
||||||
wrap: false
|
if (comparisonMatch) {
|
||||||
})
|
const [, leftExpr, operator, rightExpr] = comparisonMatch
|
||||||
|
|
||||||
this.logger.debug({
|
// Evaluate left side (should be JSONPath)
|
||||||
condition,
|
const leftValue = this.resolveJSONPathValue(leftExpr.trim(), context)
|
||||||
result,
|
|
||||||
resultType: Array.isArray(result) ? 'array' : typeof result,
|
// Parse right side (could be string, number, boolean, or JSONPath)
|
||||||
resultLength: Array.isArray(result) ? result.length : undefined
|
const rightValue = this.parseConditionValue(rightExpr.trim(), context)
|
||||||
}, 'JSONPath evaluation result')
|
|
||||||
|
this.logger.debug({
|
||||||
// Handle different result types
|
condition,
|
||||||
let finalResult: boolean
|
leftExpr: leftExpr.trim(),
|
||||||
if (Array.isArray(result)) {
|
leftValue,
|
||||||
finalResult = result.length > 0 && Boolean(result[0])
|
operator,
|
||||||
|
rightExpr: rightExpr.trim(),
|
||||||
|
rightValue,
|
||||||
|
leftType: typeof leftValue,
|
||||||
|
rightType: typeof rightValue
|
||||||
|
}, 'Evaluating comparison condition')
|
||||||
|
|
||||||
|
// Perform comparison
|
||||||
|
let result: boolean
|
||||||
|
switch (operator) {
|
||||||
|
case '==':
|
||||||
|
result = leftValue === rightValue
|
||||||
|
break
|
||||||
|
case '!=':
|
||||||
|
result = leftValue !== rightValue
|
||||||
|
break
|
||||||
|
case '>':
|
||||||
|
result = Number(leftValue) > Number(rightValue)
|
||||||
|
break
|
||||||
|
case '<':
|
||||||
|
result = Number(leftValue) < Number(rightValue)
|
||||||
|
break
|
||||||
|
case '>=':
|
||||||
|
result = Number(leftValue) >= Number(rightValue)
|
||||||
|
break
|
||||||
|
case '<=':
|
||||||
|
result = Number(leftValue) <= Number(rightValue)
|
||||||
|
break
|
||||||
|
default:
|
||||||
|
throw new Error(`Unknown comparison operator: ${operator}`)
|
||||||
|
}
|
||||||
|
|
||||||
|
this.logger.debug({
|
||||||
|
condition,
|
||||||
|
result,
|
||||||
|
leftValue,
|
||||||
|
rightValue,
|
||||||
|
operator
|
||||||
|
}, 'Comparison condition evaluation completed')
|
||||||
|
|
||||||
|
return result
|
||||||
} else {
|
} else {
|
||||||
finalResult = Boolean(result)
|
// Treat as simple JSONPath boolean evaluation
|
||||||
|
const result = JSONPath({
|
||||||
|
json: context,
|
||||||
|
path: condition,
|
||||||
|
wrap: false
|
||||||
|
})
|
||||||
|
|
||||||
|
this.logger.debug({
|
||||||
|
condition,
|
||||||
|
result,
|
||||||
|
resultType: Array.isArray(result) ? 'array' : typeof result,
|
||||||
|
resultLength: Array.isArray(result) ? result.length : undefined
|
||||||
|
}, 'JSONPath boolean evaluation result')
|
||||||
|
|
||||||
|
// Handle different result types
|
||||||
|
let finalResult: boolean
|
||||||
|
if (Array.isArray(result)) {
|
||||||
|
finalResult = result.length > 0 && Boolean(result[0])
|
||||||
|
} else {
|
||||||
|
finalResult = Boolean(result)
|
||||||
|
}
|
||||||
|
|
||||||
|
this.logger.debug({
|
||||||
|
condition,
|
||||||
|
finalResult,
|
||||||
|
originalResult: result
|
||||||
|
}, 'Boolean condition evaluation completed')
|
||||||
|
|
||||||
|
return finalResult
|
||||||
}
|
}
|
||||||
|
|
||||||
this.logger.debug({
|
|
||||||
condition,
|
|
||||||
finalResult,
|
|
||||||
originalResult: result
|
|
||||||
}, 'Condition evaluation completed')
|
|
||||||
|
|
||||||
return finalResult
|
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
this.logger.warn({
|
this.logger.warn({
|
||||||
condition,
|
condition,
|
||||||
@@ -531,6 +591,49 @@ export class WorkflowExecutor {
|
|||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Resolve a JSONPath value from the context
|
||||||
|
*/
|
||||||
|
private resolveJSONPathValue(expr: string, context: ExecutionContext): any {
|
||||||
|
if (expr.startsWith('$')) {
|
||||||
|
const result = JSONPath({
|
||||||
|
json: context,
|
||||||
|
path: expr,
|
||||||
|
wrap: false
|
||||||
|
})
|
||||||
|
// Return first result if array, otherwise the result itself
|
||||||
|
return Array.isArray(result) && result.length > 0 ? result[0] : result
|
||||||
|
}
|
||||||
|
return expr
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Parse a condition value (string literal, number, boolean, or JSONPath)
|
||||||
|
*/
|
||||||
|
private parseConditionValue(expr: string, context: ExecutionContext): any {
|
||||||
|
// Handle string literals
|
||||||
|
if ((expr.startsWith('"') && expr.endsWith('"')) || (expr.startsWith("'") && expr.endsWith("'"))) {
|
||||||
|
return expr.slice(1, -1) // Remove quotes
|
||||||
|
}
|
||||||
|
|
||||||
|
// Handle boolean literals
|
||||||
|
if (expr === 'true') return true
|
||||||
|
if (expr === 'false') return false
|
||||||
|
|
||||||
|
// Handle number literals
|
||||||
|
if (/^-?\d+(\.\d+)?$/.test(expr)) {
|
||||||
|
return Number(expr)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Handle JSONPath expressions
|
||||||
|
if (expr.startsWith('$')) {
|
||||||
|
return this.resolveJSONPathValue(expr, context)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Return as string if nothing else matches
|
||||||
|
return expr
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Execute a workflow with the given context
|
* Execute a workflow with the given context
|
||||||
@@ -555,19 +658,48 @@ export class WorkflowExecutor {
|
|||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
|
this.logger.info({
|
||||||
|
workflowId: workflow.id,
|
||||||
|
workflowName: workflow.name,
|
||||||
|
contextSummary: {
|
||||||
|
triggerType: context.trigger.type,
|
||||||
|
triggerCollection: context.trigger.collection,
|
||||||
|
triggerOperation: context.trigger.operation,
|
||||||
|
hasDoc: !!context.trigger.doc,
|
||||||
|
userEmail: context.trigger.req?.user?.email
|
||||||
|
}
|
||||||
|
}, 'About to create workflow run record')
|
||||||
|
|
||||||
// Create a workflow run record
|
// Create a workflow run record
|
||||||
const workflowRun = await this.payload.create({
|
let workflowRun;
|
||||||
collection: 'workflow-runs',
|
try {
|
||||||
data: {
|
workflowRun = await this.payload.create({
|
||||||
context: serializeContext(),
|
collection: 'workflow-runs',
|
||||||
startedAt: new Date().toISOString(),
|
data: {
|
||||||
status: 'running',
|
context: serializeContext(),
|
||||||
triggeredBy: context.trigger.req?.user?.email || 'system',
|
startedAt: new Date().toISOString(),
|
||||||
workflow: workflow.id,
|
status: 'running',
|
||||||
workflowVersion: 1 // Default version since generated type doesn't have _version field
|
triggeredBy: context.trigger.req?.user?.email || 'system',
|
||||||
},
|
workflow: workflow.id,
|
||||||
req
|
workflowVersion: 1 // Default version since generated type doesn't have _version field
|
||||||
})
|
},
|
||||||
|
req
|
||||||
|
})
|
||||||
|
|
||||||
|
this.logger.info({
|
||||||
|
workflowRunId: workflowRun.id,
|
||||||
|
workflowId: workflow.id,
|
||||||
|
workflowName: workflow.name
|
||||||
|
}, 'Workflow run record created successfully')
|
||||||
|
} catch (error) {
|
||||||
|
this.logger.error({
|
||||||
|
error: error instanceof Error ? error.message : 'Unknown error',
|
||||||
|
errorStack: error instanceof Error ? error.stack : undefined,
|
||||||
|
workflowId: workflow.id,
|
||||||
|
workflowName: workflow.name
|
||||||
|
}, 'Failed to create workflow run record')
|
||||||
|
throw error
|
||||||
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
// Resolve execution order based on dependencies
|
// Resolve execution order based on dependencies
|
||||||
@@ -655,6 +787,13 @@ export class WorkflowExecutor {
|
|||||||
previousDoc: unknown,
|
previousDoc: unknown,
|
||||||
req: PayloadRequest
|
req: PayloadRequest
|
||||||
): Promise<void> {
|
): 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({
|
this.logger.info({
|
||||||
collection,
|
collection,
|
||||||
operation,
|
operation,
|
||||||
@@ -711,6 +850,18 @@ export class WorkflowExecutor {
|
|||||||
}, 'Matching triggers found')
|
}, 'Matching triggers found')
|
||||||
|
|
||||||
for (const trigger of matchingTriggers) {
|
for (const trigger of matchingTriggers) {
|
||||||
|
this.logger.info({
|
||||||
|
workflowId: workflow.id,
|
||||||
|
workflowName: workflow.name,
|
||||||
|
triggerDetails: {
|
||||||
|
type: trigger.type,
|
||||||
|
collection: trigger.collection,
|
||||||
|
collectionSlug: trigger.collectionSlug,
|
||||||
|
operation: trigger.operation,
|
||||||
|
hasCondition: !!trigger.condition
|
||||||
|
}
|
||||||
|
}, 'Processing matching trigger - about to execute workflow')
|
||||||
|
|
||||||
// Create execution context for condition evaluation
|
// Create execution context for condition evaluation
|
||||||
const context: ExecutionContext = {
|
const context: ExecutionContext = {
|
||||||
steps: {},
|
steps: {},
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
import type {Config} from 'payload'
|
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 {createWorkflowCollection} from '../collections/Workflow.js'
|
||||||
import {WorkflowRunsCollection} from '../collections/WorkflowRuns.js'
|
import {WorkflowRunsCollection} from '../collections/WorkflowRuns.js'
|
||||||
@@ -15,6 +15,24 @@ import {getConfigLogger, initializeLogger} from './logger.js'
|
|||||||
|
|
||||||
export {getLogger} 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
|
||||||
|
|
||||||
|
// 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 => {
|
||||||
|
return globalExecutor
|
||||||
|
}
|
||||||
|
|
||||||
const applyCollectionsConfig = <T extends string>(pluginOptions: WorkflowsPluginConfig<T>, config: Config) => {
|
const applyCollectionsConfig = <T extends string>(pluginOptions: WorkflowsPluginConfig<T>, config: Config) => {
|
||||||
// Add workflow collections
|
// Add workflow collections
|
||||||
if (!config.collections) {
|
if (!config.collections) {
|
||||||
@@ -27,8 +45,8 @@ const applyCollectionsConfig = <T extends string>(pluginOptions: WorkflowsPlugin
|
|||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Track if hooks have been initialized to prevent double registration
|
// Removed config-phase hook registration - user collections don't exist during config phase
|
||||||
let hooksInitialized = false
|
|
||||||
|
|
||||||
export const workflowsPlugin =
|
export const workflowsPlugin =
|
||||||
<TSlug extends string>(pluginOptions: WorkflowsPluginConfig<TSlug>) =>
|
<TSlug extends string>(pluginOptions: WorkflowsPluginConfig<TSlug>) =>
|
||||||
@@ -39,6 +57,92 @@ export const workflowsPlugin =
|
|||||||
}
|
}
|
||||||
|
|
||||||
applyCollectionsConfig<TSlug>(pluginOptions, config)
|
applyCollectionsConfig<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) {
|
if (!config.jobs) {
|
||||||
config.jobs = {tasks: []}
|
config.jobs = {tasks: []}
|
||||||
@@ -65,13 +169,7 @@ export const workflowsPlugin =
|
|||||||
// Set up onInit to register collection hooks and initialize features
|
// Set up onInit to register collection hooks and initialize features
|
||||||
const incomingOnInit = config.onInit
|
const incomingOnInit = config.onInit
|
||||||
config.onInit = async (payload) => {
|
config.onInit = async (payload) => {
|
||||||
configLogger.info(`onInit called - hooks already initialized: ${hooksInitialized}, collections: ${Object.keys(payload.collections).length}`)
|
configLogger.info(`onInit called - collections: ${Object.keys(payload.collections).length}`)
|
||||||
|
|
||||||
// Prevent double initialization in dev mode
|
|
||||||
if (hooksInitialized) {
|
|
||||||
configLogger.warn('Hooks already initialized, skipping to prevent duplicate registration')
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
// Execute any existing onInit functions first
|
// Execute any existing onInit functions first
|
||||||
if (incomingOnInit) {
|
if (incomingOnInit) {
|
||||||
@@ -87,11 +185,16 @@ export const workflowsPlugin =
|
|||||||
logger.info(`Plugin configuration: ${Object.keys(pluginOptions.collectionTriggers || {}).length} collection triggers, ${pluginOptions.steps?.length || 0} steps`)
|
logger.info(`Plugin configuration: ${Object.keys(pluginOptions.collectionTriggers || {}).length} collection triggers, ${pluginOptions.steps?.length || 0} steps`)
|
||||||
|
|
||||||
// Create workflow executor instance
|
// Create workflow executor instance
|
||||||
|
console.log('🚨 CREATING WORKFLOW EXECUTOR INSTANCE')
|
||||||
const executor = new WorkflowExecutor(payload, logger)
|
const executor = new WorkflowExecutor(payload, logger)
|
||||||
|
console.log('🚨 EXECUTOR CREATED:', typeof executor)
|
||||||
|
console.log('🚨 EXECUTOR METHODS:', Object.getOwnPropertyNames(Object.getPrototypeOf(executor)))
|
||||||
|
|
||||||
|
// Register executor globally
|
||||||
|
setWorkflowExecutor(executor)
|
||||||
|
|
||||||
// Initialize hooks
|
// Hooks are now registered during config phase - just log status
|
||||||
logger.info('Initializing collection hooks...')
|
logger.info('Hooks were registered during config phase - executor now available')
|
||||||
initCollectionHooks(pluginOptions, payload, logger, executor)
|
|
||||||
|
|
||||||
logger.info('Initializing global hooks...')
|
logger.info('Initializing global hooks...')
|
||||||
initGlobalHooks(payload, logger, executor)
|
initGlobalHooks(payload, logger, executor)
|
||||||
@@ -107,7 +210,6 @@ export const workflowsPlugin =
|
|||||||
await registerCronJobs(payload, logger)
|
await registerCronJobs(payload, logger)
|
||||||
|
|
||||||
logger.info('Plugin initialized successfully - all hooks registered')
|
logger.info('Plugin initialized successfully - all hooks registered')
|
||||||
hooksInitialized = true
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return config
|
return config
|
||||||
|
|||||||
@@ -39,19 +39,55 @@ export function initCollectionHooks<T extends string>(pluginOptions: WorkflowsPl
|
|||||||
collection.config.hooks.afterChange = collection.config.hooks.afterChange || []
|
collection.config.hooks.afterChange = collection.config.hooks.afterChange || []
|
||||||
collection.config.hooks.afterChange.push(async (change) => {
|
collection.config.hooks.afterChange.push(async (change) => {
|
||||||
const operation = change.operation as 'create' | 'update'
|
const operation = change.operation as 'create' | 'update'
|
||||||
logger.debug({
|
|
||||||
|
// 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,
|
slug: change.collection.slug,
|
||||||
operation,
|
operation,
|
||||||
}, 'Collection hook triggered')
|
docId: change.doc?.id,
|
||||||
|
previousDocId: change.previousDoc?.id,
|
||||||
|
hasExecutor: !!executor,
|
||||||
|
executorType: typeof executor
|
||||||
|
}, 'AUTOMATION PLUGIN: Collection hook triggered')
|
||||||
|
|
||||||
// Execute workflows for this trigger
|
try {
|
||||||
await executor.executeTriggeredWorkflows(
|
console.log('🚨 About to call executeTriggeredWorkflows')
|
||||||
change.collection.slug,
|
|
||||||
operation,
|
// Execute workflows for this trigger
|
||||||
change.doc,
|
await executor.executeTriggeredWorkflows(
|
||||||
change.previousDoc,
|
change.collection.slug,
|
||||||
change.req
|
operation,
|
||||||
)
|
change.doc,
|
||||||
|
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,
|
||||||
|
docId: change.doc?.id,
|
||||||
|
error: error instanceof Error ? error.message : 'Unknown error',
|
||||||
|
stack: error instanceof Error ? error.stack : undefined
|
||||||
|
}, 'AUTOMATION PLUGIN: executeTriggeredWorkflows failed')
|
||||||
|
// Don't re-throw to avoid breaking other hooks
|
||||||
|
}
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user