diff --git a/application/Espo/Controllers/MassAction.php b/application/Espo/Controllers/MassAction.php index 65a7234680..0ee61aceeb 100644 --- a/application/Espo/Controllers/MassAction.php +++ b/application/Espo/Controllers/MassAction.php @@ -38,6 +38,7 @@ use Espo\Core\MassAction\Params; use Espo\Core\MassAction\ServiceParams; use Espo\Core\Api\Request; +use Espo\Core\Api\Response; use stdClass; use RuntimeException; @@ -101,6 +102,19 @@ class MassAction return $this->service->getStatusData($id); } + public function postActionSubscribeToNotificationOnSuccess(Request $request, Response $response): void + { + $id = $request->getParsedBody()->id ?? null; + + if (!$id || !is_string($id)) { + throw new BadRequest(); + } + + $this->service->subscribeToNotificationOnSuccess($id); + + $response->writeBody('true'); + } + private function prepareMassActionParams(stdClass $data): array { $where = $data->where ?? null; diff --git a/application/Espo/Core/MassAction/Jobs/Process.php b/application/Espo/Core/MassAction/Jobs/Process.php index 5190e3fd7b..7050a45846 100644 --- a/application/Espo/Core/MassAction/Jobs/Process.php +++ b/application/Espo/Core/MassAction/Jobs/Process.php @@ -119,6 +119,7 @@ class Process implements Job $message = $this->language->translate('massActionProcessed', 'messages'); $notification + ->setType(Notification::TYPE_MESSAGE) ->setMessage($message) ->setUserId($entity->getCreatedBy()->getId()); diff --git a/application/Espo/Core/MassAction/Service.php b/application/Espo/Core/MassAction/Service.php index c91c289ad9..b7ccb8d53a 100644 --- a/application/Espo/Core/MassAction/Service.php +++ b/application/Espo/Core/MassAction/Service.php @@ -135,6 +135,24 @@ class Service ]; } + public function subscribeToNotificationOnSuccess(string $id): void + { + /** @var MassActionEntity|null $entity */ + $entity = $this->entityManager->getEntity(MassActionEntity::ENTITY_TYPE, $id); + + if (!$entity) { + throw new NotFound(); + } + + if ($entity->getCreatedBy()->getId() !== $this->user->getId()) { + throw new Forbidden(); + } + + $entity->setNotifyOnFinish(); + + $this->entityManager->saveEntity($entity); + } + private function schedule(string $entityType, string $action, Params $params, stdClass $data): ServiceResult { $entity = $this->entityManager->createEntity(MassActionEntity::ENTITY_TYPE, [ diff --git a/application/Espo/Entities/MassAction.php b/application/Espo/Entities/MassAction.php index 7d59a495c8..81ed51f97a 100644 --- a/application/Espo/Entities/MassAction.php +++ b/application/Espo/Entities/MassAction.php @@ -155,4 +155,11 @@ class MassAction extends Entity return $this; } + + public function setNotifyOnFinish(bool $notifyOnFinish = true): self + { + $this->set('notifyOnFinish', $notifyOnFinish); + + return $this; + } } diff --git a/client/src/views/modals/mass-action.js b/client/src/views/modals/mass-action.js index 8e231145fc..c3b66644b3 100644 --- a/client/src/views/modals/mass-action.js +++ b/client/src/views/modals/mass-action.js @@ -105,6 +105,16 @@ define('views/modals/mass-action', ['views/modal', 'model'], function (Dep, Mode ], }); + this.on('close', () => { + if (this.model.get('status') !== 'Pending') { + return; + } + + Espo.Ajax.postRequest('MassAction/action/subscribeToNotificationOnSuccess', { + id: this.id, + }); + }); + this.checkStatus(); },