nodes ?? [])->firstWhere('type', 'trigger'); $context = ['trigger' => $this->synthesizePayload($automation, $triggerNode ?? [])]; $firstNodeId = $this->findFirstRealNodeId($automation, $triggerNode); $run = AutomationRun::create([ 'automation_id' => $automation->id, 'status' => Status::Pending, 'is_manual' => true, 'is_dry_run' => ! $withRealData, 'context' => $context, ]); if ($firstNodeId === null) { $run->update([ 'status' => Status::Failed, 'error' => ['message' => __('automations.errors.no_trigger_connection')], 'finished_at' => now(), ]); return $run; } ProcessAutomationNode::dispatch($run, $firstNodeId); return $run; } /** * @param array $triggerNode * @return array */ private function synthesizePayload(Automation $automation, array $triggerNode): array { $type = data_get($triggerNode, 'data.trigger_type'); return match ($type) { TriggerType::PostPublished->value, TriggerType::PostScheduled->value => $this->synthesizePostPayload($automation, (string) $type), default => ['event' => $type ?? TriggerType::Schedule->value, 'fired_at' => now()->toIso8601String(), 'manual' => true], }; } /** * Picks the most recent post in the automation's workspace so the test run * reflects something the user actually sees. Falls back to a placeholder * payload when the workspace has no posts yet. * * @return array */ private function synthesizePostPayload(Automation $automation, string $event): array { $post = Post::query() ->where('workspace_id', $automation->workspace_id) ->latest() ->first(); $base = [ 'event' => $event, 'fired_at' => now()->toIso8601String(), 'manual' => true, ]; if ($post === null) { return array_merge($base, ['post' => null, 'fetch_error' => 'no posts in workspace']); } return array_merge($base, [ 'post' => [ 'id' => $post->id, 'content' => $post->content, 'status' => $post->status->value, 'scheduled_at' => $post->scheduled_at?->toIso8601String(), 'published_at' => $post->published_at?->toIso8601String(), ], ]); } /** * @param array|null $triggerNode */ private function findFirstRealNodeId(Automation $automation, ?array $triggerNode): ?string { if ($triggerNode === null) { return null; } $connection = collect($automation->connections ?? []) ->firstWhere('source', $triggerNode['id']); return $connection['target'] ?? null; } }