Skip to content

Commit bdeddeb

Browse files
committed
Add: testSendMessage endpoint
1 parent 6264e47 commit bdeddeb

4 files changed

Lines changed: 127 additions & 4 deletions

File tree

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
services:
2-
PhpList\Core\Domain\Messaging\MessageHandler\CampaignProcessorMessageHandler:
2+
PhpList\Core\Domain\Messaging\MessageHandler\CampaignProcessor\CampaignProcessorMessageHandler:
3+
autowire: true
4+
autoconfigure: true
5+
public: false
6+
7+
PhpList\Core\Domain\Messaging\MessageHandler\CampaignProcessor\TestCampaignProcessorMessageHandler:
38
autowire: true
49
autoconfigure: true
510
public: false

src/Messaging/Controller/CampaignActionController.php

Lines changed: 75 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@
66

77
use Doctrine\ORM\EntityManagerInterface;
88
use OpenApi\Attributes as OA;
9-
use PhpList\Core\Domain\Messaging\Message\SyncCampaignProcessorMessage;
9+
use PhpList\Core\Domain\Messaging\Message\CampaignProcessor\SyncCampaignProcessorMessage;
10+
use PhpList\Core\Domain\Messaging\Message\CampaignProcessor\TestCampaignProcessorMessage;
1011
use PhpList\Core\Domain\Messaging\Model\Message;
1112
use PhpList\Core\Domain\Messaging\Model\Message\MessageStatus;
1213
use PhpList\Core\Domain\Messaging\Service\Manager\MessageManager;
@@ -15,6 +16,7 @@
1516
use PhpList\RestBundle\Common\Validator\RequestValidator;
1617
use PhpList\RestBundle\Messaging\Request\Message\MessageMetadataRequest;
1718
use PhpList\RestBundle\Messaging\Request\ResendMessageToListsRequest;
19+
use PhpList\RestBundle\Messaging\Request\TestSendMessageToSubscribersRequest;
1820
use PhpList\RestBundle\Messaging\Serializer\MessageNormalizer;
1921
use PhpList\RestBundle\Messaging\Service\CampaignService;
2022
use Symfony\Bridge\Doctrine\Attribute\MapEntity;
@@ -28,6 +30,7 @@
2830
* This controller provides REST API to manage campaign actions.
2931
*
3032
* @author Tatevik Grigoryan <tatevik@phplist.com>
33+
* @SuppressWarnings("PHPMD.CouplingBetweenObjects")
3134
*/
3235
#[Route('/campaigns', name: 'campaign_')]
3336
class CampaignActionController extends BaseController
@@ -283,4 +286,75 @@ public function resendMessageToLists(
283286

284287
return $this->json($this->campaignService->getMessage($message), Response::HTTP_OK);
285288
}
289+
290+
#[Route(
291+
'/{messageId}/test-send',
292+
name: 'test_send_campaign',
293+
requirements: ['messageId' => '\d+'],
294+
methods: ['POST']
295+
)]
296+
#[OA\Post(
297+
path: '/api/v2/campaigns/{messageId}/test-send',
298+
description: '🚧 **Status: Beta** – This method is under development. Avoid using in production. ' .
299+
'Processes/sends campaign/message by id to specified subscribers.',
300+
summary: 'Processes/sends campaign/message by id to specified subscribers.',
301+
requestBody: new OA\RequestBody(
302+
description: 'Subscribers email to send this campaign to.',
303+
required: true,
304+
content: new OA\JsonContent(ref: '#/components/schemas/ResendMessageToListsRequest')
305+
),
306+
tags: ['campaigns'],
307+
parameters: [
308+
new OA\Parameter(
309+
name: 'php-auth-pw',
310+
description: 'Session key obtained from login',
311+
in: 'header',
312+
required: true,
313+
schema: new OA\Schema(type: 'string')
314+
),
315+
new OA\Parameter(
316+
name: 'messageId',
317+
description: 'message ID',
318+
in: 'path',
319+
required: true,
320+
schema: new OA\Schema(type: 'string')
321+
)
322+
],
323+
responses: [
324+
new OA\Response(
325+
response: 200,
326+
description: 'Success',
327+
content: new OA\JsonContent(ref: '#/components/schemas/Message')
328+
),
329+
new OA\Response(
330+
response: 403,
331+
description: 'Failure',
332+
content: new OA\JsonContent(ref: '#/components/schemas/UnauthorizedResponse')
333+
),
334+
new OA\Response(
335+
response: 404,
336+
description: 'Failure',
337+
content: new OA\JsonContent(ref: '#/components/schemas/NotFoundErrorResponse')
338+
),
339+
]
340+
)]
341+
public function testSendMessage(
342+
Request $request,
343+
#[MapEntity(mapping: ['messageId' => 'id'])] ?Message $message = null
344+
): JsonResponse {
345+
$this->requireAuthentication($request);
346+
if ($message === null) {
347+
throw $this->createNotFoundException('Campaign not found.');
348+
}
349+
350+
/** @var TestSendMessageToSubscribersRequest $testSendRequest */
351+
$testSendRequest = $this->validator->validate($request, TestSendMessageToSubscribersRequest::class);
352+
353+
$this->messageBus->dispatch(new TestCampaignProcessorMessage(
354+
messageId: $message->getId(),
355+
subscriberEmails: $testSendRequest->emails
356+
));
357+
358+
return $this->json($this->campaignService->getMessage($message), Response::HTTP_OK);
359+
}
286360
}

src/Messaging/Request/Message/MessageScheduleRequest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,12 +34,12 @@ class MessageScheduleRequest implements RequestDtoInterface
3434
{
3535
public ?int $repeatInterval = null;
3636

37-
#[Assert\DateTime(format: "Y-m-d\TH:i:s.uP")]
37+
#[Assert\DateTime(format: 'Y-m-d\TH:i:s.uP')]
3838
public ?string $repeatUntil = null;
3939

4040
public ?int $requeueInterval = null;
4141

42-
#[Assert\DateTime(format: "Y-m-d\TH:i:s.uP")]
42+
#[Assert\DateTime(format: 'Y-m-d\TH:i:s.uP')]
4343
public ?string $requeueUntil = null;
4444

4545
#[Assert\NotBlank]
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace PhpList\RestBundle\Messaging\Request;
6+
7+
use OpenApi\Attributes as OA;
8+
use PhpList\RestBundle\Common\Request\RequestInterface;
9+
use PhpList\RestBundle\Subscription\Validator\Constraint\EmailExists;
10+
use Symfony\Component\Validator\Constraints as Assert;
11+
12+
#[OA\Schema(
13+
schema: 'TestSendMessageToSubscribersRequest',
14+
required: ['emails'],
15+
properties: [
16+
new OA\Property(
17+
property: 'emails',
18+
description: 'Target subscribers emails.',
19+
type: 'array',
20+
items: new OA\Items(type: 'string', format: 'email'),
21+
example: ['user1@example.com', 'user2@example.com']
22+
),
23+
],
24+
type: 'object'
25+
)]
26+
class TestSendMessageToSubscribersRequest implements RequestInterface
27+
{
28+
#[Assert\NotNull]
29+
#[Assert\Type('array')]
30+
#[Assert\Count(min: 1)]
31+
#[Assert\All([
32+
new Assert\Type('string'),
33+
new Assert\Email(),
34+
new EmailExists(),
35+
])]
36+
public array $emails;
37+
38+
public function getDto(): array
39+
{
40+
return [
41+
'emails' => $this->emails,
42+
];
43+
}
44+
}

0 commit comments

Comments
 (0)