Add ability to duplicate dynamic segments

MAILPOET-4635
This commit is contained in:
John Oleksowicz
2023-05-11 16:10:25 -05:00
committed by Aschepikov
parent c0ffcbac9b
commit e636537580
7 changed files with 121 additions and 1 deletions

View File

@ -19,6 +19,7 @@ use MailPoet\Segments\DynamicSegments\SegmentSaveController;
use MailPoet\Segments\SegmentsRepository;
use MailPoet\Segments\SegmentSubscribersRepository;
use MailPoet\UnexpectedValueException;
use Throwable;
class DynamicSegments extends APIEndpoint {
@ -124,6 +125,29 @@ class DynamicSegments extends APIEndpoint {
}
}
public function duplicate($data = []) {
$segment = $this->getSegment($data);
if ($segment instanceof SegmentEntity) {
try {
$duplicate = $this->saveController->duplicate($segment);
} catch (Throwable $e) {
return $this->errorResponse([
// translators: %s is the error message
Error::UNKNOWN => sprintf(__('Duplicating of segment failed: %s', 'mailpoet'), $e->getMessage()),
], [], Response::STATUS_UNKNOWN);
}
return $this->successResponse(
$this->segmentsResponseBuilder->build($duplicate),
['count' => 1]
);
} else {
return $this->errorResponse([
Error::NOT_FOUND => __('This segment does not exist.', 'mailpoet'),
]);
}
}
private function getErrorString(InvalidFilterException $e) {
switch ($e->getCode()) {
case InvalidFilterException::MISSING_TYPE: