Add bulk action for removing tag from subscribers
[MAILPOET-5454]
This commit is contained in:
@ -305,6 +305,36 @@ const bulkActions = [
|
||||
);
|
||||
},
|
||||
},
|
||||
{
|
||||
name: 'removeTag',
|
||||
label: MailPoet.I18n.t('removeTag'),
|
||||
onSelect: function onSelect(submitModal, closeModal) {
|
||||
const field = {
|
||||
id: 'remove_tag',
|
||||
name: 'remove_tag',
|
||||
endpoint: 'tags',
|
||||
};
|
||||
|
||||
return createModal(
|
||||
submitModal,
|
||||
closeModal,
|
||||
field,
|
||||
MailPoet.I18n.t('removeTag'),
|
||||
);
|
||||
},
|
||||
getData: function getData() {
|
||||
return {
|
||||
tag_id: Number(jQuery('#remove_tag').val()),
|
||||
};
|
||||
},
|
||||
onSuccess: function onSuccess(response) {
|
||||
MailPoet.Notice.success(
|
||||
MailPoet.I18n.t('tagRemovedFromMultipleSubscribers')
|
||||
.replace('%1$s', response.meta.tag)
|
||||
.replace('%2$d', Number(response.meta.count).toLocaleString()),
|
||||
);
|
||||
},
|
||||
},
|
||||
];
|
||||
|
||||
const itemActions = [
|
||||
|
@ -293,6 +293,8 @@ class Subscribers extends APIEndpoint {
|
||||
$count = $this->subscribersRepository->bulkUnsubscribe($ids);
|
||||
} elseif ($data['action'] === 'addTag' && $tag instanceof TagEntity) {
|
||||
$count = $this->subscribersRepository->bulkAddTag($tag, $ids);
|
||||
} elseif ($data['action'] === 'removeTag' && $tag instanceof TagEntity) {
|
||||
$count = $this->subscribersRepository->bulkRemoveTag($tag, $ids);
|
||||
} else {
|
||||
throw UnexpectedValueException::create()
|
||||
->withErrors([APIError::BAD_REQUEST => "Invalid bulk action '{$data['action']}' provided."]);
|
||||
|
@ -520,6 +520,25 @@ class SubscribersRepository extends Repository {
|
||||
return $count;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return int - number of processed ids
|
||||
*/
|
||||
public function bulkRemoveTag(TagEntity $tag, array $ids): int {
|
||||
if (empty($ids)) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
$subscriberTagsTable = $this->entityManager->getClassMetadata(SubscriberTagEntity::class)->getTableName();
|
||||
$count = (int)$this->entityManager->getConnection()->executeStatement("
|
||||
DELETE st FROM $subscriberTagsTable st
|
||||
WHERE st.`subscriber_id` IN (:ids)
|
||||
AND st.`tag_id` = :tag_id
|
||||
", ['ids' => $ids, 'tag_id' => $tag->getId()], ['ids' => Connection::PARAM_INT_ARRAY]);
|
||||
|
||||
$this->changesNotifier->subscribersUpdated($ids);
|
||||
return $count;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return int - number of processed ids
|
||||
*/
|
||||
|
@ -1052,6 +1052,46 @@ class SubscribersTest extends \MailPoetTest {
|
||||
expect($response->meta['count'])->equals(0);
|
||||
}
|
||||
|
||||
public function testItCanBulkRemoveTagFromSubscribers(): void {
|
||||
$tag = (new TagFactory())->withName('Tag 1')->create();
|
||||
$subscriber1 = (new SubscriberFactory())
|
||||
->withEmail('withTag1@test.com')
|
||||
->withTags([$tag])
|
||||
->create();
|
||||
$subscriber2 = (new SubscriberFactory())
|
||||
->withEmail('withTag2@test.com')
|
||||
->withTags([$tag])
|
||||
->create();
|
||||
|
||||
|
||||
$bulkActionData = [
|
||||
'action' => 'removeTag',
|
||||
'listing' => [
|
||||
'selection' => [
|
||||
$subscriber1->getId(),
|
||||
$subscriber2->getId(),
|
||||
],
|
||||
],
|
||||
'tag_id' => $tag->getId(),
|
||||
];
|
||||
|
||||
$response = $this->endpoint->bulkAction($bulkActionData);
|
||||
|
||||
$subscriberTagRepository = $this->diContainer->get(SubscriberTagRepository::class);
|
||||
$subscriberTag1 = $subscriberTagRepository->findOneBy(['subscriber' => $subscriber1, 'tag' => $tag]);
|
||||
$subscriberTag2 = $subscriberTagRepository->findOneBy(['subscriber' => $subscriber2, 'tag' => $tag]);
|
||||
|
||||
expect($response->status)->equals(APIResponse::STATUS_OK);
|
||||
expect($response->meta['count'])->equals(2);
|
||||
expect($subscriberTag1)->null();
|
||||
expect($subscriberTag2)->null();
|
||||
|
||||
// Testing that removing the same tag again does not return an error
|
||||
$response = $this->endpoint->bulkAction($bulkActionData);
|
||||
expect($response->status)->equals(APIResponse::STATUS_OK);
|
||||
expect($response->meta['count'])->equals(0);
|
||||
}
|
||||
|
||||
private function _createWelcomeNewsletter(): void {
|
||||
$newsletterFactory = new NewsletterFactory();
|
||||
$newsletterFactory
|
||||
|
@ -157,7 +157,9 @@
|
||||
'tags': __('Tags'),
|
||||
'addNewTag': __('Add new tag'),
|
||||
'tagAddedToMultipleSubscribers': __('Tag <strong>%1$s</strong> was added to %2$d subscribers.'),
|
||||
'tagRemovedFromMultipleSubscribers': __('Tag <strong>%1$s</strong> was removed from %2$d subscribers.'),
|
||||
'addTag': __('Add tag...'),
|
||||
'removeTag': __('Remove tag...'),
|
||||
}) %>
|
||||
<% endblock %>
|
||||
|
||||
|
Reference in New Issue
Block a user