Skip to content

Commit e466471

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

4 files changed

Lines changed: 126 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: 74 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;
@@ -283,4 +285,75 @@ public function resendMessageToLists(
283285

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

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 $mails;
37+
38+
public function getDto(): array
39+
{
40+
return [
41+
'emails' => $this->mails,
42+
];
43+
}
44+
}

0 commit comments

Comments
 (0)