Add bulk action for tagging subscribers
[MAILPOET-5454]
This commit is contained in:
@@ -13,6 +13,7 @@ use MailPoet\ConflictException;
|
||||
use MailPoet\Doctrine\Validator\ValidationException;
|
||||
use MailPoet\Entities\SegmentEntity;
|
||||
use MailPoet\Entities\SubscriberEntity;
|
||||
use MailPoet\Entities\TagEntity;
|
||||
use MailPoet\Exception;
|
||||
use MailPoet\Listing;
|
||||
use MailPoet\Segments\SegmentsRepository;
|
||||
@@ -22,6 +23,7 @@ use MailPoet\Subscribers\SubscriberListingRepository;
|
||||
use MailPoet\Subscribers\SubscriberSaveController;
|
||||
use MailPoet\Subscribers\SubscribersRepository;
|
||||
use MailPoet\Subscribers\SubscriberSubscribeController;
|
||||
use MailPoet\Tags\TagRepository;
|
||||
use MailPoet\UnexpectedValueException;
|
||||
use MailPoet\Util\Helpers;
|
||||
|
||||
@@ -51,6 +53,9 @@ class Subscribers extends APIEndpoint {
|
||||
/** @var SegmentsRepository */
|
||||
private $segmentsRepository;
|
||||
|
||||
/** @var TagRepository */
|
||||
private $tagRepository;
|
||||
|
||||
/** @var SubscriberSaveController */
|
||||
private $saveController;
|
||||
|
||||
@@ -67,6 +72,7 @@ class Subscribers extends APIEndpoint {
|
||||
SubscribersResponseBuilder $subscribersResponseBuilder,
|
||||
SubscriberListingRepository $subscriberListingRepository,
|
||||
SegmentsRepository $segmentsRepository,
|
||||
TagRepository $tagRepository,
|
||||
SubscriberSaveController $saveController,
|
||||
SubscriberSubscribeController $subscribeController,
|
||||
SettingsController $settings
|
||||
@@ -77,6 +83,7 @@ class Subscribers extends APIEndpoint {
|
||||
$this->subscribersResponseBuilder = $subscribersResponseBuilder;
|
||||
$this->subscriberListingRepository = $subscriberListingRepository;
|
||||
$this->segmentsRepository = $segmentsRepository;
|
||||
$this->tagRepository = $tagRepository;
|
||||
$this->saveController = $saveController;
|
||||
$this->subscribeController = $subscribeController;
|
||||
$this->settings = $settings;
|
||||
@@ -258,6 +265,16 @@ class Subscribers extends APIEndpoint {
|
||||
}
|
||||
}
|
||||
|
||||
$tag = null;
|
||||
if (isset($data['tag_id'])) {
|
||||
$tag = $this->getTag($data);
|
||||
if (!$tag) {
|
||||
return $this->errorResponse([
|
||||
APIError::NOT_FOUND => __('This tag does not exist.', 'mailpoet'),
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
||||
if ($data['action'] === 'trash') {
|
||||
$count = $this->subscribersRepository->bulkTrash($ids);
|
||||
} elseif ($data['action'] === 'restore') {
|
||||
@@ -274,6 +291,8 @@ class Subscribers extends APIEndpoint {
|
||||
$count = $this->subscribersRepository->bulkMoveToSegment($segment, $ids);
|
||||
} elseif ($data['action'] === 'unsubscribe') {
|
||||
$count = $this->subscribersRepository->bulkUnsubscribe($ids);
|
||||
} elseif ($data['action'] === 'addTag' && $tag instanceof TagEntity) {
|
||||
$count = $this->subscribersRepository->bulkAddTag($tag, $ids);
|
||||
} else {
|
||||
throw UnexpectedValueException::create()
|
||||
->withErrors([APIError::BAD_REQUEST => "Invalid bulk action '{$data['action']}' provided."]);
|
||||
@@ -285,6 +304,9 @@ class Subscribers extends APIEndpoint {
|
||||
if ($segment) {
|
||||
$meta['segment'] = $segment->getName();
|
||||
}
|
||||
if ($tag) {
|
||||
$meta['tag'] = $tag->getName();
|
||||
}
|
||||
return $this->successResponse(null, $meta);
|
||||
}
|
||||
|
||||
@@ -304,6 +326,12 @@ class Subscribers extends APIEndpoint {
|
||||
: null;
|
||||
}
|
||||
|
||||
private function getTag(array $data): ?TagEntity {
|
||||
return isset($data['tag_id'])
|
||||
? $this->tagRepository->findOneById((int)$data['tag_id'])
|
||||
: null;
|
||||
}
|
||||
|
||||
private function getErrorMessage(ValidationException $exception): string {
|
||||
$exceptionMessage = $exception->getMessage();
|
||||
if (strpos($exceptionMessage, 'This value should not be blank.') !== false) {
|
||||
|
Reference in New Issue
Block a user