where('workspace_id', $post->workspace_id) ->where('status', AutomationStatus::Active) ->where('trigger_type', $triggerType->value) ->get(); foreach ($automations as $automation) { $triggerNode = collect($automation->nodes ?? [])->firstWhere('type', NodeType::Trigger->value); if ($triggerNode === null) { continue; } $this->dispatchRun($automation, $triggerNode, $post); } } private function dispatchRun(Automation $automation, array $triggerNode, Post $post): void { $context = [ 'trigger' => [ 'event' => $triggerNode['data']['trigger_type'], 'fired_at' => now()->toIso8601String(), 'post' => [ 'id' => $post->id, 'content' => $post->content, 'status' => $post->status->value, 'scheduled_at' => $post->scheduled_at?->toIso8601String(), 'published_at' => $post->published_at?->toIso8601String(), ], ], ]; $run = AutomationRun::create([ 'automation_id' => $automation->id, 'status' => RunStatus::Pending, 'context' => $context, ]); $targets = $this->advance->targetsFor($automation, $triggerNode['id']); if ($targets === []) { $run->update([ 'status' => RunStatus::Failed, 'error' => ['message' => __('automations.errors.no_trigger_connection')], 'finished_at' => now(), ]); return; } $this->advance->dispatchBranches($run, $targets); } }