mirror of
https://github.com/xtr-dev/payload-automation.git
synced 2025-12-11 09:13:24 +00:00
Compare commits
24 Commits
add-claude
...
v0.0.22
| Author | SHA1 | Date | |
|---|---|---|---|
| 397559079f | |||
| c352da91fa | |||
| d6aedbc59d | |||
| cd85f90ef1 | |||
| 38fbb1922a | |||
| dfcc5c0fce | |||
| 089e12ac7a | |||
| 8ff65ca7c3 | |||
| bdfc311009 | |||
| 3c54f00f57 | |||
| cbb74206e9 | |||
| 41c4d8bdcb | |||
| 46c9f11534 | |||
| 08a4022a41 | |||
| c24610b3d9 | |||
| 87893ac612 | |||
| a711fbdbea | |||
| 4adc5cbdaa | |||
| f3f18d5b4c | |||
| 6397250045 | |||
| 964b11c0c9 | |||
| 3d7b746779 | |||
| 7686495283 | |||
| 265d5affc6 |
54
.github/workflows/claude-code-review.yml
vendored
54
.github/workflows/claude-code-review.yml
vendored
@@ -1,54 +0,0 @@
|
|||||||
name: Claude Code Review
|
|
||||||
|
|
||||||
on:
|
|
||||||
pull_request:
|
|
||||||
types: [opened, synchronize]
|
|
||||||
# Optional: Only run on specific file changes
|
|
||||||
# paths:
|
|
||||||
# - "src/**/*.ts"
|
|
||||||
# - "src/**/*.tsx"
|
|
||||||
# - "src/**/*.js"
|
|
||||||
# - "src/**/*.jsx"
|
|
||||||
|
|
||||||
jobs:
|
|
||||||
claude-review:
|
|
||||||
# Optional: Filter by PR author
|
|
||||||
# if: |
|
|
||||||
# github.event.pull_request.user.login == 'external-contributor' ||
|
|
||||||
# github.event.pull_request.user.login == 'new-developer' ||
|
|
||||||
# github.event.pull_request.author_association == 'FIRST_TIME_CONTRIBUTOR'
|
|
||||||
|
|
||||||
runs-on: ubuntu-latest
|
|
||||||
permissions:
|
|
||||||
contents: read
|
|
||||||
pull-requests: read
|
|
||||||
issues: read
|
|
||||||
id-token: write
|
|
||||||
|
|
||||||
steps:
|
|
||||||
- name: Checkout repository
|
|
||||||
uses: actions/checkout@v4
|
|
||||||
with:
|
|
||||||
fetch-depth: 1
|
|
||||||
|
|
||||||
- name: Run Claude Code Review
|
|
||||||
id: claude-review
|
|
||||||
uses: anthropics/claude-code-action@v1
|
|
||||||
with:
|
|
||||||
claude_code_oauth_token: ${{ secrets.CLAUDE_CODE_OAUTH_TOKEN }}
|
|
||||||
prompt: |
|
|
||||||
Please review this pull request and provide feedback on:
|
|
||||||
- Code quality and best practices
|
|
||||||
- Potential bugs or issues
|
|
||||||
- Performance considerations
|
|
||||||
- Security concerns
|
|
||||||
- Test coverage
|
|
||||||
|
|
||||||
Use the repository's CLAUDE.md for guidance on style and conventions. Be constructive and helpful in your feedback.
|
|
||||||
|
|
||||||
Use `gh pr comment` with your Bash tool to leave your review as a comment on the PR.
|
|
||||||
|
|
||||||
# See https://github.com/anthropics/claude-code-action/blob/main/docs/usage.md
|
|
||||||
# or https://docs.anthropic.com/en/docs/claude-code/sdk#command-line for available options
|
|
||||||
claude_args: '--allowed-tools "Bash(gh issue view:*),Bash(gh search:*),Bash(gh issue list:*),Bash(gh pr comment:*),Bash(gh pr diff:*),Bash(gh pr view:*),Bash(gh pr list:*)"'
|
|
||||||
|
|
||||||
50
.github/workflows/claude.yml
vendored
50
.github/workflows/claude.yml
vendored
@@ -1,50 +0,0 @@
|
|||||||
name: Claude Code
|
|
||||||
|
|
||||||
on:
|
|
||||||
issue_comment:
|
|
||||||
types: [created]
|
|
||||||
pull_request_review_comment:
|
|
||||||
types: [created]
|
|
||||||
issues:
|
|
||||||
types: [opened, assigned]
|
|
||||||
pull_request_review:
|
|
||||||
types: [submitted]
|
|
||||||
|
|
||||||
jobs:
|
|
||||||
claude:
|
|
||||||
if: |
|
|
||||||
(github.event_name == 'issue_comment' && contains(github.event.comment.body, '@claude')) ||
|
|
||||||
(github.event_name == 'pull_request_review_comment' && contains(github.event.comment.body, '@claude')) ||
|
|
||||||
(github.event_name == 'pull_request_review' && contains(github.event.review.body, '@claude')) ||
|
|
||||||
(github.event_name == 'issues' && (contains(github.event.issue.body, '@claude') || contains(github.event.issue.title, '@claude')))
|
|
||||||
runs-on: ubuntu-latest
|
|
||||||
permissions:
|
|
||||||
contents: read
|
|
||||||
pull-requests: read
|
|
||||||
issues: read
|
|
||||||
id-token: write
|
|
||||||
actions: read # Required for Claude to read CI results on PRs
|
|
||||||
steps:
|
|
||||||
- name: Checkout repository
|
|
||||||
uses: actions/checkout@v4
|
|
||||||
with:
|
|
||||||
fetch-depth: 1
|
|
||||||
|
|
||||||
- name: Run Claude Code
|
|
||||||
id: claude
|
|
||||||
uses: anthropics/claude-code-action@v1
|
|
||||||
with:
|
|
||||||
claude_code_oauth_token: ${{ secrets.CLAUDE_CODE_OAUTH_TOKEN }}
|
|
||||||
|
|
||||||
# This is an optional setting that allows Claude to read CI results on PRs
|
|
||||||
additional_permissions: |
|
|
||||||
actions: read
|
|
||||||
|
|
||||||
# Optional: Give a custom prompt to Claude. If this is not specified, Claude will perform the instructions specified in the comment that tagged it.
|
|
||||||
# prompt: 'Update the pull request description to include a summary of changes.'
|
|
||||||
|
|
||||||
# Optional: Add claude_args to customize behavior and configuration
|
|
||||||
# See https://github.com/anthropics/claude-code-action/blob/main/docs/usage.md
|
|
||||||
# or https://docs.anthropic.com/en/docs/claude-code/sdk#command-line for available options
|
|
||||||
# claude_args: '--model claude-opus-4-1-20250805 --allowed-tools Bash(gh pr:*)'
|
|
||||||
|
|
||||||
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
|
||||||
122
dev/app/test-trigger/route.ts
Normal file
122
dev/app/test-trigger/route.ts
Normal file
@@ -0,0 +1,122 @@
|
|||||||
|
import { NextResponse } from 'next/server'
|
||||||
|
import { getPayload } from 'payload'
|
||||||
|
import config from '../../payload.config'
|
||||||
|
|
||||||
|
export async function GET() {
|
||||||
|
console.log('Starting workflow trigger test...')
|
||||||
|
|
||||||
|
// Get payload instance
|
||||||
|
const payload = await getPayload({ config })
|
||||||
|
|
||||||
|
try {
|
||||||
|
// Create a test user
|
||||||
|
const user = await payload.create({
|
||||||
|
collection: 'users',
|
||||||
|
data: {
|
||||||
|
email: `test-${Date.now()}@example.com`,
|
||||||
|
password: 'password123'
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
console.log('Created test user:', user.id)
|
||||||
|
|
||||||
|
// Create a workflow with collection trigger
|
||||||
|
const workflow = await payload.create({
|
||||||
|
collection: 'workflows',
|
||||||
|
data: {
|
||||||
|
name: 'Test Post Creation Workflow',
|
||||||
|
description: 'Triggers when a post is created',
|
||||||
|
triggers: [
|
||||||
|
{
|
||||||
|
type: 'collection-trigger',
|
||||||
|
collectionSlug: 'posts',
|
||||||
|
operation: 'create'
|
||||||
|
}
|
||||||
|
],
|
||||||
|
steps: [
|
||||||
|
{
|
||||||
|
name: 'log-post',
|
||||||
|
taskSlug: 'http-request-step',
|
||||||
|
input: JSON.stringify({
|
||||||
|
url: 'https://httpbin.org/post',
|
||||||
|
method: 'POST',
|
||||||
|
headers: {
|
||||||
|
'Content-Type': 'application/json'
|
||||||
|
},
|
||||||
|
body: {
|
||||||
|
message: 'Post created',
|
||||||
|
postId: '$.trigger.doc.id',
|
||||||
|
postTitle: '$.trigger.doc.title'
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
]
|
||||||
|
},
|
||||||
|
user: user.id
|
||||||
|
})
|
||||||
|
|
||||||
|
console.log('Created workflow:', workflow.id, workflow.name)
|
||||||
|
console.log('Workflow triggers:', JSON.stringify(workflow.triggers, null, 2))
|
||||||
|
|
||||||
|
// Create a post to trigger the workflow
|
||||||
|
console.log('Creating post to trigger workflow...')
|
||||||
|
const post = await payload.create({
|
||||||
|
collection: 'posts',
|
||||||
|
data: {
|
||||||
|
title: 'Test Post',
|
||||||
|
content: 'This should trigger the workflow',
|
||||||
|
_status: 'published'
|
||||||
|
},
|
||||||
|
user: user.id
|
||||||
|
})
|
||||||
|
|
||||||
|
console.log('Created post:', post.id)
|
||||||
|
|
||||||
|
// Wait a bit for workflow to execute
|
||||||
|
await new Promise(resolve => setTimeout(resolve, 3000))
|
||||||
|
|
||||||
|
// Check for workflow runs
|
||||||
|
const runs = await payload.find({
|
||||||
|
collection: 'workflow-runs',
|
||||||
|
where: {
|
||||||
|
workflow: {
|
||||||
|
equals: workflow.id
|
||||||
|
}
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
console.log('Workflow runs found:', runs.totalDocs)
|
||||||
|
|
||||||
|
const result = {
|
||||||
|
success: runs.totalDocs > 0,
|
||||||
|
workflowId: workflow.id,
|
||||||
|
postId: post.id,
|
||||||
|
runsFound: runs.totalDocs,
|
||||||
|
runs: runs.docs.map(r => ({
|
||||||
|
id: r.id,
|
||||||
|
status: r.status,
|
||||||
|
triggeredBy: r.triggeredBy,
|
||||||
|
startedAt: r.startedAt,
|
||||||
|
completedAt: r.completedAt,
|
||||||
|
error: r.error
|
||||||
|
}))
|
||||||
|
}
|
||||||
|
|
||||||
|
if (runs.totalDocs > 0) {
|
||||||
|
console.log('✅ SUCCESS: Workflow was triggered!')
|
||||||
|
console.log('Run status:', runs.docs[0].status)
|
||||||
|
console.log('Run context:', JSON.stringify(runs.docs[0].context, null, 2))
|
||||||
|
} else {
|
||||||
|
console.log('❌ FAILURE: Workflow was not triggered')
|
||||||
|
}
|
||||||
|
|
||||||
|
return NextResponse.json(result)
|
||||||
|
|
||||||
|
} catch (error) {
|
||||||
|
console.error('Test failed:', error)
|
||||||
|
return NextResponse.json({
|
||||||
|
success: false,
|
||||||
|
error: error instanceof Error ? error.message : 'Unknown error'
|
||||||
|
}, { status: 500 })
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -217,7 +217,7 @@ export interface Workflow {
|
|||||||
/**
|
/**
|
||||||
* Collection that triggers the workflow
|
* Collection that triggers the workflow
|
||||||
*/
|
*/
|
||||||
collection?: 'posts' | null;
|
collectionSlug?: ('posts' | 'media') | null;
|
||||||
/**
|
/**
|
||||||
* Collection operation that triggers the workflow
|
* Collection operation that triggers the workflow
|
||||||
*/
|
*/
|
||||||
@@ -242,6 +242,10 @@ export interface Workflow {
|
|||||||
* Timezone for cron execution (e.g., "America/New_York", "Europe/London"). Defaults to UTC.
|
* Timezone for cron execution (e.g., "America/New_York", "Europe/London"). Defaults to UTC.
|
||||||
*/
|
*/
|
||||||
timezone?: string | null;
|
timezone?: string | null;
|
||||||
|
/**
|
||||||
|
* JSONPath expression that must evaluate to true for this trigger to execute the workflow (e.g., "$.doc.status == 'published'")
|
||||||
|
*/
|
||||||
|
condition?: string | null;
|
||||||
id?: string | null;
|
id?: string | null;
|
||||||
}[]
|
}[]
|
||||||
| null;
|
| null;
|
||||||
@@ -262,6 +266,10 @@ export interface Workflow {
|
|||||||
* Step names that must complete before this step can run
|
* Step names that must complete before this step can run
|
||||||
*/
|
*/
|
||||||
dependencies?: string[] | null;
|
dependencies?: string[] | null;
|
||||||
|
/**
|
||||||
|
* JSONPath expression that must evaluate to true for this step to execute (e.g., "$.trigger.doc.status == 'published'")
|
||||||
|
*/
|
||||||
|
condition?: string | null;
|
||||||
id?: string | null;
|
id?: string | null;
|
||||||
}[]
|
}[]
|
||||||
| null;
|
| null;
|
||||||
@@ -584,13 +592,14 @@ export interface WorkflowsSelect<T extends boolean = true> {
|
|||||||
| T
|
| T
|
||||||
| {
|
| {
|
||||||
type?: T;
|
type?: T;
|
||||||
collection?: T;
|
collectionSlug?: T;
|
||||||
operation?: T;
|
operation?: T;
|
||||||
webhookPath?: T;
|
webhookPath?: T;
|
||||||
global?: T;
|
global?: T;
|
||||||
globalOperation?: T;
|
globalOperation?: T;
|
||||||
cronExpression?: T;
|
cronExpression?: T;
|
||||||
timezone?: T;
|
timezone?: T;
|
||||||
|
condition?: T;
|
||||||
id?: T;
|
id?: T;
|
||||||
};
|
};
|
||||||
steps?:
|
steps?:
|
||||||
@@ -600,6 +609,7 @@ export interface WorkflowsSelect<T extends boolean = true> {
|
|||||||
name?: T;
|
name?: T;
|
||||||
input?: T;
|
input?: T;
|
||||||
dependencies?: T;
|
dependencies?: T;
|
||||||
|
condition?: T;
|
||||||
id?: T;
|
id?: T;
|
||||||
};
|
};
|
||||||
updatedAt?: T;
|
updatedAt?: T;
|
||||||
@@ -741,7 +751,7 @@ export interface TaskCreateDocument {
|
|||||||
/**
|
/**
|
||||||
* The collection slug to create a document in
|
* The collection slug to create a document in
|
||||||
*/
|
*/
|
||||||
collection: string;
|
collectionSlug: string;
|
||||||
/**
|
/**
|
||||||
* The document data to create
|
* The document data to create
|
||||||
*/
|
*/
|
||||||
|
|||||||
@@ -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,
|
||||||
|
|||||||
132
dev/simple-trigger.spec.ts
Normal file
132
dev/simple-trigger.spec.ts
Normal file
@@ -0,0 +1,132 @@
|
|||||||
|
import { describe, it, expect, beforeAll, afterAll } from 'vitest'
|
||||||
|
import type { Payload } from 'payload'
|
||||||
|
import { getPayload } from 'payload'
|
||||||
|
import config from './payload.config'
|
||||||
|
|
||||||
|
describe('Workflow Trigger Test', () => {
|
||||||
|
let payload: Payload
|
||||||
|
|
||||||
|
beforeAll(async () => {
|
||||||
|
payload = await getPayload({ config: await config })
|
||||||
|
}, 60000)
|
||||||
|
|
||||||
|
afterAll(async () => {
|
||||||
|
if (!payload) return
|
||||||
|
|
||||||
|
try {
|
||||||
|
// Clear test data
|
||||||
|
const workflows = await payload.find({
|
||||||
|
collection: 'workflows',
|
||||||
|
limit: 100
|
||||||
|
})
|
||||||
|
|
||||||
|
for (const workflow of workflows.docs) {
|
||||||
|
await payload.delete({
|
||||||
|
collection: 'workflows',
|
||||||
|
id: workflow.id
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
const runs = await payload.find({
|
||||||
|
collection: 'workflow-runs',
|
||||||
|
limit: 100
|
||||||
|
})
|
||||||
|
|
||||||
|
for (const run of runs.docs) {
|
||||||
|
await payload.delete({
|
||||||
|
collection: 'workflow-runs',
|
||||||
|
id: run.id
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
const posts = await payload.find({
|
||||||
|
collection: 'posts',
|
||||||
|
limit: 100
|
||||||
|
})
|
||||||
|
|
||||||
|
for (const post of posts.docs) {
|
||||||
|
await payload.delete({
|
||||||
|
collection: 'posts',
|
||||||
|
id: post.id
|
||||||
|
})
|
||||||
|
}
|
||||||
|
} catch (error) {
|
||||||
|
console.warn('Cleanup failed:', error)
|
||||||
|
}
|
||||||
|
}, 30000)
|
||||||
|
|
||||||
|
it('should create a workflow run when a post is created', async () => {
|
||||||
|
// Create a workflow with collection trigger
|
||||||
|
const workflow = await payload.create({
|
||||||
|
collection: 'workflows',
|
||||||
|
data: {
|
||||||
|
name: 'Test Post Creation Workflow',
|
||||||
|
description: 'Triggers when a post is created',
|
||||||
|
triggers: [
|
||||||
|
{
|
||||||
|
type: 'collection-trigger',
|
||||||
|
collectionSlug: 'posts',
|
||||||
|
operation: 'create'
|
||||||
|
}
|
||||||
|
],
|
||||||
|
steps: [
|
||||||
|
{
|
||||||
|
name: 'log-post',
|
||||||
|
step: 'http-request-step',
|
||||||
|
input: {
|
||||||
|
url: 'https://httpbin.org/post',
|
||||||
|
method: 'POST',
|
||||||
|
headers: {
|
||||||
|
'Content-Type': 'application/json'
|
||||||
|
},
|
||||||
|
body: {
|
||||||
|
message: 'Post created',
|
||||||
|
postId: '$.trigger.doc.id',
|
||||||
|
postTitle: '$.trigger.doc.content'
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
expect(workflow).toBeDefined()
|
||||||
|
expect(workflow.id).toBeDefined()
|
||||||
|
|
||||||
|
// Create a post to trigger the workflow
|
||||||
|
const post = await payload.create({
|
||||||
|
collection: 'posts',
|
||||||
|
data: {
|
||||||
|
content: 'This should trigger the workflow'
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
expect(post).toBeDefined()
|
||||||
|
expect(post.id).toBeDefined()
|
||||||
|
|
||||||
|
// Wait a bit for workflow to execute
|
||||||
|
await new Promise(resolve => setTimeout(resolve, 3000))
|
||||||
|
|
||||||
|
// Check for workflow runs
|
||||||
|
const runs = await payload.find({
|
||||||
|
collection: 'workflow-runs',
|
||||||
|
where: {
|
||||||
|
workflow: {
|
||||||
|
equals: workflow.id
|
||||||
|
}
|
||||||
|
},
|
||||||
|
limit: 10
|
||||||
|
})
|
||||||
|
|
||||||
|
expect(runs.totalDocs).toBeGreaterThan(0)
|
||||||
|
expect(runs.docs[0].workflow).toBe(typeof workflow.id === 'object' ? workflow.id.toString() : workflow.id)
|
||||||
|
|
||||||
|
console.log('✅ Workflow run created successfully!')
|
||||||
|
console.log(`Run status: ${runs.docs[0].status}`)
|
||||||
|
console.log(`Run ID: ${runs.docs[0].id}`)
|
||||||
|
|
||||||
|
if (runs.docs[0].status === 'failed' && runs.docs[0].error) {
|
||||||
|
console.log(`Error: ${runs.docs[0].error}`)
|
||||||
|
}
|
||||||
|
}, 30000)
|
||||||
|
})
|
||||||
104
dev/test-trigger.ts
Normal file
104
dev/test-trigger.ts
Normal file
@@ -0,0 +1,104 @@
|
|||||||
|
import type { Payload } from 'payload'
|
||||||
|
import { getPayload } from 'payload'
|
||||||
|
import config from './payload.config'
|
||||||
|
|
||||||
|
async function testWorkflowTrigger() {
|
||||||
|
console.log('Starting workflow trigger test...')
|
||||||
|
|
||||||
|
// Get payload instance
|
||||||
|
const payload = await getPayload({ config })
|
||||||
|
|
||||||
|
try {
|
||||||
|
// Create a test user
|
||||||
|
const user = await payload.create({
|
||||||
|
collection: 'users',
|
||||||
|
data: {
|
||||||
|
email: 'test@example.com',
|
||||||
|
password: 'password123'
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
console.log('Created test user:', user.id)
|
||||||
|
|
||||||
|
// Create a workflow with collection trigger
|
||||||
|
const workflow = await payload.create({
|
||||||
|
collection: 'workflows',
|
||||||
|
data: {
|
||||||
|
name: 'Test Post Creation Workflow',
|
||||||
|
description: 'Triggers when a post is created',
|
||||||
|
triggers: [
|
||||||
|
{
|
||||||
|
type: 'collection-trigger',
|
||||||
|
collectionSlug: 'posts',
|
||||||
|
operation: 'create'
|
||||||
|
}
|
||||||
|
],
|
||||||
|
steps: [
|
||||||
|
{
|
||||||
|
name: 'log-post',
|
||||||
|
step: 'http-request-step',
|
||||||
|
input: {
|
||||||
|
url: 'https://httpbin.org/post',
|
||||||
|
method: 'POST',
|
||||||
|
headers: {
|
||||||
|
'Content-Type': 'application/json'
|
||||||
|
},
|
||||||
|
body: {
|
||||||
|
message: 'Post created',
|
||||||
|
postId: '$.trigger.doc.id',
|
||||||
|
postTitle: '$.trigger.doc.title'
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
]
|
||||||
|
},
|
||||||
|
user: user.id
|
||||||
|
})
|
||||||
|
|
||||||
|
console.log('Created workflow:', workflow.id)
|
||||||
|
|
||||||
|
// Create a post to trigger the workflow
|
||||||
|
console.log('Creating post to trigger workflow...')
|
||||||
|
const post = await payload.create({
|
||||||
|
collection: 'posts',
|
||||||
|
data: {
|
||||||
|
title: 'Test Post',
|
||||||
|
content: 'This should trigger the workflow',
|
||||||
|
_status: 'published'
|
||||||
|
},
|
||||||
|
user: user.id
|
||||||
|
})
|
||||||
|
|
||||||
|
console.log('Created post:', post.id)
|
||||||
|
|
||||||
|
// Wait a bit for workflow to execute
|
||||||
|
await new Promise(resolve => setTimeout(resolve, 2000))
|
||||||
|
|
||||||
|
// Check for workflow runs
|
||||||
|
const runs = await payload.find({
|
||||||
|
collection: 'workflow-runs',
|
||||||
|
where: {
|
||||||
|
workflow: {
|
||||||
|
equals: workflow.id
|
||||||
|
}
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
console.log('Workflow runs found:', runs.totalDocs)
|
||||||
|
|
||||||
|
if (runs.totalDocs > 0) {
|
||||||
|
console.log('✅ SUCCESS: Workflow was triggered!')
|
||||||
|
console.log('Run status:', runs.docs[0].status)
|
||||||
|
console.log('Run context:', JSON.stringify(runs.docs[0].context, null, 2))
|
||||||
|
} else {
|
||||||
|
console.log('❌ FAILURE: Workflow was not triggered')
|
||||||
|
}
|
||||||
|
|
||||||
|
} catch (error) {
|
||||||
|
console.error('Test failed:', error)
|
||||||
|
} finally {
|
||||||
|
await payload.shutdown()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
testWorkflowTrigger().catch(console.error)
|
||||||
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.11",
|
"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.11",
|
"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.11",
|
"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",
|
||||||
@@ -85,6 +85,7 @@
|
|||||||
"react-dom": "19.1.0",
|
"react-dom": "19.1.0",
|
||||||
"rimraf": "3.0.2",
|
"rimraf": "3.0.2",
|
||||||
"sharp": "0.34.3",
|
"sharp": "0.34.3",
|
||||||
|
"tsx": "^4.20.5",
|
||||||
"typescript": "5.7.3",
|
"typescript": "5.7.3",
|
||||||
"vitest": "^3.1.2"
|
"vitest": "^3.1.2"
|
||||||
},
|
},
|
||||||
|
|||||||
4
pnpm-lock.yaml
generated
4
pnpm-lock.yaml
generated
@@ -90,6 +90,9 @@ importers:
|
|||||||
sharp:
|
sharp:
|
||||||
specifier: 0.34.3
|
specifier: 0.34.3
|
||||||
version: 0.34.3
|
version: 0.34.3
|
||||||
|
tsx:
|
||||||
|
specifier: ^4.20.5
|
||||||
|
version: 4.20.5
|
||||||
typescript:
|
typescript:
|
||||||
specifier: 5.7.3
|
specifier: 5.7.3
|
||||||
version: 5.7.3
|
version: 5.7.3
|
||||||
@@ -9617,7 +9620,6 @@ snapshots:
|
|||||||
get-tsconfig: 4.10.1
|
get-tsconfig: 4.10.1
|
||||||
optionalDependencies:
|
optionalDependencies:
|
||||||
fsevents: 2.3.3
|
fsevents: 2.3.3
|
||||||
optional: true
|
|
||||||
|
|
||||||
type-check@0.4.0:
|
type-check@0.4.0:
|
||||||
dependencies:
|
dependencies:
|
||||||
|
|||||||
@@ -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',
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
import type { Payload, PayloadRequest } from 'payload'
|
import type { Payload, PayloadRequest } from 'payload'
|
||||||
|
|
||||||
import { initializeLogger } from '../plugin/logger.js'
|
import { initializeLogger } from '../plugin/logger.js'
|
||||||
import { type Workflow, WorkflowExecutor } from './workflow-executor.js'
|
import { type PayloadWorkflow, WorkflowExecutor } from './workflow-executor.js'
|
||||||
|
|
||||||
export interface CustomTriggerOptions {
|
export interface CustomTriggerOptions {
|
||||||
/**
|
/**
|
||||||
@@ -142,7 +142,7 @@ export async function triggerCustomWorkflow(
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Execute the workflow
|
// Execute the workflow
|
||||||
await executor.execute(workflow as Workflow, context, workflowReq)
|
await executor.execute(workflow as PayloadWorkflow, context, workflowReq)
|
||||||
|
|
||||||
// Get the latest run for this workflow to get the run ID
|
// Get the latest run for this workflow to get the run ID
|
||||||
const runs = await payload.find({
|
const runs = await payload.find({
|
||||||
@@ -255,7 +255,7 @@ export async function triggerWorkflowById(
|
|||||||
|
|
||||||
// Create executor and execute
|
// Create executor and execute
|
||||||
const executor = new WorkflowExecutor(payload, logger)
|
const executor = new WorkflowExecutor(payload, logger)
|
||||||
await executor.execute(workflow as Workflow, context, workflowReq)
|
await executor.execute(workflow as PayloadWorkflow, context, workflowReq)
|
||||||
|
|
||||||
// Get the latest run to get the run ID
|
// Get the latest run to get the run ID
|
||||||
const runs = await payload.find({
|
const runs = await payload.find({
|
||||||
|
|||||||
@@ -1,31 +1,39 @@
|
|||||||
import type { Payload, PayloadRequest } from 'payload'
|
import type { Payload, PayloadRequest } from 'payload'
|
||||||
|
|
||||||
|
// We need to reference the generated types dynamically since they're not available at build time
|
||||||
|
// Using generic types and casting where necessary
|
||||||
|
export type PayloadWorkflow = {
|
||||||
|
id: number
|
||||||
|
name: string
|
||||||
|
description?: string | null
|
||||||
|
triggers?: Array<{
|
||||||
|
type?: string | null
|
||||||
|
collectionSlug?: string | null
|
||||||
|
operation?: string | null
|
||||||
|
condition?: string | null
|
||||||
|
[key: string]: unknown
|
||||||
|
}> | null
|
||||||
|
steps?: Array<{
|
||||||
|
step?: string | null
|
||||||
|
name?: string | null
|
||||||
|
input?: unknown
|
||||||
|
dependencies?: string[] | null
|
||||||
|
condition?: string | null
|
||||||
|
[key: string]: unknown
|
||||||
|
}> | null
|
||||||
|
[key: string]: unknown
|
||||||
|
}
|
||||||
|
|
||||||
import { JSONPath } from 'jsonpath-plus'
|
import { JSONPath } from 'jsonpath-plus'
|
||||||
|
|
||||||
export type Workflow = {
|
// Helper type to extract workflow step data from the generated types
|
||||||
_version?: number
|
export type WorkflowStep = NonNullable<PayloadWorkflow['steps']>[0] & {
|
||||||
id: string
|
name: string // Ensure name is always present for our execution logic
|
||||||
name: string
|
|
||||||
steps: WorkflowStep[]
|
|
||||||
triggers: WorkflowTrigger[]
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export type WorkflowStep = {
|
// Helper type to extract workflow trigger data from the generated types
|
||||||
condition?: string
|
export type WorkflowTrigger = NonNullable<PayloadWorkflow['triggers']>[0] & {
|
||||||
dependencies?: string[]
|
type: string // Ensure type is always present for our execution logic
|
||||||
input?: null | Record<string, unknown>
|
|
||||||
name: string
|
|
||||||
step: string
|
|
||||||
}
|
|
||||||
|
|
||||||
export interface WorkflowTrigger {
|
|
||||||
collection?: string
|
|
||||||
condition?: string
|
|
||||||
global?: string
|
|
||||||
globalOperation?: string
|
|
||||||
operation?: string
|
|
||||||
type: string
|
|
||||||
webhookPath?: string
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface ExecutionContext {
|
export interface ExecutionContext {
|
||||||
@@ -154,7 +162,7 @@ export class WorkflowExecutor {
|
|||||||
|
|
||||||
try {
|
try {
|
||||||
// Resolve input data using JSONPath
|
// Resolve input data using JSONPath
|
||||||
const resolvedInput = this.resolveStepInput(step.input || {}, context)
|
const resolvedInput = this.resolveStepInput(step.input as Record<string, unknown> || {}, context)
|
||||||
context.steps[stepName].input = resolvedInput
|
context.steps[stepName].input = resolvedInput
|
||||||
|
|
||||||
if (!taskSlug) {
|
if (!taskSlug) {
|
||||||
@@ -398,6 +406,47 @@ export class WorkflowExecutor {
|
|||||||
return resolved
|
return resolved
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Safely serialize an object, handling circular references and non-serializable values
|
||||||
|
*/
|
||||||
|
private safeSerialize(obj: unknown): unknown {
|
||||||
|
const seen = new WeakSet()
|
||||||
|
|
||||||
|
const serialize = (value: unknown): unknown => {
|
||||||
|
if (value === null || typeof value !== 'object') {
|
||||||
|
return value
|
||||||
|
}
|
||||||
|
|
||||||
|
if (seen.has(value as object)) {
|
||||||
|
return '[Circular Reference]'
|
||||||
|
}
|
||||||
|
|
||||||
|
seen.add(value as object)
|
||||||
|
|
||||||
|
if (Array.isArray(value)) {
|
||||||
|
return value.map(serialize)
|
||||||
|
}
|
||||||
|
|
||||||
|
const result: Record<string, unknown> = {}
|
||||||
|
for (const [key, val] of Object.entries(value as Record<string, unknown>)) {
|
||||||
|
try {
|
||||||
|
// Skip non-serializable properties that are likely internal database objects
|
||||||
|
if (key === 'table' || key === 'schema' || key === '_' || key === '__') {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
result[key] = serialize(val)
|
||||||
|
} catch {
|
||||||
|
// Skip properties that can't be accessed or serialized
|
||||||
|
result[key] = '[Non-serializable]'
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return result
|
||||||
|
}
|
||||||
|
|
||||||
|
return serialize(obj)
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Update workflow run with current context
|
* Update workflow run with current context
|
||||||
*/
|
*/
|
||||||
@@ -407,14 +456,14 @@ export class WorkflowExecutor {
|
|||||||
req: PayloadRequest
|
req: PayloadRequest
|
||||||
): Promise<void> {
|
): Promise<void> {
|
||||||
const serializeContext = () => ({
|
const serializeContext = () => ({
|
||||||
steps: context.steps,
|
steps: this.safeSerialize(context.steps),
|
||||||
trigger: {
|
trigger: {
|
||||||
type: context.trigger.type,
|
type: context.trigger.type,
|
||||||
collection: context.trigger.collection,
|
collection: context.trigger.collection,
|
||||||
data: context.trigger.data,
|
data: this.safeSerialize(context.trigger.data),
|
||||||
doc: context.trigger.doc,
|
doc: this.safeSerialize(context.trigger.doc),
|
||||||
operation: context.trigger.operation,
|
operation: context.trigger.operation,
|
||||||
previousDoc: context.trigger.previousDoc,
|
previousDoc: this.safeSerialize(context.trigger.previousDoc),
|
||||||
triggeredAt: context.trigger.triggeredAt,
|
triggeredAt: context.trigger.triggeredAt,
|
||||||
user: context.trigger.req?.user
|
user: context.trigger.req?.user
|
||||||
}
|
}
|
||||||
@@ -431,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({
|
||||||
@@ -443,6 +492,65 @@ export class WorkflowExecutor {
|
|||||||
}, 'Starting condition evaluation')
|
}, 'Starting condition evaluation')
|
||||||
|
|
||||||
try {
|
try {
|
||||||
|
// Check if this is a comparison expression
|
||||||
|
const comparisonMatch = condition.match(/^(.+?)\s*(==|!=|>|<|>=|<=)\s*(.+)$/)
|
||||||
|
|
||||||
|
if (comparisonMatch) {
|
||||||
|
const [, leftExpr, operator, rightExpr] = comparisonMatch
|
||||||
|
|
||||||
|
// Evaluate left side (should be JSONPath)
|
||||||
|
const leftValue = this.resolveJSONPathValue(leftExpr.trim(), context)
|
||||||
|
|
||||||
|
// Parse right side (could be string, number, boolean, or JSONPath)
|
||||||
|
const rightValue = this.parseConditionValue(rightExpr.trim(), context)
|
||||||
|
|
||||||
|
this.logger.debug({
|
||||||
|
condition,
|
||||||
|
leftExpr: leftExpr.trim(),
|
||||||
|
leftValue,
|
||||||
|
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 {
|
||||||
|
// Treat as simple JSONPath boolean evaluation
|
||||||
const result = JSONPath({
|
const result = JSONPath({
|
||||||
json: context,
|
json: context,
|
||||||
path: condition,
|
path: condition,
|
||||||
@@ -454,7 +562,7 @@ export class WorkflowExecutor {
|
|||||||
result,
|
result,
|
||||||
resultType: Array.isArray(result) ? 'array' : typeof result,
|
resultType: Array.isArray(result) ? 'array' : typeof result,
|
||||||
resultLength: Array.isArray(result) ? result.length : undefined
|
resultLength: Array.isArray(result) ? result.length : undefined
|
||||||
}, 'JSONPath evaluation result')
|
}, 'JSONPath boolean evaluation result')
|
||||||
|
|
||||||
// Handle different result types
|
// Handle different result types
|
||||||
let finalResult: boolean
|
let finalResult: boolean
|
||||||
@@ -468,9 +576,10 @@ export class WorkflowExecutor {
|
|||||||
condition,
|
condition,
|
||||||
finalResult,
|
finalResult,
|
||||||
originalResult: result
|
originalResult: result
|
||||||
}, 'Condition evaluation completed')
|
}, 'Boolean condition evaluation completed')
|
||||||
|
|
||||||
return finalResult
|
return finalResult
|
||||||
|
}
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
this.logger.warn({
|
this.logger.warn({
|
||||||
condition,
|
condition,
|
||||||
@@ -483,31 +592,88 @@ export class WorkflowExecutor {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 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
|
||||||
*/
|
*/
|
||||||
async execute(workflow: Workflow, context: ExecutionContext, req: PayloadRequest): Promise<void> {
|
async execute(workflow: PayloadWorkflow, context: ExecutionContext, req: PayloadRequest): Promise<void> {
|
||||||
this.logger.info({
|
this.logger.info({
|
||||||
workflowId: workflow.id,
|
workflowId: workflow.id,
|
||||||
workflowName: workflow.name
|
workflowName: workflow.name
|
||||||
}, 'Starting workflow execution')
|
}, 'Starting workflow execution')
|
||||||
|
|
||||||
const serializeContext = () => ({
|
const serializeContext = () => ({
|
||||||
steps: context.steps,
|
steps: this.safeSerialize(context.steps),
|
||||||
trigger: {
|
trigger: {
|
||||||
type: context.trigger.type,
|
type: context.trigger.type,
|
||||||
collection: context.trigger.collection,
|
collection: context.trigger.collection,
|
||||||
data: context.trigger.data,
|
data: this.safeSerialize(context.trigger.data),
|
||||||
doc: context.trigger.doc,
|
doc: this.safeSerialize(context.trigger.doc),
|
||||||
operation: context.trigger.operation,
|
operation: context.trigger.operation,
|
||||||
previousDoc: context.trigger.previousDoc,
|
previousDoc: this.safeSerialize(context.trigger.previousDoc),
|
||||||
triggeredAt: context.trigger.triggeredAt,
|
triggeredAt: context.trigger.triggeredAt,
|
||||||
user: context.trigger.req?.user
|
user: context.trigger.req?.user
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
|
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;
|
||||||
|
try {
|
||||||
|
workflowRun = await this.payload.create({
|
||||||
collection: 'workflow-runs',
|
collection: 'workflow-runs',
|
||||||
data: {
|
data: {
|
||||||
context: serializeContext(),
|
context: serializeContext(),
|
||||||
@@ -515,14 +681,29 @@ export class WorkflowExecutor {
|
|||||||
status: 'running',
|
status: 'running',
|
||||||
triggeredBy: context.trigger.req?.user?.email || 'system',
|
triggeredBy: context.trigger.req?.user?.email || 'system',
|
||||||
workflow: workflow.id,
|
workflow: workflow.id,
|
||||||
workflowVersion: workflow._version || 1
|
workflowVersion: 1 // Default version since generated type doesn't have _version field
|
||||||
},
|
},
|
||||||
req
|
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
|
||||||
const executionBatches = this.resolveExecutionOrder(workflow.steps)
|
const executionBatches = this.resolveExecutionOrder(workflow.steps as WorkflowStep[] || [])
|
||||||
|
|
||||||
this.logger.info({
|
this.logger.info({
|
||||||
batchSizes: executionBatches.map(batch => batch.length),
|
batchSizes: executionBatches.map(batch => batch.length),
|
||||||
@@ -606,6 +787,19 @@ 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({
|
||||||
|
collection,
|
||||||
|
operation,
|
||||||
|
docId: (doc as any)?.id
|
||||||
|
}, 'executeTriggeredWorkflows called')
|
||||||
|
|
||||||
try {
|
try {
|
||||||
// Find workflows with matching triggers
|
// Find workflows with matching triggers
|
||||||
const workflows = await this.payload.find({
|
const workflows = await this.payload.find({
|
||||||
@@ -615,22 +809,59 @@ export class WorkflowExecutor {
|
|||||||
req
|
req
|
||||||
})
|
})
|
||||||
|
|
||||||
|
this.logger.info({
|
||||||
|
workflowCount: workflows.docs.length
|
||||||
|
}, 'Found workflows to check')
|
||||||
|
|
||||||
for (const workflow of workflows.docs) {
|
for (const workflow of workflows.docs) {
|
||||||
// Check if this workflow has a matching trigger
|
// Check if this workflow has a matching trigger
|
||||||
const triggers = workflow.triggers as Array<{
|
const triggers = workflow.triggers as Array<{
|
||||||
collection: string
|
collection?: string
|
||||||
|
collectionSlug?: string
|
||||||
condition?: string
|
condition?: string
|
||||||
operation: string
|
operation: string
|
||||||
type: string
|
type: string
|
||||||
}>
|
}>
|
||||||
|
|
||||||
|
this.logger.debug({
|
||||||
|
workflowId: workflow.id,
|
||||||
|
workflowName: workflow.name,
|
||||||
|
triggerCount: triggers?.length || 0,
|
||||||
|
triggers: triggers?.map(t => ({
|
||||||
|
type: t.type,
|
||||||
|
collection: t.collection,
|
||||||
|
collectionSlug: t.collectionSlug,
|
||||||
|
operation: t.operation
|
||||||
|
}))
|
||||||
|
}, 'Checking workflow triggers')
|
||||||
|
|
||||||
const matchingTriggers = triggers?.filter(trigger =>
|
const matchingTriggers = triggers?.filter(trigger =>
|
||||||
trigger.type === 'collection-trigger' &&
|
trigger.type === 'collection-trigger' &&
|
||||||
trigger.collection === collection &&
|
(trigger.collection === collection || trigger.collectionSlug === collection) &&
|
||||||
trigger.operation === operation
|
trigger.operation === operation
|
||||||
) || []
|
) || []
|
||||||
|
|
||||||
|
this.logger.info({
|
||||||
|
workflowId: workflow.id,
|
||||||
|
workflowName: workflow.name,
|
||||||
|
matchingTriggerCount: matchingTriggers.length,
|
||||||
|
targetCollection: collection,
|
||||||
|
targetOperation: operation
|
||||||
|
}, '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: {},
|
||||||
@@ -689,7 +920,7 @@ export class WorkflowExecutor {
|
|||||||
}, 'Triggering workflow')
|
}, 'Triggering workflow')
|
||||||
|
|
||||||
// Execute the workflow
|
// Execute the workflow
|
||||||
await this.execute(workflow as Workflow, context, req)
|
await this.execute(workflow as PayloadWorkflow, context, req)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
|
|||||||
17
src/index.ts
17
src/index.ts
@@ -1,10 +1,19 @@
|
|||||||
// Main export contains only types and client-safe utilities
|
// Main export contains only types and client-safe utilities
|
||||||
// Server-side functions are exported via '@xtr-dev/payload-automation/server'
|
// Server-side functions are exported via '@xtr-dev/payload-automation/server'
|
||||||
|
|
||||||
// Types only - safe for client bundling
|
// Pure types only - completely safe for client bundling
|
||||||
export type { CustomTriggerOptions, TriggerResult } from './core/trigger-custom-workflow.js'
|
export type {
|
||||||
export type { ExecutionContext, Workflow, WorkflowStep, WorkflowTrigger } from './core/workflow-executor.js'
|
CustomTriggerOptions,
|
||||||
export type { WorkflowsPluginConfig } from './plugin/config-types.js'
|
TriggerResult,
|
||||||
|
ExecutionContext,
|
||||||
|
WorkflowsPluginConfig
|
||||||
|
} from './types/index.js'
|
||||||
|
|
||||||
|
export type {
|
||||||
|
PayloadWorkflow as Workflow,
|
||||||
|
WorkflowStep,
|
||||||
|
WorkflowTrigger
|
||||||
|
} from './core/workflow-executor.js'
|
||||||
|
|
||||||
// Server-side functions are NOT re-exported here to avoid bundling issues
|
// Server-side functions are NOT re-exported here to avoid bundling issues
|
||||||
// Import server-side functions from the /server export instead
|
// Import server-side functions from the /server export instead
|
||||||
|
|||||||
@@ -2,7 +2,7 @@ import type {Config, Payload, TaskConfig} from 'payload'
|
|||||||
|
|
||||||
import cron from 'node-cron'
|
import cron from 'node-cron'
|
||||||
|
|
||||||
import {type Workflow, WorkflowExecutor} from '../core/workflow-executor.js'
|
import {type PayloadWorkflow, WorkflowExecutor} from '../core/workflow-executor.js'
|
||||||
import {getConfigLogger} from './logger.js'
|
import {getConfigLogger} from './logger.js'
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -101,7 +101,7 @@ export function generateCronTasks(config: Config): void {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Execute the workflow
|
// Execute the workflow
|
||||||
await executor.execute(workflow as Workflow, context, req)
|
await executor.execute(workflow as PayloadWorkflow, context, req)
|
||||||
|
|
||||||
// Re-queue the job for the next scheduled execution if cronExpression is provided
|
// Re-queue the job for the next scheduled execution if cronExpression is provided
|
||||||
if (cronExpression) {
|
if (cronExpression) {
|
||||||
|
|||||||
@@ -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>) =>
|
||||||
@@ -40,6 +58,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)))
|
||||||
|
|
||||||
// Initialize hooks
|
// Register executor globally
|
||||||
logger.info('Initializing collection hooks...')
|
setWorkflowExecutor(executor)
|
||||||
initCollectionHooks(pluginOptions, payload, logger, executor)
|
|
||||||
|
// 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...')
|
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,10 +39,26 @@ 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')
|
||||||
|
|
||||||
|
try {
|
||||||
|
console.log('🚨 About to call executeTriggeredWorkflows')
|
||||||
|
|
||||||
// Execute workflows for this trigger
|
// Execute workflows for this trigger
|
||||||
await executor.executeTriggeredWorkflows(
|
await executor.executeTriggeredWorkflows(
|
||||||
@@ -52,6 +68,26 @@ export function initCollectionHooks<T extends string>(pluginOptions: WorkflowsPl
|
|||||||
change.previousDoc,
|
change.previousDoc,
|
||||||
change.req
|
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
|
||||||
|
}
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
import type { Payload, PayloadRequest } from "payload"
|
import type { Payload, PayloadRequest } from "payload"
|
||||||
import type { Logger } from "pino"
|
import type { Logger } from "pino"
|
||||||
|
|
||||||
import type { WorkflowExecutor, Workflow } from "../core/workflow-executor.js"
|
import type { WorkflowExecutor, PayloadWorkflow } from "../core/workflow-executor.js"
|
||||||
|
|
||||||
export function initGlobalHooks(payload: Payload, logger: Payload['logger'], executor: WorkflowExecutor) {
|
export function initGlobalHooks(payload: Payload, logger: Payload['logger'], executor: WorkflowExecutor) {
|
||||||
// Get all globals from the config
|
// Get all globals from the config
|
||||||
@@ -100,7 +100,7 @@ async function executeTriggeredGlobalWorkflows(
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Execute the workflow
|
// Execute the workflow
|
||||||
await executor.execute(workflow as Workflow, context, req)
|
await executor.execute(workflow as PayloadWorkflow, context, req)
|
||||||
}
|
}
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
logger.error({
|
logger.error({
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
import type {Config, PayloadRequest} from 'payload'
|
import type {Config, PayloadRequest} from 'payload'
|
||||||
|
|
||||||
import {type Workflow, WorkflowExecutor} from '../core/workflow-executor.js'
|
import {type PayloadWorkflow, WorkflowExecutor} from '../core/workflow-executor.js'
|
||||||
import {getConfigLogger, initializeLogger} from './logger.js'
|
import {getConfigLogger, initializeLogger} from './logger.js'
|
||||||
|
|
||||||
export function initWebhookEndpoint(config: Config, webhookPrefix = 'webhook'): void {
|
export function initWebhookEndpoint(config: Config, webhookPrefix = 'webhook'): void {
|
||||||
@@ -110,7 +110,7 @@ export function initWebhookEndpoint(config: Config, webhookPrefix = 'webhook'):
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Execute the workflow
|
// Execute the workflow
|
||||||
await executor.execute(workflow as Workflow, context, req)
|
await executor.execute(workflow as PayloadWorkflow, context, req)
|
||||||
|
|
||||||
return { status: 'triggered', workflowId: workflow.id }
|
return { status: 'triggered', workflowId: workflow.id }
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
|
|||||||
41
src/types/index.ts
Normal file
41
src/types/index.ts
Normal file
@@ -0,0 +1,41 @@
|
|||||||
|
// Pure type definitions for client-safe exports
|
||||||
|
// This file contains NO runtime code and can be safely bundled
|
||||||
|
|
||||||
|
export interface CustomTriggerOptions {
|
||||||
|
workflowId: string
|
||||||
|
triggerData?: any
|
||||||
|
req?: any // PayloadRequest type, but avoiding import to keep this client-safe
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface TriggerResult {
|
||||||
|
success: boolean
|
||||||
|
runId?: string
|
||||||
|
error?: string
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface ExecutionContext {
|
||||||
|
trigger: {
|
||||||
|
type: string
|
||||||
|
doc?: any
|
||||||
|
data?: any
|
||||||
|
}
|
||||||
|
steps: Record<string, {
|
||||||
|
output?: any
|
||||||
|
state: 'pending' | 'running' | 'succeeded' | 'failed'
|
||||||
|
}>
|
||||||
|
payload: any // Payload instance
|
||||||
|
req: any // PayloadRequest
|
||||||
|
}
|
||||||
|
|
||||||
|
// NOTE: Workflow, WorkflowStep, and WorkflowTrigger types are now imported from the generated PayloadCMS types
|
||||||
|
// These interfaces have been removed to avoid duplication and inconsistencies
|
||||||
|
// Import them from 'payload' or the generated payload-types.ts file instead
|
||||||
|
|
||||||
|
export interface WorkflowsPluginConfig {
|
||||||
|
collections?: string[]
|
||||||
|
globals?: string[]
|
||||||
|
logging?: {
|
||||||
|
level?: 'debug' | 'info' | 'warn' | 'error'
|
||||||
|
enabled?: boolean
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user