mirror of
https://github.com/xtr-dev/payload-automation.git
synced 2025-12-13 01:53:23 +00:00
Compare commits
4 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| dfcc5c0fce | |||
| 089e12ac7a | |||
| 8ff65ca7c3 | |||
| bdfc311009 |
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.18",
|
"version": "0.0.20",
|
||||||
"lockfileVersion": 3,
|
"lockfileVersion": 3,
|
||||||
"requires": true,
|
"requires": true,
|
||||||
"packages": {
|
"packages": {
|
||||||
"": {
|
"": {
|
||||||
"name": "@xtr-dev/payload-workflows",
|
"name": "@xtr-dev/payload-workflows",
|
||||||
"version": "0.0.18",
|
"version": "0.0.20",
|
||||||
"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.18",
|
"version": "0.0.20",
|
||||||
"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",
|
||||||
|
|||||||
@@ -39,81 +39,7 @@ const applyCollectionsConfig = <T extends string>(pluginOptions: WorkflowsPlugin
|
|||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
const applyHooksToCollections = <T extends string>(pluginOptions: WorkflowsPluginConfig<T>, config: Config) => {
|
// Removed config-phase hook registration - user collections don't exist during config phase
|
||||||
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 =
|
export const workflowsPlugin =
|
||||||
@@ -126,8 +52,71 @@ export const workflowsPlugin =
|
|||||||
|
|
||||||
applyCollectionsConfig<TSlug>(pluginOptions, config)
|
applyCollectionsConfig<TSlug>(pluginOptions, config)
|
||||||
|
|
||||||
// CRITICAL FIX: Apply hooks during config phase, not onInit
|
// CRITICAL: Modify existing collection configs BEFORE PayloadCMS processes them
|
||||||
applyHooksToCollections<TSlug>(pluginOptions, config)
|
// 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 = []
|
||||||
|
}
|
||||||
|
|
||||||
|
// Add our hook DIRECTLY to the collection config
|
||||||
|
// This happens BEFORE PayloadCMS processes the config
|
||||||
|
const automationHook = async (args: any) => {
|
||||||
|
console.log('🔥🔥🔥 AUTOMATION HOOK FROM CONFIG PHASE! 🔥🔥🔥')
|
||||||
|
console.log('Collection:', args.collection.slug)
|
||||||
|
console.log('Operation:', args.operation)
|
||||||
|
console.log('Doc ID:', args.doc?.id)
|
||||||
|
|
||||||
|
// We'll need to get the executor from somewhere
|
||||||
|
// For now, just prove the hook works
|
||||||
|
console.log('🔥🔥🔥 CONFIG-PHASE HOOK SUCCESSFULLY EXECUTED! 🔥🔥🔥')
|
||||||
|
|
||||||
|
// Try to get executor from global registry
|
||||||
|
const executor = getWorkflowExecutor()
|
||||||
|
if (executor) {
|
||||||
|
console.log('✅ Executor available - executing workflows!')
|
||||||
|
try {
|
||||||
|
await executor.executeTriggeredWorkflows(
|
||||||
|
args.collection.slug,
|
||||||
|
args.operation,
|
||||||
|
args.doc,
|
||||||
|
args.previousDoc,
|
||||||
|
args.req
|
||||||
|
)
|
||||||
|
console.log('✅ Workflow execution completed!')
|
||||||
|
} catch (error) {
|
||||||
|
console.error('❌ Workflow execution failed:', error)
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
console.log('⚠️ Executor not yet available')
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 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: []}
|
||||||
@@ -175,11 +164,11 @@ export const workflowsPlugin =
|
|||||||
console.log('🚨 EXECUTOR CREATED:', typeof executor)
|
console.log('🚨 EXECUTOR CREATED:', typeof executor)
|
||||||
console.log('🚨 EXECUTOR METHODS:', Object.getOwnPropertyNames(Object.getPrototypeOf(executor)))
|
console.log('🚨 EXECUTOR METHODS:', Object.getOwnPropertyNames(Object.getPrototypeOf(executor)))
|
||||||
|
|
||||||
// Register executor globally for config-phase hooks
|
// Register executor globally
|
||||||
setWorkflowExecutor(executor)
|
setWorkflowExecutor(executor)
|
||||||
|
|
||||||
// Note: Collection hooks are now applied during config phase, not here
|
// Hooks are now registered during config phase - just log status
|
||||||
logger.info('Collection hooks applied during config phase - executor now available for them')
|
logger.info('Hooks were registered during config phase - executor now available')
|
||||||
|
|
||||||
logger.info('Initializing global hooks...')
|
logger.info('Initializing global hooks...')
|
||||||
initGlobalHooks(payload, logger, executor)
|
initGlobalHooks(payload, logger, executor)
|
||||||
|
|||||||
Reference in New Issue
Block a user