From e754677aa2bf229632cc3750c5fcbd2c711e4e1f Mon Sep 17 00:00:00 2001 From: Arne <1255879+arneee@users.noreply.github.com> Date: Thu, 15 Jan 2026 04:38:30 +0000 Subject: [PATCH] Make profile / name optional --- .../MessageNotificationFactory.php | 2 +- src/WebHook/Notification/Support/Customer.php | 6 +- .../Unit/WebHook/NotificationFactoryTest.php | 81 +++++++++++++++++++ 3 files changed, 85 insertions(+), 4 deletions(-) diff --git a/src/WebHook/Notification/MessageNotificationFactory.php b/src/WebHook/Notification/MessageNotificationFactory.php index 8db65cf..1d6633e 100644 --- a/src/WebHook/Notification/MessageNotificationFactory.php +++ b/src/WebHook/Notification/MessageNotificationFactory.php @@ -130,7 +130,7 @@ private function decorateNotification(MessageNotification $notification, array $ if ($contact) { $notification->withCustomer(new Support\Customer( $contact['wa_id'], - $contact['profile']['name'], + $contact['profile']['name'] ?? null, $message['from'] )); } diff --git a/src/WebHook/Notification/Support/Customer.php b/src/WebHook/Notification/Support/Customer.php index 0ce44ef..79ab692 100644 --- a/src/WebHook/Notification/Support/Customer.php +++ b/src/WebHook/Notification/Support/Customer.php @@ -6,11 +6,11 @@ final class Customer { private string $id; - private string $name; + private ?string $name; private string $phone_number; - public function __construct(string $id, string $name, string $phone_number) + public function __construct(string $id, ?string $name, string $phone_number) { $this->id = $id; $this->name = $name; @@ -22,7 +22,7 @@ public function id(): string return $this->id; } - public function name(): string + public function name(): ?string { return $this->name; } diff --git a/tests/Unit/WebHook/NotificationFactoryTest.php b/tests/Unit/WebHook/NotificationFactoryTest.php index b21e935..fa84f52 100644 --- a/tests/Unit/WebHook/NotificationFactoryTest.php +++ b/tests/Unit/WebHook/NotificationFactoryTest.php @@ -1446,4 +1446,85 @@ public function test_build_from_payload_can_build_a_service_status_notification( $this->assertTrue($notification->isMessageDelivered()); $this->assertTrue($notification->isMessageSent()); } + + public function test_build_from_payload_can_handle_empty_profile() + { + $payload = json_decode('{ + "object": "whatsapp_business_account", + "entry": [{ + "id": "WHATSAPP_BUSINESS_ACCOUNT_ID", + "changes": [{ + "value": { + "messaging_product": "whatsapp", + "metadata": { + "display_phone_number": "PHONE_NUMBER", + "phone_number_id": "PHONE_NUMBER_ID" + }, + "contacts": [{ + "wa_id": "WHATSAPP_ID" + }], + "messages": [{ + "from": "16315551234", + "id": "wamid.ID", + "timestamp": 1669233778, + "type": "text", + "text": { + "body": "Hello World" + } + }] + }, + "field": "messages" + }] + }] + }', true); + + $notification = $this->notification_factory->buildFromPayload($payload); + + $this->assertInstanceOf(Notification\Text::class, $notification); + $this->assertEquals('wamid.ID', $notification->id()); + $this->assertEquals('WHATSAPP_ID', $notification->customer()->id()); + $this->assertNull($notification->customer()->name()); + $this->assertEquals('16315551234', $notification->customer()->phoneNumber()); + } + + public function test_build_from_payload_can_handle_empty_profile_name() + { + $payload = json_decode('{ + "object": "whatsapp_business_account", + "entry": [{ + "id": "WHATSAPP_BUSINESS_ACCOUNT_ID", + "changes": [{ + "value": { + "messaging_product": "whatsapp", + "metadata": { + "display_phone_number": "PHONE_NUMBER", + "phone_number_id": "PHONE_NUMBER_ID" + }, + "contacts": [{ + "profile": {}, + "wa_id": "WHATSAPP_ID" + }], + "messages": [{ + "from": "16315551234", + "id": "wamid.ID", + "timestamp": 1669233778, + "type": "text", + "text": { + "body": "Hello World" + } + }] + }, + "field": "messages" + }] + }] + }', true); + + $notification = $this->notification_factory->buildFromPayload($payload); + + $this->assertInstanceOf(Notification\Text::class, $notification); + $this->assertEquals('wamid.ID', $notification->id()); + $this->assertEquals('WHATSAPP_ID', $notification->customer()->id()); + $this->assertNull($notification->customer()->name()); + $this->assertEquals('16315551234', $notification->customer()->phoneNumber()); + } }