@xtr-dev/payload-automation 0.0.1 → 0.0.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -22,11 +22,28 @@ export class WorkflowExecutor {
22
22
  }, 'Executing step');
23
23
  // Check step condition if present
24
24
  if (step.condition) {
25
+ this.logger.debug({
26
+ condition: step.condition,
27
+ stepName,
28
+ availableSteps: Object.keys(context.steps),
29
+ completedSteps: Object.entries(context.steps).filter(([_, s])=>s.state === 'succeeded').map(([name])=>name),
30
+ triggerType: context.trigger?.type
31
+ }, 'Evaluating step condition');
25
32
  const conditionMet = this.evaluateStepCondition(step.condition, context);
26
33
  if (!conditionMet) {
27
34
  this.logger.info({
28
35
  condition: step.condition,
29
- stepName
36
+ stepName,
37
+ contextSnapshot: JSON.stringify({
38
+ stepOutputs: Object.entries(context.steps).reduce((acc, [name, step])=>{
39
+ acc[name] = {
40
+ state: step.state,
41
+ hasOutput: !!step.output
42
+ };
43
+ return acc;
44
+ }, {}),
45
+ triggerData: context.trigger?.data ? 'present' : 'absent'
46
+ })
30
47
  }, 'Step condition not met, skipping');
31
48
  // Mark step as completed but skipped
32
49
  context.steps[stepName] = {
@@ -46,7 +63,17 @@ export class WorkflowExecutor {
46
63
  }
47
64
  this.logger.info({
48
65
  condition: step.condition,
49
- stepName
66
+ stepName,
67
+ contextSnapshot: JSON.stringify({
68
+ stepOutputs: Object.entries(context.steps).reduce((acc, [name, step])=>{
69
+ acc[name] = {
70
+ state: step.state,
71
+ hasOutput: !!step.output
72
+ };
73
+ return acc;
74
+ }, {}),
75
+ triggerData: context.trigger?.data ? 'present' : 'absent'
76
+ })
50
77
  }, 'Step condition met, proceeding with execution');
51
78
  }
52
79
  // Initialize step context
@@ -210,32 +237,59 @@ export class WorkflowExecutor {
210
237
  * Resolve step input using JSONPath expressions
211
238
  */ resolveStepInput(config, context) {
212
239
  const resolved = {};
240
+ this.logger.debug({
241
+ configKeys: Object.keys(config),
242
+ contextSteps: Object.keys(context.steps),
243
+ triggerType: context.trigger?.type
244
+ }, 'Starting step input resolution');
213
245
  for (const [key, value] of Object.entries(config)){
214
246
  if (typeof value === 'string' && value.startsWith('$')) {
215
247
  // This is a JSONPath expression
248
+ this.logger.debug({
249
+ key,
250
+ jsonPath: value,
251
+ availableSteps: Object.keys(context.steps),
252
+ hasTriggerData: !!context.trigger?.data,
253
+ hasTriggerDoc: !!context.trigger?.doc
254
+ }, 'Resolving JSONPath expression');
216
255
  try {
217
256
  const result = JSONPath({
218
257
  json: context,
219
258
  path: value,
220
259
  wrap: false
221
260
  });
261
+ this.logger.debug({
262
+ key,
263
+ jsonPath: value,
264
+ result: JSON.stringify(result).substring(0, 200),
265
+ resultType: Array.isArray(result) ? 'array' : typeof result
266
+ }, 'JSONPath resolved successfully');
222
267
  resolved[key] = result;
223
268
  } catch (error) {
224
269
  this.logger.warn({
225
270
  error: error instanceof Error ? error.message : 'Unknown error',
226
271
  key,
227
- path: value
272
+ path: value,
273
+ contextSnapshot: JSON.stringify(context).substring(0, 500)
228
274
  }, 'Failed to resolve JSONPath');
229
275
  resolved[key] = value; // Keep original value if resolution fails
230
276
  }
231
277
  } else if (typeof value === 'object' && value !== null) {
232
278
  // Recursively resolve nested objects
279
+ this.logger.debug({
280
+ key,
281
+ nestedKeys: Object.keys(value)
282
+ }, 'Recursively resolving nested object');
233
283
  resolved[key] = this.resolveStepInput(value, context);
234
284
  } else {
235
285
  // Keep literal values as-is
236
286
  resolved[key] = value;
237
287
  }
238
288
  }
289
+ this.logger.debug({
290
+ resolvedKeys: Object.keys(resolved),
291
+ originalKeys: Object.keys(config)
292
+ }, 'Step input resolution completed');
239
293
  return resolved;
240
294
  }
241
295
  /**
@@ -266,21 +320,43 @@ export class WorkflowExecutor {
266
320
  /**
267
321
  * Evaluate a condition using JSONPath
268
322
  */ evaluateCondition(condition, context) {
323
+ this.logger.debug({
324
+ condition,
325
+ contextKeys: Object.keys(context),
326
+ triggerType: context.trigger?.type,
327
+ triggerData: context.trigger?.data,
328
+ triggerDoc: context.trigger?.doc ? 'present' : 'absent'
329
+ }, 'Starting condition evaluation');
269
330
  try {
270
331
  const result = JSONPath({
271
332
  json: context,
272
333
  path: condition,
273
334
  wrap: false
274
335
  });
336
+ this.logger.debug({
337
+ condition,
338
+ result,
339
+ resultType: Array.isArray(result) ? 'array' : typeof result,
340
+ resultLength: Array.isArray(result) ? result.length : undefined
341
+ }, 'JSONPath evaluation result');
275
342
  // Handle different result types
343
+ let finalResult;
276
344
  if (Array.isArray(result)) {
277
- return result.length > 0 && Boolean(result[0]);
345
+ finalResult = result.length > 0 && Boolean(result[0]);
346
+ } else {
347
+ finalResult = Boolean(result);
278
348
  }
279
- return Boolean(result);
349
+ this.logger.debug({
350
+ condition,
351
+ finalResult,
352
+ originalResult: result
353
+ }, 'Condition evaluation completed');
354
+ return finalResult;
280
355
  } catch (error) {
281
356
  this.logger.warn({
282
357
  condition,
283
- error: error instanceof Error ? error.message : 'Unknown error'
358
+ error: error instanceof Error ? error.message : 'Unknown error',
359
+ errorStack: error instanceof Error ? error.stack : undefined
284
360
  }, 'Failed to evaluate condition');
285
361
  // If condition evaluation fails, assume false
286
362
  return false;
@@ -411,6 +487,16 @@ export class WorkflowExecutor {
411
487
  };
412
488
  // Check trigger condition if present
413
489
  if (trigger.condition) {
490
+ this.logger.debug({
491
+ collection,
492
+ operation,
493
+ condition: trigger.condition,
494
+ docId: doc?.id,
495
+ docFields: doc ? Object.keys(doc) : [],
496
+ previousDocId: previousDoc?.id,
497
+ workflowId: workflow.id,
498
+ workflowName: workflow.name
499
+ }, 'Evaluating collection trigger condition');
414
500
  const conditionMet = this.evaluateCondition(trigger.condition, context);
415
501
  if (!conditionMet) {
416
502
  this.logger.info({
@@ -418,7 +504,8 @@ export class WorkflowExecutor {
418
504
  condition: trigger.condition,
419
505
  operation,
420
506
  workflowId: workflow.id,
421
- workflowName: workflow.name
507
+ workflowName: workflow.name,
508
+ docSnapshot: JSON.stringify(doc).substring(0, 200)
422
509
  }, 'Trigger condition not met, skipping workflow');
423
510
  continue;
424
511
  }
@@ -427,7 +514,8 @@ export class WorkflowExecutor {
427
514
  condition: trigger.condition,
428
515
  operation,
429
516
  workflowId: workflow.id,
430
- workflowName: workflow.name
517
+ workflowName: workflow.name,
518
+ docSnapshot: JSON.stringify(doc).substring(0, 200)
431
519
  }, 'Trigger condition met');
432
520
  }
433
521
  this.logger.info({
@@ -1 +1 @@
1
- {"version":3,"sources":["../../src/core/workflow-executor.ts"],"sourcesContent":["import type { Payload, PayloadRequest } from 'payload'\n\nimport { JSONPath } from 'jsonpath-plus'\n\nexport type Workflow = {\n _version?: number\n id: string\n name: string\n steps: WorkflowStep[]\n triggers: WorkflowTrigger[]\n}\n\nexport type WorkflowStep = {\n condition?: string\n dependencies?: string[]\n input?: null | Record<string, unknown>\n name: string\n step: string\n}\n\nexport interface WorkflowTrigger {\n collection?: string\n condition?: string\n global?: string\n globalOperation?: string\n operation?: string\n type: string\n webhookPath?: string\n}\n\nexport interface ExecutionContext {\n steps: Record<string, {\n error?: string\n input: unknown\n output: unknown\n state: 'failed' | 'pending' | 'running' | 'succeeded'\n }>\n trigger: {\n collection?: string\n data?: unknown\n doc?: unknown\n headers?: Record<string, string>\n operation?: string\n path?: string\n previousDoc?: unknown\n req?: PayloadRequest\n triggeredAt?: string\n type: string\n user?: {\n collection?: string\n email?: string\n id?: string\n }\n }\n}\n\nexport class WorkflowExecutor {\n constructor(\n private payload: Payload,\n private logger: Payload['logger']\n ) {}\n\n /**\n * Evaluate a step condition using JSONPath\n */\n private evaluateStepCondition(condition: string, context: ExecutionContext): boolean {\n return this.evaluateCondition(condition, context)\n }\n\n /**\n * Execute a single workflow step\n */\n private async executeStep(\n step: WorkflowStep,\n stepIndex: number,\n context: ExecutionContext,\n req: PayloadRequest,\n workflowRunId?: number | string\n ): Promise<void> {\n const stepName = step.name || 'step-' + stepIndex\n\n this.logger.info({\n hasStep: 'step' in step,\n step: JSON.stringify(step),\n stepName\n }, 'Executing step')\n\n // Check step condition if present\n if (step.condition) {\n const conditionMet = this.evaluateStepCondition(step.condition, context)\n\n if (!conditionMet) {\n this.logger.info({\n condition: step.condition,\n stepName\n }, 'Step condition not met, skipping')\n\n // Mark step as completed but skipped\n context.steps[stepName] = {\n error: undefined,\n input: undefined,\n output: { reason: 'Condition not met', skipped: true },\n state: 'succeeded'\n }\n\n // Update workflow run context if needed\n if (workflowRunId) {\n await this.updateWorkflowRunContext(workflowRunId, context, req)\n }\n\n return\n }\n\n this.logger.info({\n condition: step.condition,\n stepName\n }, 'Step condition met, proceeding with execution')\n }\n\n // Initialize step context\n context.steps[stepName] = {\n error: undefined,\n input: undefined,\n output: undefined,\n state: 'running'\n }\n\n // Move taskSlug declaration outside try block so it's accessible in catch\n const taskSlug = step.step // Use the 'step' field for task type\n\n try {\n // Resolve input data using JSONPath\n const resolvedInput = this.resolveStepInput(step.input || {}, context)\n context.steps[stepName].input = resolvedInput\n\n if (!taskSlug) {\n throw new Error(`Step ${stepName} is missing a task type`)\n }\n\n this.logger.info({\n hasInput: !!resolvedInput,\n hasReq: !!req,\n stepName,\n taskSlug\n }, 'Queueing task')\n\n const job = await this.payload.jobs.queue({\n input: resolvedInput,\n req,\n task: taskSlug\n })\n\n // Run the job immediately\n await this.payload.jobs.run({\n limit: 1,\n req\n })\n\n // Get the job result\n const completedJob = await this.payload.findByID({\n id: job.id,\n collection: 'payload-jobs',\n req\n })\n\n const taskStatus = completedJob.taskStatus?.[completedJob.taskSlug]?.[completedJob.totalTried]\n const isComplete = taskStatus?.complete === true\n const hasError = completedJob.hasError || !isComplete\n\n // Extract error information from job if available\n let errorMessage: string | undefined\n if (hasError) {\n // Try to get error from the latest log entry\n if (completedJob.log && completedJob.log.length > 0) {\n const latestLog = completedJob.log[completedJob.log.length - 1]\n errorMessage = latestLog.error?.message || latestLog.error\n }\n\n // Fallback to top-level error\n if (!errorMessage && completedJob.error) {\n errorMessage = completedJob.error.message || completedJob.error\n }\n\n // Final fallback to generic message\n if (!errorMessage) {\n errorMessage = `Task ${taskSlug} failed without detailed error information`\n }\n }\n\n const result: {\n error: string | undefined\n output: unknown\n state: 'failed' | 'succeeded'\n } = {\n error: errorMessage,\n output: taskStatus?.output || {},\n state: isComplete ? 'succeeded' : 'failed'\n }\n\n // Store the output and error\n context.steps[stepName].output = result.output\n context.steps[stepName].state = result.state\n if (result.error) {\n context.steps[stepName].error = result.error\n }\n\n this.logger.debug({context}, 'Step execution context')\n\n if (result.state !== 'succeeded') {\n throw new Error(result.error || `Step ${stepName} failed`)\n }\n\n this.logger.info({\n output: result.output,\n stepName\n }, 'Step completed')\n\n // Update workflow run with current step results if workflowRunId is provided\n if (workflowRunId) {\n await this.updateWorkflowRunContext(workflowRunId, context, req)\n }\n\n } catch (error) {\n const errorMessage = error instanceof Error ? error.message : 'Unknown error'\n context.steps[stepName].state = 'failed'\n context.steps[stepName].error = errorMessage\n\n this.logger.error({\n error: errorMessage,\n input: context.steps[stepName].input,\n stepName,\n taskSlug\n }, 'Step execution failed')\n\n // Update workflow run with current step results if workflowRunId is provided\n if (workflowRunId) {\n try {\n await this.updateWorkflowRunContext(workflowRunId, context, req)\n } catch (updateError) {\n this.logger.error({\n error: updateError instanceof Error ? updateError.message : 'Unknown error',\n stepName\n }, 'Failed to update workflow run context after step failure')\n }\n }\n\n throw error\n }\n }\n\n /**\n * Resolve step execution order based on dependencies\n */\n private resolveExecutionOrder(steps: WorkflowStep[]): WorkflowStep[][] {\n const stepMap = new Map<string, WorkflowStep>()\n const dependencyGraph = new Map<string, string[]>()\n const indegree = new Map<string, number>()\n\n // Build the step map and dependency graph\n for (const step of steps) {\n const stepName = step.name || `step-${steps.indexOf(step)}`\n const dependencies = step.dependencies || []\n\n stepMap.set(stepName, { ...step, name: stepName, dependencies })\n dependencyGraph.set(stepName, dependencies)\n indegree.set(stepName, dependencies.length)\n }\n\n // Topological sort to determine execution batches\n const executionBatches: WorkflowStep[][] = []\n const processed = new Set<string>()\n\n while (processed.size < steps.length) {\n const currentBatch: WorkflowStep[] = []\n\n // Find all steps with no remaining dependencies\n for (const [stepName, inDegree] of indegree.entries()) {\n if (inDegree === 0 && !processed.has(stepName)) {\n const step = stepMap.get(stepName)\n if (step) {\n currentBatch.push(step)\n }\n }\n }\n\n if (currentBatch.length === 0) {\n throw new Error('Circular dependency detected in workflow steps')\n }\n\n executionBatches.push(currentBatch)\n\n // Update indegrees for next iteration\n for (const step of currentBatch) {\n processed.add(step.name)\n\n // Reduce indegree for steps that depend on completed steps\n for (const [otherStepName, dependencies] of dependencyGraph.entries()) {\n if (dependencies.includes(step.name) && !processed.has(otherStepName)) {\n indegree.set(otherStepName, (indegree.get(otherStepName) || 0) - 1)\n }\n }\n }\n }\n\n return executionBatches\n }\n\n /**\n * Resolve step input using JSONPath expressions\n */\n private resolveStepInput(config: Record<string, unknown>, context: ExecutionContext): Record<string, unknown> {\n const resolved: Record<string, unknown> = {}\n\n for (const [key, value] of Object.entries(config)) {\n if (typeof value === 'string' && value.startsWith('$')) {\n // This is a JSONPath expression\n try {\n const result = JSONPath({\n json: context,\n path: value,\n wrap: false\n })\n resolved[key] = result\n } catch (error) {\n this.logger.warn({\n error: error instanceof Error ? error.message : 'Unknown error',\n key,\n path: value\n }, 'Failed to resolve JSONPath')\n resolved[key] = value // Keep original value if resolution fails\n }\n } else if (typeof value === 'object' && value !== null) {\n // Recursively resolve nested objects\n resolved[key] = this.resolveStepInput(value as Record<string, unknown>, context)\n } else {\n // Keep literal values as-is\n resolved[key] = value\n }\n }\n\n return resolved\n }\n\n /**\n * Update workflow run with current context\n */\n private async updateWorkflowRunContext(\n workflowRunId: number | string,\n context: ExecutionContext,\n req: PayloadRequest\n ): Promise<void> {\n const serializeContext = () => ({\n steps: context.steps,\n trigger: {\n type: context.trigger.type,\n collection: context.trigger.collection,\n data: context.trigger.data,\n doc: context.trigger.doc,\n operation: context.trigger.operation,\n previousDoc: context.trigger.previousDoc,\n triggeredAt: context.trigger.triggeredAt,\n user: context.trigger.req?.user\n }\n })\n\n await this.payload.update({\n id: workflowRunId,\n collection: 'workflow-runs',\n data: {\n context: serializeContext()\n },\n req\n })\n }\n\n /**\n * Evaluate a condition using JSONPath\n */\n public evaluateCondition(condition: string, context: ExecutionContext): boolean {\n try {\n const result = JSONPath({\n json: context,\n path: condition,\n wrap: false\n })\n\n // Handle different result types\n if (Array.isArray(result)) {\n return result.length > 0 && Boolean(result[0])\n }\n\n return Boolean(result)\n } catch (error) {\n this.logger.warn({\n condition,\n error: error instanceof Error ? error.message : 'Unknown error'\n }, 'Failed to evaluate condition')\n\n // If condition evaluation fails, assume false\n return false\n }\n }\n\n /**\n * Execute a workflow with the given context\n */\n async execute(workflow: Workflow, context: ExecutionContext, req: PayloadRequest): Promise<void> {\n this.logger.info({\n workflowId: workflow.id,\n workflowName: workflow.name\n }, 'Starting workflow execution')\n\n const serializeContext = () => ({\n steps: context.steps,\n trigger: {\n type: context.trigger.type,\n collection: context.trigger.collection,\n data: context.trigger.data,\n doc: context.trigger.doc,\n operation: context.trigger.operation,\n previousDoc: context.trigger.previousDoc,\n triggeredAt: context.trigger.triggeredAt,\n user: context.trigger.req?.user\n }\n })\n\n // Create a workflow run record\n const workflowRun = await this.payload.create({\n collection: 'workflow-runs',\n data: {\n context: serializeContext(),\n startedAt: new Date().toISOString(),\n status: 'running',\n triggeredBy: context.trigger.req?.user?.email || 'system',\n workflow: workflow.id,\n workflowVersion: workflow._version || 1\n },\n req\n })\n\n try {\n // Resolve execution order based on dependencies\n const executionBatches = this.resolveExecutionOrder(workflow.steps)\n\n this.logger.info({\n batchSizes: executionBatches.map(batch => batch.length),\n totalBatches: executionBatches.length\n }, 'Resolved step execution order')\n\n // Execute each batch in sequence, but steps within each batch in parallel\n for (let batchIndex = 0; batchIndex < executionBatches.length; batchIndex++) {\n const batch = executionBatches[batchIndex]\n\n this.logger.info({\n batchIndex,\n stepCount: batch.length,\n stepNames: batch.map(s => s.name)\n }, 'Executing batch')\n\n // Execute all steps in this batch in parallel\n const batchPromises = batch.map((step, stepIndex) =>\n this.executeStep(step, stepIndex, context, req, workflowRun.id)\n )\n\n // Wait for all steps in the current batch to complete\n await Promise.all(batchPromises)\n\n this.logger.info({\n batchIndex,\n stepCount: batch.length\n }, 'Batch completed')\n }\n\n // Update workflow run as completed\n await this.payload.update({\n id: workflowRun.id,\n collection: 'workflow-runs',\n data: {\n completedAt: new Date().toISOString(),\n context: serializeContext(),\n status: 'completed'\n },\n req\n })\n\n this.logger.info({\n runId: workflowRun.id,\n workflowId: workflow.id,\n workflowName: workflow.name\n }, 'Workflow execution completed')\n\n } catch (error) {\n // Update workflow run as failed\n await this.payload.update({\n id: workflowRun.id,\n collection: 'workflow-runs',\n data: {\n completedAt: new Date().toISOString(),\n context: serializeContext(),\n error: error instanceof Error ? error.message : 'Unknown error',\n status: 'failed'\n },\n req\n })\n\n this.logger.error({\n error: error instanceof Error ? error.message : 'Unknown error',\n runId: workflowRun.id,\n workflowId: workflow.id,\n workflowName: workflow.name\n }, 'Workflow execution failed')\n\n throw error\n }\n }\n\n /**\n * Find and execute workflows triggered by a collection operation\n */\n async executeTriggeredWorkflows(\n collection: string,\n operation: 'create' | 'delete' | 'read' | 'update',\n doc: unknown,\n previousDoc: unknown,\n req: PayloadRequest\n ): Promise<void> {\n try {\n // Find workflows with matching triggers\n const workflows = await this.payload.find({\n collection: 'workflows',\n depth: 2, // Include steps and triggers\n limit: 100,\n req\n })\n\n for (const workflow of workflows.docs) {\n // Check if this workflow has a matching trigger\n const triggers = workflow.triggers as Array<{\n collection: string\n condition?: string\n operation: string\n type: string\n }>\n\n const matchingTriggers = triggers?.filter(trigger =>\n trigger.type === 'collection-trigger' &&\n trigger.collection === collection &&\n trigger.operation === operation\n ) || []\n\n for (const trigger of matchingTriggers) {\n // Create execution context for condition evaluation\n const context: ExecutionContext = {\n steps: {},\n trigger: {\n type: 'collection',\n collection,\n doc,\n operation,\n previousDoc,\n req\n }\n }\n\n // Check trigger condition if present\n if (trigger.condition) {\n const conditionMet = this.evaluateCondition(trigger.condition, context)\n\n if (!conditionMet) {\n this.logger.info({\n collection,\n condition: trigger.condition,\n operation,\n workflowId: workflow.id,\n workflowName: workflow.name\n }, 'Trigger condition not met, skipping workflow')\n continue\n }\n\n this.logger.info({\n collection,\n condition: trigger.condition,\n operation,\n workflowId: workflow.id,\n workflowName: workflow.name\n }, 'Trigger condition met')\n }\n\n this.logger.info({\n collection,\n operation,\n workflowId: workflow.id,\n workflowName: workflow.name\n }, 'Triggering workflow')\n\n // Execute the workflow\n await this.execute(workflow as Workflow, context, req)\n }\n }\n } catch (error) {\n this.logger.error({ error: error instanceof Error ? error.message : 'Unknown error' }, 'Workflow execution failed')\n this.logger.error({\n collection,\n error: error instanceof Error ? error.message : 'Unknown error',\n operation\n }, 'Failed to execute triggered workflows')\n }\n }\n}\n"],"names":["JSONPath","WorkflowExecutor","payload","logger","evaluateStepCondition","condition","context","evaluateCondition","executeStep","step","stepIndex","req","workflowRunId","stepName","name","info","hasStep","JSON","stringify","conditionMet","steps","error","undefined","input","output","reason","skipped","state","updateWorkflowRunContext","taskSlug","resolvedInput","resolveStepInput","Error","hasInput","hasReq","job","jobs","queue","task","run","limit","completedJob","findByID","id","collection","taskStatus","totalTried","isComplete","complete","hasError","errorMessage","log","length","latestLog","message","result","debug","updateError","resolveExecutionOrder","stepMap","Map","dependencyGraph","indegree","indexOf","dependencies","set","executionBatches","processed","Set","size","currentBatch","inDegree","entries","has","get","push","add","otherStepName","includes","config","resolved","key","value","Object","startsWith","json","path","wrap","warn","serializeContext","trigger","type","data","doc","operation","previousDoc","triggeredAt","user","update","Array","isArray","Boolean","execute","workflow","workflowId","workflowName","workflowRun","create","startedAt","Date","toISOString","status","triggeredBy","email","workflowVersion","_version","batchSizes","map","batch","totalBatches","batchIndex","stepCount","stepNames","s","batchPromises","Promise","all","completedAt","runId","executeTriggeredWorkflows","workflows","find","depth","docs","triggers","matchingTriggers","filter"],"mappings":"AAEA,SAASA,QAAQ,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c;4BACjB,IAAI,CAAChB,MAAM,CAACY,IAAI,CAAC;gCACf6B;gCACAvC,WAAWqF,QAAQrF,SAAS;gCAC5ByF;gCACAU,YAAYD,SAAS5D,EAAE;gCACvB8D,cAAcF,SAASzF,IAAI;4BAC7B,GAAG;4BACH;wBACF;wBAEA,IAAI,CAACX,MAAM,CAACY,IAAI,CAAC;4BACf6B;4BACAvC,WAAWqF,QAAQrF,SAAS;4BAC5ByF;4BACAU,YAAYD,SAAS5D,EAAE;4BACvB8D,cAAcF,SAASzF,IAAI;wBAC7B,GAAG;oBACL;oBAEA,IAAI,CAACX,MAAM,CAACY,IAAI,CAAC;wBACf6B;wBACAkD;wBACAU,YAAYD,SAAS5D,EAAE;wBACvB8D,cAAcF,SAASzF,IAAI;oBAC7B,GAAG;oBAEH,uBAAuB;oBACvB,MAAM,IAAI,CAACwF,OAAO,CAACC,UAAsBjG,SAASK;gBACpD;YACF;QACF,EAAE,OAAOU,OAAO;YACd,IAAI,CAAClB,MAAM,CAACkB,KAAK,CAAC;gBAAEA,OAAOA,iBAAiBW,QAAQX,MAAMiC,OAAO,GAAG;YAAgB,GAAG;YACvF,IAAI,CAACnD,MAAM,CAACkB,KAAK,CAAC;gBAChBuB;gBACAvB,OAAOA,iBAAiBW,QAAQX,MAAMiC,OAAO,GAAG;gBAChDwC;YACF,GAAG;QACL;IACF;AACF"}
1
+ {"version":3,"sources":["../../src/core/workflow-executor.ts"],"sourcesContent":["import type { Payload, PayloadRequest } from 'payload'\n\nimport { JSONPath } from 'jsonpath-plus'\n\nexport type Workflow = {\n _version?: number\n id: string\n name: string\n steps: WorkflowStep[]\n triggers: WorkflowTrigger[]\n}\n\nexport type WorkflowStep = {\n condition?: string\n dependencies?: string[]\n input?: null | Record<string, unknown>\n name: string\n step: string\n}\n\nexport interface WorkflowTrigger {\n collection?: string\n condition?: string\n global?: string\n globalOperation?: string\n operation?: string\n type: string\n webhookPath?: string\n}\n\nexport interface ExecutionContext {\n steps: Record<string, {\n error?: string\n input: unknown\n output: unknown\n state: 'failed' | 'pending' | 'running' | 'succeeded'\n }>\n trigger: {\n collection?: string\n data?: unknown\n doc?: unknown\n headers?: Record<string, string>\n operation?: string\n path?: string\n previousDoc?: unknown\n req?: PayloadRequest\n triggeredAt?: string\n type: string\n user?: {\n collection?: string\n email?: string\n id?: string\n }\n }\n}\n\nexport class WorkflowExecutor {\n constructor(\n private payload: Payload,\n private logger: Payload['logger']\n ) {}\n\n /**\n * Evaluate a step condition using JSONPath\n */\n private evaluateStepCondition(condition: string, context: ExecutionContext): boolean {\n return this.evaluateCondition(condition, context)\n }\n\n /**\n * Execute a single workflow step\n */\n private async executeStep(\n step: WorkflowStep,\n stepIndex: number,\n context: ExecutionContext,\n req: PayloadRequest,\n workflowRunId?: number | string\n ): Promise<void> {\n const stepName = step.name || 'step-' + stepIndex\n\n this.logger.info({\n hasStep: 'step' in step,\n step: JSON.stringify(step),\n stepName\n }, 'Executing step')\n\n // Check step condition if present\n if (step.condition) {\n this.logger.debug({\n condition: step.condition,\n stepName,\n availableSteps: Object.keys(context.steps),\n completedSteps: Object.entries(context.steps)\n .filter(([_, s]) => s.state === 'succeeded')\n .map(([name]) => name),\n triggerType: context.trigger?.type\n }, 'Evaluating step condition')\n\n const conditionMet = this.evaluateStepCondition(step.condition, context)\n\n if (!conditionMet) {\n this.logger.info({\n condition: step.condition,\n stepName,\n contextSnapshot: JSON.stringify({\n stepOutputs: Object.entries(context.steps).reduce((acc, [name, step]) => {\n acc[name] = { state: step.state, hasOutput: !!step.output }\n return acc\n }, {} as Record<string, any>),\n triggerData: context.trigger?.data ? 'present' : 'absent'\n })\n }, 'Step condition not met, skipping')\n\n // Mark step as completed but skipped\n context.steps[stepName] = {\n error: undefined,\n input: undefined,\n output: { reason: 'Condition not met', skipped: true },\n state: 'succeeded'\n }\n\n // Update workflow run context if needed\n if (workflowRunId) {\n await this.updateWorkflowRunContext(workflowRunId, context, req)\n }\n\n return\n }\n\n this.logger.info({\n condition: step.condition,\n stepName,\n contextSnapshot: JSON.stringify({\n stepOutputs: Object.entries(context.steps).reduce((acc, [name, step]) => {\n acc[name] = { state: step.state, hasOutput: !!step.output }\n return acc\n }, {} as Record<string, any>),\n triggerData: context.trigger?.data ? 'present' : 'absent'\n })\n }, 'Step condition met, proceeding with execution')\n }\n\n // Initialize step context\n context.steps[stepName] = {\n error: undefined,\n input: undefined,\n output: undefined,\n state: 'running'\n }\n\n // Move taskSlug declaration outside try block so it's accessible in catch\n const taskSlug = step.step // Use the 'step' field for task type\n\n try {\n // Resolve input data using JSONPath\n const resolvedInput = this.resolveStepInput(step.input || {}, context)\n context.steps[stepName].input = resolvedInput\n\n if (!taskSlug) {\n throw new Error(`Step ${stepName} is missing a task type`)\n }\n\n this.logger.info({\n hasInput: !!resolvedInput,\n hasReq: !!req,\n stepName,\n taskSlug\n }, 'Queueing task')\n\n const job = await this.payload.jobs.queue({\n input: resolvedInput,\n req,\n task: taskSlug\n })\n\n // Run the job immediately\n await this.payload.jobs.run({\n limit: 1,\n req\n })\n\n // Get the job result\n const completedJob = await this.payload.findByID({\n id: job.id,\n collection: 'payload-jobs',\n req\n })\n\n const taskStatus = completedJob.taskStatus?.[completedJob.taskSlug]?.[completedJob.totalTried]\n const isComplete = taskStatus?.complete === true\n const hasError = completedJob.hasError || !isComplete\n\n // Extract error information from job if available\n let errorMessage: string | undefined\n if (hasError) {\n // Try to get error from the latest log entry\n if (completedJob.log && completedJob.log.length > 0) {\n const latestLog = completedJob.log[completedJob.log.length - 1]\n errorMessage = latestLog.error?.message || latestLog.error\n }\n\n // Fallback to top-level error\n if (!errorMessage && completedJob.error) {\n errorMessage = completedJob.error.message || completedJob.error\n }\n\n // Final fallback to generic message\n if (!errorMessage) {\n errorMessage = `Task ${taskSlug} failed without detailed error information`\n }\n }\n\n const result: {\n error: string | undefined\n output: unknown\n state: 'failed' | 'succeeded'\n } = {\n error: errorMessage,\n output: taskStatus?.output || {},\n state: isComplete ? 'succeeded' : 'failed'\n }\n\n // Store the output and error\n context.steps[stepName].output = result.output\n context.steps[stepName].state = result.state\n if (result.error) {\n context.steps[stepName].error = result.error\n }\n\n this.logger.debug({context}, 'Step execution context')\n\n if (result.state !== 'succeeded') {\n throw new Error(result.error || `Step ${stepName} failed`)\n }\n\n this.logger.info({\n output: result.output,\n stepName\n }, 'Step completed')\n\n // Update workflow run with current step results if workflowRunId is provided\n if (workflowRunId) {\n await this.updateWorkflowRunContext(workflowRunId, context, req)\n }\n\n } catch (error) {\n const errorMessage = error instanceof Error ? error.message : 'Unknown error'\n context.steps[stepName].state = 'failed'\n context.steps[stepName].error = errorMessage\n\n this.logger.error({\n error: errorMessage,\n input: context.steps[stepName].input,\n stepName,\n taskSlug\n }, 'Step execution failed')\n\n // Update workflow run with current step results if workflowRunId is provided\n if (workflowRunId) {\n try {\n await this.updateWorkflowRunContext(workflowRunId, context, req)\n } catch (updateError) {\n this.logger.error({\n error: updateError instanceof Error ? updateError.message : 'Unknown error',\n stepName\n }, 'Failed to update workflow run context after step failure')\n }\n }\n\n throw error\n }\n }\n\n /**\n * Resolve step execution order based on dependencies\n */\n private resolveExecutionOrder(steps: WorkflowStep[]): WorkflowStep[][] {\n const stepMap = new Map<string, WorkflowStep>()\n const dependencyGraph = new Map<string, string[]>()\n const indegree = new Map<string, number>()\n\n // Build the step map and dependency graph\n for (const step of steps) {\n const stepName = step.name || `step-${steps.indexOf(step)}`\n const dependencies = step.dependencies || []\n\n stepMap.set(stepName, { ...step, name: stepName, dependencies })\n dependencyGraph.set(stepName, dependencies)\n indegree.set(stepName, dependencies.length)\n }\n\n // Topological sort to determine execution batches\n const executionBatches: WorkflowStep[][] = []\n const processed = new Set<string>()\n\n while (processed.size < steps.length) {\n const currentBatch: WorkflowStep[] = []\n\n // Find all steps with no remaining dependencies\n for (const [stepName, inDegree] of indegree.entries()) {\n if (inDegree === 0 && !processed.has(stepName)) {\n const step = stepMap.get(stepName)\n if (step) {\n currentBatch.push(step)\n }\n }\n }\n\n if (currentBatch.length === 0) {\n throw new Error('Circular dependency detected in workflow steps')\n }\n\n executionBatches.push(currentBatch)\n\n // Update indegrees for next iteration\n for (const step of currentBatch) {\n processed.add(step.name)\n\n // Reduce indegree for steps that depend on completed steps\n for (const [otherStepName, dependencies] of dependencyGraph.entries()) {\n if (dependencies.includes(step.name) && !processed.has(otherStepName)) {\n indegree.set(otherStepName, (indegree.get(otherStepName) || 0) - 1)\n }\n }\n }\n }\n\n return executionBatches\n }\n\n /**\n * Resolve step input using JSONPath expressions\n */\n private resolveStepInput(config: Record<string, unknown>, context: ExecutionContext): Record<string, unknown> {\n const resolved: Record<string, unknown> = {}\n\n this.logger.debug({\n configKeys: Object.keys(config),\n contextSteps: Object.keys(context.steps),\n triggerType: context.trigger?.type\n }, 'Starting step input resolution')\n\n for (const [key, value] of Object.entries(config)) {\n if (typeof value === 'string' && value.startsWith('$')) {\n // This is a JSONPath expression\n this.logger.debug({\n key,\n jsonPath: value,\n availableSteps: Object.keys(context.steps),\n hasTriggerData: !!context.trigger?.data,\n hasTriggerDoc: !!context.trigger?.doc\n }, 'Resolving JSONPath expression')\n\n try {\n const result = JSONPath({\n json: context,\n path: value,\n wrap: false\n })\n \n this.logger.debug({\n key,\n jsonPath: value,\n result: JSON.stringify(result).substring(0, 200),\n resultType: Array.isArray(result) ? 'array' : typeof result\n }, 'JSONPath resolved successfully')\n \n resolved[key] = result\n } catch (error) {\n this.logger.warn({\n error: error instanceof Error ? error.message : 'Unknown error',\n key,\n path: value,\n contextSnapshot: JSON.stringify(context).substring(0, 500)\n }, 'Failed to resolve JSONPath')\n resolved[key] = value // Keep original value if resolution fails\n }\n } else if (typeof value === 'object' && value !== null) {\n // Recursively resolve nested objects\n this.logger.debug({\n key,\n nestedKeys: Object.keys(value as Record<string, unknown>)\n }, 'Recursively resolving nested object')\n \n resolved[key] = this.resolveStepInput(value as Record<string, unknown>, context)\n } else {\n // Keep literal values as-is\n resolved[key] = value\n }\n }\n\n this.logger.debug({\n resolvedKeys: Object.keys(resolved),\n originalKeys: Object.keys(config)\n }, 'Step input resolution completed')\n\n return resolved\n }\n\n /**\n * Update workflow run with current context\n */\n private async updateWorkflowRunContext(\n workflowRunId: number | string,\n context: ExecutionContext,\n req: PayloadRequest\n ): Promise<void> {\n const serializeContext = () => ({\n steps: context.steps,\n trigger: {\n type: context.trigger.type,\n collection: context.trigger.collection,\n data: context.trigger.data,\n doc: context.trigger.doc,\n operation: context.trigger.operation,\n previousDoc: context.trigger.previousDoc,\n triggeredAt: context.trigger.triggeredAt,\n user: context.trigger.req?.user\n }\n })\n\n await this.payload.update({\n id: workflowRunId,\n collection: 'workflow-runs',\n data: {\n context: serializeContext()\n },\n req\n })\n }\n\n /**\n * Evaluate a condition using JSONPath\n */\n public evaluateCondition(condition: string, context: ExecutionContext): boolean {\n this.logger.debug({\n condition,\n contextKeys: Object.keys(context),\n triggerType: context.trigger?.type,\n triggerData: context.trigger?.data,\n triggerDoc: context.trigger?.doc ? 'present' : 'absent'\n }, 'Starting condition evaluation')\n\n try {\n const result = JSONPath({\n json: context,\n path: condition,\n wrap: false\n })\n\n this.logger.debug({\n condition,\n result,\n resultType: Array.isArray(result) ? 'array' : typeof result,\n resultLength: Array.isArray(result) ? result.length : undefined\n }, 'JSONPath evaluation result')\n\n // Handle different result types\n let finalResult: boolean\n if (Array.isArray(result)) {\n finalResult = result.length > 0 && Boolean(result[0])\n } else {\n finalResult = Boolean(result)\n }\n\n this.logger.debug({\n condition,\n finalResult,\n originalResult: result\n }, 'Condition evaluation completed')\n\n return finalResult\n } catch (error) {\n this.logger.warn({\n condition,\n error: error instanceof Error ? error.message : 'Unknown error',\n errorStack: error instanceof Error ? error.stack : undefined\n }, 'Failed to evaluate condition')\n\n // If condition evaluation fails, assume false\n return false\n }\n }\n\n /**\n * Execute a workflow with the given context\n */\n async execute(workflow: Workflow, context: ExecutionContext, req: PayloadRequest): Promise<void> {\n this.logger.info({\n workflowId: workflow.id,\n workflowName: workflow.name\n }, 'Starting workflow execution')\n\n const serializeContext = () => ({\n steps: context.steps,\n trigger: {\n type: context.trigger.type,\n collection: context.trigger.collection,\n data: context.trigger.data,\n doc: context.trigger.doc,\n operation: context.trigger.operation,\n previousDoc: context.trigger.previousDoc,\n triggeredAt: context.trigger.triggeredAt,\n user: context.trigger.req?.user\n }\n })\n\n // Create a workflow run record\n const workflowRun = await this.payload.create({\n collection: 'workflow-runs',\n data: {\n context: serializeContext(),\n startedAt: new Date().toISOString(),\n status: 'running',\n triggeredBy: context.trigger.req?.user?.email || 'system',\n workflow: workflow.id,\n workflowVersion: workflow._version || 1\n },\n req\n })\n\n try {\n // Resolve execution order based on dependencies\n const executionBatches = this.resolveExecutionOrder(workflow.steps)\n\n this.logger.info({\n batchSizes: executionBatches.map(batch => batch.length),\n totalBatches: executionBatches.length\n }, 'Resolved step execution order')\n\n // Execute each batch in sequence, but steps within each batch in parallel\n for (let batchIndex = 0; batchIndex < executionBatches.length; batchIndex++) {\n const batch = executionBatches[batchIndex]\n\n this.logger.info({\n batchIndex,\n stepCount: batch.length,\n stepNames: batch.map(s => s.name)\n }, 'Executing batch')\n\n // Execute all steps in this batch in parallel\n const batchPromises = batch.map((step, stepIndex) =>\n this.executeStep(step, stepIndex, context, req, workflowRun.id)\n )\n\n // Wait for all steps in the current batch to complete\n await Promise.all(batchPromises)\n\n this.logger.info({\n batchIndex,\n stepCount: batch.length\n }, 'Batch completed')\n }\n\n // Update workflow run as completed\n await this.payload.update({\n id: workflowRun.id,\n collection: 'workflow-runs',\n data: {\n completedAt: new Date().toISOString(),\n context: serializeContext(),\n status: 'completed'\n },\n req\n })\n\n this.logger.info({\n runId: workflowRun.id,\n workflowId: workflow.id,\n workflowName: workflow.name\n }, 'Workflow execution completed')\n\n } catch (error) {\n // Update workflow run as failed\n await this.payload.update({\n id: workflowRun.id,\n collection: 'workflow-runs',\n data: {\n completedAt: new Date().toISOString(),\n context: serializeContext(),\n error: error instanceof Error ? error.message : 'Unknown error',\n status: 'failed'\n },\n req\n })\n\n this.logger.error({\n error: error instanceof Error ? error.message : 'Unknown error',\n runId: workflowRun.id,\n workflowId: workflow.id,\n workflowName: workflow.name\n }, 'Workflow execution failed')\n\n throw error\n }\n }\n\n /**\n * Find and execute workflows triggered by a collection operation\n */\n async executeTriggeredWorkflows(\n collection: string,\n operation: 'create' | 'delete' | 'read' | 'update',\n doc: unknown,\n previousDoc: unknown,\n req: PayloadRequest\n ): Promise<void> {\n try {\n // Find workflows with matching triggers\n const workflows = await this.payload.find({\n collection: 'workflows',\n depth: 2, // Include steps and triggers\n limit: 100,\n req\n })\n\n for (const workflow of workflows.docs) {\n // Check if this workflow has a matching trigger\n const triggers = workflow.triggers as Array<{\n collection: string\n condition?: string\n operation: string\n type: string\n }>\n\n const matchingTriggers = triggers?.filter(trigger =>\n trigger.type === 'collection-trigger' &&\n trigger.collection === collection &&\n trigger.operation === operation\n ) || []\n\n for (const trigger of matchingTriggers) {\n // Create execution context for condition evaluation\n const context: ExecutionContext = {\n steps: {},\n trigger: {\n type: 'collection',\n collection,\n doc,\n operation,\n previousDoc,\n req\n }\n }\n\n // Check trigger condition if present\n if (trigger.condition) {\n this.logger.debug({\n collection,\n operation,\n condition: trigger.condition,\n docId: (doc as any)?.id,\n docFields: doc ? Object.keys(doc) : [],\n previousDocId: (previousDoc as any)?.id,\n workflowId: workflow.id,\n workflowName: workflow.name\n }, 'Evaluating collection trigger condition')\n\n const conditionMet = this.evaluateCondition(trigger.condition, context)\n\n if (!conditionMet) {\n this.logger.info({\n collection,\n condition: trigger.condition,\n operation,\n workflowId: workflow.id,\n workflowName: workflow.name,\n docSnapshot: JSON.stringify(doc).substring(0, 200)\n }, 'Trigger condition not met, skipping workflow')\n continue\n }\n\n this.logger.info({\n collection,\n condition: trigger.condition,\n operation,\n workflowId: workflow.id,\n workflowName: workflow.name,\n docSnapshot: JSON.stringify(doc).substring(0, 200)\n }, 'Trigger condition met')\n }\n\n this.logger.info({\n collection,\n operation,\n workflowId: workflow.id,\n workflowName: workflow.name\n }, 'Triggering workflow')\n\n // Execute the workflow\n await this.execute(workflow as Workflow, context, req)\n }\n }\n } catch (error) {\n this.logger.error({ error: error instanceof Error ? error.message : 'Unknown error' }, 'Workflow execution failed')\n this.logger.error({\n collection,\n error: error instanceof Error ? error.message : 'Unknown error',\n operation\n }, 'Failed to execute triggered workflows')\n }\n }\n}\n"],"names":["JSONPath","WorkflowExecutor","payload","logger","evaluateStepCondition","condition","context","evaluateCondition","executeStep","step","stepIndex","req","workflowRunId","stepName","name","info","hasStep","JSON","stringify","debug","availableSteps","Object","keys","steps","completedSteps","entries","filter","_","s","state","map","triggerType","trigger","type","conditionMet","contextSnapshot","stepOutputs","reduce","acc","hasOutput","output","triggerData","data","error","undefined","input","reason","skipped","updateWorkflowRunContext","taskSlug","resolvedInput","resolveStepInput","Error","hasInput","hasReq","job","jobs","queue","task","run","limit","completedJob","findByID","id","collection","taskStatus","totalTried","isComplete","complete","hasError","errorMessage","log","length","latestLog","message","result","updateError","resolveExecutionOrder","stepMap","Map","dependencyGraph","indegree","indexOf","dependencies","set","executionBatches","processed","Set","size","currentBatch","inDegree","has","get","push","add","otherStepName","includes","config","resolved","configKeys","contextSteps","key","value","startsWith","jsonPath","hasTriggerData","hasTriggerDoc","doc","json","path","wrap","substring","resultType","Array","isArray","warn","nestedKeys","resolvedKeys","originalKeys","serializeContext","operation","previousDoc","triggeredAt","user","update","contextKeys","triggerDoc","resultLength","finalResult","Boolean","originalResult","errorStack","stack","execute","workflow","workflowId","workflowName","workflowRun","create","startedAt","Date","toISOString","status","triggeredBy","email","workflowVersion","_version","batchSizes","batch","totalBatches","batchIndex","stepCount","stepNames","batchPromises","Promise","all","completedAt","runId","executeTriggeredWorkflows","workflows","find","depth","docs","triggers","matchingTriggers","docId","docFields","previousDocId","docSnapshot"],"mappings":"AAEA,SAASA,QAAQ,QAAQ,gBAAe;AAsDxC,OAAO,MAAMC;;;IACX,YACE,AAAQC,OAAgB,EACxB,AAAQC,MAAyB,CACjC;aAFQD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BAAiBS,QAAQT,MAAM+B,OAAO,GAAG;YAAgB,GAAG;YACvF,IAAI,CAACvE,MAAM,CAACwC,KAAK,CAAC;gBAChBqB;gBACArB,OAAOA,iBAAiBS,QAAQT,MAAM+B,OAAO,GAAG;gBAChD8C;YACF,GAAG;QACL;IACF;AACF"}
@@ -57,8 +57,8 @@ import { getConfigLogger } from './logger.js';
57
57
  return {
58
58
  output: {
59
59
  executedAt: new Date().toISOString(),
60
- status: 'skipped',
61
60
  reason: 'Condition not met',
61
+ status: 'skipped',
62
62
  workflowId
63
63
  },
64
64
  state: 'succeeded'
@@ -1 +1 @@
1
- {"version":3,"sources":["../../src/plugin/cron-scheduler.ts"],"sourcesContent":["import type {Config, Payload, TaskConfig} from 'payload'\nimport * as cron from 'node-cron'\n\nimport {type Workflow, WorkflowExecutor} from '../core/workflow-executor.js'\nimport {getConfigLogger} from './logger.js'\n\n/**\n * Generate dynamic cron tasks for all workflows with cron triggers\n * This is called at config time to register all scheduled tasks\n */\nexport function generateCronTasks(config: Config): void {\n const logger = getConfigLogger()\n \n // Note: We can't query the database at config time, so we'll need a different approach\n // We'll create a single task that handles all cron-triggered workflows\n const cronTask: TaskConfig = {\n slug: 'workflow-cron-executor',\n handler: async ({ input, req }) => {\n const { cronExpression, timezone, workflowId } = input as { \n cronExpression?: string\n timezone?: string\n workflowId: string\n }\n \n const logger = req.payload.logger.child({ plugin: '@xtr-dev/payload-automation' })\n \n try {\n // Get the workflow\n const workflow = await req.payload.findByID({\n id: workflowId,\n collection: 'workflows',\n depth: 2,\n req\n })\n \n if (!workflow) {\n throw new Error(`Workflow ${workflowId} not found`)\n }\n \n // Create execution context for cron trigger\n const context = {\n steps: {},\n trigger: {\n type: 'cron',\n req,\n triggeredAt: new Date().toISOString()\n }\n }\n \n // Create executor\n const executor = new WorkflowExecutor(req.payload, logger)\n \n // Find the matching cron trigger and check its condition if present\n const triggers = workflow.triggers as Array<{\n condition?: string\n cronExpression?: string\n timezone?: string\n type: string\n }>\n\n const matchingTrigger = triggers?.find(trigger =>\n trigger.type === 'cron-trigger' &&\n trigger.cronExpression === cronExpression\n )\n\n // Check trigger condition if present\n if (matchingTrigger?.condition) {\n const conditionMet = executor.evaluateCondition(matchingTrigger.condition, context)\n \n if (!conditionMet) {\n logger.info({\n condition: matchingTrigger.condition,\n cronExpression,\n workflowId,\n workflowName: workflow.name\n }, 'Cron trigger condition not met, skipping workflow execution')\n \n // Re-queue for next execution but don't run workflow\n if (cronExpression) {\n void requeueCronJob(workflowId, cronExpression, timezone, req.payload, logger)\n }\n \n return {\n output: {\n executedAt: new Date().toISOString(),\n status: 'skipped',\n reason: 'Condition not met',\n workflowId\n },\n state: 'succeeded'\n }\n }\n \n logger.info({\n condition: matchingTrigger.condition,\n cronExpression,\n workflowId,\n workflowName: workflow.name\n }, 'Cron trigger condition met')\n }\n \n // Execute the workflow\n await executor.execute(workflow as Workflow, context, req)\n \n // Re-queue the job for the next scheduled execution if cronExpression is provided\n if (cronExpression) {\n void requeueCronJob(workflowId, cronExpression, timezone, req.payload, logger)\n }\n \n return {\n output: {\n executedAt: new Date().toISOString(),\n status: 'completed',\n workflowId\n },\n state: 'succeeded'\n }\n } catch (error) {\n logger.error({\n error: error instanceof Error ? error.message : 'Unknown error',\n workflowId\n }, 'Cron job execution failed')\n \n // Re-queue even on failure to ensure continuity (unless it's a validation error)\n if (cronExpression && !(error instanceof Error && error.message.includes('Invalid cron'))) {\n void requeueCronJob(workflowId, cronExpression, timezone, req.payload, logger)\n .catch((requeueError) => {\n logger.error({\n error: requeueError instanceof Error ? requeueError.message : 'Unknown error',\n workflowId\n }, 'Failed to re-queue cron job after execution failure')\n })\n }\n \n return {\n output: {\n error: error instanceof Error ? error.message : 'Unknown error',\n workflowId\n },\n state: 'failed'\n }\n }\n }\n }\n \n // Add the cron task to config if not already present\n if (!config.jobs) {\n config.jobs = { tasks: [] }\n }\n \n if (!config.jobs.tasks) {\n config.jobs.tasks = []\n }\n \n if (!config.jobs.tasks.find(task => task.slug === cronTask.slug)) {\n logger.debug(`Registering cron executor task: ${cronTask.slug}`)\n config.jobs.tasks.push(cronTask)\n } else {\n logger.debug(`Cron executor task ${cronTask.slug} already registered, skipping`)\n }\n}\n\n/**\n * Register cron jobs for workflows with cron triggers\n * This is called at runtime after PayloadCMS is initialized\n */\nexport async function registerCronJobs(payload: Payload, logger: Payload['logger']): Promise<void> {\n try {\n // Find all workflows with cron triggers\n const workflows = await payload.find({\n collection: 'workflows',\n depth: 0,\n limit: 1000,\n where: {\n 'triggers.type': {\n equals: 'cron-trigger'\n }\n }\n })\n \n logger.info(`Found ${workflows.docs.length} workflows with cron triggers`)\n \n for (const workflow of workflows.docs) {\n const triggers = workflow.triggers as Array<{\n cronExpression?: string\n timezone?: string\n type: string\n }>\n \n // Find all cron triggers for this workflow\n const cronTriggers = triggers?.filter(t => t.type === 'cron-trigger') || []\n \n for (const trigger of cronTriggers) {\n if (trigger.cronExpression) {\n try {\n // Validate cron expression before queueing\n if (!validateCronExpression(trigger.cronExpression)) {\n logger.error({\n cronExpression: trigger.cronExpression,\n workflowId: workflow.id,\n workflowName: workflow.name\n }, 'Invalid cron expression format')\n continue\n }\n \n // Validate timezone if provided\n if (trigger.timezone) {\n try {\n // Test if timezone is valid by trying to create a date with it\n new Intl.DateTimeFormat('en', { timeZone: trigger.timezone })\n } catch {\n logger.error({\n timezone: trigger.timezone,\n workflowId: workflow.id,\n workflowName: workflow.name\n }, 'Invalid timezone specified')\n continue\n }\n }\n \n // Calculate next execution time\n const nextExecution = getNextCronTime(trigger.cronExpression, trigger.timezone)\n \n // Queue the job\n await payload.jobs.queue({\n input: { cronExpression: trigger.cronExpression, timezone: trigger.timezone, workflowId: workflow.id },\n task: 'workflow-cron-executor',\n waitUntil: nextExecution\n })\n \n logger.info({\n cronExpression: trigger.cronExpression,\n nextExecution: nextExecution.toISOString(),\n timezone: trigger.timezone || 'UTC',\n workflowId: workflow.id,\n workflowName: workflow.name\n }, 'Queued initial cron job for workflow')\n } catch (error) {\n logger.error({\n cronExpression: trigger.cronExpression,\n error: error instanceof Error ? error.message : 'Unknown error',\n timezone: trigger.timezone,\n workflowId: workflow.id,\n workflowName: workflow.name\n }, 'Failed to queue cron job')\n }\n } else {\n logger.warn({\n workflowId: workflow.id,\n workflowName: workflow.name\n }, 'Cron trigger found but no cron expression specified')\n }\n }\n }\n } catch (error) {\n logger.error({\n error: error instanceof Error ? error.message : 'Unknown error'\n }, 'Failed to register cron jobs')\n }\n}\n\n/**\n * Validate a cron expression\n */\nexport function validateCronExpression(cronExpression: string): boolean {\n return cron.validate(cronExpression)\n}\n\n/**\n * Calculate the next time a cron expression should run\n */\nfunction getNextCronTime(cronExpression: string, timezone?: string): Date {\n if (!validateCronExpression(cronExpression)) {\n throw new Error(`Invalid cron expression: ${cronExpression}`)\n }\n\n const now = new Date()\n const options: { timezone?: string } = timezone ? { timezone } : {}\n \n // Create a task to find the next execution time\n const task = cron.schedule(cronExpression, () => {}, {\n ...options\n })\n \n // Parse cron expression parts\n const cronParts = cronExpression.trim().split(/\\s+/)\n if (cronParts.length !== 5) {\n void task.destroy()\n throw new Error(`Invalid cron format: ${cronExpression}. Expected 5 parts.`)\n }\n \n const [minutePart, hourPart, dayPart, monthPart, weekdayPart] = cronParts\n \n // Calculate next execution with proper lookahead for any schedule frequency\n // Start from next minute and look ahead systematically\n let testTime = new Date(now.getTime() + 60 * 1000) // Start 1 minute from now\n testTime.setSeconds(0, 0) // Reset seconds and milliseconds\n \n // Maximum iterations to prevent infinite loops (covers ~2 years)\n const maxIterations = 2 * 365 * 24 * 60 // 2 years worth of minutes\n let iterations = 0\n \n while (iterations < maxIterations) {\n const minute = testTime.getMinutes()\n const hour = testTime.getHours()\n const dayOfMonth = testTime.getDate()\n const month = testTime.getMonth() + 1\n const dayOfWeek = testTime.getDay()\n \n if (matchesCronPart(minute, minutePart) &&\n matchesCronPart(hour, hourPart) &&\n matchesCronPart(dayOfMonth, dayPart) &&\n matchesCronPart(month, monthPart) &&\n matchesCronPart(dayOfWeek, weekdayPart)) {\n void task.destroy()\n return testTime\n }\n \n // Increment time intelligently based on cron pattern\n testTime = incrementTimeForCronPattern(testTime, cronParts)\n iterations++\n }\n \n void task.destroy()\n throw new Error(`Could not calculate next execution time for cron expression: ${cronExpression} within reasonable timeframe`)\n}\n\n/**\n * Intelligently increment time based on cron pattern to avoid unnecessary iterations\n */\nfunction incrementTimeForCronPattern(currentTime: Date, cronParts: string[]): Date {\n const [minutePart, hourPart, _dayPart, _monthPart, _weekdayPart] = cronParts\n const nextTime = new Date(currentTime)\n \n // If minute is specific (not wildcard), we can jump to next hour\n if (minutePart !== '*' && !minutePart.includes('/')) {\n const targetMinute = getNextValidCronValue(currentTime.getMinutes(), minutePart)\n if (targetMinute <= currentTime.getMinutes()) {\n // Move to next hour\n nextTime.setHours(nextTime.getHours() + 1, targetMinute, 0, 0)\n } else {\n nextTime.setMinutes(targetMinute, 0, 0)\n }\n return nextTime\n }\n \n // If hour is specific and we're past it, jump to next day\n if (hourPart !== '*' && !hourPart.includes('/')) {\n const targetHour = getNextValidCronValue(currentTime.getHours(), hourPart)\n if (targetHour <= currentTime.getHours()) {\n // Move to next day\n nextTime.setDate(nextTime.getDate() + 1)\n nextTime.setHours(targetHour, 0, 0, 0)\n } else {\n nextTime.setHours(targetHour, 0, 0, 0)\n }\n return nextTime\n }\n \n // Default: increment by 1 minute\n nextTime.setTime(nextTime.getTime() + 60 * 1000)\n return nextTime\n}\n\n/**\n * Get the next valid value for a cron part\n */\nfunction getNextValidCronValue(currentValue: number, cronPart: string): number {\n if (cronPart === '*') {return currentValue + 1}\n \n // Handle specific values and ranges\n const values = parseCronPart(cronPart)\n return values.find(v => v > currentValue) || values[0]\n}\n\n/**\n * Parse a cron part into an array of valid values\n */\nfunction parseCronPart(cronPart: string): number[] {\n if (cronPart === '*') {return []}\n \n const values: number[] = []\n \n // Handle comma-separated values\n if (cronPart.includes(',')) {\n cronPart.split(',').forEach(part => {\n values.push(...parseCronPart(part.trim()))\n })\n return values.sort((a, b) => a - b)\n }\n \n // Handle ranges\n if (cronPart.includes('-')) {\n const [start, end] = cronPart.split('-').map(n => parseInt(n, 10))\n for (let i = start; i <= end; i++) {\n values.push(i)\n }\n return values\n }\n \n // Handle step values\n if (cronPart.includes('/')) {\n const [range, step] = cronPart.split('/')\n const stepNum = parseInt(step, 10)\n \n if (range === '*') {\n // For wildcards with steps, return empty - handled elsewhere\n return []\n }\n \n const baseValues = parseCronPart(range)\n return baseValues.filter((_, index) => index % stepNum === 0)\n }\n \n // Single value\n values.push(parseInt(cronPart, 10))\n return values\n}\n\n/**\n * Check if a value matches a cron expression part\n */\nfunction matchesCronPart(value: number, cronPart: string): boolean {\n if (cronPart === '*') {return true}\n \n // Handle step values (e.g., */5)\n if (cronPart.includes('/')) {\n const [range, step] = cronPart.split('/')\n const stepNum = parseInt(step, 10)\n \n if (range === '*') {\n return value % stepNum === 0\n }\n }\n \n // Handle ranges (e.g., 1-5)\n if (cronPart.includes('-')) {\n const [start, end] = cronPart.split('-').map(n => parseInt(n, 10))\n return value >= start && value <= end\n }\n \n // Handle comma-separated values (e.g., 1,3,5)\n if (cronPart.includes(',')) {\n const values = cronPart.split(',').map(n => parseInt(n, 10))\n return values.includes(value)\n }\n \n // Handle single value\n const cronValue = parseInt(cronPart, 10)\n return value === cronValue\n}\n\n/**\n * Handle re-queueing of cron jobs after they execute\n * This ensures the job runs again at the next scheduled time\n */\nexport async function requeueCronJob(\n workflowId: string,\n cronExpression: string,\n timezone: string | undefined,\n payload: Payload,\n logger: Payload['logger']\n): Promise<void> {\n try {\n // Queue the job to run at the next scheduled time\n await payload.jobs.queue({\n input: { cronExpression, timezone, workflowId },\n task: 'workflow-cron-executor',\n waitUntil: getNextCronTime(cronExpression, timezone)\n })\n \n logger.debug({\n nextRun: getNextCronTime(cronExpression, timezone),\n timezone: timezone || 'UTC',\n workflowId\n }, 'Re-queued cron job')\n } catch (error) {\n logger.error({\n error: error instanceof Error ? error.message : 'Unknown error',\n workflowId\n }, 'Failed to re-queue cron job')\n }\n}\n\n/**\n * Register or update cron jobs for a specific workflow\n */\nexport async function updateWorkflowCronJobs(\n workflowId: string,\n payload: Payload, \n logger: Payload['logger']\n): Promise<void> {\n try {\n // First, cancel any existing cron jobs for this workflow\n cancelWorkflowCronJobs(workflowId, payload, logger)\n \n // Get the workflow\n const workflow = await payload.findByID({\n id: workflowId,\n collection: 'workflows',\n depth: 0\n })\n \n if (!workflow) {\n logger.warn({ workflowId }, 'Workflow not found for cron job update')\n return\n }\n \n const triggers = workflow.triggers as Array<{\n cronExpression?: string\n timezone?: string\n type: string\n }>\n \n // Find all cron triggers for this workflow\n const cronTriggers = triggers?.filter(t => t.type === 'cron-trigger') || []\n \n if (cronTriggers.length === 0) {\n logger.debug({ workflowId }, 'No cron triggers found for workflow')\n return\n }\n \n let scheduledJobs = 0\n \n for (const trigger of cronTriggers) {\n if (trigger.cronExpression) {\n try {\n // Validate cron expression before queueing\n if (!validateCronExpression(trigger.cronExpression)) {\n logger.error({\n cronExpression: trigger.cronExpression,\n workflowId,\n workflowName: workflow.name\n }, 'Invalid cron expression format')\n continue\n }\n \n // Validate timezone if provided\n if (trigger.timezone) {\n try {\n new Intl.DateTimeFormat('en', { timeZone: trigger.timezone })\n } catch {\n logger.error({\n timezone: trigger.timezone,\n workflowId,\n workflowName: workflow.name\n }, 'Invalid timezone specified')\n continue\n }\n }\n \n // Calculate next execution time\n const nextExecution = getNextCronTime(trigger.cronExpression, trigger.timezone)\n \n // Queue the job\n await payload.jobs.queue({\n input: { cronExpression: trigger.cronExpression, timezone: trigger.timezone, workflowId },\n task: 'workflow-cron-executor',\n waitUntil: nextExecution\n })\n \n scheduledJobs++\n \n logger.info({\n cronExpression: trigger.cronExpression,\n nextExecution: nextExecution.toISOString(),\n timezone: trigger.timezone || 'UTC',\n workflowId,\n workflowName: workflow.name\n }, 'Scheduled cron job for workflow')\n } catch (error) {\n logger.error({\n cronExpression: trigger.cronExpression,\n error: error instanceof Error ? error.message : 'Unknown error',\n timezone: trigger.timezone,\n workflowId,\n workflowName: workflow.name\n }, 'Failed to schedule cron job')\n }\n }\n }\n \n if (scheduledJobs > 0) {\n logger.info({ scheduledJobs, workflowId }, 'Updated cron jobs for workflow')\n }\n } catch (error) {\n logger.error({\n error: error instanceof Error ? error.message : 'Unknown error',\n workflowId\n }, 'Failed to update workflow cron jobs')\n }\n}\n\n/**\n * Cancel all cron jobs for a specific workflow\n */\nexport function cancelWorkflowCronJobs(\n workflowId: string,\n payload: Payload,\n logger: Payload['logger']\n): void {\n try {\n // Note: PayloadCMS job system doesn't have a built-in way to cancel specific jobs by input\n // This is a limitation we need to work around\n // For now, we log that we would cancel jobs for this workflow\n logger.debug({ workflowId }, 'Would cancel existing cron jobs for workflow (PayloadCMS limitation: cannot selectively cancel jobs)')\n } catch (error) {\n logger.error({\n error: error instanceof Error ? error.message : 'Unknown error',\n workflowId\n }, 'Failed to cancel workflow cron jobs')\n }\n}\n\n/**\n * Remove cron jobs for a deleted workflow\n */\nexport function removeWorkflowCronJobs(\n workflowId: string,\n payload: Payload,\n logger: Payload['logger']\n): void {\n try {\n cancelWorkflowCronJobs(workflowId, payload, logger)\n logger.info({ workflowId }, 'Removed cron jobs for deleted workflow')\n } catch (error) {\n logger.error({\n error: error instanceof Error ? error.message : 'Unknown error',\n workflowId\n }, 'Failed to remove workflow cron jobs')\n }\n}\n"],"names":["cron","WorkflowExecutor","getConfigLogger","generateCronTasks","config","logger","cronTask","slug","handler","input","req","cronExpression","timezone","workflowId","payload","child","plugin","workflow","findByID","id","collection","depth","Error","context","steps","trigger","type","triggeredAt","Date","toISOString","executor","triggers","matchingTrigger","find","condition","conditionMet","evaluateCondition","info","workflowName","name","requeueCronJob","output","executedAt","status","reason","state","execute","error","message","includes","catch","requeueError","jobs","tasks","task","debug","push","registerCronJobs","workflows","limit","where","equals","docs","length","cronTriggers","filter","t","validateCronExpression","Intl","DateTimeFormat","timeZone","nextExecution","getNextCronTime","queue","waitUntil","warn","validate","now","options","schedule","cronParts","trim","split","destroy","minutePart","hourPart","dayPart","monthPart","weekdayPart","testTime","getTime","setSeconds","maxIterations","iterations","minute","getMinutes","hour","getHours","dayOfMonth","getDate","month","getMonth","dayOfWeek","getDay","matchesCronPart","incrementTimeForCronPattern","currentTime","_dayPart","_monthPart","_weekdayPart","nextTime","targetMinute","getNextValidCronValue","setHours","setMinutes","targetHour","setDate","setTime","currentValue","cronPart","values","parseCronPart","v","forEach","part","sort","a","b","start","end","map","n","parseInt","i","range","step","stepNum","baseValues","_","index","value","cronValue","nextRun","updateWorkflowCronJobs","cancelWorkflowCronJobs","scheduledJobs","removeWorkflowCronJobs"],"mappings":"AACA,YAAYA,UAAU,YAAW;AAEjC,SAAuBC,gBAAgB,QAAO,+BAA8B;AAC5E,SAAQC,eAAe,QAAO,cAAa;AAE3C;;;CAGC,GACD,OAAO,SAASC,kBAAkBC,MAAc;IAC9C,MAAMC,SAASH;IAEf,uFAAuF;IACvF,uEAAuE;IACvE,MAAMI,WAAuB;QAC3BC,MAAM;QACNC,SAAS,OAAO,EAAEC,KAAK,EAAEC,GAAG,EAAE;YAC5B,MAAM,EAAEC,cAAc,EAAEC,QAAQ,EAAEC,UAAU,EAAE,GAAGJ;YAMjD,MAAMJ,SAASK,IAAII,OAAO,CAACT,MAAM,CAACU,KAAK,CAAC;gBAAEC,QAAQ;YAA8B;YAEhF,IAAI;gBACF,mBAAmB;gBACnB,MAAMC,WAAW,MAAMP,IAAII,OAAO,CAACI,QAAQ,CAAC;oBAC1CC,IAAIN;oBACJO,YAAY;oBACZC,OAAO;oBACPX;gBACF;gBAEA,IAAI,CAACO,UAAU;oBACb,MAAM,IAAIK,MAAM,CAAC,SAAS,EAAET,WAAW,UAAU,CAAC;gBACpD;gBAEA,4CAA4C;gBAC5C,MAAMU,UAAU;oBACdC,OAAO,CAAC;oBACRC,SAAS;wBACPC,MAAM;wBACNhB;wBACAiB,aAAa,IAAIC,OAAOC,WAAW;oBACrC;gBACF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BACL,EAAE,OAAOQ,OAAO;oBACd1C,OAAO0C,KAAK,CAAC;wBACXpC,gBAAgBc,QAAQd,cAAc;wBACtCoC,OAAOA,iBAAiBzB,QAAQyB,MAAMC,OAAO,GAAG;wBAChDpC,UAAUa,QAAQb,QAAQ;wBAC1BC;wBACAyB,cAAcrB,SAASsB,IAAI;oBAC7B,GAAG;gBACL;YACF;QACF;QAEA,IAAI0G,gBAAgB,GAAG;YACrB5I,OAAOgC,IAAI,CAAC;gBAAE4G;gBAAepI;YAAW,GAAG;QAC7C;IACF,EAAE,OAAOkC,OAAO;QACd1C,OAAO0C,KAAK,CAAC;YACXA,OAAOA,iBAAiBzB,QAAQyB,MAAMC,OAAO,GAAG;YAChDnC;QACF,GAAG;IACL;AACF;AAEA;;CAEC,GACD,OAAO,SAASmI,uBACdnI,UAAkB,EAClBC,OAAgB,EAChBT,MAAyB;IAEzB,IAAI;QACF,2FAA2F;QAC3F,8CAA8C;QAC9C,8DAA8D;QAC9DA,OAAOkD,KAAK,CAAC;YAAE1C;QAAW,GAAG;IAC/B,EAAE,OAAOkC,OAAO;QACd1C,OAAO0C,KAAK,CAAC;YACXA,OAAOA,iBAAiBzB,QAAQyB,MAAMC,OAAO,GAAG;YAChDnC;QACF,GAAG;IACL;AACF;AAEA;;CAEC,GACD,OAAO,SAASqI,uBACdrI,UAAkB,EAClBC,OAAgB,EAChBT,MAAyB;IAEzB,IAAI;QACF2I,uBAAuBnI,YAAYC,SAAST;QAC5CA,OAAOgC,IAAI,CAAC;YAAExB;QAAW,GAAG;IAC9B,EAAE,OAAOkC,OAAO;QACd1C,OAAO0C,KAAK,CAAC;YACXA,OAAOA,iBAAiBzB,QAAQyB,MAAMC,OAAO,GAAG;YAChDnC;QACF,GAAG;IACL;AACF"}
1
+ {"version":3,"sources":["../../src/plugin/cron-scheduler.ts"],"sourcesContent":["import type {Config, Payload, TaskConfig} from 'payload'\n\nimport * as cron from 'node-cron'\n\nimport {type Workflow, WorkflowExecutor} from '../core/workflow-executor.js'\nimport {getConfigLogger} from './logger.js'\n\n/**\n * Generate dynamic cron tasks for all workflows with cron triggers\n * This is called at config time to register all scheduled tasks\n */\nexport function generateCronTasks(config: Config): void {\n const logger = getConfigLogger()\n\n // Note: We can't query the database at config time, so we'll need a different approach\n // We'll create a single task that handles all cron-triggered workflows\n const cronTask: TaskConfig = {\n slug: 'workflow-cron-executor',\n handler: async ({ input, req }) => {\n const { cronExpression, timezone, workflowId } = input as {\n cronExpression?: string\n timezone?: string\n workflowId: string\n }\n\n const logger = req.payload.logger.child({ plugin: '@xtr-dev/payload-automation' })\n\n try {\n // Get the workflow\n const workflow = await req.payload.findByID({\n id: workflowId,\n collection: 'workflows',\n depth: 2,\n req\n })\n\n if (!workflow) {\n throw new Error(`Workflow ${workflowId} not found`)\n }\n\n // Create execution context for cron trigger\n const context = {\n steps: {},\n trigger: {\n type: 'cron',\n req,\n triggeredAt: new Date().toISOString()\n }\n }\n\n // Create executor\n const executor = new WorkflowExecutor(req.payload, logger)\n\n // Find the matching cron trigger and check its condition if present\n const triggers = workflow.triggers as Array<{\n condition?: string\n cronExpression?: string\n timezone?: string\n type: string\n }>\n\n const matchingTrigger = triggers?.find(trigger =>\n trigger.type === 'cron-trigger' &&\n trigger.cronExpression === cronExpression\n )\n\n // Check trigger condition if present\n if (matchingTrigger?.condition) {\n const conditionMet = executor.evaluateCondition(matchingTrigger.condition, context)\n\n if (!conditionMet) {\n logger.info({\n condition: matchingTrigger.condition,\n cronExpression,\n workflowId,\n workflowName: workflow.name\n }, 'Cron trigger condition not met, skipping workflow execution')\n\n // Re-queue for next execution but don't run workflow\n if (cronExpression) {\n void requeueCronJob(workflowId, cronExpression, timezone, req.payload, logger)\n }\n\n return {\n output: {\n executedAt: new Date().toISOString(),\n reason: 'Condition not met',\n status: 'skipped',\n workflowId\n },\n state: 'succeeded'\n }\n }\n\n logger.info({\n condition: matchingTrigger.condition,\n cronExpression,\n workflowId,\n workflowName: workflow.name\n }, 'Cron trigger condition met')\n }\n\n // Execute the workflow\n await executor.execute(workflow as Workflow, context, req)\n\n // Re-queue the job for the next scheduled execution if cronExpression is provided\n if (cronExpression) {\n void requeueCronJob(workflowId, cronExpression, timezone, req.payload, logger)\n }\n\n return {\n output: {\n executedAt: new Date().toISOString(),\n status: 'completed',\n workflowId\n },\n state: 'succeeded'\n }\n } catch (error) {\n logger.error({\n error: error instanceof Error ? error.message : 'Unknown error',\n workflowId\n }, 'Cron job execution failed')\n\n // Re-queue even on failure to ensure continuity (unless it's a validation error)\n if (cronExpression && !(error instanceof Error && error.message.includes('Invalid cron'))) {\n void requeueCronJob(workflowId, cronExpression, timezone, req.payload, logger)\n .catch((requeueError) => {\n logger.error({\n error: requeueError instanceof Error ? requeueError.message : 'Unknown error',\n workflowId\n }, 'Failed to re-queue cron job after execution failure')\n })\n }\n\n return {\n output: {\n error: error instanceof Error ? error.message : 'Unknown error',\n workflowId\n },\n state: 'failed'\n }\n }\n }\n }\n\n // Add the cron task to config if not already present\n if (!config.jobs) {\n config.jobs = { tasks: [] }\n }\n\n if (!config.jobs.tasks) {\n config.jobs.tasks = []\n }\n\n if (!config.jobs.tasks.find(task => task.slug === cronTask.slug)) {\n logger.debug(`Registering cron executor task: ${cronTask.slug}`)\n config.jobs.tasks.push(cronTask)\n } else {\n logger.debug(`Cron executor task ${cronTask.slug} already registered, skipping`)\n }\n}\n\n/**\n * Register cron jobs for workflows with cron triggers\n * This is called at runtime after PayloadCMS is initialized\n */\nexport async function registerCronJobs(payload: Payload, logger: Payload['logger']): Promise<void> {\n try {\n // Find all workflows with cron triggers\n const workflows = await payload.find({\n collection: 'workflows',\n depth: 0,\n limit: 1000,\n where: {\n 'triggers.type': {\n equals: 'cron-trigger'\n }\n }\n })\n\n logger.info(`Found ${workflows.docs.length} workflows with cron triggers`)\n\n for (const workflow of workflows.docs) {\n const triggers = workflow.triggers as Array<{\n cronExpression?: string\n timezone?: string\n type: string\n }>\n\n // Find all cron triggers for this workflow\n const cronTriggers = triggers?.filter(t => t.type === 'cron-trigger') || []\n\n for (const trigger of cronTriggers) {\n if (trigger.cronExpression) {\n try {\n // Validate cron expression before queueing\n if (!validateCronExpression(trigger.cronExpression)) {\n logger.error({\n cronExpression: trigger.cronExpression,\n workflowId: workflow.id,\n workflowName: workflow.name\n }, 'Invalid cron expression format')\n continue\n }\n\n // Validate timezone if provided\n if (trigger.timezone) {\n try {\n // Test if timezone is valid by trying to create a date with it\n new Intl.DateTimeFormat('en', { timeZone: trigger.timezone })\n } catch {\n logger.error({\n timezone: trigger.timezone,\n workflowId: workflow.id,\n workflowName: workflow.name\n }, 'Invalid timezone specified')\n continue\n }\n }\n\n // Calculate next execution time\n const nextExecution = getNextCronTime(trigger.cronExpression, trigger.timezone)\n\n // Queue the job\n await payload.jobs.queue({\n input: { cronExpression: trigger.cronExpression, timezone: trigger.timezone, workflowId: workflow.id },\n task: 'workflow-cron-executor',\n waitUntil: nextExecution\n })\n\n logger.info({\n cronExpression: trigger.cronExpression,\n nextExecution: nextExecution.toISOString(),\n timezone: trigger.timezone || 'UTC',\n workflowId: workflow.id,\n workflowName: workflow.name\n }, 'Queued initial cron job for workflow')\n } catch (error) {\n logger.error({\n cronExpression: trigger.cronExpression,\n error: error instanceof Error ? error.message : 'Unknown error',\n timezone: trigger.timezone,\n workflowId: workflow.id,\n workflowName: workflow.name\n }, 'Failed to queue cron job')\n }\n } else {\n logger.warn({\n workflowId: workflow.id,\n workflowName: workflow.name\n }, 'Cron trigger found but no cron expression specified')\n }\n }\n }\n } catch (error) {\n logger.error({\n error: error instanceof Error ? error.message : 'Unknown error'\n }, 'Failed to register cron jobs')\n }\n}\n\n/**\n * Validate a cron expression\n */\nexport function validateCronExpression(cronExpression: string): boolean {\n return cron.validate(cronExpression)\n}\n\n/**\n * Calculate the next time a cron expression should run\n */\nfunction getNextCronTime(cronExpression: string, timezone?: string): Date {\n if (!validateCronExpression(cronExpression)) {\n throw new Error(`Invalid cron expression: ${cronExpression}`)\n }\n\n const now = new Date()\n const options: { timezone?: string } = timezone ? { timezone } : {}\n\n // Create a task to find the next execution time\n const task = cron.schedule(cronExpression, () => {}, {\n ...options\n })\n\n // Parse cron expression parts\n const cronParts = cronExpression.trim().split(/\\s+/)\n if (cronParts.length !== 5) {\n void task.destroy()\n throw new Error(`Invalid cron format: ${cronExpression}. Expected 5 parts.`)\n }\n\n const [minutePart, hourPart, dayPart, monthPart, weekdayPart] = cronParts\n\n // Calculate next execution with proper lookahead for any schedule frequency\n // Start from next minute and look ahead systematically\n let testTime = new Date(now.getTime() + 60 * 1000) // Start 1 minute from now\n testTime.setSeconds(0, 0) // Reset seconds and milliseconds\n\n // Maximum iterations to prevent infinite loops (covers ~2 years)\n const maxIterations = 2 * 365 * 24 * 60 // 2 years worth of minutes\n let iterations = 0\n\n while (iterations < maxIterations) {\n const minute = testTime.getMinutes()\n const hour = testTime.getHours()\n const dayOfMonth = testTime.getDate()\n const month = testTime.getMonth() + 1\n const dayOfWeek = testTime.getDay()\n\n if (matchesCronPart(minute, minutePart) &&\n matchesCronPart(hour, hourPart) &&\n matchesCronPart(dayOfMonth, dayPart) &&\n matchesCronPart(month, monthPart) &&\n matchesCronPart(dayOfWeek, weekdayPart)) {\n void task.destroy()\n return testTime\n }\n\n // Increment time intelligently based on cron pattern\n testTime = incrementTimeForCronPattern(testTime, cronParts)\n iterations++\n }\n\n void task.destroy()\n throw new Error(`Could not calculate next execution time for cron expression: ${cronExpression} within reasonable timeframe`)\n}\n\n/**\n * Intelligently increment time based on cron pattern to avoid unnecessary iterations\n */\nfunction incrementTimeForCronPattern(currentTime: Date, cronParts: string[]): Date {\n const [minutePart, hourPart, _dayPart, _monthPart, _weekdayPart] = cronParts\n const nextTime = new Date(currentTime)\n\n // If minute is specific (not wildcard), we can jump to next hour\n if (minutePart !== '*' && !minutePart.includes('/')) {\n const targetMinute = getNextValidCronValue(currentTime.getMinutes(), minutePart)\n if (targetMinute <= currentTime.getMinutes()) {\n // Move to next hour\n nextTime.setHours(nextTime.getHours() + 1, targetMinute, 0, 0)\n } else {\n nextTime.setMinutes(targetMinute, 0, 0)\n }\n return nextTime\n }\n\n // If hour is specific and we're past it, jump to next day\n if (hourPart !== '*' && !hourPart.includes('/')) {\n const targetHour = getNextValidCronValue(currentTime.getHours(), hourPart)\n if (targetHour <= currentTime.getHours()) {\n // Move to next day\n nextTime.setDate(nextTime.getDate() + 1)\n nextTime.setHours(targetHour, 0, 0, 0)\n } else {\n nextTime.setHours(targetHour, 0, 0, 0)\n }\n return nextTime\n }\n\n // Default: increment by 1 minute\n nextTime.setTime(nextTime.getTime() + 60 * 1000)\n return nextTime\n}\n\n/**\n * Get the next valid value for a cron part\n */\nfunction getNextValidCronValue(currentValue: number, cronPart: string): number {\n if (cronPart === '*') {return currentValue + 1}\n\n // Handle specific values and ranges\n const values = parseCronPart(cronPart)\n return values.find(v => v > currentValue) || values[0]\n}\n\n/**\n * Parse a cron part into an array of valid values\n */\nfunction parseCronPart(cronPart: string): number[] {\n if (cronPart === '*') {return []}\n\n const values: number[] = []\n\n // Handle comma-separated values\n if (cronPart.includes(',')) {\n cronPart.split(',').forEach(part => {\n values.push(...parseCronPart(part.trim()))\n })\n return values.sort((a, b) => a - b)\n }\n\n // Handle ranges\n if (cronPart.includes('-')) {\n const [start, end] = cronPart.split('-').map(n => parseInt(n, 10))\n for (let i = start; i <= end; i++) {\n values.push(i)\n }\n return values\n }\n\n // Handle step values\n if (cronPart.includes('/')) {\n const [range, step] = cronPart.split('/')\n const stepNum = parseInt(step, 10)\n\n if (range === '*') {\n // For wildcards with steps, return empty - handled elsewhere\n return []\n }\n\n const baseValues = parseCronPart(range)\n return baseValues.filter((_, index) => index % stepNum === 0)\n }\n\n // Single value\n values.push(parseInt(cronPart, 10))\n return values\n}\n\n/**\n * Check if a value matches a cron expression part\n */\nfunction matchesCronPart(value: number, cronPart: string): boolean {\n if (cronPart === '*') {return true}\n\n // Handle step values (e.g., */5)\n if (cronPart.includes('/')) {\n const [range, step] = cronPart.split('/')\n const stepNum = parseInt(step, 10)\n\n if (range === '*') {\n return value % stepNum === 0\n }\n }\n\n // Handle ranges (e.g., 1-5)\n if (cronPart.includes('-')) {\n const [start, end] = cronPart.split('-').map(n => parseInt(n, 10))\n return value >= start && value <= end\n }\n\n // Handle comma-separated values (e.g., 1,3,5)\n if (cronPart.includes(',')) {\n const values = cronPart.split(',').map(n => parseInt(n, 10))\n return values.includes(value)\n }\n\n // Handle single value\n const cronValue = parseInt(cronPart, 10)\n return value === cronValue\n}\n\n/**\n * Handle re-queueing of cron jobs after they execute\n * This ensures the job runs again at the next scheduled time\n */\nexport async function requeueCronJob(\n workflowId: string,\n cronExpression: string,\n timezone: string | undefined,\n payload: Payload,\n logger: Payload['logger']\n): Promise<void> {\n try {\n // Queue the job to run at the next scheduled time\n await payload.jobs.queue({\n input: { cronExpression, timezone, workflowId },\n task: 'workflow-cron-executor',\n waitUntil: getNextCronTime(cronExpression, timezone)\n })\n\n logger.debug({\n nextRun: getNextCronTime(cronExpression, timezone),\n timezone: timezone || 'UTC',\n workflowId\n }, 'Re-queued cron job')\n } catch (error) {\n logger.error({\n error: error instanceof Error ? error.message : 'Unknown error',\n workflowId\n }, 'Failed to re-queue cron job')\n }\n}\n\n/**\n * Register or update cron jobs for a specific workflow\n */\nexport async function updateWorkflowCronJobs(\n workflowId: string,\n payload: Payload,\n logger: Payload['logger']\n): Promise<void> {\n try {\n // First, cancel any existing cron jobs for this workflow\n cancelWorkflowCronJobs(workflowId, payload, logger)\n\n // Get the workflow\n const workflow = await payload.findByID({\n id: workflowId,\n collection: 'workflows',\n depth: 0\n })\n\n if (!workflow) {\n logger.warn({ workflowId }, 'Workflow not found for cron job update')\n return\n }\n\n const triggers = workflow.triggers as Array<{\n cronExpression?: string\n timezone?: string\n type: string\n }>\n\n // Find all cron triggers for this workflow\n const cronTriggers = triggers?.filter(t => t.type === 'cron-trigger') || []\n\n if (cronTriggers.length === 0) {\n logger.debug({ workflowId }, 'No cron triggers found for workflow')\n return\n }\n\n let scheduledJobs = 0\n\n for (const trigger of cronTriggers) {\n if (trigger.cronExpression) {\n try {\n // Validate cron expression before queueing\n if (!validateCronExpression(trigger.cronExpression)) {\n logger.error({\n cronExpression: trigger.cronExpression,\n workflowId,\n workflowName: workflow.name\n }, 'Invalid cron expression format')\n continue\n }\n\n // Validate timezone if provided\n if (trigger.timezone) {\n try {\n new Intl.DateTimeFormat('en', { timeZone: trigger.timezone })\n } catch {\n logger.error({\n timezone: trigger.timezone,\n workflowId,\n workflowName: workflow.name\n }, 'Invalid timezone specified')\n continue\n }\n }\n\n // Calculate next execution time\n const nextExecution = getNextCronTime(trigger.cronExpression, trigger.timezone)\n\n // Queue the job\n await payload.jobs.queue({\n input: { cronExpression: trigger.cronExpression, timezone: trigger.timezone, workflowId },\n task: 'workflow-cron-executor',\n waitUntil: nextExecution\n })\n\n scheduledJobs++\n\n logger.info({\n cronExpression: trigger.cronExpression,\n nextExecution: nextExecution.toISOString(),\n timezone: trigger.timezone || 'UTC',\n workflowId,\n workflowName: workflow.name\n }, 'Scheduled cron job for workflow')\n } catch (error) {\n logger.error({\n cronExpression: trigger.cronExpression,\n error: error instanceof Error ? error.message : 'Unknown error',\n timezone: trigger.timezone,\n workflowId,\n workflowName: workflow.name\n }, 'Failed to schedule cron job')\n }\n }\n }\n\n if (scheduledJobs > 0) {\n logger.info({ scheduledJobs, workflowId }, 'Updated cron jobs for workflow')\n }\n } catch (error) {\n logger.error({\n error: error instanceof Error ? error.message : 'Unknown error',\n workflowId\n }, 'Failed to update workflow cron jobs')\n }\n}\n\n/**\n * Cancel all cron jobs for a specific workflow\n */\nexport function cancelWorkflowCronJobs(\n workflowId: string,\n payload: Payload,\n logger: Payload['logger']\n): void {\n try {\n // Note: PayloadCMS job system doesn't have a built-in way to cancel specific jobs by input\n // This is a limitation we need to work around\n // For now, we log that we would cancel jobs for this workflow\n logger.debug({ workflowId }, 'Would cancel existing cron jobs for workflow (PayloadCMS limitation: cannot selectively cancel jobs)')\n } catch (error) {\n logger.error({\n error: error instanceof Error ? error.message : 'Unknown error',\n workflowId\n }, 'Failed to cancel workflow cron jobs')\n }\n}\n\n/**\n * Remove cron jobs for a deleted workflow\n */\nexport function removeWorkflowCronJobs(\n workflowId: string,\n payload: Payload,\n logger: Payload['logger']\n): void {\n try {\n cancelWorkflowCronJobs(workflowId, payload, logger)\n logger.info({ workflowId }, 'Removed cron jobs for deleted workflow')\n } catch (error) {\n logger.error({\n error: error instanceof Error ? error.message : 'Unknown error',\n workflowId\n }, 'Failed to remove workflow cron jobs')\n }\n}\n"],"names":["cron","WorkflowExecutor","getConfigLogger","generateCronTasks","config","logger","cronTask","slug","handler","input","req","cronExpression","timezone","workflowId","payload","child","plugin","workflow","findByID","id","collection","depth","Error","context","steps","trigger","type","triggeredAt","Date","toISOString","executor","triggers","matchingTrigger","find","condition","conditionMet","evaluateCondition","info","workflowName","name","requeueCronJob","output","executedAt","reason","status","state","execute","error","message","includes","catch","requeueError","jobs","tasks","task","debug","push","registerCronJobs","workflows","limit","where","equals","docs","length","cronTriggers","filter","t","validateCronExpression","Intl","DateTimeFormat","timeZone","nextExecution","getNextCronTime","queue","waitUntil","warn","validate","now","options","schedule","cronParts","trim","split","destroy","minutePart","hourPart","dayPart","monthPart","weekdayPart","testTime","getTime","setSeconds","maxIterations","iterations","minute","getMinutes","hour","getHours","dayOfMonth","getDate","month","getMonth","dayOfWeek","getDay","matchesCronPart","incrementTimeForCronPattern","currentTime","_dayPart","_monthPart","_weekdayPart","nextTime","targetMinute","getNextValidCronValue","setHours","setMinutes","targetHour","setDate","setTime","currentValue","cronPart","values","parseCronPart","v","forEach","part","sort","a","b","start","end","map","n","parseInt","i","range","step","stepNum","baseValues","_","index","value","cronValue","nextRun","updateWorkflowCronJobs","cancelWorkflowCronJobs","scheduledJobs","removeWorkflowCronJobs"],"mappings":"AAEA,YAAYA,UAAU,YAAW;AAEjC,SAAuBC,gBAAgB,QAAO,+BAA8B;AAC5E,SAAQC,eAAe,QAAO,c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kB,EAClBC,OAAgB,EAChBT,MAAyB;IAEzB,IAAI;QACF,2FAA2F;QAC3F,8CAA8C;QAC9C,8DAA8D;QAC9DA,OAAOkD,KAAK,CAAC;YAAE1C;QAAW,GAAG;IAC/B,EAAE,OAAOkC,OAAO;QACd1C,OAAO0C,KAAK,CAAC;YACXA,OAAOA,iBAAiBzB,QAAQyB,MAAMC,OAAO,GAAG;YAChDnC;QACF,GAAG;IACL;AACF;AAEA;;CAEC,GACD,OAAO,SAASqI,uBACdrI,UAAkB,EAClBC,OAAgB,EAChBT,MAAyB;IAEzB,IAAI;QACF2I,uBAAuBnI,YAAYC,SAAST;QAC5CA,OAAOgC,IAAI,CAAC;YAAExB;QAAW,GAAG;IAC9B,EAAE,OAAOkC,OAAO;QACd1C,OAAO0C,KAAK,CAAC;YACXA,OAAOA,iBAAiBzB,QAAQyB,MAAMC,OAAO,GAAG;YAChDnC;QACF,GAAG;IACL;AACF"}
@@ -38,7 +38,7 @@ export function initWebhookEndpoint(config, webhookPrefix = 'webhook') {
38
38
  status: 404
39
39
  });
40
40
  }
41
- // Create workflow executor for this request
41
+ // Create a workflow executor for this request
42
42
  const logger = initializeLogger(req.payload);
43
43
  const executor = new WorkflowExecutor(req.payload, logger);
44
44
  const executionPromises = workflows.docs.map(async (workflow)=>{
@@ -59,23 +59,33 @@ export function initWebhookEndpoint(config, webhookPrefix = 'webhook') {
59
59
  const matchingTrigger = triggers?.find((trigger)=>trigger.type === 'webhook-trigger' && trigger.webhookPath === path);
60
60
  // Check trigger condition if present
61
61
  if (matchingTrigger?.condition) {
62
+ logger.debug({
63
+ condition: matchingTrigger.condition,
64
+ path,
65
+ webhookData: JSON.stringify(webhookData).substring(0, 200),
66
+ headers: Object.keys(context.trigger.headers || {}),
67
+ workflowId: workflow.id,
68
+ workflowName: workflow.name
69
+ }, 'Evaluating webhook trigger condition');
62
70
  const conditionMet = executor.evaluateCondition(matchingTrigger.condition, context);
63
71
  if (!conditionMet) {
64
72
  logger.info({
65
73
  condition: matchingTrigger.condition,
66
74
  path,
75
+ webhookDataSnapshot: JSON.stringify(webhookData).substring(0, 200),
67
76
  workflowId: workflow.id,
68
77
  workflowName: workflow.name
69
78
  }, 'Webhook trigger condition not met, skipping workflow');
70
79
  return {
80
+ reason: 'Condition not met',
71
81
  status: 'skipped',
72
- workflowId: workflow.id,
73
- reason: 'Condition not met'
82
+ workflowId: workflow.id
74
83
  };
75
84
  }
76
85
  logger.info({
77
86
  condition: matchingTrigger.condition,
78
87
  path,
88
+ webhookDataSnapshot: JSON.stringify(webhookData).substring(0, 200),
79
89
  workflowId: workflow.id,
80
90
  workflowName: workflow.name
81
91
  }, 'Webhook trigger condition met');
@@ -136,7 +146,7 @@ export function initWebhookEndpoint(config, webhookPrefix = 'webhook') {
136
146
  method: 'post',
137
147
  path: `${normalizedPrefix}/:path`
138
148
  };
139
- // Check if webhook endpoint already exists to avoid duplicates
149
+ // Check if the webhook endpoint already exists to avoid duplicates
140
150
  const existingEndpoint = config.endpoints?.find((endpoint)=>endpoint.path === webhookEndpoint.path && endpoint.method === webhookEndpoint.method);
141
151
  if (!existingEndpoint) {
142
152
  // Combine existing endpoints with the webhook endpoint
@@ -1 +1 @@
1
- {"version":3,"sources":["../../src/plugin/init-webhook.ts"],"sourcesContent":["import type {Config, PayloadRequest} from 'payload'\n\nimport {type Workflow, WorkflowExecutor} from '../core/workflow-executor.js'\nimport {getConfigLogger, initializeLogger} from './logger.js'\n\nexport function initWebhookEndpoint(config: Config, webhookPrefix = 'webhook'): void {\n const logger = getConfigLogger()\n // Ensure the prefix starts with a slash\n const normalizedPrefix = webhookPrefix.startsWith('/') ? webhookPrefix : `/${webhookPrefix}`\n logger.debug(`Adding webhook endpoint to config with prefix: ${normalizedPrefix}`)\n logger.debug('Current config.endpoints length:', config.endpoints?.length || 0)\n\n // Define webhook endpoint\n const webhookEndpoint = {\n handler: async (req: PayloadRequest) => {\n const {path} = req.routeParams as { path: string }\n const webhookData = req.body || {}\n\n logger.debug('Webhook endpoint handler called, path: ' + path)\n\n try {\n // Find workflows with matching webhook triggers\n const workflows = await req.payload.find({\n collection: 'workflows',\n depth: 2,\n limit: 100,\n req,\n where: {\n 'triggers.type': {\n equals: 'webhook-trigger'\n },\n 'triggers.webhookPath': {\n equals: path\n }\n }\n })\n\n if (workflows.docs.length === 0) {\n return new Response(\n JSON.stringify({error: 'No workflows found for this webhook path'}),\n {\n headers: {'Content-Type': 'application/json'},\n status: 404\n }\n )\n }\n\n // Create workflow executor for this request\n const logger = initializeLogger(req.payload)\n const executor = new WorkflowExecutor(req.payload, logger)\n\n const executionPromises = workflows.docs.map(async (workflow) => {\n try {\n // Create execution context for the webhook trigger\n const context = {\n steps: {},\n trigger: {\n type: 'webhook',\n data: webhookData,\n headers: Object.fromEntries(req.headers?.entries() || []),\n path,\n req\n }\n }\n\n // Find the matching trigger and check its condition if present\n const triggers = workflow.triggers as Array<{\n condition?: string\n type: string\n webhookPath?: string\n }>\n\n const matchingTrigger = triggers?.find(trigger =>\n trigger.type === 'webhook-trigger' &&\n trigger.webhookPath === path\n )\n\n // Check trigger condition if present\n if (matchingTrigger?.condition) {\n const conditionMet = executor.evaluateCondition(matchingTrigger.condition, context)\n \n if (!conditionMet) {\n logger.info({\n condition: matchingTrigger.condition,\n path,\n workflowId: workflow.id,\n workflowName: workflow.name\n }, 'Webhook trigger condition not met, skipping workflow')\n \n return { status: 'skipped', workflowId: workflow.id, reason: 'Condition not met' }\n }\n \n logger.info({\n condition: matchingTrigger.condition,\n path,\n workflowId: workflow.id,\n workflowName: workflow.name\n }, 'Webhook trigger condition met')\n }\n\n // Execute the workflow\n await executor.execute(workflow as Workflow, context, req)\n\n return { status: 'triggered', workflowId: workflow.id }\n } catch (error) {\n return {\n error: error instanceof Error ? error.message : 'Unknown error',\n status: 'failed',\n workflowId: workflow.id\n }\n }\n })\n\n const results = await Promise.allSettled(executionPromises)\n const resultsData = results.map((result, index) => {\n const baseResult = { workflowId: workflows.docs[index].id }\n if (result.status === 'fulfilled') {\n return { ...baseResult, ...result.value }\n } else {\n return { ...baseResult, error: result.reason, status: 'failed' }\n }\n })\n\n return new Response(\n JSON.stringify({\n message: `Triggered ${workflows.docs.length} workflow(s)`,\n results: resultsData\n }),\n {\n headers: { 'Content-Type': 'application/json' },\n status: 200\n }\n )\n\n } catch (error) {\n return new Response(\n JSON.stringify({\n details: error instanceof Error ? error.message : 'Unknown error',\n error: 'Failed to process webhook'\n }),\n {\n headers: { 'Content-Type': 'application/json' },\n status: 500\n }\n )\n }\n },\n method: 'post' as const,\n path: `${normalizedPrefix}/:path`\n }\n\n // Check if webhook endpoint already exists to avoid duplicates\n const existingEndpoint = config.endpoints?.find(endpoint => \n endpoint.path === webhookEndpoint.path && endpoint.method === webhookEndpoint.method\n )\n \n if (!existingEndpoint) {\n // Combine existing endpoints with the webhook endpoint\n config.endpoints = [...(config.endpoints || []), webhookEndpoint]\n logger.debug(`Webhook endpoint added at path: ${webhookEndpoint.path}`)\n logger.debug('New config.endpoints length:', config.endpoints.length)\n } else {\n logger.debug(`Webhook endpoint already exists at path: ${webhookEndpoint.path}`)\n }\n}\n"],"names":["WorkflowExecutor","getConfigLogger","initializeLogger","initWebhookEndpoint","config","webhookPrefix","logger","normalizedPrefix","startsWith","debug","endpoints","length","webhookEndpoint","handler","req","path","routeParams","webhookData","body","workflows","payload","find","collection","depth","limit","where","equals","docs","Response","JSON","stringify","error","headers","status","executor","executionPromises","map","workflow","context","steps","trigger","type","data","Object","fromEntries","entries","triggers","matchingTrigger","webhookPath","condition","conditionMet","evaluateCondition","info","workflowId","id","workflowName","name","reason","execute","Error","message","results","Promise","allSettled","resultsData","result","index","baseResult","value","details","method","existingEndpoint","endpoint"],"mappings":"AAEA,SAAuBA,gBAAgB,QAAO,+BAA8B;AAC5E,SAAQC,eAAe,EAAEC,gBAAgB,QAAO,cAAa;AAE7D,OAAO,SAASC,oBAAoBC,MAAc,EAAEC,gBAAgB,SAAS;IAC3E,MAAMC,SAASL;IACf,wCAA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ayloadRequest} from 'payload'\n\nimport {type Workflow, WorkflowExecutor} from '../core/workflow-executor.js'\nimport {getConfigLogger, initializeLogger} from './logger.js'\n\nexport function initWebhookEndpoint(config: Config, webhookPrefix = 'webhook'): void {\n const logger = getConfigLogger()\n // Ensure the prefix starts with a slash\n const normalizedPrefix = webhookPrefix.startsWith('/') ? webhookPrefix : `/${webhookPrefix}`\n logger.debug(`Adding webhook endpoint to config with prefix: ${normalizedPrefix}`)\n logger.debug('Current config.endpoints length:', config.endpoints?.length || 0)\n\n // Define webhook endpoint\n const webhookEndpoint = {\n handler: async (req: PayloadRequest) => {\n const {path} = req.routeParams as { path: string }\n const webhookData = req.body || {}\n\n logger.debug('Webhook endpoint handler called, path: ' + path)\n\n try {\n // Find workflows with matching webhook triggers\n const workflows = await req.payload.find({\n collection: 'workflows',\n depth: 2,\n limit: 100,\n req,\n where: {\n 'triggers.type': {\n equals: 'webhook-trigger'\n },\n 'triggers.webhookPath': {\n equals: path\n }\n }\n })\n\n if (workflows.docs.length === 0) {\n return new Response(\n JSON.stringify({error: 'No workflows found for this webhook path'}),\n {\n headers: {'Content-Type': 'application/json'},\n status: 404\n }\n )\n }\n\n // Create a workflow executor for this request\n const logger = initializeLogger(req.payload)\n const executor = new WorkflowExecutor(req.payload, logger)\n\n const executionPromises = workflows.docs.map(async (workflow) => {\n try {\n // Create execution context for the webhook trigger\n const context = {\n steps: {},\n trigger: {\n type: 'webhook',\n data: webhookData,\n headers: Object.fromEntries(req.headers?.entries() || []),\n path,\n req\n }\n }\n\n // Find the matching trigger and check its condition if present\n const triggers = workflow.triggers as Array<{\n condition?: string\n type: string\n webhookPath?: string\n }>\n\n const matchingTrigger = triggers?.find(trigger =>\n trigger.type === 'webhook-trigger' &&\n trigger.webhookPath === path\n )\n\n // Check trigger condition if present\n if (matchingTrigger?.condition) {\n logger.debug({\n condition: matchingTrigger.condition,\n path,\n webhookData: JSON.stringify(webhookData).substring(0, 200),\n headers: Object.keys(context.trigger.headers || {}),\n workflowId: workflow.id,\n workflowName: workflow.name\n }, 'Evaluating webhook trigger condition')\n\n const conditionMet = executor.evaluateCondition(matchingTrigger.condition, context)\n\n if (!conditionMet) {\n logger.info({\n condition: matchingTrigger.condition,\n path,\n webhookDataSnapshot: JSON.stringify(webhookData).substring(0, 200),\n workflowId: workflow.id,\n workflowName: workflow.name\n }, 'Webhook trigger condition not met, skipping workflow')\n\n return { reason: 'Condition not met', status: 'skipped', workflowId: workflow.id }\n }\n\n logger.info({\n condition: matchingTrigger.condition,\n path,\n webhookDataSnapshot: JSON.stringify(webhookData).substring(0, 200),\n workflowId: workflow.id,\n workflowName: workflow.name\n }, 'Webhook trigger condition met')\n }\n\n // Execute the workflow\n await executor.execute(workflow as Workflow, context, req)\n\n return { status: 'triggered', workflowId: workflow.id }\n } catch (error) {\n return {\n error: error instanceof Error ? error.message : 'Unknown error',\n status: 'failed',\n workflowId: workflow.id\n }\n }\n })\n\n const results = await Promise.allSettled(executionPromises)\n const resultsData = results.map((result, index) => {\n const baseResult = { workflowId: workflows.docs[index].id }\n if (result.status === 'fulfilled') {\n return { ...baseResult, ...result.value }\n } else {\n return { ...baseResult, error: result.reason, status: 'failed' }\n }\n })\n\n return new Response(\n JSON.stringify({\n message: `Triggered ${workflows.docs.length} workflow(s)`,\n results: resultsData\n }),\n {\n headers: { 'Content-Type': 'application/json' },\n status: 200\n }\n )\n\n } catch (error) {\n return new Response(\n JSON.stringify({\n details: error instanceof Error ? error.message : 'Unknown error',\n error: 'Failed to process webhook'\n }),\n {\n headers: { 'Content-Type': 'application/json' },\n status: 500\n }\n )\n }\n },\n method: 'post' as const,\n path: `${normalizedPrefix}/:path`\n }\n\n // Check if the webhook endpoint already exists to avoid duplicates\n const existingEndpoint = config.endpoints?.find(endpoint =>\n endpoint.path === webhookEndpoint.path && endpoint.method === webhookEndpoint.method\n )\n\n if (!existingEndpoint) {\n // Combine existing endpoints with the webhook endpoint\n config.endpoints = [...(config.endpoints || []), webhookEndpoint]\n logger.debug(`Webhook endpoint added at path: ${webhookEndpoint.path}`)\n logger.debug('New config.endpoints length:', config.endpoints.length)\n } else {\n logger.debug(`Webhook endpoint already exists at path: ${webhookEndpoint.path}`)\n }\n}\n"],"names":["WorkflowExecutor","getConfigLogger","initializeLogger","initWebhookEndpoint","config","webhookPrefix","logger","normalizedPrefix","startsWith","debug","endpoints","length","webhookEndpoint","handler","req","path","routeParams","webhookData","body","workflows","payload","find","collection","depth","limit","where","equals","docs","Response","JSON","stringify","error","headers","status","executor","executionPromises","map","workflow","context","steps","trigger","type","data","Object","fromEntries","entries","triggers","matchingTrigger","webhookPath","condition","substring","keys","workflowId","id","workflowName","name","conditionMet","evaluateCondition","info","webhookDataSnapshot","reason","execute","Error","message","results","Promise","allSettled","resultsData","result","index","baseResult","value","details","method","existingEndpoint","endpoint"],"mappings":"AAEA,SAAuBA,gBAAgB,QAAO,+BAA8B;AAC5E,SAAQC,eAAe,EAAEC,gBAAgB,QAAO,cAAa;AAE7D,OAAO,SAASC,oBAAoBC,MAAc,EAAEC,gBAAgB,SAAS;IAC3E,MAAMC,SAASL;IACf,wCAAwC;IACxC,MAAMM,mBAAmBF,cAAcG,UAAU,CAAC,OAAOH,gBAAgB,CAAC,CAAC,EAAEA,eAAe;IAC5FC,OAAOG,KAAK,CAAC,CAAC,+CAA+C,EAAEF,kBAAkB;IACjFD,OAAOG,KAAK,CAAC,oCAAoCL,OAAOM,SAAS,EAAEC,UAAU;IAE7E,0BAA0B;IAC1B,MAAMC,kBAAkB;QACtBC,SAAS,OAAOC;YACd,MAAM,EAACC,IAAI,EAAC,GAAGD,IAAIE,WAAW;YAC9B,MAAMC,cAAcH,IAAII,IAAI,IAAI,CAAC;YAEjCZ,OAAOG,KAAK,CAAC,4CAA4CM;YAEzD,IAAI;gBACF,gDAAgD;gBAChD,MAAMI,YAAY,MAAML,IAAIM,OAAO,CAACC,IAAI,CAAC;oBACvCC,YAAY;oBACZC,OAAO;oBACPC,OAAO;oBACPV;oBACAW,OAAO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AAQ;wBAAS;oBACjE;gBACF;gBAEA,OAAO,IAAIL,SACTC,KAAKC,SAAS,CAAC;oBACbiC,SAAS,CAAC,UAAU,EAAE5C,UAAUQ,IAAI,CAAChB,MAAM,CAAC,YAAY,CAAC;oBACzDqD,SAASG;gBACX,IACA;oBACEnC,SAAS;wBAAE,gBAAgB;oBAAmB;oBAC9CC,QAAQ;gBACV;YAGJ,EAAE,OAAOF,OAAO;gBACd,OAAO,IAAIH,SACTC,KAAKC,SAAS,CAAC;oBACb0C,SAASzC,iBAAiB+B,QAAQ/B,MAAMgC,OAAO,GAAG;oBAClDhC,OAAO;gBACT,IACA;oBACEC,SAAS;wBAAE,gBAAgB;oBAAmB;oBAC9CC,QAAQ;gBACV;YAEJ;QACF;QACAwC,QAAQ;QACR1D,MAAM,GAAGR,iBAAiB,MAAM,CAAC;IACnC;IAEA,mEAAmE;IACnE,MAAMmE,mBAAmBtE,OAAOM,SAAS,EAAEW,KAAKsD,CAAAA,WAC9CA,SAAS5D,IAAI,KAAKH,gBAAgBG,IAAI,IAAI4D,SAASF,MAAM,KAAK7D,gBAAgB6D,MAAM;IAGtF,IAAI,CAACC,kBAAkB;QACrB,uDAAuD;QACvDtE,OAAOM,SAAS,GAAG;eAAKN,OAAOM,SAAS,IAAI,EAAE;YAAGE;SAAgB;QACjEN,OAAOG,KAAK,CAAC,CAAC,gCAAgC,EAAEG,gBAAgBG,IAAI,EAAE;QACtET,OAAOG,KAAK,CAAC,gCAAgCL,OAAOM,SAAS,CAACC,MAAM;IACtE,OAAO;QACLL,OAAOG,KAAK,CAAC,CAAC,yCAAyC,EAAEG,gBAAgBG,IAAI,EAAE;IACjF;AACF"}
@@ -3,10 +3,10 @@ import type { Payload } from 'payload';
3
3
  * Get a logger for config-time use (before Payload initialization)
4
4
  */
5
5
  export declare function getConfigLogger(): {
6
- debug: (message: string, ...args: any[]) => void;
7
- error: (message: string, ...args: any[]) => void;
8
- info: (message: string, ...args: any[]) => void;
9
- warn: (message: string, ...args: any[]) => void;
6
+ debug: <T>(message: string, ...args: T[]) => void;
7
+ error: <T>(message: string, ...args: T[]) => void;
8
+ info: <T>(message: string, ...args: T[]) => void;
9
+ warn: <T>(message: string, ...args: T[]) => void;
10
10
  };
11
11
  /**
12
12
  * Initialize the plugin logger using Payload's Pino instance
@@ -5,17 +5,27 @@ let pluginLogger = null;
5
5
  * Uses console with plugin prefix since Payload logger isn't available yet
6
6
  */ const configLogger = {
7
7
  debug: (message, ...args)=>{
8
- if (process.env.NODE_ENV === 'development') {
9
- console.log(`[payload-automation] ${message}`, ...args);
8
+ if (!process.env.PAYLOAD_AUTOMATION_LOGGING) {
9
+ return;
10
10
  }
11
+ console.log(`[payload-automation] ${message}`, ...args);
11
12
  },
12
13
  error: (message, ...args)=>{
14
+ if (!process.env.PAYLOAD_AUTOMATION_LOGGING) {
15
+ return;
16
+ }
13
17
  console.error(`[payload-automation] ${message}`, ...args);
14
18
  },
15
19
  info: (message, ...args)=>{
20
+ if (!process.env.PAYLOAD_AUTOMATION_LOGGING) {
21
+ return;
22
+ }
16
23
  console.log(`[payload-automation] ${message}`, ...args);
17
24
  },
18
25
  warn: (message, ...args)=>{
26
+ if (!process.env.PAYLOAD_AUTOMATION_LOGGING) {
27
+ return;
28
+ }
19
29
  console.warn(`[payload-automation] ${message}`, ...args);
20
30
  }
21
31
  };
@@ -30,6 +40,7 @@ let pluginLogger = null;
30
40
  */ export function initializeLogger(payload) {
31
41
  // Create a child logger with plugin identification
32
42
  pluginLogger = payload.logger.child({
43
+ level: process.env.PAYLOAD_AUTOMATION_LOGGING || 'silent',
33
44
  plugin: '@xtr-dev/payload-automation'
34
45
  });
35
46
  return pluginLogger;
@@ -1 +1 @@
1
- {"version":3,"sources":["../../src/plugin/logger.ts"],"sourcesContent":["import type { Payload } from 'payload'\n\n// Global logger instance - use Payload's logger type\nlet pluginLogger: Payload['logger'] | null = null\n\n/**\n * Simple config-time logger for use during plugin configuration\n * Uses console with plugin prefix since Payload logger isn't available yet\n */\nconst configLogger = {\n debug: (message: string, ...args: any[]) => {\n if (process.env.NODE_ENV === 'development') {\n console.log(`[payload-automation] ${message}`, ...args)\n }\n },\n error: (message: string, ...args: any[]) => {\n console.error(`[payload-automation] ${message}`, ...args)\n },\n info: (message: string, ...args: any[]) => {\n console.log(`[payload-automation] ${message}`, ...args)\n },\n warn: (message: string, ...args: any[]) => {\n console.warn(`[payload-automation] ${message}`, ...args)\n }\n}\n\n/**\n * Get a logger for config-time use (before Payload initialization)\n */\nexport function getConfigLogger() {\n return configLogger\n}\n\n/**\n * Initialize the plugin logger using Payload's Pino instance\n * This creates a child logger with plugin identification\n */\nexport function initializeLogger(payload: Payload): Payload['logger'] {\n // Create a child logger with plugin identification\n pluginLogger = payload.logger.child({\n plugin: '@xtr-dev/payload-automation'\n })\n return pluginLogger\n}\n\n/**\n * Get the plugin logger instance\n * Throws error if not initialized\n */\nexport function getLogger(): Payload['logger'] {\n if (!pluginLogger) {\n throw new Error('@xtr-dev/payload-automation: Logger not initialized. Make sure the plugin is properly configured.')\n }\n\n return pluginLogger\n}\n"],"names":["pluginLogger","configLogger","debug","message","args","process","env","NODE_ENV","console","log","error","info","warn","getConfigLogger","initializeLogger","payload","logger","child","plugin","getLogger","Error"],"mappings":"AAEA,qDAAqD;AACrD,IAAIA,eAAyC;AAE7C;;;CAGC,GACD,MAAMC,eAAe;IACnBC,OAAO,CAACC,SAAiB,GAAGC;QAC1B,IAAIC,QAAQC,GAAG,CAACC,QAAQ,KAAK,eAAe;YAC1CC,QAAQC,GAAG,CAAC,CAAC,qBAAqB,EAAEN,SAAS,KAAKC;QACpD;IACF;IACAM,OAAO,CAACP,SAAiB,GAAGC;QAC1BI,QAAQE,KAAK,CAAC,CAAC,qBAAqB,EAAEP,SAAS,KAAKC;IACtD;IACAO,MAAM,CAACR,SAAiB,GAAGC;QACzBI,QAAQC,GAAG,CAAC,CAAC,qBAAqB,EAAEN,SAAS,KAAKC;IACpD;IACAQ,MAAM,CAACT,SAAiB,GAAGC;QACzBI,QAAQI,IAAI,CAAC,CAAC,qBAAqB,EAAET,SAAS,KAAKC;IACrD;AACF;AAEA;;CAEC,GACD,OAAO,SAASS;IACd,OAAOZ;AACT;AAEA;;;CAGC,GACD,OAAO,SAASa,iBAAiBC,OAAgB;IAC/C,mDAAmD;IACnDf,eAAee,QAAQC,MAAM,CAACC,KAAK,CAAC;QAClCC,QAAQ;IACV;IACA,OAAOlB;AACT;AAEA;;;CAGC,GACD,OAAO,SAASmB;IACd,IAAI,CAACnB,cAAc;QACjB,MAAM,IAAIoB,MAAM;IAClB;IAEA,OAAOpB;AACT"}
1
+ {"version":3,"sources":["../../src/plugin/logger.ts"],"sourcesContent":["import type { Payload } from 'payload'\n\n// Global logger instance - use Payload's logger type\nlet pluginLogger: null | Payload['logger'] = null\n\n/**\n * Simple config-time logger for use during plugin configuration\n * Uses console with plugin prefix since Payload logger isn't available yet\n */\nconst configLogger = {\n debug: <T>(message: string, ...args: T[]) => {\n if (!process.env.PAYLOAD_AUTOMATION_LOGGING) {return}\n console.log(`[payload-automation] ${message}`, ...args)\n },\n error: <T>(message: string, ...args: T[]) => {\n if (!process.env.PAYLOAD_AUTOMATION_LOGGING) {return}\n console.error(`[payload-automation] ${message}`, ...args)\n },\n info: <T>(message: string, ...args: T[]) => {\n if (!process.env.PAYLOAD_AUTOMATION_LOGGING) {return}\n console.log(`[payload-automation] ${message}`, ...args)\n },\n warn: <T>(message: string, ...args: T[]) => {\n if (!process.env.PAYLOAD_AUTOMATION_LOGGING) {return}\n console.warn(`[payload-automation] ${message}`, ...args)\n }\n}\n\n/**\n * Get a logger for config-time use (before Payload initialization)\n */\nexport function getConfigLogger() {\n return configLogger\n}\n\n/**\n * Initialize the plugin logger using Payload's Pino instance\n * This creates a child logger with plugin identification\n */\nexport function initializeLogger(payload: Payload): Payload['logger'] {\n // Create a child logger with plugin identification\n pluginLogger = payload.logger.child({\n level: process.env.PAYLOAD_AUTOMATION_LOGGING || 'silent',\n plugin: '@xtr-dev/payload-automation'\n })\n return pluginLogger\n}\n\n/**\n * Get the plugin logger instance\n * Throws error if not initialized\n */\nexport function getLogger(): Payload['logger'] {\n if (!pluginLogger) {\n throw new Error('@xtr-dev/payload-automation: Logger not initialized. Make sure the plugin is properly configured.')\n }\n\n return pluginLogger\n}\n"],"names":["pluginLogger","configLogger","debug","message","args","process","env","PAYLOAD_AUTOMATION_LOGGING","console","log","error","info","warn","getConfigLogger","initializeLogger","payload","logger","child","level","plugin","getLogger","Error"],"mappings":"AAEA,qDAAqD;AACrD,IAAIA,eAAyC;AAE7C;;;CAGC,GACD,MAAMC,eAAe;IACnBC,OAAO,CAAIC,SAAiB,GAAGC;QAC7B,IAAI,CAACC,QAAQC,GAAG,CAACC,0BAA0B,EAAE;YAAC;QAAM;QACpDC,QAAQC,GAAG,CAAC,CAAC,qBAAqB,EAAEN,SAAS,KAAKC;IACpD;IACAM,OAAO,CAAIP,SAAiB,GAAGC;QAC7B,IAAI,CAACC,QAAQC,GAAG,CAACC,0BAA0B,EAAE;YAAC;QAAM;QACpDC,QAAQE,KAAK,CAAC,CAAC,qBAAqB,EAAEP,SAAS,KAAKC;IACtD;IACAO,MAAM,CAAIR,SAAiB,GAAGC;QAC5B,IAAI,CAACC,QAAQC,GAAG,CAACC,0BAA0B,EAAE;YAAC;QAAM;QACpDC,QAAQC,GAAG,CAAC,CAAC,qBAAqB,EAAEN,SAAS,KAAKC;IACpD;IACAQ,MAAM,CAAIT,SAAiB,GAAGC;QAC5B,IAAI,CAACC,QAAQC,GAAG,CAACC,0BAA0B,EAAE;YAAC;QAAM;QACpDC,QAAQI,IAAI,CAAC,CAAC,qBAAqB,EAAET,SAAS,KAAKC;IACrD;AACF;AAEA;;CAEC,GACD,OAAO,SAASS;IACd,OAAOZ;AACT;AAEA;;;CAGC,GACD,OAAO,SAASa,iBAAiBC,OAAgB;IAC/C,mDAAmD;IACnDf,eAAee,QAAQC,MAAM,CAACC,KAAK,CAAC;QAClCC,OAAOb,QAAQC,GAAG,CAACC,0BAA0B,IAAI;QACjDY,QAAQ;IACV;IACA,OAAOnB;AACT;AAEA;;;CAGC,GACD,OAAO,SAASoB;IACd,IAAI,CAACpB,cAAc;QACjB,MAAM,IAAIqB,MAAM;IAClB;IAEA,OAAOrB;AACT"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@xtr-dev/payload-automation",
3
- "version": "0.0.1",
3
+ "version": "0.0.3",
4
4
  "description": "PayloadCMS Automation Plugin - Comprehensive workflow automation system with visual workflow building, execution tracking, and step types",
5
5
  "license": "MIT",
6
6
  "type": "module",