Skip to content

Commit

Permalink
Merge pull request #289 from matt-h/embedded-threads
Browse files Browse the repository at this point in the history
Support hydrating embedded threads in incoming webhooks
  • Loading branch information
miguelrs authored Dec 18, 2023
2 parents 479a72e + ca8ae94 commit 3fbe4d4
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 6 deletions.
22 changes: 16 additions & 6 deletions src/Conversations/Conversation.php
Original file line number Diff line number Diff line change
Expand Up @@ -176,19 +176,19 @@ public function hydrate(array $data, array $embedded = [])
$this->setThreadCount($data['threadCount'] ?? null);
}

if (isset($embedded['threads'])) {
$this->hydrateThreads($embedded['threads']);
}

if (isset($data['threads'])) {
// On some API calls these value is used to pass the thread count
if (is_numeric($data['threads'])) {
$this->setThreadCount($data['threads']);
} elseif (is_array($data['threads'])) {
$this->threads = new Collection();
$threadFactory = new ThreadFactory();
foreach ($data['threads'] as $threadData) {
$thread = $threadFactory->make($threadData['type'], $threadData);
$this->threads->append($thread);
}
$this->hydrateThreads($data['threads']);
}
}

$this->setNumber($data['number'] ?? null);
$this->setType($data['type'] ?? null);
$this->setFolderId($data['folderId'] ?? null);
Expand Down Expand Up @@ -281,6 +281,16 @@ public function hydrate(array $data, array $embedded = [])
}
}

protected function hydrateThreads(array $threads): void
{
$this->threads = new Collection();
$threadFactory = new ThreadFactory();
foreach ($threads as $threadData) {
$thread = $threadFactory->make($threadData['type'], $threadData);
$this->threads->append($thread);
}
}

/**
* {@inheritdoc}
*/
Expand Down
1 change: 1 addition & 0 deletions src/Conversations/ConversationFilters.php
Original file line number Diff line number Diff line change
Expand Up @@ -239,4 +239,5 @@ public function withQuery(string $query): ConversationFilters

return $filters;
}

}
50 changes: 50 additions & 0 deletions tests/Conversations/ConversationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,56 @@ public function testHydrateWhenCustomerExistsButIsNull()
$this->assertFalse($conversation->wasCreatedByUser());
}

public function testHydrateEmbedded()
{
$conversation = new Conversation();
$embeddedFixation = [
'threads' => [
[
'id' => 2198262392,
'assignedTo' => null,
'status' => 'active',
'createdAt' => '2019-02-28T15:48:20Z',
'createdBy' => [
'id' => 179783313,
'firstName' => 'John',
'lastName' => 'Smith',
'email' => '[email protected]',
'type' => 'customer',
],
'source' => [
'type' => 'email',
'via' => 'customer',
],
'actionType' => null,
'actionSourceId' => 0,
'fromMailbox' => null,
'type' => 'customer',
'state' => 'published',
'customer' => [
'id' => 179783313,
'firstName' => 'John',
'lastName' => 'Smith',
'email' => '[email protected]',
'type' => 'customer',
],
'body' => 'Are you still interested in a demo?',
'to' => [],
'cc' => null,
'bcc' => null,
'attachments' => null,
],
],
];
$conversation->hydrate(['threads' => 1], $embeddedFixation);

$this->assertSame(1, $conversation->getThreadCount());

$thread = $conversation->getThreads()[0];
$this->assertInstanceOf(CustomerThread::class, $thread);
$this->assertSame(2198262392, $thread->getId());
}

public function testExtractsCreatedByUser()
{
$conversation = new Conversation();
Expand Down

0 comments on commit 3fbe4d4

Please sign in to comment.