Skip to content

[Notifier][Novu] Implement overrides #51577

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions src/Symfony/Component/Notifier/Bridge/Novu/NovuOptions.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,19 @@
*/
class NovuOptions implements MessageOptionsInterface
{
/**
* @param array{
* email?: array{
* from?: string,
* senderName?: string,
* replyTo?: string,
* cc?: string[],
* bcc?: string[]
* }|null
* } $overrides
*
* @see https://docs.novu.co/channels/email/#sending-email-overrides
*/
public function __construct(
private readonly ?string $subscriberId = null,
private readonly ?string $firstName = null,
Expand All @@ -26,6 +39,7 @@ public function __construct(
private readonly ?string $phone = null,
private readonly ?string $avatar = null,
private readonly ?string $locale = null,
private readonly array $overrides = [],
private readonly array $options = [],
) {
}
Expand All @@ -39,6 +53,7 @@ public function toArray(): array
'phone' => $this->phone,
'avatar' => $this->avatar,
'locale' => $this->locale,
'overrides' => $this->overrides,
]);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,19 @@
*/
class NovuSubscriberRecipient implements RecipientInterface
{
/**
* @param array{
* email?: array{
* from?: string,
* senderName?: string,
* replyTo?: string,
* cc?: string[],
* bcc?: string[]
* }|null
* } $overrides
*
* @see https://docs.novu.co/channels/email/#sending-email-overrides
*/
public function __construct(
private readonly string $subscriberId,
private readonly ?string $firstName = null,
Expand All @@ -26,6 +39,7 @@ public function __construct(
private readonly ?string $phone = null,
private readonly ?string $avatar = null,
private readonly ?string $locale = null,
private readonly array $overrides = [],
) {
}

Expand Down Expand Up @@ -63,4 +77,9 @@ public function getLocale(): ?string
{
return $this->locale;
}

public function getOverrides(): array
{
return $this->overrides;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ protected function doSend(MessageInterface $message): SentMessage
'locale' => $options['locale'],
],
'payload' => json_decode($message->getContent()),
'overrides' => $options['overrides'] ?? [],
];

$endpoint = sprintf('https://%s/v1/events/trigger', $this->getEndpoint());
Expand Down
7 changes: 7 additions & 0 deletions src/Symfony/Component/Notifier/Bridge/Novu/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ class NovuNotification extends Notification implements PushNotificationInterface
$recipient->getPhone(),
$recipient->getAvatar(),
$recipient->getLocale(),
$recipient->getOverrides(),
[],
),
);
Expand Down Expand Up @@ -60,6 +61,12 @@ $this->notifier->send(
null,
null,
null,
[
'email' => [
'from' => 'no-reply@toppy.nl',
'senderName' => 'No-Reply',
],
],
),
);
```
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,12 @@ public function testToArray()
null,
null,
null,
[
'email' => [
'from' => 'no-reply@example.com',
'senderName' => 'No-Reply',
],
],
[],
);

Expand All @@ -40,6 +46,12 @@ public function testToArray()
'phone' => null,
'avatar' => null,
'locale' => null,
'overrides' => [
'email' => [
'from' => 'no-reply@example.com',
'senderName' => 'No-Reply',
],
],
],
$options->toArray()
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public static function toStringProvider(): iterable

public static function supportedMessagesProvider(): iterable
{
yield [new PushMessage('test', '{}', new NovuOptions(123, null, null, 'test@example.com', null, null, null, []))];
yield [new PushMessage('test', '{}', new NovuOptions(123, null, null, 'test@example.com', null, null, null, ['email' => ['from' => 'no-reply@example.com', 'senderName' => 'No-Reply']], []))];
}

public static function unsupportedMessagesProvider(): iterable
Expand All @@ -63,6 +63,6 @@ public function testWithErrorResponseThrows()
$this->expectException(TransportException::class);
$this->expectExceptionMessageMatches('/400: "subscriberId under property to is not configured"/');

$transport->send(new PushMessage('test', '{}', new NovuOptions(123, null, null, 'test@example.com', null, null, null, [])));
$transport->send(new PushMessage('test', '{}', new NovuOptions(123, null, null, 'test@example.com', null, null, null, ['email' => ['from' => 'no-reply@example.com', 'senderName' => 'No-Reply']], [])));
}
}