refactor(notifications): consolidate channel constants under NOTIFICATION_TYPE_* prefix

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
Jake Barnby
2026-05-06 13:41:59 +12:00
co-authored by Claude Opus 4.7
parent 0ce3978f2c
commit 3df219df66
4 changed files with 26 additions and 29 deletions
+6 -9
View File
@@ -254,15 +254,12 @@ const FUNCTION_ALLOWLIST_HEADERS_RESPONSE = ['content-type', 'content-length'];
const MESSAGE_TYPE_EMAIL = 'email';
const MESSAGE_TYPE_SMS = 'sms';
const MESSAGE_TYPE_PUSH = 'push';
// Notification channels
const NOTIFICATION_CHANNEL_EMAIL = 'email';
const NOTIFICATION_CHANNEL_SMS = 'sms';
const NOTIFICATION_CHANNEL_PUSH = 'push';
const NOTIFICATION_CHANNEL_CONSOLE = 'console';
const NOTIFICATION_CHANNEL_WEBHOOK = 'webhook';
// Notification provider types (custom — extend MESSAGE_TYPE_*)
const MESSAGE_TYPE_CONSOLE = 'console';
const MESSAGE_TYPE_WEBHOOK = 'webhook';
// Notification types
const NOTIFICATION_TYPE_EMAIL = 'email';
const NOTIFICATION_TYPE_SMS = 'sms';
const NOTIFICATION_TYPE_PUSH = 'push';
const NOTIFICATION_TYPE_CONSOLE = 'console';
const NOTIFICATION_TYPE_WEBHOOK = 'webhook';
const RESOURCE_TYPE_ALERTS = 'alerts';
// API key types
const API_KEY_STANDARD = 'standard';
+2 -2
View File
@@ -321,7 +321,7 @@ class Notification extends Event
return $this->recipients;
}
public function addRecipient(string $address, string $channel = NOTIFICATION_CHANNEL_EMAIL, ?string $signatureKey = null): self
public function addRecipient(string $address, string $channel = NOTIFICATION_TYPE_EMAIL, ?string $signatureKey = null): self
{
$recipient = ['address' => $address, 'channel' => $channel];
if ($signatureKey !== null && $signatureKey !== '') {
@@ -429,7 +429,7 @@ class Notification extends Event
if (empty($recipients) && !empty($this->recipient)) {
$recipients = [[
'address' => $this->recipient,
'channel' => NOTIFICATION_CHANNEL_EMAIL,
'channel' => NOTIFICATION_TYPE_EMAIL,
]];
}
@@ -107,7 +107,7 @@ class Notifications extends Action
return [];
}
return [['address' => $address, 'channel' => NOTIFICATION_CHANNEL_EMAIL]];
return [['address' => $address, 'channel' => NOTIFICATION_TYPE_EMAIL]];
}
private function alreadyDelivered(Database $database, string $messageId): bool
@@ -129,13 +129,13 @@ class Notifications extends Action
$address = $recipient['address'];
switch ($channel) {
case NOTIFICATION_CHANNEL_EMAIL:
case NOTIFICATION_TYPE_EMAIL:
$this->dispatchEmail($address, $payload, $register, $log);
return;
case NOTIFICATION_CHANNEL_CONSOLE:
case NOTIFICATION_TYPE_CONSOLE:
$this->dispatchConsole($address, $payload, $database);
return;
case NOTIFICATION_CHANNEL_WEBHOOK:
case NOTIFICATION_TYPE_WEBHOOK:
$this->dispatchWebhook($address, $payload, $recipient['signatureKey'] ?? null, $log);
return;
default:
@@ -364,7 +364,7 @@ class Notifications extends Action
'messageId' => $messageId,
'type' => 'info',
'channel' => $channel,
'userId' => $channel === NOTIFICATION_CHANNEL_CONSOLE ? $address : null,
'userId' => $channel === NOTIFICATION_TYPE_CONSOLE ? $address : null,
'projectId' => $projectId,
'title' => $payload['subject'] ?? '',
'body' => $payload['body'] ?? '',
@@ -111,9 +111,9 @@ class NotificationsTest extends TestCase
$payload = [
'project' => ['$id' => 'project-x'],
'recipients' => [
['address' => 'user@example.test', 'channel' => NOTIFICATION_CHANNEL_EMAIL],
['address' => 'user-1', 'channel' => NOTIFICATION_CHANNEL_CONSOLE],
['address' => 'https://hooks.example.test/in', 'channel' => NOTIFICATION_CHANNEL_WEBHOOK],
['address' => 'user@example.test', 'channel' => NOTIFICATION_TYPE_EMAIL],
['address' => 'user-1', 'channel' => NOTIFICATION_TYPE_CONSOLE],
['address' => 'https://hooks.example.test/in', 'channel' => NOTIFICATION_TYPE_WEBHOOK],
],
'subject' => 'Hi',
'body' => 'Body',
@@ -124,7 +124,7 @@ class NotificationsTest extends TestCase
$this->assertCount(3, $worker->dispatched);
$channels = \array_map(static fn ($d) => $d['channel'], $worker->dispatched);
$this->assertSame([NOTIFICATION_CHANNEL_EMAIL, NOTIFICATION_CHANNEL_CONSOLE, NOTIFICATION_CHANNEL_WEBHOOK], $channels);
$this->assertSame([NOTIFICATION_TYPE_EMAIL, NOTIFICATION_TYPE_CONSOLE, NOTIFICATION_TYPE_WEBHOOK], $channels);
}
public function testPersistsOneAlertPerRecipientChannel(): void
@@ -133,8 +133,8 @@ class NotificationsTest extends TestCase
$payload = [
'project' => ['$id' => 'project-x'],
'recipients' => [
['address' => 'user-1', 'channel' => NOTIFICATION_CHANNEL_CONSOLE],
['address' => 'user-2', 'channel' => NOTIFICATION_CHANNEL_CONSOLE],
['address' => 'user-1', 'channel' => NOTIFICATION_TYPE_CONSOLE],
['address' => 'user-2', 'channel' => NOTIFICATION_TYPE_CONSOLE],
],
'subject' => 'Heads up',
'body' => 'Read me',
@@ -163,7 +163,7 @@ class NotificationsTest extends TestCase
$worker = new SpyNotifications();
$payload = [
'project' => ['$id' => 'project-x'],
'recipients' => [['address' => 'user-1', 'channel' => NOTIFICATION_CHANNEL_CONSOLE]],
'recipients' => [['address' => 'user-1', 'channel' => NOTIFICATION_TYPE_CONSOLE]],
'subject' => 'Sub',
'body' => 'B',
'deduplicationKey' => 'dup-key',
@@ -218,7 +218,7 @@ class NotificationsTest extends TestCase
$this->assertCount(1, $worker->dispatched);
$this->assertSame('legacy@example.test', $worker->dispatched[0]['address']);
$this->assertSame(NOTIFICATION_CHANNEL_EMAIL, $worker->dispatched[0]['channel']);
$this->assertSame(NOTIFICATION_TYPE_EMAIL, $worker->dispatched[0]['channel']);
}
public function testWebhookRecipientForwardsSignatureKey(): void
@@ -229,12 +229,12 @@ class NotificationsTest extends TestCase
'recipients' => [
[
'address' => 'https://hooks.example.test/signed',
'channel' => NOTIFICATION_CHANNEL_WEBHOOK,
'channel' => NOTIFICATION_TYPE_WEBHOOK,
'signatureKey' => 'tenant-secret',
],
[
'address' => 'https://hooks.example.test/unsigned',
'channel' => NOTIFICATION_CHANNEL_WEBHOOK,
'channel' => NOTIFICATION_TYPE_WEBHOOK,
],
],
'subject' => 's',
@@ -251,11 +251,11 @@ class NotificationsTest extends TestCase
public function testDispatchErrorTagsLogAndPropagates(): void
{
$worker = new SpyNotifications();
$worker->throwOn[NOTIFICATION_CHANNEL_WEBHOOK] = new \RuntimeException('boom');
$worker->throwOn[NOTIFICATION_TYPE_WEBHOOK] = new \RuntimeException('boom');
$payload = [
'project' => ['$id' => 'project-x'],
'recipients' => [['address' => 'https://h.example.test', 'channel' => NOTIFICATION_CHANNEL_WEBHOOK]],
'recipients' => [['address' => 'https://h.example.test', 'channel' => NOTIFICATION_TYPE_WEBHOOK]],
'subject' => 's',
'body' => 'b',
'deduplicationKey' => 'err-1',
@@ -269,7 +269,7 @@ class NotificationsTest extends TestCase
}
$tags = $this->log->getTags();
$this->assertSame(NOTIFICATION_CHANNEL_WEBHOOK, $tags['channel'] ?? null);
$this->assertSame(NOTIFICATION_TYPE_WEBHOOK, $tags['channel'] ?? null);
$this->assertSame('boom', $tags['error'] ?? null);
$rows = $this->database->find('alerts');