Skip to content

Commit

Permalink
Merge pull request #317 from wotta/add-missing-embed-threads-option-f…
Browse files Browse the repository at this point in the history
…or-conversation-filters

feat(conversations): Added "missing" functionality to get truncated threads
  • Loading branch information
miguelrs authored Jun 11, 2024
2 parents 3fbe4d4 + 8548471 commit 80d7c9a
Show file tree
Hide file tree
Showing 4 changed files with 96 additions and 6 deletions.
17 changes: 17 additions & 0 deletions src/Conversations/ConversationFilters.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,11 @@ class ConversationFilters
*/
private $query;

/**
* @var string
*/
public $embed;

public function getParams(): array
{
$params = [
Expand All @@ -78,6 +83,7 @@ public function getParams(): array
'sortField' => $this->sortField,
'sortOrder' => $this->sortOrder,
'query' => $this->query,
'embed' => $this->embed,
];

if (\is_array($this->tag)) {
Expand Down Expand Up @@ -240,4 +246,15 @@ public function withQuery(string $query): ConversationFilters
return $filters;
}

public function withEmbed(string $embed): ConversationFilters
{
Assert::oneOf($embed, [
'threads',
]);

$filters = clone $this;
$filters->embed = $embed;

return $filters;
}
}
4 changes: 3 additions & 1 deletion tests/Conversations/ConversationFiltersTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@ public function testGetParams()
->sortField('createdAt')
->sortOrder('asc')
->withQuery('query')
->byCustomField(123, 'blue');
->byCustomField(123, 'blue')
->withEmbed('threads');

$this->assertSame([
'mailbox' => 1,
Expand All @@ -44,6 +45,7 @@ public function testGetParams()
'sortField' => 'createdAt',
'sortOrder' => 'asc',
'query' => 'query',
'embed' => 'threads',
'tag' => 'testing',
'customFieldsByIds' => '123:blue',
], $filters->getParams());
Expand Down
16 changes: 16 additions & 0 deletions tests/Conversations/ConversationIntegrationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,22 @@ public function testGetConversationPreloadsThreads()
]);
}

public function testGetConversationWithEmbedThreads()
{
$this->stubResponse($this->getResponse(200, ConversationPayloads::getConversations(1, 10, true)));

$filters = (new ConversationFilters())
->withEmbed('threads');

$conversations = $this->client->conversations()->list($filters);

$this->assertCount(10, $conversations);
$this->assertInstanceOf(Conversation::class, $firstConversation = $conversations[0]);
$this->assertInstanceOf(CustomerThread::class, $firstConversation->getThreads()->toArray()[0]);

$this->verifySingleRequest('https://api.helpscout.net/v2/conversations?embed=threads');
}

public function testListConversations()
{
$this->stubResponse(
Expand Down
65 changes: 60 additions & 5 deletions tests/Payloads/ConversationPayloads.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,15 @@ public static function getConversation(int $id): string
return json_encode(static::conversation($id));
}

public static function getConversations(int $pageNumber, int $totalElements): string
public static function getConversations(int $pageNumber, int $totalElements, bool $embedThread = false): string
{
$pageSize = 10;
$pageElements = min($totalElements, $pageSize);
$totalPages = ceil($totalElements / $pageSize);

// Create embedded resources
$conversations = array_map(function ($id) {
return static::conversation($id);
$conversations = array_map(function ($id) use ($embedThread) {
return static::conversation($id, $embedThread);
}, range(1, $pageElements));

$data = [
Expand Down Expand Up @@ -60,9 +60,9 @@ public static function getConversations(int $pageNumber, int $totalElements): st
return json_encode($data);
}

private static function conversation(int $id): array
private static function conversation(int $id, bool $embedThread = false): array
{
return [
$conversation = [
'id' => $id,
'number' => 15473,
'threads' => 2,
Expand Down Expand Up @@ -144,6 +144,61 @@ private static function conversation(int $id): array
],
],
];

if ($embedThread) {
$conversation['_embedded']['threads'] = [
[
'id' => 1,
'type' => 'customer',
'status' => 'active',
'state' => 'published',
'action' => [
'type' => 'default',
'associatedEntities' => [],
],
'body' => 'This is a test',
'source' => [
'type' => 'email',
'via' => 'user',
],
'customer' => [
'id' => 472611182,
'first' => 'John',
'last' => 'Doe',
'photoUrl' => 'https://d33v4339jhl8k0.cloudfront.net/customer-avatar/05.png',
'email' => '[email protected]',
],
'createdBy' => [
'id' => 1,
'type' => 'user',
'first' => 'John',
'last' => 'Doe',
'email' => '[email protected]',
'to' => [],
'cc' => [],
'bcc' => [],
'createdAt' => '2017-04-21T14:39:56Z',
],
'assignedTo' => [
'id' => 12,
'first' => 'Help',
'last' => 'Scout',
'email' => '[email protected]',
],
'savedReplyId' => 0,
'_embedded' => [
'attachments' => [],
],
'_links' => [
'createdByUser' => [
'href' => 'https://api.helpscout.net/v2/users/1',
]
]
],
];
}

return $conversation;
}

public static function getThreads(int $conversationId): string
Expand Down

0 comments on commit 80d7c9a

Please sign in to comment.