Skip to content

Commit

Permalink
Merge pull request #100 from angelomelonas/context-id
Browse files Browse the repository at this point in the history
The Context ID should be optional in forwarded messages
  • Loading branch information
aalbarca authored Mar 29, 2023
2 parents 3b7aa74 + 646ed9b commit d94f1dc
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 4 deletions.
2 changes: 1 addition & 1 deletion src/WebHook/Notification/MessageNotificationFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ private function decorateNotification(MessageNotification $notification, array $
}

$notification->withContext(new Support\Context(
$message['context']['id'],
$message['context']['id'] ?? null,
$message['context']['forwarded'] ?? false,
$referred_product ?? null
));
Expand Down
6 changes: 3 additions & 3 deletions src/WebHook/Notification/Support/Context.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@

final class Context
{
private string $replying_to_message_id;
private ?string $replying_to_message_id;

private bool $forwarded;

private ?ReferredProduct $referred_product;

public function __construct(
string $replying_to_message_id,
string $replying_to_message_id = null,
bool $forwarded = false,
ReferredProduct $referred_product = null
) {
Expand All @@ -20,7 +20,7 @@ public function __construct(
$this->referred_product = $referred_product;
}

public function replyingToMessageId(): string
public function replyingToMessageId(): ?string
{
return $this->replying_to_message_id;
}
Expand Down
47 changes: 47 additions & 0 deletions tests/Unit/WebHook/NotificationFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -892,6 +892,53 @@ public function test_build_from_payload_can_build_a_status_notification_with_err
$this->assertEquals('ERROR_TITLE', $notification->errorTitle());
}

public function test_build_from_payload_can_build_a_forwarded_notification()
{
$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": {
"name": "NAME"
},
"wa_id": "WHATSAPP_ID"
}],
"messages": [{
"context": {
"forwarded": true
},
"from": "16315551234",
"id": "wamid.ID",
"timestamp": 1669233778,
"type": "text",
"text": {
"body": "MESSAGE_BODY"
}
}]
},
"field": "messages"
}]
}]
}', true);

$notification = $this->notification_factory->buildFromPayload($payload);

$this->assertNull($notification->replyingToMessageId());
$this->assertEquals('PHONE_NUMBER_ID', $notification->businessPhoneNumberId());
$this->assertEquals('PHONE_NUMBER', $notification->businessPhoneNumber());
$this->assertTrue($notification->isForwarded());
$this->assertEquals('WHATSAPP_ID', $notification->customer()->id());
$this->assertEquals('NAME', $notification->customer()->name());
}

public function test_build_from_payload_return_null_when_payload_is_empty()
{
$notification = $this->notification_factory->buildFromPayload([]);
Expand Down

0 comments on commit d94f1dc

Please sign in to comment.