Skip to content
Open
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
2 changes: 1 addition & 1 deletion src/WebHook/Notification/MessageNotificationFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -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']
));
}
Expand Down
6 changes: 3 additions & 3 deletions src/WebHook/Notification/Support/Customer.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -22,7 +22,7 @@ public function id(): string
return $this->id;
}

public function name(): string
public function name(): ?string
{
return $this->name;
}
Expand Down
81 changes: 81 additions & 0 deletions tests/Unit/WebHook/NotificationFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
}
Loading