diff --git a/mailpoet/lib/Automation/Engine/Endpoints/Workflows/WorkflowsPutEndpoint.php b/mailpoet/lib/Automation/Engine/Endpoints/Workflows/WorkflowsPutEndpoint.php new file mode 100644 index 0000000000..26856725f0 --- /dev/null +++ b/mailpoet/lib/Automation/Engine/Endpoints/Workflows/WorkflowsPutEndpoint.php @@ -0,0 +1,57 @@ +updateController = $updateController; + } + + public function handle(Request $request): Response { + $data = $request->getParams(); + $workflow = $this->updateController->updateWorkflow(intval($request->getParam('id')), $data); + return new Response($this->buildWorkflow($workflow)); + } + + public static function getRequestSchema(): array { + return [ + 'id' => Builder::integer()->required(), + 'name' => Builder::string(), + 'status' => Builder::string(), + ]; + } + + private function buildWorkflow(Workflow $workflow): array { + return [ + 'id' => $workflow->getId(), + 'name' => $workflow->getName(), + 'status' => $workflow->getStatus(), + 'created_at' => $workflow->getCreatedAt()->format(DateTimeImmutable::W3C), + 'updated_at' => $workflow->getUpdatedAt()->format(DateTimeImmutable::W3C), + 'steps' => array_map(function (Step $step) { + return [ + 'id' => $step->getId(), + 'type' => $step->getType(), + 'key' => $step->getKey(), + 'next_step_id' => $step->getNextStepId(), + 'args' => $step->getArgs() ?: new stdClass(), + ]; + }, $workflow->getSteps()), + ]; + } +} diff --git a/mailpoet/lib/Automation/Engine/Engine.php b/mailpoet/lib/Automation/Engine/Engine.php index 0867d47161..2fe7f33c29 100644 --- a/mailpoet/lib/Automation/Engine/Engine.php +++ b/mailpoet/lib/Automation/Engine/Engine.php @@ -9,6 +9,7 @@ use MailPoet\Automation\Engine\Endpoints\System\DatabaseDeleteEndpoint; use MailPoet\Automation\Engine\Endpoints\System\DatabasePostEndpoint; use MailPoet\Automation\Engine\Endpoints\Workflows\WorkflowsCreateFromTemplateEndpoint; use MailPoet\Automation\Engine\Endpoints\Workflows\WorkflowsGetEndpoint; +use MailPoet\Automation\Engine\Endpoints\Workflows\WorkflowsPutEndpoint; use MailPoet\Automation\Engine\Storage\WorkflowStorage; use MailPoet\Automation\Integrations\Core\CoreIntegration; @@ -70,6 +71,7 @@ class Engine { private function registerApiRoutes(): void { $this->wordPress->addAction(Hooks::API_INITIALIZE, function (API $api) { $api->registerGetRoute('workflows', WorkflowsGetEndpoint::class); + $api->registerPutRoute('workflows/(?P\d+)', WorkflowsPutEndpoint::class); $api->registerPostRoute('workflows/create-from-template', WorkflowsCreateFromTemplateEndpoint::class); $api->registerPostRoute('system/database', DatabasePostEndpoint::class); $api->registerDeleteRoute('system/database', DatabaseDeleteEndpoint::class); diff --git a/mailpoet/lib/DI/ContainerConfigurator.php b/mailpoet/lib/DI/ContainerConfigurator.php index fb5dbc0b02..6a6bb64fa6 100644 --- a/mailpoet/lib/DI/ContainerConfigurator.php +++ b/mailpoet/lib/DI/ContainerConfigurator.php @@ -123,6 +123,7 @@ class ContainerConfigurator implements IContainerConfigurator { $container->autowire(\MailPoet\Automation\Engine\WordPress::class)->setPublic(true); // Automation - API endpoints $container->autowire(\MailPoet\Automation\Engine\Endpoints\Workflows\WorkflowsGetEndpoint::class)->setPublic(true); + $container->autowire(\MailPoet\Automation\Engine\Endpoints\Workflows\WorkflowsPutEndpoint::class)->setPublic(true); $container->autowire(\MailPoet\Automation\Engine\Endpoints\Workflows\WorkflowsCreateFromTemplateEndpoint::class)->setPublic(true); $container->autowire(\MailPoet\Automation\Engine\Endpoints\System\DatabasePostEndpoint::class)->setPublic(true); $container->autowire(\MailPoet\Automation\Engine\Endpoints\System\DatabaseDeleteEndpoint::class)->setPublic(true);