Add basic duplication ability
[MAILPOET-4540]
This commit is contained in:
committed by
David Remer
parent
35325bdf06
commit
a4c8caa664
@ -191,8 +191,8 @@ class Workflow {
|
|||||||
}, Json::decode($data['steps'])),
|
}, Json::decode($data['steps'])),
|
||||||
new \WP_User((int)$data['author'])
|
new \WP_User((int)$data['author'])
|
||||||
);
|
);
|
||||||
$workflow->id = (int)$data['id'];
|
$workflow->id = (int)($data['id'] ?? 0);
|
||||||
$workflow->versionId = (int)$data['version_id'];
|
$workflow->versionId = (int)($data['version_id'] ?? 0);
|
||||||
$workflow->status = $data['status'];
|
$workflow->status = $data['status'];
|
||||||
$workflow->createdAt = new DateTimeImmutable($data['created_at']);
|
$workflow->createdAt = new DateTimeImmutable($data['created_at']);
|
||||||
$workflow->updatedAt = new DateTimeImmutable($data['updated_at']);
|
$workflow->updatedAt = new DateTimeImmutable($data['updated_at']);
|
||||||
|
@ -0,0 +1,51 @@
|
|||||||
|
<?php declare(strict_types = 1);
|
||||||
|
|
||||||
|
namespace MailPoet\Automation\Engine\Endpoints\Workflows;
|
||||||
|
|
||||||
|
use MailPoet\API\REST\Request;
|
||||||
|
use MailPoet\API\REST\Response;
|
||||||
|
use MailPoet\Automation\Engine\API\Endpoint;
|
||||||
|
use MailPoet\Automation\Engine\Data\Workflow;
|
||||||
|
use MailPoet\Automation\Engine\Exceptions\InvalidStateException;
|
||||||
|
use MailPoet\Automation\Engine\Mappers\WorkflowMapper;
|
||||||
|
use MailPoet\Automation\Engine\Storage\WorkflowStorage;
|
||||||
|
use MailPoet\Validator\Builder;
|
||||||
|
|
||||||
|
class WorkflowsDuplicateEndpoint extends Endpoint {
|
||||||
|
/** @var WorkflowMapper */
|
||||||
|
private $workflowMapper;
|
||||||
|
|
||||||
|
/** @var WorkflowStorage */
|
||||||
|
private $workflowStorage;
|
||||||
|
|
||||||
|
public function __construct(
|
||||||
|
WorkflowMapper $workflowMapper,
|
||||||
|
WorkflowStorage $workflowStorage
|
||||||
|
) {
|
||||||
|
$this->workflowMapper = $workflowMapper;
|
||||||
|
$this->workflowStorage = $workflowStorage;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function handle(Request $request): Response {
|
||||||
|
$workflowId = $request->getParam('id');
|
||||||
|
if (!is_int($workflowId)) {
|
||||||
|
throw InvalidStateException::create();
|
||||||
|
}
|
||||||
|
$existingWorkflow = $this->workflowStorage->getWorkflow($workflowId);
|
||||||
|
if (!$existingWorkflow instanceof Workflow) {
|
||||||
|
throw InvalidStateException::create();
|
||||||
|
}
|
||||||
|
$duplicateId = $this->workflowStorage->duplicateWorkflow($existingWorkflow);
|
||||||
|
$duplicate = $this->workflowStorage->getWorkflow($duplicateId);
|
||||||
|
if (!$duplicate instanceof Workflow) {
|
||||||
|
throw InvalidStateException::create();
|
||||||
|
}
|
||||||
|
return new Response($this->workflowMapper->buildWorkflow($duplicate));
|
||||||
|
}
|
||||||
|
|
||||||
|
public static function getRequestSchema(): array {
|
||||||
|
return [
|
||||||
|
'id' => Builder::integer()->required(),
|
||||||
|
];
|
||||||
|
}
|
||||||
|
}
|
@ -2,15 +2,11 @@
|
|||||||
|
|
||||||
namespace MailPoet\Automation\Engine\Endpoints\Workflows;
|
namespace MailPoet\Automation\Engine\Endpoints\Workflows;
|
||||||
|
|
||||||
use DateTimeImmutable;
|
|
||||||
use MailPoet\API\REST\Request;
|
use MailPoet\API\REST\Request;
|
||||||
use MailPoet\API\REST\Response;
|
use MailPoet\API\REST\Response;
|
||||||
use MailPoet\Automation\Engine\API\Endpoint;
|
use MailPoet\Automation\Engine\API\Endpoint;
|
||||||
use MailPoet\Automation\Engine\Builder\UpdateWorkflowController;
|
use MailPoet\Automation\Engine\Builder\UpdateWorkflowController;
|
||||||
use MailPoet\Automation\Engine\Data\NextStep;
|
use MailPoet\Automation\Engine\Mappers\WorkflowMapper;
|
||||||
use MailPoet\Automation\Engine\Data\Step;
|
|
||||||
use MailPoet\Automation\Engine\Data\Workflow;
|
|
||||||
use MailPoet\Automation\Engine\Storage\WorkflowStatisticsStorage;
|
|
||||||
use MailPoet\Automation\Engine\Validation\WorkflowSchema;
|
use MailPoet\Automation\Engine\Validation\WorkflowSchema;
|
||||||
use MailPoet\Validator\Builder;
|
use MailPoet\Validator\Builder;
|
||||||
|
|
||||||
@ -18,21 +14,21 @@ class WorkflowsPutEndpoint extends Endpoint {
|
|||||||
/** @var UpdateWorkflowController */
|
/** @var UpdateWorkflowController */
|
||||||
private $updateController;
|
private $updateController;
|
||||||
|
|
||||||
/** @var WorkflowStatisticsStorage */
|
/** @var WorkflowMapper */
|
||||||
private $statisticsStorage;
|
private $workflowMapper;
|
||||||
|
|
||||||
public function __construct(
|
public function __construct(
|
||||||
UpdateWorkflowController $updateController,
|
UpdateWorkflowController $updateController,
|
||||||
WorkflowStatisticsStorage $statisticsStorage
|
WorkflowMapper $workflowMapper
|
||||||
) {
|
) {
|
||||||
$this->updateController = $updateController;
|
$this->updateController = $updateController;
|
||||||
$this->statisticsStorage = $statisticsStorage;
|
$this->workflowMapper = $workflowMapper;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function handle(Request $request): Response {
|
public function handle(Request $request): Response {
|
||||||
$data = $request->getParams();
|
$data = $request->getParams();
|
||||||
$workflow = $this->updateController->updateWorkflow(intval($request->getParam('id')), $data);
|
$workflow = $this->updateController->updateWorkflow(intval($request->getParam('id')), $data);
|
||||||
return new Response($this->buildWorkflow($workflow));
|
return new Response($this->workflowMapper->buildWorkflow($workflow));
|
||||||
}
|
}
|
||||||
|
|
||||||
public static function getRequestSchema(): array {
|
public static function getRequestSchema(): array {
|
||||||
@ -43,31 +39,4 @@ class WorkflowsPutEndpoint extends Endpoint {
|
|||||||
'steps' => WorkflowSchema::getStepsSchema(),
|
'steps' => WorkflowSchema::getStepsSchema(),
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
|
|
||||||
protected 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),
|
|
||||||
'activated_at' => $workflow->getActivatedAt() ? $workflow->getActivatedAt()->format(DateTimeImmutable::W3C) : null,
|
|
||||||
'author' => [
|
|
||||||
'id' => $workflow->getAuthor()->ID,
|
|
||||||
'name' => $workflow->getAuthor()->display_name,
|
|
||||||
],
|
|
||||||
'stats' => $this->statisticsStorage->getWorkflowStats($workflow->getId())->toArray(),
|
|
||||||
'steps' => array_map(function (Step $step) {
|
|
||||||
return [
|
|
||||||
'id' => $step->getId(),
|
|
||||||
'type' => $step->getType(),
|
|
||||||
'key' => $step->getKey(),
|
|
||||||
'args' => $step->getArgs(),
|
|
||||||
'next_steps' => array_map(function (NextStep $nextStep) {
|
|
||||||
return $nextStep->toArray();
|
|
||||||
}, $step->getNextSteps()),
|
|
||||||
];
|
|
||||||
}, $workflow->getSteps()),
|
|
||||||
];
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
@ -8,6 +8,7 @@ use MailPoet\Automation\Engine\Control\TriggerHandler;
|
|||||||
use MailPoet\Automation\Engine\Endpoints\System\DatabaseDeleteEndpoint;
|
use MailPoet\Automation\Engine\Endpoints\System\DatabaseDeleteEndpoint;
|
||||||
use MailPoet\Automation\Engine\Endpoints\System\DatabasePostEndpoint;
|
use MailPoet\Automation\Engine\Endpoints\System\DatabasePostEndpoint;
|
||||||
use MailPoet\Automation\Engine\Endpoints\Workflows\WorkflowsCreateFromTemplateEndpoint;
|
use MailPoet\Automation\Engine\Endpoints\Workflows\WorkflowsCreateFromTemplateEndpoint;
|
||||||
|
use MailPoet\Automation\Engine\Endpoints\Workflows\WorkflowsDuplicateEndpoint;
|
||||||
use MailPoet\Automation\Engine\Endpoints\Workflows\WorkflowsGetEndpoint;
|
use MailPoet\Automation\Engine\Endpoints\Workflows\WorkflowsGetEndpoint;
|
||||||
use MailPoet\Automation\Engine\Endpoints\Workflows\WorkflowsPutEndpoint;
|
use MailPoet\Automation\Engine\Endpoints\Workflows\WorkflowsPutEndpoint;
|
||||||
use MailPoet\Automation\Engine\Endpoints\Workflows\WorkflowTemplatesGetEndpoint;
|
use MailPoet\Automation\Engine\Endpoints\Workflows\WorkflowTemplatesGetEndpoint;
|
||||||
@ -73,6 +74,7 @@ class Engine {
|
|||||||
$api->registerGetRoute('workflows', WorkflowsGetEndpoint::class);
|
$api->registerGetRoute('workflows', WorkflowsGetEndpoint::class);
|
||||||
$api->registerPutRoute('workflows/(?P<id>\d+)', WorkflowsPutEndpoint::class);
|
$api->registerPutRoute('workflows/(?P<id>\d+)', WorkflowsPutEndpoint::class);
|
||||||
$api->registerPostRoute('workflows/create-from-template', WorkflowsCreateFromTemplateEndpoint::class);
|
$api->registerPostRoute('workflows/create-from-template', WorkflowsCreateFromTemplateEndpoint::class);
|
||||||
|
$api->registerPostRoute('workflows/(?P<id>\d+)/duplicate', WorkflowsDuplicateEndpoint::class);
|
||||||
$api->registerPostRoute('system/database', DatabasePostEndpoint::class);
|
$api->registerPostRoute('system/database', DatabasePostEndpoint::class);
|
||||||
$api->registerDeleteRoute('system/database', DatabaseDeleteEndpoint::class);
|
$api->registerDeleteRoute('system/database', DatabaseDeleteEndpoint::class);
|
||||||
$api->registerGetRoute('workflow-templates', WorkflowTemplatesGetEndpoint::class);
|
$api->registerGetRoute('workflow-templates', WorkflowTemplatesGetEndpoint::class);
|
||||||
|
47
mailpoet/lib/Automation/Engine/Mappers/WorkflowMapper.php
Normal file
47
mailpoet/lib/Automation/Engine/Mappers/WorkflowMapper.php
Normal file
@ -0,0 +1,47 @@
|
|||||||
|
<?php declare(strict_types = 1);
|
||||||
|
|
||||||
|
namespace MailPoet\Automation\Engine\Mappers;
|
||||||
|
|
||||||
|
use DateTimeImmutable;
|
||||||
|
use MailPoet\Automation\Engine\Data\NextStep;
|
||||||
|
use MailPoet\Automation\Engine\Data\Step;
|
||||||
|
use MailPoet\Automation\Engine\Data\Workflow;
|
||||||
|
use MailPoet\Automation\Engine\Storage\WorkflowStatisticsStorage;
|
||||||
|
|
||||||
|
class WorkflowMapper {
|
||||||
|
/** @var WorkflowStatisticsStorage */
|
||||||
|
private $statisticsStorage;
|
||||||
|
|
||||||
|
public function __construct(
|
||||||
|
WorkflowStatisticsStorage $statisticsStorage
|
||||||
|
) {
|
||||||
|
$this->statisticsStorage = $statisticsStorage;
|
||||||
|
}
|
||||||
|
|
||||||
|
public 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),
|
||||||
|
'activated_at' => $workflow->getActivatedAt() ? $workflow->getActivatedAt()->format(DateTimeImmutable::W3C) : null,
|
||||||
|
'author' => [
|
||||||
|
'id' => $workflow->getAuthor()->ID,
|
||||||
|
'name' => $workflow->getAuthor()->display_name,
|
||||||
|
],
|
||||||
|
'stats' => $this->statisticsStorage->getWorkflowStats($workflow->getId())->toArray(),
|
||||||
|
'steps' => array_map(function (Step $step) {
|
||||||
|
return [
|
||||||
|
'id' => $step->getId(),
|
||||||
|
'type' => $step->getType(),
|
||||||
|
'key' => $step->getKey(),
|
||||||
|
'args' => $step->getArgs(),
|
||||||
|
'next_steps' => array_map(function (NextStep $nextStep) {
|
||||||
|
return $nextStep->toArray();
|
||||||
|
}, $step->getNextSteps()),
|
||||||
|
];
|
||||||
|
}, $workflow->getSteps()),
|
||||||
|
];
|
||||||
|
}
|
||||||
|
}
|
@ -30,11 +30,22 @@ class WorkflowStorage {
|
|||||||
if (!$result) {
|
if (!$result) {
|
||||||
throw Exceptions::databaseError($this->wpdb->last_error);
|
throw Exceptions::databaseError($this->wpdb->last_error);
|
||||||
}
|
}
|
||||||
$id = (int)$this->wpdb->insert_id;
|
$id = $this->wpdb->insert_id;
|
||||||
$this->insertWorkflowVersion($id, $workflow);
|
$this->insertWorkflowVersion($id, $workflow);
|
||||||
return $id;
|
return $id;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function duplicateWorkflow(Workflow $workflow): int {
|
||||||
|
$data = $workflow->toArray();
|
||||||
|
$now = (new DateTimeImmutable())->format(DateTimeImmutable::W3C);
|
||||||
|
$data['created_at'] = $now;
|
||||||
|
$data['updated_at'] = $now;
|
||||||
|
$data['status'] = Workflow::STATUS_DRAFT;
|
||||||
|
$data['name'] = 'Copy of ' . $workflow->getName();
|
||||||
|
$duplicate = Workflow::fromArray($data);
|
||||||
|
return $this->createWorkflow($duplicate);
|
||||||
|
}
|
||||||
|
|
||||||
public function updateWorkflow(Workflow $workflow): void {
|
public function updateWorkflow(Workflow $workflow): void {
|
||||||
$oldRecord = $this->getWorkflow($workflow->getId());
|
$oldRecord = $this->getWorkflow($workflow->getId());
|
||||||
if ($oldRecord && $oldRecord->equals($workflow)) {
|
if ($oldRecord && $oldRecord->equals($workflow)) {
|
||||||
@ -146,7 +157,6 @@ class WorkflowStorage {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private function insertWorkflowVersion(int $workflowId, Workflow $workflow): void {
|
private function insertWorkflowVersion(int $workflowId, Workflow $workflow): void {
|
||||||
|
|
||||||
$dateString = (new DateTimeImmutable())->format(DateTimeImmutable::W3C);
|
$dateString = (new DateTimeImmutable())->format(DateTimeImmutable::W3C);
|
||||||
$data = [
|
$data = [
|
||||||
'workflow_id' => $workflowId,
|
'workflow_id' => $workflowId,
|
||||||
|
@ -122,6 +122,7 @@ class ContainerConfigurator implements IContainerConfigurator {
|
|||||||
$container->autowire(\MailPoet\Automation\Engine\Control\TriggerHandler::class)->setPublic(true);
|
$container->autowire(\MailPoet\Automation\Engine\Control\TriggerHandler::class)->setPublic(true);
|
||||||
$container->autowire(\MailPoet\Automation\Engine\Engine::class)->setPublic(true);
|
$container->autowire(\MailPoet\Automation\Engine\Engine::class)->setPublic(true);
|
||||||
$container->autowire(\MailPoet\Automation\Engine\Hooks::class)->setPublic(true);
|
$container->autowire(\MailPoet\Automation\Engine\Hooks::class)->setPublic(true);
|
||||||
|
$container->autowire(\MailPoet\Automation\Engine\Mappers\WorkflowMapper::class)->setPublic(true);
|
||||||
$container->autowire(\MailPoet\Automation\Engine\Migrations\Migrator::class)->setPublic(true);
|
$container->autowire(\MailPoet\Automation\Engine\Migrations\Migrator::class)->setPublic(true);
|
||||||
$container->autowire(\MailPoet\Automation\Engine\Registry::class)->setPublic(true);
|
$container->autowire(\MailPoet\Automation\Engine\Registry::class)->setPublic(true);
|
||||||
$container->autowire(\MailPoet\Automation\Engine\Storage\WorkflowRunStorage::class)->setPublic(true);
|
$container->autowire(\MailPoet\Automation\Engine\Storage\WorkflowRunStorage::class)->setPublic(true);
|
||||||
@ -141,6 +142,7 @@ class ContainerConfigurator implements IContainerConfigurator {
|
|||||||
$container->autowire(\MailPoet\Automation\Engine\Endpoints\Workflows\WorkflowTemplatesGetEndpoint::class)->setPublic(true);
|
$container->autowire(\MailPoet\Automation\Engine\Endpoints\Workflows\WorkflowTemplatesGetEndpoint::class)->setPublic(true);
|
||||||
$container->autowire(\MailPoet\Automation\Engine\Endpoints\Workflows\WorkflowsPutEndpoint::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\Workflows\WorkflowsCreateFromTemplateEndpoint::class)->setPublic(true);
|
||||||
|
$container->autowire(\MailPoet\Automation\Engine\Endpoints\Workflows\WorkflowsDuplicateEndpoint::class)->setPublic(true);
|
||||||
$container->autowire(\MailPoet\Automation\Engine\Endpoints\System\DatabasePostEndpoint::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);
|
$container->autowire(\MailPoet\Automation\Engine\Endpoints\System\DatabaseDeleteEndpoint::class)->setPublic(true);
|
||||||
// Automation - core integration
|
// Automation - core integration
|
||||||
|
Reference in New Issue
Block a user