mirror of
https://github.com/xtr-dev/payload-automation.git
synced 2025-12-12 09:43:22 +00:00
Compare commits
10 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 59a97e519e | |||
| b3d2877f0a | |||
| c050ee835a | |||
| 1f80028042 | |||
| 14d1ecf036 | |||
| 3749881d5f | |||
| c46b58f43e | |||
| 398a2d160e | |||
| 96b36a3caa | |||
| 71ecca8253 |
@@ -1,9 +1,9 @@
|
|||||||
import { default as default_4845c503d8eeb95a2cf39d519276b9b7 } from '../../../../../components/WorkflowExecutionStatus'
|
import { WorkflowExecutionStatus as WorkflowExecutionStatus_6f365a93b6cb4b34ad564b391e21db6f } from '@xtr-dev/payload-automation/client'
|
||||||
import { default as default_28774f13376b69227276b43eee64e5a1 } from '../../../../../components/StatusCell'
|
import { StatusCell as StatusCell_6f365a93b6cb4b34ad564b391e21db6f } from '@xtr-dev/payload-automation/client'
|
||||||
import { default as default_623fcff70b12e3e87839f97bf237499a } from '../../../../../components/ErrorDisplay'
|
import { ErrorDisplay as ErrorDisplay_6f365a93b6cb4b34ad564b391e21db6f } from '@xtr-dev/payload-automation/client'
|
||||||
|
|
||||||
export const importMap = {
|
export const importMap = {
|
||||||
"../components/WorkflowExecutionStatus#default": default_4845c503d8eeb95a2cf39d519276b9b7,
|
"@xtr-dev/payload-automation/client#WorkflowExecutionStatus": WorkflowExecutionStatus_6f365a93b6cb4b34ad564b391e21db6f,
|
||||||
"../components/StatusCell#default": default_28774f13376b69227276b43eee64e5a1,
|
"@xtr-dev/payload-automation/client#StatusCell": StatusCell_6f365a93b6cb4b34ad564b391e21db6f,
|
||||||
"../components/ErrorDisplay#default": default_623fcff70b12e3e87839f97bf237499a
|
"@xtr-dev/payload-automation/client#ErrorDisplay": ErrorDisplay_6f365a93b6cb4b34ad564b391e21db6f
|
||||||
}
|
}
|
||||||
|
|||||||
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.29",
|
"version": "0.0.34",
|
||||||
"lockfileVersion": 3,
|
"lockfileVersion": 3,
|
||||||
"requires": true,
|
"requires": true,
|
||||||
"packages": {
|
"packages": {
|
||||||
"": {
|
"": {
|
||||||
"name": "@xtr-dev/payload-workflows",
|
"name": "@xtr-dev/payload-workflows",
|
||||||
"version": "0.0.29",
|
"version": "0.0.34",
|
||||||
"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.29",
|
"version": "0.0.34",
|
||||||
"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",
|
||||||
|
|||||||
@@ -41,7 +41,7 @@ export const createWorkflowCollection: <T extends string>(options: WorkflowsPlug
|
|||||||
type: 'ui',
|
type: 'ui',
|
||||||
admin: {
|
admin: {
|
||||||
components: {
|
components: {
|
||||||
Field: '../components/WorkflowExecutionStatus'
|
Field: '@xtr-dev/payload-automation/client#WorkflowExecutionStatus'
|
||||||
},
|
},
|
||||||
condition: (data) => !!data?.id // Only show for existing workflows
|
condition: (data) => !!data?.id // Only show for existing workflows
|
||||||
}
|
}
|
||||||
@@ -319,17 +319,45 @@ export const createWorkflowCollection: <T extends string>(options: WorkflowsPlug
|
|||||||
}
|
}
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
...(steps || []).flatMap(step => (step.inputSchema || []).map(field => ({
|
...(steps || []).flatMap(step => (step.inputSchema || []).map(field => {
|
||||||
...field,
|
const originalName = (field as any).name;
|
||||||
admin: {
|
const resultField: any = {
|
||||||
...(field.admin || {}),
|
...field,
|
||||||
condition: (...args) => args[1]?.step === step.slug && (
|
// Prefix field name with step slug to avoid conflicts
|
||||||
field.admin?.condition ?
|
name: `__step_${step.slug}_${originalName}`,
|
||||||
field.admin.condition.call(this, ...args) :
|
admin: {
|
||||||
true
|
...(field.admin || {}),
|
||||||
),
|
condition: (...args: any[]) => args[1]?.step === step.slug && (
|
||||||
},
|
(field.admin as any)?.condition ?
|
||||||
} as Field))),
|
(field.admin as any).condition.call(this, ...args) :
|
||||||
|
true
|
||||||
|
),
|
||||||
|
},
|
||||||
|
virtual: true,
|
||||||
|
};
|
||||||
|
|
||||||
|
// Add hooks to store/retrieve from the step's input data
|
||||||
|
resultField.hooks = {
|
||||||
|
...((field as any).hooks || {}),
|
||||||
|
afterRead: [
|
||||||
|
...(((field as any).hooks)?.afterRead || []),
|
||||||
|
({ siblingData }: any) => {
|
||||||
|
// Read from step input data using original field name
|
||||||
|
return siblingData?.[originalName] || (field as any).defaultValue;
|
||||||
|
}
|
||||||
|
],
|
||||||
|
beforeChange: [
|
||||||
|
...(((field as any).hooks)?.beforeChange || []),
|
||||||
|
({ siblingData, value }: any) => {
|
||||||
|
// Store in step data using original field name
|
||||||
|
siblingData[originalName] = value;
|
||||||
|
return undefined; // Don't store the prefixed field
|
||||||
|
}
|
||||||
|
]
|
||||||
|
};
|
||||||
|
|
||||||
|
return resultField as Field;
|
||||||
|
})),
|
||||||
{
|
{
|
||||||
name: 'dependencies',
|
name: 'dependencies',
|
||||||
type: 'text',
|
type: 'text',
|
||||||
|
|||||||
@@ -40,7 +40,7 @@ export const WorkflowRunsCollection: CollectionConfig = {
|
|||||||
admin: {
|
admin: {
|
||||||
description: 'Current execution status',
|
description: 'Current execution status',
|
||||||
components: {
|
components: {
|
||||||
Cell: '../components/StatusCell'
|
Cell: '@xtr-dev/payload-automation/client#StatusCell'
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
defaultValue: 'pending',
|
defaultValue: 'pending',
|
||||||
@@ -141,7 +141,7 @@ export const WorkflowRunsCollection: CollectionConfig = {
|
|||||||
description: 'Error message if workflow execution failed',
|
description: 'Error message if workflow execution failed',
|
||||||
condition: (_, siblingData) => siblingData?.status === 'failed',
|
condition: (_, siblingData) => siblingData?.status === 'failed',
|
||||||
components: {
|
components: {
|
||||||
Field: '../components/ErrorDisplay'
|
Field: '@xtr-dev/payload-automation/client#ErrorDisplay'
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
|||||||
@@ -1029,17 +1029,24 @@ export class WorkflowExecutor {
|
|||||||
}
|
}
|
||||||
}>
|
}>
|
||||||
|
|
||||||
this.logger.debug({
|
this.logger.info({
|
||||||
workflowId: workflow.id,
|
workflowId: workflow.id,
|
||||||
workflowName: workflow.name,
|
workflowName: workflow.name,
|
||||||
triggerCount: triggers?.length || 0,
|
triggerCount: triggers?.length || 0,
|
||||||
triggers: triggers?.map(t => ({
|
triggers: triggers?.map(t => ({
|
||||||
type: t.type,
|
type: t.type,
|
||||||
|
parameters: t.parameters,
|
||||||
collection: t.parameters?.collection,
|
collection: t.parameters?.collection,
|
||||||
collectionSlug: t.parameters?.collectionSlug,
|
collectionSlug: t.parameters?.collectionSlug,
|
||||||
operation: t.parameters?.operation
|
operation: t.parameters?.operation,
|
||||||
}))
|
// Debug matching criteria
|
||||||
}, 'Checking workflow triggers')
|
typeMatch: t.type === 'collection-trigger',
|
||||||
|
collectionMatch: (t.parameters?.collection === collection || t.parameters?.collectionSlug === collection),
|
||||||
|
operationMatch: t.parameters?.operation === operation
|
||||||
|
})),
|
||||||
|
targetCollection: collection,
|
||||||
|
targetOperation: operation
|
||||||
|
}, 'Checking workflow triggers with detailed matching info')
|
||||||
|
|
||||||
const matchingTriggers = triggers?.filter(trigger =>
|
const matchingTriggers = triggers?.filter(trigger =>
|
||||||
trigger.type === 'collection-trigger' &&
|
trigger.type === 'collection-trigger' &&
|
||||||
@@ -1083,7 +1090,7 @@ export class WorkflowExecutor {
|
|||||||
|
|
||||||
// Check trigger condition if present
|
// Check trigger condition if present
|
||||||
if (trigger.condition) {
|
if (trigger.condition) {
|
||||||
this.logger.debug({
|
this.logger.info({
|
||||||
collection,
|
collection,
|
||||||
operation,
|
operation,
|
||||||
condition: trigger.condition,
|
condition: trigger.condition,
|
||||||
@@ -1091,8 +1098,10 @@ export class WorkflowExecutor {
|
|||||||
docFields: doc ? Object.keys(doc) : [],
|
docFields: doc ? Object.keys(doc) : [],
|
||||||
previousDocId: (previousDoc as any)?.id,
|
previousDocId: (previousDoc as any)?.id,
|
||||||
workflowId: workflow.id,
|
workflowId: workflow.id,
|
||||||
workflowName: workflow.name
|
workflowName: workflow.name,
|
||||||
}, 'Evaluating collection trigger condition')
|
docStatus: (doc as any)?.status,
|
||||||
|
previousDocStatus: (previousDoc as any)?.status
|
||||||
|
}, 'Evaluating trigger condition before execution')
|
||||||
|
|
||||||
const conditionMet = this.evaluateCondition(trigger.condition, context)
|
const conditionMet = this.evaluateCondition(trigger.condition, context)
|
||||||
|
|
||||||
|
|||||||
@@ -159,7 +159,7 @@ export const workflowsPlugin =
|
|||||||
}
|
}
|
||||||
|
|
||||||
const collection = config.collections[collectionIndex]
|
const collection = config.collections[collectionIndex]
|
||||||
logger.info(`Found collection '${triggerSlug}' - modifying its hooks...`)
|
logger.debug(`Found collection '${triggerSlug}' - modifying its hooks...`)
|
||||||
|
|
||||||
// Initialize hooks if needed
|
// Initialize hooks if needed
|
||||||
if (!collection.hooks) {
|
if (!collection.hooks) {
|
||||||
@@ -186,19 +186,40 @@ export const workflowsPlugin =
|
|||||||
}, 'Collection automation hook triggered')
|
}, 'Collection automation hook triggered')
|
||||||
|
|
||||||
if (!registry.isInitialized) {
|
if (!registry.isInitialized) {
|
||||||
logger.warn('Workflow executor not yet initialized, skipping execution')
|
logger.warn('Workflow executor not yet initialized, attempting lazy initialization')
|
||||||
return undefined
|
|
||||||
|
try {
|
||||||
|
// Try to create executor if we have a payload instance
|
||||||
|
if (args.req?.payload) {
|
||||||
|
logger.info('Creating workflow executor via lazy initialization')
|
||||||
|
const { WorkflowExecutor } = await import('../core/workflow-executor.js')
|
||||||
|
const executor = new WorkflowExecutor(args.req.payload, logger)
|
||||||
|
setWorkflowExecutor(executor, logger)
|
||||||
|
logger.info('Lazy initialization successful')
|
||||||
|
} else {
|
||||||
|
logger.error('Cannot lazy initialize - no payload instance available')
|
||||||
|
await createFailedWorkflowRun(args, 'Workflow executor not initialized and lazy initialization failed - no payload instance', logger)
|
||||||
|
return undefined
|
||||||
|
}
|
||||||
|
} catch (error) {
|
||||||
|
logger.error('Lazy initialization failed:', error)
|
||||||
|
const errorMessage = error instanceof Error ? error.message : String(error)
|
||||||
|
await createFailedWorkflowRun(args, `Workflow executor lazy initialization failed: ${errorMessage}`, logger)
|
||||||
|
return undefined
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!registry.executor) {
|
// Re-check registry after potential lazy initialization
|
||||||
|
const updatedRegistry = getExecutorRegistry()
|
||||||
|
if (!updatedRegistry.executor) {
|
||||||
logger.error('Workflow executor is null despite being marked as initialized')
|
logger.error('Workflow executor is null despite being marked as initialized')
|
||||||
// Create a failed workflow run to track this issue
|
// Create a failed workflow run to track this issue
|
||||||
await createFailedWorkflowRun(args, 'Executor not available', logger)
|
await createFailedWorkflowRun(args, 'Executor not available after initialization', logger)
|
||||||
return undefined
|
return undefined
|
||||||
}
|
}
|
||||||
|
|
||||||
logger.debug('Executing triggered workflows...')
|
logger.debug('Executing triggered workflows...')
|
||||||
await registry.executor.executeTriggeredWorkflows(
|
await updatedRegistry.executor.executeTriggeredWorkflows(
|
||||||
args.collection.slug,
|
args.collection.slug,
|
||||||
args.operation,
|
args.operation,
|
||||||
args.doc,
|
args.doc,
|
||||||
@@ -245,7 +266,7 @@ export const workflowsPlugin =
|
|||||||
|
|
||||||
// Add the hook to the collection config
|
// Add the hook to the collection config
|
||||||
collection.hooks.afterChange.push(automationHook)
|
collection.hooks.afterChange.push(automationHook)
|
||||||
logger.info(`Added automation hook to '${triggerSlug}' - hook count: ${collection.hooks.afterChange.length}`)
|
logger.debug(`Added automation hook to '${triggerSlug}' - hook count: ${collection.hooks.afterChange.length}`)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -254,7 +275,7 @@ export const workflowsPlugin =
|
|||||||
}
|
}
|
||||||
|
|
||||||
const configLogger = getConfigLogger()
|
const configLogger = getConfigLogger()
|
||||||
configLogger.info(`Configuring workflow plugin with ${Object.keys(pluginOptions.collectionTriggers || {}).length} collection triggers`)
|
configLogger.debug(`Configuring workflow plugin with ${Object.keys(pluginOptions.collectionTriggers || {}).length} collection triggers`)
|
||||||
|
|
||||||
// Generate cron tasks for workflows with cron triggers
|
// Generate cron tasks for workflows with cron triggers
|
||||||
generateCronTasks(config)
|
generateCronTasks(config)
|
||||||
|
|||||||
Reference in New Issue
Block a user