trypost/app/Actions/Automation/Run/TestAutomation.php
Paulo Castellano b23ab0166e feat(automations): implement automation features and UI enhancements
- Added new automation-related routes and controllers for managing automations.
- Introduced automation nodes in the UI with distinct styles and interactions.
- Updated sidebar to include navigation for automations.
- Enhanced post creation logic to support automation metadata.
- Refactored content type and platform enums into types for better type safety.
- Added localization for automation-related terms in English, Spanish, and Portuguese.
- Improved error handling in various components to accommodate new features.
2026-05-24 09:17:19 -03:00

121 lines
4 KiB
PHP

<?php
declare(strict_types=1);
namespace App\Actions\Automation\Run;
use App\Enums\Automation\Run\Status;
use App\Enums\Automation\Trigger\Type as TriggerType;
use App\Jobs\Automation\ProcessAutomationNode;
use App\Models\Automation;
use App\Models\AutomationRun;
use App\Models\Post;
/**
* Kicks off a manual run from the editor without waiting for the real trigger
* to fire. Mirrors n8n's "Execute workflow" — the same job pipeline runs, but
* the trigger payload is synthesized so the user can see exactly what each
* node does end-to-end. Runs are flagged `is_manual=true` so they're filtered
* from production audit views.
*
* When `$withRealData` is false (the default), the run is marked `is_dry_run`
* so side-effectful nodes (publish, generate, watermark advancement, sibling
* spawning) short-circuit. The run row is auto-deleted after reaching a
* terminal state — dry tests intentionally leave no trace.
*/
class TestAutomation
{
public function __invoke(Automation $automation, bool $withRealData = false): AutomationRun
{
$triggerNode = collect($automation->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<string, mixed> $triggerNode
* @return array<string, mixed>
*/
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<string, mixed>
*/
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<string, mixed>|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;
}
}