mirror of
https://github.com/xtr-dev/payload-automation.git
synced 2025-12-10 00:43:23 +00:00
WIP: Refactor triggers to TriggerConfig pattern
- Convert webhook, global, and cron triggers to use TriggerConfig pattern like collectionTrigger - Simplify trigger slug names (remove '-trigger' suffix) - Update validation to use new slug names - Add perfectionist/sort-exports rule disable - Note: Workflow.ts integration still needs fixes for type compatibility
This commit is contained in:
@@ -1,19 +1,14 @@
|
||||
import type {Field} from 'payload'
|
||||
import type {TriggerConfig} from '../plugin/config-types.js'
|
||||
|
||||
import type { WorkflowsPluginConfig } from '../plugin/config-types.js'
|
||||
|
||||
import {triggerField} from "./helpers.js"
|
||||
|
||||
export function getCollectionTriggerFields<T extends string>(
|
||||
collectionTriggers: WorkflowsPluginConfig<T>['collectionTriggers']
|
||||
): Field[] {
|
||||
return [
|
||||
triggerField({
|
||||
export const collectionTrigger: TriggerConfig = ({collectionTriggers}) => ({
|
||||
slug: 'collection',
|
||||
fields: [
|
||||
{
|
||||
name: 'collectionSlug',
|
||||
type: 'select',
|
||||
options: Object.keys(collectionTriggers || {}),
|
||||
}),
|
||||
triggerField({
|
||||
},
|
||||
{
|
||||
name: 'operation',
|
||||
type: 'select',
|
||||
options: [
|
||||
@@ -22,6 +17,6 @@ export function getCollectionTriggerFields<T extends string>(
|
||||
'read',
|
||||
'update',
|
||||
],
|
||||
})
|
||||
}
|
||||
]
|
||||
}
|
||||
})
|
||||
|
||||
@@ -1,25 +1,23 @@
|
||||
import type {Field} from 'payload'
|
||||
import type {TriggerConfig} from '../plugin/config-types.js'
|
||||
|
||||
import {triggerField} from "./helpers.js"
|
||||
|
||||
export function getCronTriggerFields(): Field[] {
|
||||
return [
|
||||
triggerField({
|
||||
export const cronTrigger: TriggerConfig = () => ({
|
||||
slug: 'cron',
|
||||
fields: [
|
||||
{
|
||||
name: 'cronExpression',
|
||||
type: 'text',
|
||||
admin: {
|
||||
condition: (_, siblingData) => siblingData?.type === 'cron-trigger',
|
||||
description: 'Cron expression for scheduled execution (e.g., "0 0 * * *" for daily at midnight)',
|
||||
placeholder: '0 0 * * *'
|
||||
},
|
||||
validate: (value: any, {siblingData}: any) => {
|
||||
const cronValue = value || siblingData?.parameters?.cronExpression
|
||||
if (siblingData?.type === 'cron-trigger' && !cronValue) {
|
||||
if (siblingData?.type === 'cron' && !cronValue) {
|
||||
return 'Cron expression is required for cron triggers'
|
||||
}
|
||||
|
||||
// Validate cron expression format if provided
|
||||
if (siblingData?.type === 'cron-trigger' && cronValue) {
|
||||
if (siblingData?.type === 'cron' && cronValue) {
|
||||
// Basic format validation - should be 5 parts separated by spaces
|
||||
const cronParts = cronValue.trim().split(/\s+/)
|
||||
if (cronParts.length !== 5) {
|
||||
@@ -32,19 +30,18 @@ export function getCronTriggerFields(): Field[] {
|
||||
|
||||
return true
|
||||
},
|
||||
}),
|
||||
triggerField({
|
||||
},
|
||||
{
|
||||
name: 'timezone',
|
||||
type: 'text',
|
||||
admin: {
|
||||
condition: (_, siblingData) => siblingData?.type === 'cron-trigger',
|
||||
description: 'Timezone for cron execution (e.g., "America/New_York", "Europe/London"). Defaults to UTC.',
|
||||
placeholder: 'UTC'
|
||||
},
|
||||
defaultValue: 'UTC',
|
||||
validate: (value: any, {siblingData}: any) => {
|
||||
const tzValue = value || siblingData?.parameters?.timezone
|
||||
if (siblingData?.type === 'cron-trigger' && tzValue) {
|
||||
if (siblingData?.type === 'cron' && tzValue) {
|
||||
try {
|
||||
// Test if timezone is valid by trying to create a date with it
|
||||
new Intl.DateTimeFormat('en', {timeZone: tzValue})
|
||||
@@ -55,6 +52,6 @@ export function getCronTriggerFields(): Field[] {
|
||||
}
|
||||
return true
|
||||
},
|
||||
})
|
||||
}
|
||||
]
|
||||
}
|
||||
})
|
||||
@@ -1,28 +1,25 @@
|
||||
import type {Field} from 'payload'
|
||||
import type {TriggerConfig} from '../plugin/config-types.js'
|
||||
|
||||
import {triggerField} from "./helpers.js"
|
||||
|
||||
export function getGlobalTriggerFields(): Field[] {
|
||||
return [
|
||||
triggerField({
|
||||
export const globalTrigger: TriggerConfig = () => ({
|
||||
slug: 'global',
|
||||
fields: [
|
||||
{
|
||||
name: 'global',
|
||||
type: 'select',
|
||||
admin: {
|
||||
condition: (_, siblingData) => siblingData?.type === 'global-trigger',
|
||||
description: 'Global that triggers the workflow',
|
||||
},
|
||||
options: [], // Will be populated dynamically based on available globals
|
||||
}),
|
||||
triggerField({
|
||||
},
|
||||
{
|
||||
name: 'globalOperation',
|
||||
type: 'select',
|
||||
admin: {
|
||||
condition: (_, siblingData) => siblingData?.type === 'global-trigger',
|
||||
description: 'Global operation that triggers the workflow',
|
||||
},
|
||||
options: [
|
||||
'update'
|
||||
],
|
||||
})
|
||||
}
|
||||
]
|
||||
}
|
||||
})
|
||||
@@ -1,12 +1,10 @@
|
||||
import type {Field, TextField, SelectField} from "payload"
|
||||
import type {Field} from "payload"
|
||||
|
||||
import type {Trigger} from "./types.js"
|
||||
|
||||
type FieldWithName = TextField | SelectField | (Field & { name: string })
|
||||
|
||||
type Options = {
|
||||
slug: string,
|
||||
fields?: FieldWithName[]
|
||||
fields?: ({name: string} & Field)[]
|
||||
}
|
||||
|
||||
export const trigger = ({
|
||||
@@ -15,18 +13,18 @@ export const trigger = ({
|
||||
}: Options): Trigger => {
|
||||
return {
|
||||
slug,
|
||||
fields: (fields || []).map(triggerField) as Field[]
|
||||
fields: (fields || []).map(f => triggerField(slug, f))
|
||||
}
|
||||
}
|
||||
|
||||
export const triggerField = (field: FieldWithName): Field => ({
|
||||
export const triggerField = (slug: string, field: {name: string} & Field): Field => ({
|
||||
...field,
|
||||
name: '__trigger_' + field.name,
|
||||
admin: {
|
||||
...(field.admin as any || {}),
|
||||
...(field.admin as unknown || {}),
|
||||
condition: (_, siblingData, __) => {
|
||||
const previous = field.admin?.condition?.call(null, _, siblingData, __)
|
||||
return previous !== false // Preserve existing condition if it exists
|
||||
return previous || (siblingData?.type === slug)
|
||||
},
|
||||
},
|
||||
hooks: {
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
export { getCollectionTriggerFields } from './collection-trigger.js'
|
||||
export { getCronTriggerFields } from './cron-trigger.js'
|
||||
export { getGlobalTriggerFields } from './global-trigger.js'
|
||||
export { getWebhookTriggerFields } from './webhook-trigger.js'
|
||||
export { collectionTrigger } from './collection-trigger.js'
|
||||
export { cronTrigger } from './cron-trigger.js'
|
||||
export { globalTrigger } from './global-trigger.js'
|
||||
export { webhookTrigger } from './webhook-trigger.js'
|
||||
|
||||
@@ -1,22 +1,20 @@
|
||||
import type {Field} from 'payload'
|
||||
import type {TriggerConfig} from '../plugin/config-types.js'
|
||||
|
||||
import {triggerField} from "./helpers.js"
|
||||
|
||||
export function getWebhookTriggerFields(): Field[] {
|
||||
return [
|
||||
triggerField({
|
||||
export const webhookTrigger: TriggerConfig = () => ({
|
||||
slug: 'webhook',
|
||||
fields: [
|
||||
{
|
||||
name: 'webhookPath',
|
||||
type: 'text',
|
||||
admin: {
|
||||
condition: (_, siblingData) => siblingData?.type === 'webhook-trigger',
|
||||
description: 'URL path for the webhook (e.g., "my-webhook"). Full URL will be /api/workflows-webhook/my-webhook',
|
||||
},
|
||||
validate: (value: any, {siblingData}: any) => {
|
||||
if (siblingData?.type === 'webhook-trigger' && !value && !siblingData?.parameters?.webhookPath) {
|
||||
if (siblingData?.type === 'webhook' && !value && !siblingData?.parameters?.webhookPath) {
|
||||
return 'Webhook path is required for webhook triggers'
|
||||
}
|
||||
return true
|
||||
},
|
||||
})
|
||||
}
|
||||
]
|
||||
}
|
||||
})
|
||||
Reference in New Issue
Block a user